{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="float-end">
        <button type="submit" form="form-geo-zone" formaction="{{ save }}" data-bs-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary"><i class="fa-solid fa-floppy-disk"></i></button>
        <a href="{{ back }}" data-bs-toggle="tooltip" title="{{ button_back }}" class="btn btn-light"><i class="fa-solid fa-reply"></i></a></div>
      <h1>{{ heading_title }}</h1>
      <ol class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
          <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ol>
    </div>
  </div>
  <div class="container-fluid">
    <div class="card">
      <div class="card-header"><i class="fa-solid fa-pencil"></i> {{ text_form }}</div>
      <div class="card-body">
        <form id="form-geo-zone" action="{{ save }}" method="post" data-oc-toggle="ajax">
          <div class="row mb-3 required">
            <label for="input-name" class="col-sm-2 col-form-label">{{ entry_name }}</label>
            <div class="col-sm-10">
              <input type="text" name="name" value="{{ name }}" placeholder="{{ entry_name }}" id="input-name" class="form-control"/>
              <div id="error-name" class="invalid-feedback"></div>
            </div>
          </div>
          <div class="row mb-3 required">
            <label for="input-description" class="col-sm-2 col-form-label">{{ entry_description }}</label>
            <div class="col-sm-10">
              <input type="text" name="description" value="{{ description }}" placeholder="{{ entry_description }}" id="input-description" class="form-control"/>
              <div id="error-description" class="invalid-feedback"></div>
            </div>
          </div>
          <fieldset>
            <legend>{{ text_geo_zone }}</legend>
            <table id="zone-to-geo-zone" class="table table-bordered table-hover">
              <thead>
                <tr>
                  <th>{{ entry_country }}</th>
                  <th>{{ entry_zone }}</th>
                  <th></th>
                </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><select name="zone_to_geo_zone[{{ zone_to_geo_zone_row }}][country_id]" class="form-select" data-zone-to-geo-zone-row="{{ zone_to_geo_zone_row }}" data-zone-id="{{ zone_to_geo_zone.zone_id }}" disabled>
                        {% for country in countries %}
                          <option value="{{ country.country_id }}"{% if country.country_id == zone_to_geo_zone.country_id %} selected{% endif %}>{{ country.name }}</option>
                        {% endfor %}
                      </select></td>
                    <td><select name="zone_to_geo_zone[{{ zone_to_geo_zone_row }}][zone_id]" class="form-select" disabled></select></td>
                    <td class="text-end"><button type="button" onclick="$('#zone-to-geo-zone-row-{{ zone_to_geo_zone_row }}').remove();" data-bs-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa-solid 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-end"><button type="button" id="button-geo-zone" data-bs-toggle="tooltip" title="{{ button_geo_zone_add }}" class="btn btn-primary"><i class="fa-solid fa-plus-circle"></i></button></td>
                </tr>
              </tfoot>
            </table>
          </fieldset>
          <input type="hidden" name="geo_zone_id" value="{{ geo_zone_id }}" id="input-geo-zone-id"/>
        </form>
      </div>
    </div>
  </div>
</div>
<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><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][country_id]" class="form-select" data-zone-to-geo-zone-row="' + zone_to_geo_zone_row + '" disabled>';
  {% for country in countries %}
    html += '    <option value="{{ country.country_id }}">{{ country.name|escape('js') }}</option>';
  {% endfor %}
    html += '  </select></td>';
    html += '  <td><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][zone_id]" class="form-select" disabled><option value="0">{{ text_all_zones|escape('js') }}</option></select></td>';
    html += '  <td class="text-end"><button type="button" onclick="$(\'#zone-to-geo-zone-row-' + zone_to_geo_zone_row + '\').remove();" data-bs-toggle="tooltip" title="{{ button_remove|escape('js') }}" class="btn btn-danger"><i class="fa-solid fa-minus-circle"></i></button></td>';
    html += '</tr>';

    $('#zone-to-geo-zone tbody').append(html);

    $('select[name=\'zone_to_geo_zone[' + zone_to_geo_zone_row + '][country_id]\']').trigger('change');

    zone_to_geo_zone_row++;
});

var zone = [];

$('#zone-to-geo-zone').on('change', 'select[name$=\'[country_id]\']', function() {
    var element = this;

    $(element).prop('disabled', true);

    $('select[name=\'zone_to_geo_zone[' + $(element).attr('data-zone-to-geo-zone-row') + '][zone_id]\']').prop('disabled', false);

    if (!zone[$(element).val()]) {
        $.ajax({
            url: 'index.php?route=localisation/country.country&user_token={{ user_token }}&country_id=' + $(element).val(),
            dataType: 'json',
            beforeSend: function() {
                $('button[form=\'form-geo-zone\']').prop('disabled', true);
            },
            complete: function() {
                $('button[form=\'form-geo-zone\']').prop('disabled', false);
            },
            success: function(json) {
                zone[$(element).val()] = json;

                html = '<option value="0">{{ text_all_zones|escape('js') }}</option>';

                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';
                    }

                    html += '>' + json['zone'][i]['name'] + '</option>';
                }

                $('#zone-to-geo-zone select[name=\'zone_to_geo_zone[' + $(element).attr('data-zone-to-geo-zone-row') + '][zone_id]\']').html(html);

                $('#zone-to-geo-zone select[name=\'zone_to_geo_zone[' + $(element).attr('data-zone-to-geo-zone-row') + '][zone_id]\']').prop('disabled', false);

                $(element).prop('disabled', false);

                $('#zone-to-geo-zone select[name$=\'[country_id]\']:disabled:first').trigger('change');
            },
            error: function(xhr, ajaxOptions, thrownError) {
                console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    } else {
        html = '<option value="0">{{ text_all_zones|escape('js') }}</option>';

        for (i = 0; i < zone[$(element).val()]['zone'].length; i++) {
            html += '<option value="' + zone[element.value]['zone'][i]['zone_id'] + '"';

            if (zone[$(element).val()]['zone'][i]['zone_id'] == $(element).attr('data-zone-id')) {
                html += ' selected';
            }

            html += '>' + zone[$(element).val()]['zone'][i]['name'] + '</option>';
        }

        $('#zone-to-geo-zone select[name=\'zone_to_geo_zone[' + $(element).attr('data-zone-to-geo-zone-row') + '][zone_id]\']').html(html);

        $(element).prop('disabled', false);

        $('#zone-to-geo-zone select[name$=\'[country_id]\']:disabled:first').trigger('change');
    }
});

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