<?php
/**
 * Maho
 *
 * @category    design
 * @package     base_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_Onepage_Shipping_Method_Available $this */ ?>
<?php $_shippingRateGroups = $this->getShippingRates(); ?>
<?php if (!$_shippingRateGroups): ?>
    <p><?= $this->__('Sorry, no quotes are available for this order at this time.') ?></p>
<?php else: ?>
    <dl class="sp-methods">
    <?php $shippingCodePrice = []; ?>
    <?php $_sole = count($_shippingRateGroups) == 1; foreach ($_shippingRateGroups as $code => $_rates): ?>
        <dt><?= $this->escapeHtml($this->getCarrierName($code)) ?></dt>
        <dd>
            <ul>
            <?php $_sole = $_sole && count($_rates) == 1; foreach ($_rates as $_rate): ?>
                <?php $shippingCodePrice[] = "'".$_rate->getCode()."':".(float)$_rate->getPrice(); ?>
                <li>
                   <?php if ($_rate->getErrorMessage()): ?>
                    <ul class="messages"><li class="error-msg"><ul><li><?= $this->escapeHtml($_rate->getErrorMessage()) ?></li></ul></li></ul>
                   <?php else: ?>
                        <?php if ($_sole) : ?>
                        <span class="no-display"><input name="shipping_method" type="radio" value="<?= $_rate->getCode() ?>" id="s_method_<?= $_rate->getCode() ?>" checked="checked" /></span>
                        <?php else: ?>
                        <input name="shipping_method" type="radio" value="<?= $_rate->getCode() ?>" id="s_method_<?= $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> class="radio"/>

                        <?php if ($_rate->getCode() === $this->getAddressShippingMethod()): ?>
                        <script type="text/javascript">
                            lastPrice = <?= (float)$_rate->getPrice() ?>;
                        </script>
                        <?php endif ?>

                        <?php endif ?>
                        <label for="s_method_<?= $_rate->getCode() ?>"><?= $this->escapeHtml($_rate->getMethodTitle()) ?>
                        <?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
                        <?php $_incl = $this->getShippingPrice($_rate->getPrice(), true); ?>
                        <?= $_excl ?>
                        <?php if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl): ?>
                            (<?= $this->__('Incl. Tax') ?> <?= $_incl ?>)
                        <?php endif ?>
                        </label>
                   <?php endif ?>
                </li>
            <?php endforeach ?>
            </ul>
        </dd>
    <?php endforeach ?>
    </dl>
    <script type="text/javascript">
        <?php if (!empty($shippingCodePrice)): ?>
        var shippingCodePrice = {<?= implode(',',$shippingCodePrice) ?>};
        <?php endif ?>

        document.querySelectorAll('input[type="radio"][name="shipping_method"]').forEach(function(el) {
            el.addEventListener('click', function() {
                if (el.checked) {
                    var getShippingCode = el.value;
                    <?php if (!empty($shippingCodePrice)): ?>
                    var newPrice = shippingCodePrice[getShippingCode];
                    if (!lastPrice) {
                        lastPrice = newPrice;
                        quoteBaseGrandTotal += newPrice;
                    }
                    if (newPrice != lastPrice) {
                        quoteBaseGrandTotal += (newPrice-lastPrice);
                        lastPrice = newPrice;
                    }
                    <?php endif ?>
                    checkQuoteBaseGrandTotal = quoteBaseGrandTotal;
                    return false;
                }
            });
        });
    </script>
<?php endif ?>
