<?php
/**
 * Maho
 *
 * @category    design
 * @package     rwd_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2024 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)
 */
/** @var Mage_Paypal_Block_Express_Review $this */
?>
<div class="page-title">
    <h1><?= $this->__('Review Order') ?></h1>
</div>
<?= $this->getMessagesBlock()->toHtml() ?>
<?php if ($this->getShippingAddress()): ?>
    <div class="info-set col2-set">
        <h2 class="legend"><?= $this->__('Shipping Information') ?></h2>
        <div class="col-1">
            <div class="box">
                <div class="box-title">
                    <h3><?= $this->__('Shipping Address') ?><span class="separator"><?php if ($this->getCanEditShippingAddress()):?> | </span><a href="<?= $this->getEditUrl() ?>"><?= $this->__('Edit') ?></a><?php endif ?></h3>
                </div>
                <div class="box-content">
                    <address><?= $this->renderAddress($this->getShippingAddress()) ?></address>
                </div>
            </div>
        </div>
        <div class="col-2">
            <div class="box">
                <div class="box-title">
                    <h3><?= $this->__('Shipping Method') ?></h3>
                </div>
                <div class="box-content">
                    <form method="post" id="shipping_method_form" action="<?= $this->escapeHtml($this->getShippingMethodSubmitUrl()) ?>">
                        <?php if ($this->getCanEditShippingMethod() || !$this->getCurrentShippingRate()):?>
                            <?php if ($groups = $this->getShippingRateGroups()):?>
                                <?php $currentRate = $this->getCurrentShippingRate(); ?>
                                <fieldset>
                                    <select name="shipping_method" id="shipping_method" style="width:250px;" class="required-entry">
                                        <?php if (!$currentRate):?>
                                            <option value=""><?= $this->__('Please select a shipping method...') ?></option>
                                        <?php endif ?>
                                        <?php foreach ($groups as $code => $rates):?>
                                            <optgroup label="<?= $this->escapeHtml($this->getCarrierName($code)) ?>" style="font-style:normal;">
                                                <?php foreach ($rates as $rate):?>
                                                    <option value="<?= $this->renderShippingRateValue($rate) ?>"<?= ($currentRate === $rate) ? ' selected="selected"' : ''  ?>>
                                                        <?= $this->renderShippingRateOption($rate) ?>
                                                    </option>
                                                <?php endforeach ?>
                                            </optgroup>
                                        <?php endforeach ?>
                                    </select>
                                </fieldset>
                                <p class="actions">
                                    <button id="update_shipping_method_submit" type="submit" class="button"><?= $this->__('Update Shipping Method') ?></button>
                                </p>
                            <?php else: ?>
                                <p><strong><?= $this->__('Sorry, no quotes are available for this order at this time.') ?></strong></p>
                            <?php endif ?>
                        <?php else: ?>
                            <p><strong><?= $this->renderShippingRateOption($this->getCurrentShippingRate()) ?></strong></p>
                        <?php endif ?>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php endif ?>

<div class="info-set col2-set">
    <h2 class="legend"><?= $this->__('Billing Information') ?></h2>
    <div class="box-title">
        <h3><?= $this->__('Payment Method') ?> <span class="separator">|</span>
            <?php if($this->getEditUrl()):?><a href="<?= $this->getEditUrl() ?>"><?= $this->__('Edit Payment Information') ?></a> <?php endif ?>
        </h3>
    </div>
    <div class="box-content">
        <?= $this->escapeHtml($this->getPaymentMethodTitle()) ?>
    </div>
</div>

<div class="info-set">
    <h2 class="legend"><?= $this->__('Items in Your Shopping Cart') ?><span class="separator"> | </span><a href="<?= $this->getUrl('checkout/cart') ?>"><?= $this->__('Edit Shopping Cart') ?></a></h2>
    <div id="details-reload">
        <?= $this->getChildHtml('details') ?>
    </div>
</div>
<form method="post" id="order_review_form" action="<?= $this->getPlaceOrderUrl() ?>">
    <?= $this->getChildHtml('agreements') ?>
    <div class="buttons-set buttons-set-order" id="review-buttons-container">
        <button type="button" id="review_button" value="<?= $this->__('Place Order') ?>" class="button btn-checkout"><?= $this->__('Place Order') ?></button>
        <button type="button" id="review_submit" value="<?= $this->__('Place Order') ?>" class="button btn-checkout"><?= $this->__('Place Order') ?></button>
        <span class="please-wait" id="review-please-wait" style="display:none;">
            <img src="<?= $this->getSkinUrl('images/loading.svg') ?>" alt="<?= $this->quoteEscape($this->__('Submitting order information...')) ?>" title="<?= $this->quoteEscape($this->__('Submitting order information...')) ?>" class="v-middle" /> <?= $this->__('Submitting order information...') ?>
        </span>
    </div>
</form>
<script type="text/javascript">
    // submit buttons are not needed when submitting with ajax
    document.getElementById('review_submit').style.display = 'none';
    const shippingMethodSubmit = document.getElementById('update_shipping_method_submit');
    if (shippingMethodSubmit) {
        shippingMethodSubmit.style.display = 'none';
    }

    <?php if ($this->getUseAjax()):?>
    OrderReviewController.prototype._submitOrder = function() {
        if (this._canSubmitOrder) {
            if (this._pleaseWait) {
                this._pleaseWait.style.display = 'block';
            }

            fetch(this.form.action, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams({
                    isAjax: 1,
                    method: 'POST'
                })
            })
                .then(response => response.json())
                .then(response => {
                    if (response.redirect) {
                        window.location.href = response.redirect;
                        return;
                    }
                    if (response.success) {
                        window.location.href = '<?= $this->getSuccessUrl() ?>';
                        return;
                    }

                    if (response.error_messages) {
                        let msg = response.error_messages;
                        if (Array.isArray(msg)) {
                            msg = msg.join("\n");
                        }
                        if (msg) {
                            document.getElementById('review-please-wait').style.display = 'none';
                            alert(msg);
                            return;
                        }
                    }

                    document.getElementById('review-please-wait').style.display = 'none';
                    alert('<?= $this->jsQuoteEscape($this->__('Unknown Error. Please try again later.')) ?>');
                })
                .catch(() => {
                    alert('<?= $this->jsQuoteEscape($this->__('Server Error. Please try again.')) ?>');
                    document.getElementById('review-please-wait').style.display = 'none';
                });
        }
    }
    <?php endif ?>

    PayPalExpressAjax = new OrderReviewController(
        document.getElementById('order_review_form'),
        document.getElementById('review_button'),
        document.getElementById('shipping_method'),
        document.getElementById('shipping_method_form'),
        'details-reload'
    );
    PayPalExpressAjax.addPleaseWait(document.getElementById('review-please-wait'));
</script>
