<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var \Magento\Customer\Block\Address\Edit $block */
?>
<?php
$helper = $this->helper('Codazon\ShippingCostCalculator\Helper\Data');
?>
<?php if ($helper->enableDisplaying()) : ?>
<?php
$displayCountry = $helper->getConfig('codazon_shipping_cost_calculator/general/display_country');
$displayRegion = $helper->getConfig('codazon_shipping_cost_calculator/general/display_region');
$displayPostCode = $helper->getConfig('codazon_shipping_cost_calculator/general/display_postcode');
$autoCalculate = (bool)$helper->getConfig('codazon_shipping_cost_calculator/general/auto_calculate');
$displayFlag = (bool)$helper->getConfig('codazon_shipping_cost_calculator/general/display_flag');
$widget = [
    'Codazon_ShippingCostCalculator/js/shipping-estimation' => [
        'countryId'     => '#country',
        'estimateUrl'   => $this->getUrl('shippingestimation'),
        'priceFormat'   => $helper->getPriceFormat(),
        'usePriceInclucdingTax' => $helper->usePriceInclucdingTax(),
        'flagUrl'       => $helper->getMediaUrl('codazon/flags/{{code}}.jpg'),
        'emptyMsg'      => $helper->getConfig('codazon_shipping_cost_calculator/general/no_shipping_message'),
        'autoCalculate' => $autoCalculate,
        'addressFormat' => $helper->getConfig('codazon_shipping_cost_calculator/general/address_format'),
        'displayFlag'   => $displayFlag
    ]
];

$helper->prepareShippingAddress($block);
$currentCountry = $block->getCountryId();
$attributeData = $block->getAttributeData();
?>
<div class="block-shipping-estimation" data-mage-init='<?= json_encode($widget) ?>'>
    <div class="form-address-edit" id="shipping-form">
        <div class="block-title block-heading">
            <strong><?= $block->escapeHtml($helper->getConfig('codazon_shipping_cost_calculator/general/title')) ?></strong>
            <?php if ($displayFlag) : ?>
            <img alt="<?= $currentCountry ?>" class="country-flag" data-role="country-flag" src="<?= $helper->getMediaUrl('codazon/flags/'.strtolower($currentCountry).'.jpg') ?>">
            <?php endif ?>
            <div data-role="current-address" style="display:none" class="current-address"></div>
            <a href="javascript:void(0)" data-role="content-toggle" class="content-toggle"><?= $helper->getConfig('codazon_shipping_cost_calculator/general/action_label') ?></a>
        </div>
        <div class="block-content" data-role="block-content" style="display:none;">
            <fieldset class="fieldset">
                <div class="field country required<?= (!$displayCountry)?' hidden':''; ?>">
                    <?php if ($displayRegion || $displayPostCode) : ?>
                    <label class="label" for="country"><span><?=  $attributeData->getFrontendLabel('country_id') ?></span></label>
                    <?php endif ?>
                    <div class="control">
                        <?= $block->getCountryHtmlSelect() ?>
                    </div>
                </div>
                <?php if ($displayRegion) : ?>
                <div class="field region">
                    <label class="label" for="region_id">
                        <span><?=  $attributeData->getFrontendLabel('region') ?></span>
                    </label>
                    <div class="control">
                        <select id="region_id" data-name="region_id" data-update="region_id" title="<?=  $attributeData->getFrontendLabel('region') ?>" class="validate-select" <?=  !$block->getConfig('general/region/display_all') ? ' disabled="disabled"' : '' ?>>
                            <option value=""><?= $block->escapeHtml(__('Please select a region, state or province.')) ?></option>
                        </select>
                        <input type="text"
                           data-update="region"
                           id="region"
                           data-name="region"
                           value="<?= $block->getRegion() ? $block->escapeHtmlAttr($block->getRegion()->getRegion()) : '' ?>"
                           title="<?=  $attributeData->getFrontendLabel('region') ?>"
                           class="input-text validate-not-number-first <?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('region')) ?>"/>
                    </div>
                </div>
                <?php endif ?>
                <?php if ($displayPostCode) : ?>
                <div class="field zip">
                    <label class="label" for="zip">
                        <span><?=  $attributeData->getFrontendLabel('postcode') ?></span>
                    </label>
                    <div class="control">
                        <input type="text"
                           data-update="postcode"
                           data-name="postcode"
                           value="<?= $block->getPostcode() ?>"
                           title="<?=  $attributeData->getFrontendLabel('postcode') ?>"
                           id="zip"
                           class="input-text validate-zip-international <?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('postcode')) ?>">
                    </div>
                </div>
                <?php endif ?>
                <?= $block->getChildHtml('extra_fields') ?>
            </fieldset>
            <div class="bottom-section">
                <div class="shipping-cost-result">
                    <div data-role="rs-container" class="rs-inner" style="display:none;">
                        <div class="rs-title" data-role="rs-label"><span><?= __('Shipping Cost') ?></span></div>
                        <div class="content" data-role="result"></div>
                    </div>
                    <div data-role="loader" class="loader-container" <?php if (!$autoCalculate) : ?>style="display:none;"<?php endif ?>>
                        <div class="shipping-loader">
                            <img src="<?= $block->getViewFileUrl('images/loader-1.gif') ?>" alt="<?= __('Loading...') ?>">
                        </div>
                    </div>
                </div>
                <?php if (!$autoCalculate) : ?>
                <div class="actions">
                    <button data-role="submit" class="action btn-submit" type="button"><span><?= __('Calculate') ?></span></button>
                </div>
                <?php endif ?>
            </div>
        </div>
    </div>
</div>
<script type="text/x-magento-init">
    {
        <?php /* "#shipping-form": {
            "addressValidation": {}
        } */ ?>
        <?php if ($displayRegion) : ?>
         "#country": {
            "regionUpdater": {
                "optionalRegionAllowed": true,
                "regionListId": "#region_id",
                "regionInputId": "#region",
                "postcodeId": "#zip",
                "form": "#shipping-form",
                "regionJson": <?=  $this->helper(\Magento\Directory\Helper\Data::class)->getRegionJson() ?>,
                "defaultRegion": "<?= (int) $block->getRegionId() ?>",
                "countriesWithOptionalZip": <?=  $this->helper(\Magento\Directory\Helper\Data::class)->getCountriesWithOptionalZip(true) ?>
            }
        }
        <?php endif ?>
    }
</script>
<?php endif ?>