<?php
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

/**
 * @var Template $block
 * @var Escaper $escaper
 */
?>
<template x-if="cartData.items && cartData.items.length">
    <div class="coupon-form pt-6">
        <div class="mx-auto md:mx-0">
            <div class="text-left">
                <span @click="showCouponForm = !showCouponForm"
                      class="cursor-pointer text-black font-semibold select-none whitespace-nowrap"
                      id="discound-form-toggle"
                >
                    <span>
                        <?= $escaper->escapeHtml(__('Apply Discount')) ?>
                    </span>
                    <svg xmlns="http://www.w3.org/2000/svg" fill="none" class="w-4 h-4 inline-block ml-2"
                         viewBox="0 0 24 24" stroke="currentColor">
                        <path x-show="!showCouponForm" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                              d="M19 9l-7 7-7-7"/>
                        <path
                            x-show="showCouponForm" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                            d="M5 15l7-7 7 7"/>
                    </svg>
                </span>
            </div>
            <div>
                <form id="discount-coupon-form"
                      class="block my-4"
                      x-show="showCouponForm"
                      x-on:submit.prevent="dispatchCouponMutationRequest();"
                      action=""
                      method="post"
                >
                    <div class="fieldset coupon">
                        <input type="hidden" name="remove" id="remove-coupon" value="0"/>
                        <div class="flex flex-col sm:flex-row gap-2 justify-center md:justify-start">
                            <input type="text"
                                   class="form-input text-center md:text-left border-background-dark"
                                   id="coupon_code"
                                   name="coupon_code"
                                   value=""
                                   :placeholder="cartData.applied_coupons ?
                                    cartData.applied_coupons[0]['code'] :
                                    '<?= $escaper->escapeHtmlAttr(__('Enter discount code')) ?>'"
                                   :disabled="cartData.applied_coupons"
                            />
                                <button class="btn btn-primary md:text-sm lg:text-base justify-center" type="submit"
                                        value="<?= $escaper->escapeHtmlAttr(__('Apply Coupon Code')) ?>">
                                    <span x-show="!cartData.applied_coupons">
                                        <?= $escaper->escapeHtml(__('Apply Discount')) ?>
                                    </span>
                                    <span x-show="cartData.applied_coupons">
                                        <?= $escaper->escapeHtml(__('Cancel Coupon')) ?>
                                    </span>
                                </button>
                        </div>
                    </div>
                    <?php /**
                           * @todo: revive captcha integration?
                        $block->getChildHtml('captcha')
                           */ ?>
                </form>
            </div>
        </div>
    </div>
</template>
