<div id="modal-address" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title"><i class="fa-solid fa-cog"></i> {{ heading_title }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-address" action="{{ save }}" method="post" data-oc-toggle="ajax" data-oc-load="{{ action }}" data-oc-target="#address">
          <div class="mb-3 required">
            <label for="input-address-firstname" class="form-label">{{ entry_firstname }}</label>
            <input type="text" name="firstname" value="{{ firstname }}" placeholder="{{ entry_firstname }}" id="input-address-firstname" class="form-control"/>
            <div id="error-address-firstname" class="invalid-feedback"></div>
          </div>
          <div class="mb-3 required">
            <label for="input-address-lastname" class="form-label">{{ entry_lastname }}</label>
            <input type="text" name="lastname" value="{{ lastname }}" placeholder="{{ entry_lastname }}" id="input-address-lastname" class="form-control"/>
            <div id="error-address-lastname" class="invalid-feedback"></div>
          </div>
          <div class="mb-3">
            <label for="input-address-company" class="form-label">{{ entry_company }}</label>
            <input type="text" name="company" value="{{ company }}" placeholder="{{ entry_company }}" id="input-address-company" class="form-control"/>
          </div>
          <div class="mb-3 required">
            <label for="input-address-address-1" class="form-label">{{ entry_address_1 }}</label>
            <input type="text" name="address_1" value="{{ address_1 }}" placeholder="{{ entry_address_1 }}" id="input-address-address-1" class="form-control"/>
            <div id="error-address-address-1" class="invalid-feedback"></div>
          </div>
          <div class="mb-3">
            <label for="input-address-address-2" class="form-label">{{ entry_address_2 }}</label>
            <input type="text" name="address_2" value="{{ address_2 }}" placeholder="{{ entry_address_2 }}" id="input-address-address-2" class="form-control"/>
          </div>
          <div class="mb-3 required">
            <label for="input-address-city" class="form-label">{{ entry_city }}</label>
            <input type="text" name="city" value="{{ city }}" placeholder="{{ entry_city }}" id="input-address-city" class="form-control"/>
            <div id="error-address-city" class="invalid-feedback"></div>
          </div>
          <div class="mb-3 required">
            <label for="input-address-postcode" class="form-label">{{ entry_postcode }}</label>
            <input type="text" name="postcode" value="{{ postcode }}" placeholder="{{ entry_postcode }}" id="input-address-postcode" class="form-control"/>
            <div id="error-address-postcode" class="invalid-feedback"></div>
          </div>
          <div class="mb-3 required">
            <label for="input-address-country" class="form-label">{{ entry_country }}</label>
            <select name="country_id" id="input-address-country" class="form-select">
              <option value="">{{ text_select }}</option>
              {% for country in countries %}
                <option value="{{ country.country_id }}"{% if country.country_id == country_id %} selected{% endif %}>{{ country.name }}</option>
              {% endfor %}
            </select>
            <div id="error-address-country" class="invalid-feedback"></div>
          </div>
          <div class="mb-3 required">
            <label for="input-address-zone" class="form-label">{{ entry_zone }}</label>
            <select name="zone_id" id="input-address-zone" class="form-select"></select>
            <div id="error-address-zone" class="invalid-feedback"></div>
          </div>

          {% for custom_field in custom_fields %}

            {% if custom_field.type == 'select' %}
              <div class="mb-3{% if custom_field.required %} required{% endif %}">
                <label for="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label>
                <select name="custom_field[{{ custom_field.custom_field_id }}]" id="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-select">
                  <option value="">{{ text_select }}</option>
                  {% for custom_field_value in custom_field.custom_field_value %}
                    <option value="{{ custom_field_value.custom_field_value_id }}"{% if address_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == address_custom_field[custom_field.custom_field_id] %} selected{% endif %}>{{ custom_field_value.name }}</option>
                  {% endfor %}
                </select>
                <div id="error-address-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
              </div>
            {% endif %}

            {% if custom_field.type == 'radio' %}
              <div class="mb-3{% if custom_field.required %} required{% endif %}">
                <label class="form-label">{{ custom_field.name }}</label>
                <div id="input-address-custom-field-{{ custom_field.custom_field_id }}">
                  {% for custom_field_value in custom_field.custom_field_value %}
                    <div class="form-check">
                      <input type="radio" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" id="input-address-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-input"{% if address_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == address_custom_field[custom_field.custom_field_id] %} checked{% endif %}/>
                      <label for="input-address-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-label">{{ custom_field_value.name }}</label>
                    </div>
                  {% endfor %}
                </div>
                <div id="error-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
              </div>
            {% endif %}

            {% if custom_field.type == 'checkbox' %}
              <div class="mb-3{% if custom_field.required %} required{% endif %}">
                <label class="form-label">{{ custom_field.name }}</label>
                <div id="input-address-custom-field-{{ custom_field.custom_field_id }}">
                  {% for custom_field_value in custom_field.custom_field_value %}
                    <div class="form-check">
                      <input type="checkbox" name="custom_field[{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" id="input-address-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-input"{% if address_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id in address_custom_field[custom_field.custom_field_id] %} checked{% endif %}/>
                      <label for="input-address-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-label">{{ custom_field_value.name }}</label>
                    </div>
                  {% endfor %}
                </div>
                <div id="error-address-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
              </div>
            {% endif %}

            {% if custom_field.type == 'text' %}
              <div class="mb-3{% if custom_field.required %} required{% endif %}">
                <label for="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label>
                <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{% if address_custom_field[custom_field.custom_field_id] %}{{ address_custom_field[custom_field.custom_field_id] }}{% else %}{{ custom_field.value }}{% endif %}" placeholder="{{ custom_field.name }}" id="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                <div id="error-address-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
              </div>
            {% endif %}

            {% if custom_field.type == 'textarea' %}
              <div class="mb-3{% if custom_field.required %} required{% endif %}">
                <label for="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label>
                <textarea name="custom_field[{{ custom_field.custom_field_id }}]" rows="5" placeholder="{{ custom_field.name }}" id="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-control">{% if address_custom_field[custom_field.custom_field_id] %}{{ address_custom_field[custom_field.custom_field_id] }}{% else %}{{ custom_field.value }}{% endif %}</textarea>
                <div id="error-address-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
              </div>
            {% endif %}

            {% if custom_field.type == 'file' %}
              <div class="mb-3{% if custom_field.required %} required{% endif %}">
                <label class="form-label">{{ custom_field.name }}</label>
                <div class="input-group">
                <button type="button" data-oc-toggle="upload" data-oc-url="{{ upload }}" data-oc-size-max="{{ config_file_max_size }}" data-oc-size-error="{{ error_upload_size }}" data-oc-target="#input-address-custom-field-{{ custom_field.custom_field_id }}" class="btn btn-light"><i class="fa-solid fa-upload"></i> {{ button_upload }}</button>
                <input type="hidden" name="custom_field[{{ custom_field.custom_field_id }}]" value="{% if address_custom_field[custom_field.custom_field_id] %}{{ address_custom_field[custom_field.custom_field_id] }}{% endif %}" id="input-address-custom-field-{{ custom_field.custom_field_id }}"/>
                </div>
                  <div id="error-address-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
              </div>
            {% endif %}

            {% if custom_field.type == 'date' %}
              <div class="mb-3{% if custom_field.required %} required{% endif %}">
                <label for="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label>
                <input type="date" name="custom_field[{{ custom_field.custom_field_id }}]" value="{% if address_custom_field[custom_field.custom_field_id] %}{{ address_custom_field[custom_field.custom_field_id] }}{% else %}{{ custom_field.value }}{% endif %}" placeholder="{{ custom_field.name }}" id="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                <div id="error-address-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
              </div>
            {% endif %}

            {% if custom_field.type == 'time' %}
              <div class="mb-3{% if custom_field.required %} required{% endif %}">
                <label for="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label>
                <input type="time" name="custom_field[{{ custom_field.custom_field_id }}]" value="{% if address_custom_field[custom_field.custom_field_id] %}{{ address_custom_field[custom_field.custom_field_id] }}{% else %}{{ custom_field.value }}{% endif %}" placeholder="{{ custom_field.name }}" id="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                <div id="error-address-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
              </div>
            {% endif %}

            {% if custom_field.type == 'datetime' %}
              <div class="mb-3{% if custom_field.required %} required{% endif %}">
                <label for="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label>
                <input type="datetime-local" name="custom_field[{{ custom_field.custom_field_id }}]" value="{% if address_custom_field[custom_field.custom_field_id] %}{{ address_custom_field[custom_field.custom_field_id] }}{% else %}{{ custom_field.value }}{% endif %}" placeholder="{{ custom_field.name }}" id="input-address-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                <div id="error-address-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
              </div>
            {% endif %}

          {% endfor %}
          <div>
            <label class="form-label">{{ entry_default }}</label>
            <div class="form-check form-switch form-switch-lg">
              <input type="hidden" name="default" value="0"/>
              <input type="checkbox" name="default" value="1" id="input-address-default" class="form-check-input"{% if default %} checked{% endif %}/>
            </div>
          </div>
          <div class="text-end">
            <button type="submit" class="btn btn-primary"><i class="fa-solid fa-floppy-disk"></i> {{ button_save }}</button>
          </div>
          <input type="hidden" name="address_id" value="{{ address_id }}" id="input-address-id"/>
        </form>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('#input-address-country').on('change', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=localisation/country.country&user_token={{ user_token }}&country_id=' + this.value,
        dataType: 'json',
        beforeSend: function() {
            $(element).prop('disabled', true);
            $('#input-address-zone').prop('disabled', true);
        },
        complete: function() {
            $(element).prop('disabled', false);
            $('#input-address-zone').prop('disabled', false);
        },
        success: function(json) {
            if (json['postcode_required'] == '1') {
                $('#input-address-postcode').parent().parent().addClass('required');
            } else {
                $('#input-address-postcode').parent().parent().removeClass('required');
            }

            html = '<option value="">{{ text_select|escape('js') }}</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'] == '{{ zone_id }}') {
                        html += ' selected';
                    }

                    html += '>' + json['zone'][i]['name'] + '</option>';
                }
            } else {
                html += '<option value="0" selected>{{ text_none|escape('js') }}</option>';
            }

            $('#input-address-zone').html(html);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#input-address-country').trigger('change');
//--></script>