<?php
/**
 * Maho
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2022 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php $_formMessage=$this->getPartialAuthorizationFormMessage(); ?>
<?php $_isPartialAuthorization=$this->isPartialAuthorization(); ?>
<?php if ($_isPartialAuthorization || $_formMessage): ?>
<div class="form-list" id="payment_form_<?= $this->getMethodCode() ?>_before" style="display:none;">
    <?php if ($_formMessage): ?>
    <?= $this->showNoticeMessage($_formMessage) ?>
    <?php endif ?>

    <?php if ($this->isPartialAuthorization()): ?>
        <?= $this->getChildHtml('cards') ?>
        <div class="release-amounts">
            <button class="button" type="button" id="payment-button-cancel">Cancel</button>
            <span><?= $this->__('To cancel pending authorizations and release amounts that have already been processed during this payment, click Cancel.') ?></span>
        </div>
        <?= $this->showNoticeMessage($this->__('Please enter another credit card number to complete your purchase.')) ?>

        <script type="text/javascript">
            var cancelButtonId = 'payment-button-cancel';
            var cancelButton = document.getElementById(cancelButtonId);
            cancelButton.onclick = null;
            cancelButton.addEventListener('click', cancelPaymentAuthorizations);

            function cancelPaymentAuthorizations(event, hideConfirm) {
                if (!hideConfirm && !confirm('<?= Mage::helper('core')->jsQuoteEscape($this->getCancelConfirmationMessage()) ?>')) {
                    return;
                }
                document.getElementById('cancel-please-wait').style.display = 'block';

                fetch('<?= $this->getCancelUrl() ?>', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    body: new URLSearchParams({
                        form_key: typeof FORM_KEY !== 'undefined' ? FORM_KEY : ''
                    }).toString()
                })
                    .then(response => {
                        document.getElementById('cancel-please-wait').style.display = 'none';
                        return response.text();
                    })
                    .then(responseText => {
                        let response;
                        try {
                            response = JSON.parse(responseText);
                        } catch (e) {
                            response = {};
                        }

                        if (response.success) {
                            const cancelButton = document.getElementById(cancelButtonId);
                            if (cancelButton) {
                                cancelButton.remove();
                            }

                            const checkoutPaymentMethod = document.getElementById('checkout-payment-method-load');
                            if (checkoutPaymentMethod) {
                                checkoutPaymentMethod.innerHTML = response.update_html;
                            }

                            if (typeof checkout !== 'undefined') {
                                checkout.reloadProgressBlock();
                            }
                        } else {
                            const msg = response.error_message;
                            if (msg) {
                                alert(msg);
                            }
                        }
                    })
                    .catch(error => {
                        console.error('Error:', error);
                    });
            }

            <?php if ($_message=$this->getPartialAuthorizationConfirmationMessage()): ?>
            if (!confirm('<?= Mage::helper('core')->jsQuoteEscape($_message) ?>')) {
                cancelPaymentAuthorizations(true, true);
            }
            <?php endif ?>
        </script>
        <span id="cancel-please-wait" class="please-wait" style="display:none">
            <img src="<?= $this->getSkinUrl('images/loading.svg') ?>" alt="" class="v-middle" /> <?= $this->__('Processing...') ?>
        </span>
    <?php endif ?>
</div>
<?php endif ?>
<?= $this->getChildHtml('method_form_block') ?>
