<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php
/**
 * @var \Magento\Sales\Block\Adminhtml\Order\Create\Coupons $block
 * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
 */
?>
<div class="admin__field field-apply-coupon-code">
    <label class="admin__field-label"><span><?= $block->escapeHtml(__('Apply Coupon Code')) ?></span></label>
    <div class="admin__field-control">
        <?php if (!$block->getCouponCode()): ?>
        <input type="text" class="admin__control-text" id="coupons:code" value="" name="coupon_code" />
            <?= $block->getButtonHtml(__('Apply'), 'order.handleOnclickCoupon($F(\'coupons:code\'))') ?>
        <?php endif; ?>
        <?php if ($block->getCouponCode()): ?>
        <p class="added-coupon-code">
            <span><?= $block->escapeHtml($block->getCouponCode()) ?></span>
            <a href="#" title="<?= $block->escapeHtmlAttr(__('Remove Coupon Code')) ?>"
               class="action-remove"><span><?= $block->escapeHtml(__('Remove')) ?></span></a>
        </p>
            <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
                'onclick',
                "order.applyCoupon('');event.preventDefault();",
                'p.added-coupon-code a.action-remove'
            ) ?>
        <?php endif; ?>
        <?php $isVirtual = ($block->getQuote()->isVirtual() ? 'false' : 'true');
        $scriptString = <<<script
            require([
                "jquery",
                'Magento_Ui/js/modal/alert',
                'mage/translate',
                "Magento_Sales/order/create/form"
            ], function($, alert) {
                order.overlay('shipping-method-overlay', {$isVirtual});
                order.overlay('address-shipping-overlay', {$isVirtual});
                order.handleOnclickCoupon = function (code) {
                    if (!code) {
                        alert({
                            content: $.mage.__('Please enter a coupon code!')
                        });
                    } else {
                        order.applyCoupon(code);
                    }
                };
            });
script;
        ?>
        <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
    </div>
</div>
