<?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_Cart_Shipping $this */ ?>
<div class="shipping">
    <h2><?= $this->__('Estimate Shipping and Tax') ?></h2>
    <div class="shipping-form">
       <form action="<?= $this->getFormActionUrl() ?>" method="post" id="shipping-zip-form">
            <p><?= $this->__('Enter your destination to get a shipping estimate.') ?></p>
            <ul class="form-list">
                <li>
                    <label for="country" class="required"><em>*</em><?= $this->__('Country') ?></label>
                    <div class="input-box">
                        <?= Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()) ?>
                    </div>
                </li>
            <?php //if($this->getStateActive()): ?>
                <li>
                    <label for="region_id"<?php if ($this->isStateProvinceRequired()) echo ' class="required"' ?>><?php if ($this->isStateProvinceRequired()) echo '<em>*</em>' ?><?= $this->__('State/Province') ?></label>
                    <div class="input-box">
                        <select id="region_id" name="region_id" title="<?= Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" style="display:none;"<?= ($this->isStateProvinceRequired() ? ' class="validate-select"' : '') ?>>
                            <option value=""><?= $this->__('Please select region, state or province') ?></option>
                        </select>
                       <script type="text/javascript">
                           document.getElementById('region_id').setAttribute('defaultValue', "<?= $this->getEstimateRegionId() ?>");
                       </script>
                       <input type="text" id="region" name="region" value="<?= $this->escapeHtml($this->getEstimateRegion()) ?>"  title="<?= Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" class="input-text" style="display:none;" />
                   </div>
                </li>
            <?php //endif ?>
            <?php if($this->getCityActive()): ?>
                <li>
                    <label for="city"<?php if ($this->isCityRequired()) echo ' class="required"' ?>><?php if ($this->isCityRequired()) echo '<em>*</em>' ?><?= $this->__('City') ?></label>
                    <div class="input-box">
                        <input class="input-text<?php if ($this->isCityRequired()):?> required-entry<?php endif ?>" id="city" type="text" name="estimate_city" value="<?= $this->escapeHtml($this->getEstimateCity()) ?>" />
                    </div>
                </li>
            <?php endif ?>
                <li>
                    <label for="postcode"<?php if ($this->isZipCodeRequired()) echo ' class="required"' ?>><?php if ($this->isZipCodeRequired()) echo '<em>*</em>' ?><?= $this->__('Zip/Postal Code') ?></label>
                    <div class="input-box">
                        <input class="input-text validate-postcode<?php if ($this->isZipCodeRequired()):?> required-entry<?php endif ?>" type="text" id="postcode" name="estimate_postcode" value="<?= $this->escapeHtml($this->getEstimatePostcode()) ?>" />
                    </div>
                </li>
            </ul>
            <div class="buttons-set">
                <button type="button" title="<?= Mage::helper('core')->quoteEscape($this->__('Get a Quote')) ?>" onclick="coShippingMethodForm.submit()" class="button"><?= $this->__('Get a Quote') ?></button>
            </div>
        </form>
        <script type="text/javascript">
            new RegionUpdater('country', 'region', 'region_id', <?= Mage::helper('directory')->getRegionJsonByStore() ?>);
        </script>

        <?php if (($_shippingRateGroups = $this->getEstimateRates())): ?>
        <form id="co-shipping-method-form" action="<?= $this->getUpdateFormActionUrl() ?>" method="post">
            <dl class="sp-methods">
                <?php foreach ($_shippingRateGroups as $code => $_rates): ?>
                    <dt><?= $this->escapeHtml($this->getCarrierName($code)) ?></dt>
                    <dd>
                        <ul>
                        <?php foreach ($_rates as $_rate): ?>
                            <li<?php if ($_rate->getErrorMessage()) echo ' class="error-msg"';?>>
                               <?php if ($_rate->getErrorMessage()): ?>
                                    <?= $this->escapeHtml($_rate->getErrorMessage()) ?>
                               <?php else: ?>
                                    <input name="estimate_method" type="radio" value="<?= $this->escapeHtml($_rate->getCode()) ?>" id="s_method_<?= $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> class="radio" />
                                    <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>
            <div class="buttons-set">
                <button type="submit" title="<?= Mage::helper('core')->quoteEscape($this->__('Update Total')) ?>" class="button" name="do" value="<?= Mage::helper('core')->quoteEscape($this->__('Update Total')) ?>"><?= $this->__('Update Total') ?></button>
            </div>
            <?= $this->getBlockHtml('formkey') ?>
        </form>
        <?php endif ?>
        <script type="text/javascript">
            var coShippingMethodForm = new VarienForm('shipping-zip-form');
            var countriesWithOptionalZip = <?= $this->helper('directory')->getCountriesWithOptionalZip(true) ?>;

            coShippingMethodForm.submit = function () {
                const country = document.getElementById('country').value;
                const optionalZip = countriesWithOptionalZip.includes(country);
                const postcodeElement = document.getElementById('postcode');
                postcodeElement.classList.toggle('required-entry', !optionalZip);
                return VarienForm.prototype.submit.bind(coShippingMethodForm)();
            };
        </script>
    </div>
</div>
