{{ header }}{{ column_left }}
<!-- Content Wrapper. Contains page content -->
<div id="content" class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <div>
      <div class="pull-right">
        <button type="submit" form="form-geo-zone" data-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary btn-sm"><i class="fa fa-save"></i> {{ button_save }}</button>
        <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-default btn-sm"><i class="fa fa-reply"></i> {{ button_cancel }}</a>
      </div>
      <h1>
        {{ heading_title }}
      </h1>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <ul class="breadcrumb">
          {% for breadcrumb in breadcrumbs %}
          <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
          {% endfor %}
        </ul>
      </div>
    </div>
  </section>

  <!-- Alert -->
  <section class="content-header">
    {% if error_warning %}
    <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> {{ error_warning }}
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
    {% endif %}
    {% if success %}
    <div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> {{ success }}
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
    {% endif %}
  </section>
  <!-- /.alert -->

  <!-- Main content -->
  <section class="content">

    <!-- Default box -->
    <div class="box">
      <div class="box-header with-border">
        <h3 class="box-title">{{ text_form }}</h3>

        <div class="box-tools pull-right">
          <button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="{{ text_collapse }}">
            <i class="fa fa-minus"></i></button>
        </div>
      </div>
      <div class="box-body">
        <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-geo-zone" class="form-horizontal">
          <div class="form-group required">
            <label class="col-sm-2 control-label" for="input-name">{{ entry_name }}</label>
            <div class="col-sm-10">
              <input type="text" name="name" value="{{ name }}" placeholder="{{ entry_name }}" id="input-name" class="form-control" />
              {% if error_name %}
              <div class="text-danger">{{ error_name }}</div>
              {% endif %} </div>
          </div>
          <div class="form-group required">
            <label class="col-sm-2 control-label" for="input-description">{{ entry_description }}</label>
            <div class="col-sm-10">
              <input type="text" name="description" value="{{ description }}" placeholder="{{ entry_description }}" id="input-description" class="form-control" />
              {% if error_description %}
              <div class="text-danger">{{ error_description }}</div>
              {% endif %} </div>
          </div>
          <fieldset>
            <legend>{{ text_geo_zone }}</legend>
            <table id="zone-to-geo-zone" class="table table-striped table-bordered table-hover">
              <thead>
                <tr>
                  <td class="text-left">{{ entry_country }}</td>
                  <td class="text-left">{{ entry_zone }}</td>
                  <td></td>
                </tr>
              </thead>
              <tbody>
              
              {% set zone_to_geo_zone_row = 0 %}
              {% for zone_to_geo_zone in zone_to_geo_zones %}
              <tr id="zone-to-geo-zone-row{{ zone_to_geo_zone_row }}">
                <td class="text-left"><select name="zone_to_geo_zone[{{ zone_to_geo_zone_row }}][country_id]" class="form-control" data-index="{{ zone_to_geo_zone_row }}" data-zone-id="{{ zone_to_geo_zone.zone_id }}" disabled="disabled">
                    
                    {% for country in countries %}
                    {% if country.country_id == zone_to_geo_zone.country_id %}
                    
                    <option value="{{ country.country_id }}" selected="selected">{{ country.name }}</option>
                    
                    {% else %}
                    
                    <option value="{{ country.country_id }}">{{ country.name }}</option>
                    
                    {% endif %}
                    {% endfor %}
                  
                  </select></td>
                <td class="text-left"><select name="zone_to_geo_zone[{{ zone_to_geo_zone_row }}][zone_id]" class="form-control" disabled="disabled">
                  </select></td>
                <td class="text-left"><button type="button" onclick="$('#zone-to-geo-zone-row{{ zone_to_geo_zone_row }}').remove();" data-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>
              </tr>
              {% set zone_to_geo_zone_row = zone_to_geo_zone_row + 1 %}
              {% endfor %}
                </tbody>
              
              <tfoot>
                <tr>
                  <td colspan="2"></td>
                  <td class="text-left"><button type="button" id="button-geo-zone" data-toggle="tooltip" title="{{ button_geo_zone_add }}" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td>
                </tr>
              </tfoot>
            </table>
          </fieldset>
        </form>
      </div>
      <!-- /.box-body -->
    </div>
    <!-- /.box -->

  </section>
  <!-- /.content -->
</div>
<!-- /.content-wrapper -->
  <script type="text/javascript"><!--
var zone_to_geo_zone_row = {{ zone_to_geo_zone_row }};

$('#button-geo-zone').on('click', function() {
	html  = '<tr id="zone-to-geo-zone-row' + zone_to_geo_zone_row + '">';
	html += '  <td class="text-left"><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][country_id]" class="form-control" data-index="' + zone_to_geo_zone_row + '">';
	{% for country in countries %}
	html += '    <option value="{{ country.country_id }}">{{ country.name|escape('js') }}</option>';
	{% endfor %}   
	html += '</select></td>';
	html += '  <td class="text-left"><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][zone_id]" class="form-control"><option value="0">{{ text_all_zones }}</option></select></td>';
	html += '  <td class="text-left"><button type="button" onclick="$(\'#zone-to-geo-zone-row' + zone_to_geo_zone_row + '\').remove();" data-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
	html += '</tr>';
	
	$('#zone-to-geo-zone tbody').append(html);
	
	$('zone_to_geo_zone[' + zone_to_geo_zone_row + '][country_id]').trigger();
			
	zone_to_geo_zone_row++;
});

$('#zone-to-geo-zone').on('change', 'select[name$=\'[country_id]\']', function() {
	var element = this;
	
	if (element.value) { 
		$.ajax({
			url: 'index.php?route=localisation/country/country&user_token={{ user_token }}&country_id=' + element.value,
			dataType: 'json',
			beforeSend: function() {
				$(element).prop('disabled', true);
				$('button[form=\'form-geo-zone\']').prop('disabled', true);
			},
			complete: function() {
				$(element).prop('disabled', false);
				$('button[form=\'form-geo-zone\']').prop('disabled', false);
			},
			success: function(json) {
				html = '<option value="0">{{ text_all_zones }}</option>';
				
				if (json['zone'] && json['zone'] != '') {	
					for (i = 0; i < json['zone'].length; i++) {
						html += '<option value="' + json['zone'][i]['zone_id'] + '"';
	
						if (json['zone'][i]['zone_id'] == $(element).attr('data-zone-id')) {
							html += ' selected="selected"';
						}
	
						html += '>' + json['zone'][i]['name'] + '</option>';
					}
				}
	
				$('select[name=\'zone_to_geo_zone[' + $(element).attr('data-index') + '][zone_id]\']').html(html);
				
				$('select[name=\'zone_to_geo_zone[' + $(element).attr('data-index') + '][zone_id]\']').prop('disabled', false);
				
				$('select[name$=\'[country_id]\']:disabled:first').trigger('change');
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	}
});

$('select[name$=\'[country_id]\']:disabled:first').trigger('change');
//--></script>
{{ footer }}