<form id="form-payment-method">
  <fieldset>
    <legend>Step 3: Payment Method</legend>
    <div class="input-group">
      <select name="payment_method" id="input-payment-method" class="form-select"{% if not payment_methods %} disabled{% endif %}>
        <option value="">{{ text_select }}</option>
        {% for payment_method in payment_methods %}
          <option value="{{ payment_method.code }}"{% if payment_method.code == code %} selected{% endif %}>{{ payment_method.title }}</option>
        {% endfor %}

        {% if stores %}
          <optgroup label="{{ text_stored }}">
            {% for store in stores %}
              <option value="{{ store.code }}"{% if store.code == code %} selected{% endif %}>{{ store.name }}</option>
            {% endfor %}
          </optgroup>
        {% endif %}
      </select>
      <button type="button" id="button-payment-method" class="btn btn-light"><i class="fa-solid fa-rotate"></i></button>
    </div>
  </fieldset>
</form>
<br/>
<div class="mb-2">
  <fieldset for="input-comment"><legend>Step 4:  {{ text_comments }}</legend></fieldset> <textarea name="comment" rows="3" id="input-comment" class="form-control">{{ comment }}</textarea>
</div>
{% if text_agree %}
  <div class="mb-2">
    <div class="text-end">{{ text_agree }} <input type="checkbox" name="agree" value="1" id="input-agree" class="form-check-input"{% if agree %} checked{% endif %}/></div>
  </div>
{% endif %}
<script type="text/javascript"><!--
// Payment Methods
$('#button-payment-method').on('click', function () {
    var element = this;

    chain.attach(function () {
        return $.ajax({
            url: 'index.php?route=checkout/payment_method|getMethods&language={{ language }}',
            dataType: 'json',
            beforeSend: function () {
                $('#input-payment-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>');

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

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

                if (json['payment_methods']) {
                    for (i in json['payment_methods']) {
                        if (json['payment_methods'][i]['code'] == $('#input-payment-method').val()) {
                            html += '<option value="' + json['payment_methods'][i]['code'] + '" selected>' + json['payment_methods'][i]['title'] + '</option>';
                        } else {
                            html += '<option value="' + json['payment_methods'][i]['code'] + '">' + json['payment_methods'][i]['title'] + '</option>';
                        }
                    }

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

                $('#input-payment-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);
            }
        });
    });
});

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

    chain.attach(function () {
        return $.ajax({
            url: 'index.php?route=checkout/payment_method|save&language={{ language }}',
            type: 'post',
            data: $('#form-payment-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>');

                    $('#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);
            }
        });
    });
});

// Comment
$('#input-comment').on('focusout', function () {
    var element = this;

    chain.attach(function () {
        return $.ajax({
            url: 'index.php?route=checkout/payment_method|comment&language={{ language }}',
            type: 'post',
            data: $('#input-comment').serialize(),
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded',
            beforeSend: function () {
                $(element).prop('disabled', true);
            },
            complete: function () {
                $(element).prop('disabled', false);
            },
            success: function (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>');
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    });
});

/* Agree to Terms */
$('#input-agree').on('change', function () {
    var element = this;

    chain.attach(function () {
        return $.ajax({
            url: 'index.php?route=checkout/payment_method|agree&language={{ language }}',
            type: 'post',
            data: $('#input-agree').serialize(),
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded',
            beforeSend: function () {
                $(element).prop('disabled', true);
            },
            complete: function () {
                $(element).prop('disabled', false);
            },
            success: function (json) {
                $('#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);
            }
        });
    });
});
//--></script>
