{% block title %} {{ title }} {% endblock %}

{% block header %}<h2>{{ title }}</h2>{% endblock %}

{% block content %}
{% macro render_fields(docfields) -%}
{% for df in docfields -%}
	{% if df.fieldtype == "Data" -%}
	<fieldset>
		<label>{{ df.label }}</label>
		<input class="form-control" type="text" placeholder="Type {{ df.label }}"
			data-fieldname="{{ df.fieldname }}" data-fieldtype="{{ df.fieldtype }}"
			{% if doc and doc.get(df.fieldname) -%} value="{{ doc[df.fieldname] }}" {%- endif %}>
	</fieldset>
	{% elif df.fieldtype == "Check" -%}
	<fieldset class="checkbox">
		<label><input type="checkbox" data-fieldname="{{ df.fieldname }}"
			data-fieldtype="{{ df.fieldtype }}"
			{% if doc and frappe.utils.cint(doc.get(df.fieldname)) -%} checked="checked" {%- endif %}>
			{{ df.label }}</label>
	</fieldset>
	{% elif df.fieldtype in ("Select", "Link") -%}
	<fieldset>
		{% set select_options = frappe.get_list(df.options)|map(attribute="name")
			if df.fieldtype == "Link" else df.options.split("\n") %}
		<label>{{ df.label }}</label>
		<select class="form-control" data-fieldname="{{ df.fieldname }}" data-fieldtype="{{ df.fieldtype }}">
			{% for value in select_options -%}
			{% if doc and doc.get(df.fieldname) == value -%}
			<option selected="selected">{{ value }}</option>
			{% else -%}
			<option>{{ value }}</option>
			{%- endif %}
			{%- endfor %}
		</select>
	</fieldset>
	{%- endif %}
{%- endfor %}
{%- endmacro %}

<div class="container content">
    <ul class="breadcrumb">
    	<li><a href="/index">Home</a></li>
    	<li><a href="/addresses">My Addresses</a></li>
    	<li class="active"><i class="icon-map-marker icon-fixed-width"></i> {{ title }}</li>
    </ul>
	<h3><i class="icon-map-marker icon-fixed-width"></i> {{ title }}</h3>
	<button type="button" class="btn btn-primary pull-right" id="address-save"><i class="icon-ok"></i>
		{{ doc and "Save" or "Insert" }}</button>
	<div class="clearfix"></div>
	<hr>
	<div id="address-error" class="alert alert-danger" style="display:none"></div>
	<form autocomplete="on">
		<div class="row">
			<section class="col-md-6">
				{{ render_fields(meta.left_fields) }}
			</section>
			<section class="col-md-6">
				{{ render_fields(meta.right_fields) }}
			</section>
		</section>
	</form>
</div>

<script>
;(function() {
	console.log("yoyo");
	frappe.ready(function() {
		bind_save();
	});

	var bind_save = function() {
		$("#address-save").on("click", function() {
			console.log("clicked!");

			var fields = {
				name: "{{ docname or '' }}"
			};

			$("form").find("[data-fieldname]").each(function(i, input) {
				var $input = $(input);
				var fieldname = $(input).attr("data-fieldname");
				var fieldtype = $(input).attr("data-fieldtype");

				if(fieldtype == "Check") {
					fields[fieldname] = $input.is(":checked") ? 1 : 0;
				} else {
					fields[fieldname] = $input.val();
				}
			});

			frappe.call({
				btn: $(this),
				type: "POST",
				method: "erpnext.templates.pages.address.save_address",
				args: { fields: fields, address_fieldname: get_url_arg("address_fieldname") },
				error_msg: "#address-error",
				callback: function(r) {
					if(get_url_arg("address_fieldname")) {
						window.location.href = "cart";
					} else {
						window.location.href = "address?name=" + encodeURIComponent(r.message);
					}
				}
			});
		});
	};
})();
</script>

<!-- no-sidebar -->
{% endblock %}
