<?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)
 */
?>
<?php /** @var Mage_Checkout_Block_Multishipping_Overview $this */ ?>
<div class="multiple-checkout">
    <div class="page-title">
        <h1><?= $this->__('Review Order') ?></h1>
    </div>
    <?= $this->getMessagesBlock()->getGroupedHtml() ?>
    <form id="review-order-form" action="<?= $this->getPostActionUrl() ?>" method="post" onsubmit="return showLoader();">
        <?= $this->getBlockHtml('formkey') ?>
        <div class="col2-set">
            <h2 class="legend"><?= $this->__('Billing Information') ?></h2>
            <div class="col-1">
                <div class="box">
                    <?php $_address=$this->getBillingAddress() ?>
                    <div class="box-title">
                        <h3><?= $this->__('Billing Address') ?> <span class="separator">|</span> <a href="<?= $this->getEditBillingAddressUrl($_address) ?>"><?= $this->__('Change') ?></a></h3>
                    </div>
                    <div class="box-content">
                        <address>
                            <?= $_address->format('html') ?>
                        </address>
                    </div>
                </div>
            </div>
            <div class="col-2">
                <div class="box">
                    <div class="box-title">
                        <h3><?= $this->__('Payment Method') ?> <span class="separator">|</span> <a href="<?= $this->getEditBillingUrl() ?>"><?= $this->__('Change') ?></a></h3>
                    </div>
                    <div class="box-content">
                        <input type="hidden" name="payment[cc_number]" value="<?= $this->escapeHtml($this->getPayment()->getCcNumber()) ?>" />
                        <input type="hidden" name="payment[cc_cid]" value="<?= $this->escapeHtml($this->getPayment()->getCcCid()) ?>" />
                        <?= $this->getPaymentHtml() ?>
                    </div>
                </div>
            </div>
         </div>
        <?php $mergedCells = ($this->helper('tax')->displayCartBothPrices() ? 2 : 1); ?>
        <div class="col2-set">
        <h2 class="legend"><?= $this->__('Shipping Information') ?></h2>
        <?php foreach ($this->getShippingAddresses() as $_index => $_address): ?>
            <h3 class="legend"><?= $this->__('Address %s of %s', ($_index+1), $this->getShippingAddressCount()) ?></h3>
                <div class="col-1 col-narrow">
                    <div class="box">
                        <div class="box-title">
                            <h4><?= $this->__('Shipping To') ?> <span class="separator">|</span> <a href="<?= $this->getEditShippingAddressUrl($_address) ?>"><?= $this->__('Change') ?></a></h4>
                        </div>
                        <div class="box-content">
                            <address>
                                <?= $_address->format('html') ?>
                            </address>
                        </div>
                    </div>
                    <div class="box">
                        <div class="box-title">
                            <h4><?= $this->__('Shipping Method') ?> <span class="separator">|</span> <a href="<?= $this->getEditShippingUrl() ?>"><?= $this->__('Change') ?></a></h4>
                        </div>
                        <div class="box-content">
                            <?php if($_rate=$this->getShippingAddressRate($_address)): ?>
                            <p>
                            <?= $this->escapeHtml($_rate->getCarrierTitle()) ?> - <?= $this->escapeHtml($_rate->getMethodTitle()) ?>
                            <?php $_excl = $this->getShippingPriceExclTax($_address); ?>
                            <?php $_incl = $this->getShippingPriceInclTax($_address); ?>
                            <?= $_excl ?>
                            <?php if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl): ?>
                                (<?= $this->__('Incl. Tax') ?> <?= $_incl ?>)
                            <?php endif ?>
                            </p>
                            <?php endif ?>
                        </div>
                    </div>
                </div>
                <div class="col-2 col-wide">
                    <h4><?= $this->__('Items') ?> <span class="separator">|</span> <a href="<?= $this->getAddressesEditUrl() ?>"><?= $this->__('Edit Items') ?></a></h4>
                    <?php $_tableClass = $this->helper('tax')->displayCartBothPrices() ? 'linearize-table-large' : 'linearize-table'; ?>
                    <table class="data-table <?= $_tableClass ?> checkout-review-table" id="overview-table-<?= $_address->getId() ?>">
                        <col />
                        <col width="1" />
                        <?php if ($this->helper('tax')->displayCartBothPrices()): ?>
                        <col width="1" />
                        <?php endif ?>
                        <col width="1" />
                        <col width="1" />
                        <?php if ($this->helper('tax')->displayCartBothPrices()): ?>
                        <col width="1" />
                        <?php endif ?>
                        <thead>
                            <tr>
                                <th rowspan="<?= $mergedCells ?>"><?= $this->__('Product Name') ?></th>
                                <th colspan="<?= $mergedCells ?>" class="a-center"><?= $this->__('Price') ?></th>
                                <th rowspan="<?= $mergedCells ?>"><?= $this->__('Qty') ?></th>
                                <th colspan="<?= $mergedCells ?>" class="a-center"><?= $this->__('Subtotal') ?></th>
                            </tr>
                            <?php if ($this->helper('tax')->displayCartBothPrices()): ?>
                            <tr>
                                <th class="a-right"><?= $this->helper('tax')->getIncExcTaxLabel(false) ?></th>
                                <th><?= $this->helper('tax')->getIncExcTaxLabel(true) ?></th>
                                <th class="a-right"><?= $this->helper('tax')->getIncExcTaxLabel(false) ?></th>
                                <th><?= $this->helper('tax')->getIncExcTaxLabel(true) ?></th>
                            </tr>
                            <?php endif ?>
                        </thead>
                        <tfoot>
                            <?= $this->renderTotals($this->getShippingAddressTotals($_address)) ?>
                        </tfoot>
                        <tbody>
                        <?php foreach ($this->getShippingAddressItems($_address) as $_item): ?>
                            <?= $this->getRowItemHtml($_item) ?>
                        <?php endforeach ?>
                        </tbody>
                    </table>
                </div>
            <?php if($this->getShippingAddressCount()!=$_index+1): ?>
                <div class="divider"></div>
            <?php endif ?>
        <?php endforeach ?>
        </div>
        <?php if ($this->getQuote()->hasVirtualItems()): ?>
        <div class="col2-set">
            <h2 class="legend"><?= $this->__('Other Items in Your Order') ?></h2>
            <div class="col-1 col-narrow"></div>
            <div class="col-2 col-wide">
                <h3><?= $this->__('Items') ?> <span class="separator">|</span> <a href="<?= $this->getVirtualProductEditUrl() ?>"><?= $this->__('Edit Items') ?></a></h3>
                <?php $mergedCells = ($this->helper('tax')->displayCartBothPrices() ? 2 : 1); ?>
                <?php $_tableClass = $this->helper('tax')->displayCartBothPrices() ? 'linearize-table-large' : 'linearize-table'; ?>
                <table class="data-table <?= $_tableClass ?> checkout-review-table" id="virtual-overview-table">
                    <col />
                    <col width="1" />
                    <?php if ($this->helper('tax')->displayCartBothPrices()): ?>
                    <col width="1" />
                    <?php endif ?>
                    <col width="1" />
                    <col width="1" />
                    <?php if ($this->helper('tax')->displayCartBothPrices()): ?>
                    <col width="70" />
                    <?php endif ?>
                    <thead>
                        <tr>
                            <th rowspan="<?= $mergedCells ?>"><?= $this->__('Product Name') ?></th>
                            <th colspan="<?= $mergedCells ?>" class="a-center"><?= $this->__('Price') ?></th>
                            <th rowspan="<?= $mergedCells ?>"><?= $this->__('Qty') ?></th>
                            <th colspan="<?= $mergedCells ?>" class="a-center"><?= $this->__('Subtotal') ?></th>
                        </tr>
                        <?php if ($this->helper('tax')->displayCartBothPrices()): ?>
                        <tr>
                            <th class="a-right"><?= $this->helper('tax')->getIncExcTaxLabel(false) ?></th>
                            <th><?= $this->helper('tax')->getIncExcTaxLabel(true) ?></th>
                            <th class="a-right"><?= $this->helper('tax')->getIncExcTaxLabel(false) ?></th>
                            <th><?= $this->helper('tax')->getIncExcTaxLabel(true) ?></th>
                        </tr>
                        <?php endif ?>
                    </thead>
                    <tfoot>
                        <?= $this->renderTotals($this->getBillinAddressTotals()) ?>
                    </tfoot>
                    <tbody>
                    <?php foreach ($this->getVirtualItems() as $_item): ?>
                        <?= $this->getRowItemHtml($_item) ?>
                    <?php endforeach ?>
                    </tbody>
                    </table>
                </div>
        </div>
        <?php endif ?>

           <?= $this->getChildHtml('items_after') ?>

            <div id="checkout-review-submit">
                <?= $this->getChildHtml('agreements') ?>
                <div class="place-order">
                    <div class="grand-total">
                        <div class="inner">
                            <big><?= $this->__('Grand Total:') ?> <?= $this->helper('checkout')->formatPrice($this->getTotal()) ?></big>
                            <div id="review-buttons-container">
                                <button type="submit" title="<?= $this->quoteEscape($this->__('Place Order')) ?>" class="button btn-checkout" id="review-button"><?= $this->__('Place Order') ?></button>
                            </div>
                        </div>
                    </div>
                    <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>
            </div>
            <div class="buttons-set">
                <p class="back-link"><a href="<?= $this->getBackUrl() ?>"><small>&laquo; </small><?= $this->__('Back to Billing Information') ?></a></p>
            </div>
        </form>
        <script type="text/javascript">
            var submitted = false;
            function showLoader() {
                if (submitted) {
                    return false;
                }

                const agreements = document.getElementById('checkout-agreements');
                if (agreements) {
                    const checkboxes = agreements.querySelectorAll('input');
                    for (let i = 0; i < checkboxes.length; i++) {
                        if (!checkboxes[i].checked) {
                            alert("<?= $this->jsQuoteEscape($this->__('Please agree to all Terms and Conditions before placing the orders.')) ?>");
                            return false;
                        }
                    }
                }

                submitted = true;
                const step = 'review';
                document.getElementById(step + '-please-wait').style.display = 'block';

                const buttonsContainer = document.getElementById(step + '-buttons-container');
                buttonsContainer.style.opacity = '0.5';

                const elements = buttonsContainer.getElementsByTagName('*');
                Array.from(elements).forEach(element => {
                    element.disabled = true;
                });

                return true;
            }
        </script>
</div>
