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

/**
 * @var $block \Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method\Form
 * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
 */
?>
<?php
/** @var \Magento\Tax\Helper\Data $taxHelper */
$taxHelper = $block->getData('taxHelper');
?>
<?php $_shippingRateGroups = $block->getShippingRates(); ?>
<?php if ($_shippingRateGroups): ?>
    <div id="order-shipping-method-choose" class="control">
        <dl class="admin__order-shipment-methods">
        <?php foreach ($_shippingRateGroups as $code => $_rates): ?>
            <dt class="admin__order-shipment-methods-title"><?= $block->escapeHtml($block->getCarrierName($code)) ?>
            </dt>
            <dd class="admin__order-shipment-methods-options">
                <ul class="admin__order-shipment-methods-options-list">
                <?php foreach ($_rates as $_rate): ?>
                    <?php $_radioProperty = 'name="order[shipping_method]" type="radio"' ?>
                    <?php $_code = $_rate->getCode() ?>
                    <li class="admin__field-option">
                        <?php if ($_rate->getErrorMessage()): ?>
                            <div class="messages">
                               <div class="message message-error error">
                                   <div><?= $block->escapeHtml($_rate->getErrorMessage()) ?></div>
                               </div>
                            </div>
                        <?php else: ?>
                            <?php $_checked = $block->isMethodActive($_code) ? 'checked="checked"' : '' ?>
                            <input <?= /* @noEscape */ $_radioProperty ?>
                                value="<?= $block->escapeHtmlAttr($_code) ?>"
                                id="s_method_<?= $block->escapeHtmlAttr($_code) ?>" <?= /* @noEscape */ $_checked ?>
                                class="admin__control-radio required-entry"/>
                            <label class="admin__field-label" for="s_method_<?= $block->escapeHtmlAttr($_code) ?>">
                                <?= $block->escapeHtml($_rate->getMethodTitle() ?
                                    $_rate->getMethodTitle() : $_rate->getMethodDescription()) ?> -
                                <strong>
                                    <?php $_excl = $block->getShippingPrice(
                                        $_rate->getPrice(),
                                        $taxHelper->displayShippingPriceIncludingTax()
                                    ); ?>
                                    <?php $_incl = $block->getShippingPrice($_rate->getPrice(), true); ?>

                                    <?= /* @noEscape */ $_excl ?>
                                    <?php if ($taxHelper->displayShippingBothPrices() && $_incl != $_excl): ?>
                                        (<?= $block->escapeHtml(__('Incl. Tax')) ?> <?= /* @noEscape */ $_incl ?>)
                                    <?php endif; ?>
                                </strong>
                            </label>
                            <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
                                'onclick',
                                "order.setShippingMethod(this.value)",
                                'input#s_method_' . $block->escapeHtmlAttr($_code)
                            ) ?>
                        <?php endif; ?>
                    </li>
                <?php endforeach; ?>
                </ul>
            </dd>
        <?php endforeach; ?>
        </dl>
    </div>
    <?= /* @noEscape */ $secureRenderer->renderStyleAsTag("display:none", 'div#order-shipping-method-choose') ?>
    <?php if ($_rate = $block->getActiveMethodRate()): ?>
        <div id="order-shipping-method-info" class="order-shipping-method-info">
            <dl class="admin__order-shipment-methods">
                <dt class="admin__order-shipment-methods-title">
                    <?= $block->escapeHtml($block->getCarrierName($_rate->getCarrier())) ?>
                </dt>
                <dd class="admin__order-shipment-methods-options">
                    <?= $block->escapeHtml($_rate->getMethodTitle() ?
                        $_rate->getMethodTitle() : $_rate->getMethodDescription()) ?> -
                    <strong>
                        <?php $_excl = $block->getShippingPrice(
                            $_rate->getPrice(),
                            $taxHelper->displayShippingPriceIncludingTax()
                        ); ?>
                        <?php $_incl = $block->getShippingPrice($_rate->getPrice(), true); ?>

                        <?= /* @noEscape */ $_excl ?>
                        <?php if ($taxHelper->displayShippingBothPrices() && $_incl != $_excl): ?>
                            (<?= $block->escapeHtml(__('Incl. Tax')) ?> <?= /* @noEscape */ $_incl ?>)
                        <?php endif; ?>
                    </strong>
                </dd>
            </dl>
            <a href="#"
               class="action-default">
                <span><?= $block->escapeHtml(__('Click to change shipping method')) ?></span>
            </a>
        </div>
        <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
            'onclick',
            "$('order-shipping-method-info').hide();$('order-shipping-method-choose').show();event.preventDefault()",
            'div#order-shipping-method-info a.action-default'
        ) ?>
    <?php else: ?>
        <?php $scriptString = <<<script
require(['prototype'], function(){
    $('order-shipping-method-choose').show();
});
script;
        ?>
        <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
    <?php endif; ?>
<?php elseif ($block->getIsRateRequest()): ?>
    <div class="order-shipping-method-summary">
        <strong class="order-shipping-method-not-available">
            <?= $block->escapeHtml(__('Sorry, no quotes are available for this order.')) ?>
        </strong>
    </div>
<?php else: ?>
    <div id="order-shipping-method-summary" class="order-shipping-method-summary">
        <a href="#" class="action-default">
            <span><?= $block->escapeHtml(__('Get shipping methods and rates')) ?></span>
        </a>
        <input type="hidden" name="order[has_shipping]" value="" class="required-entry" />
    </div>
    <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
        'onclick',
        "order.loadShippingRates();event.preventDefault();",
        'div#order-shipping-method-summary a.action-default'
    ) ?>
<?php endif; ?>
<div id="shipping-method-overlay" class="order-methods-overlay">
    <span><?= $block->escapeHtml(__('You don\'t need to select a shipping method.')) ?></span>
</div>
<?= /* @noEscape */ $secureRenderer->renderStyleAsTag("display: none;", 'div#shipping-method-overlay') ?>
<?php $scriptString = <<<script
    require(["Magento_Sales/order/create/form"], function(){

script;
$scriptString .= "order.overlay('shipping-method-overlay', " . ($block->getQuote()->isVirtual() ? 'false' : 'true') .
    ');' . PHP_EOL;
$scriptString .= "order.overlay('address-shipping-overlay', " . ($block->getQuote()->isVirtual() ? 'false' : 'true') .
 ');' . PHP_EOL;
$scriptString .= "order.isOnlyVirtualProduct = " . ($block->getQuote()->isVirtual() ? 'true' : 'false') . ';' . PHP_EOL;
$scriptString .= <<<script
    });
script;
?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
