<form id="form-shipping-method">
  <fieldset>
    <legend>Shipping Method</legend>
    <div class="input-group">
      <select name="shipping_method" id="input-shipping-method" class="form-select"{% if not shipping_methods %} disabled{% endif %}>
        <option value="">{{ text_select }}</option>
        {% for shipping_method in shipping_methods %}
          <optgroup label="{{ shipping_method.title }}">
            {% if not shipping_method.error %}
              {% for quote in shipping_method.quote %}
                <option value="{{ quote.code }}"{% if quote.code == code %} selected{% endif %}>{{ quote.title }}{% if quote.cost %} - {{ quote.text }}{% endif %}</option>
              {% endfor %}
            {% else %}
              <option value="" class="text-danger" disabled>{{ shipping_method.error }}</option>
            {% endif %}
          </optgroup>
        {% endfor %}
      </select>
      <button type="button" id="button-shipping-method" class="btn btn-light"><i class="fa-solid fa-rotate"></i></button>
    </div>
  </fieldset>
</form>
<script type="text/javascript"><!--
// Shipping Methods
$('#button-shipping-method').on('click', function () {
    var element = this;

    chain.attach(function () {
        return $.ajax({
            url: 'index.php?route=checkout/shipping_method|getMethods&language={{ language }}',
            dataType: 'json',
            beforeSend: function () {
                $('#input-shipping-method').prop('disabled', true);
            },
            success: function (json) {
                console.log(json);

                if (json['redirect']) {
                    location = json['redirect'];
                }

                if (json['error']) {
                    $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa-solid fa-circle-exclamation"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
                }

                // Shipping Methods
                html = '<option value="">{{ text_select }}</option>';

                if (json['shipping_methods']) {
                    for (i in json['shipping_methods']) {
                        html += '<optgroup label="' + json['shipping_methods'][i]['title'] + '">';

                        if (!json['shipping_methods'][i]['error']) {
                            for (j in json['shipping_methods'][i]['quote']) {
                                if (json['shipping_methods'][i]['quote'][j]['cost']) {
                                    text = json['shipping_methods'][i]['quote'][j]['title'] + ' - ' + json['shipping_methods'][i]['quote'][j]['text'];
                                } else {
                                    text = json['shipping_methods'][i]['quote'][j]['title'];
                                }

                                if (json['shipping_methods'][i]['quote'][j]['code'] == $('#input-shipping-method').val()) {
                                    html += '<option value="' + json['shipping_methods'][i]['quote'][j]['code'] + '" selected>' + text + '</option>';
                                } else {
                                    html += '<option value="' + json['shipping_methods'][i]['quote'][j]['code'] + '">' + text + '</option>';
                                }

                            }
                        } else {
                            html += '<option value="" style="color: #ff0000;" disabled>' + json['shipping_methods'][i]['error'] + '</option>';
                        }

                        html += '</optgroup>';
                    }

                    $('#input-shipping-method').prop('disabled', false);
                }

                $('#input-shipping-method').html(html);

                $('#checkout-confirm').load('index.php?route=checkout/confirm|confirm&language={{ language }}');
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    });
});

// Shipping Method
$('#input-shipping-method').on('change', function () {
    var element = this;

    chain.attach(function () {
        return $.ajax({
            url: 'index.php?route=checkout/shipping_method|save&language={{ language }}',
            type: 'post',
            data: $('#form-shipping-method').serialize(),
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded',
            beforeSend: function () {
                $(element).prop('disabled', true);
            },
            complete: function () {
                $(element).prop('disabled', false);
            },
            success: function (json) {
                console.log(json);

                if (json['redirect']) {
                    location = json['redirect'];
                }

                if (json['error']) {
                    $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa-solid fa-circle-exclamation"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
                }

                if (json['success']) {
                    $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fa-solid fa-circle-check"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                    $('#button-payment-method').trigger('click');
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    });
});
//--></script>