<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

declare(strict_types=1);

use Hyva\Theme\ViewModel\ReCaptcha;
use Magento\Checkout\Block\Cart\Coupon;
use Magento\Framework\Escaper;

/** @var Coupon $block */
/** @var Escaper $escaper */
/** @var ReCaptcha $recaptcha */

// We should use strlen function because coupon code could be "0", converted to bool will lead to false
$hasCouponCode = (bool) strlen($block->getCouponCode() ?: "");
$recaptcha = $block->getData('viewModelRecaptcha');
?>
<script>
    function initCouponForm() {
        return {
            errors: 0,
            hasCaptchaToken: 0,
            showCouponForm: <?= $hasCouponCode ? 1 : 0 ?>,
            formData: {
                coupon_code: '<?= $escaper->escapeJs($block->getCouponCode()) ?>',
                remove: '<?= (int) $hasCouponCode ?>'
            },
            init() {
                this.showCouponForm = JSON.parse(hyva.getBrowserStorage().getItem('hyva.showCouponForm'))
            },
            toggleShowCoupon() {
                this.showCouponForm = !this.showCouponForm;

                hyva.getBrowserStorage().setItem('hyva.showCouponForm', this.showCouponForm);

                this.$nextTick(() => this.$refs.couponInput.select());
            },
            submitForm() {
                const $form = document.querySelector('#discount-coupon-form');
                <?= $recaptcha ? $recaptcha->getValidationJsHtml(ReCaptcha::RECAPTCHA_FORM_ID_COUPON_CODE) : '' ?>

                if (this.errors === 0) {
                    hyva.postCart($form);
                }
            }
        }
    }
</script>
<div class="coupon-form "
     x-data="initCouponForm()"
>
    <div class="mx-auto md:mx-0">

        <div>
            <form id="discount-coupon-form"
                  class="card shadow-none px-0"
                  x-show="showCouponForm"
                  x-cloak
                  action="<?= $escaper->escapeUrl($block->getUrl('checkout/cart/couponPost')) ?>"
                  method="post"
                  @submit.prevent="submitForm()"
            >

                <span class="text-lg font-semibold font-sen">
                    <?= $escaper->escapeHtml(__('Voucher Code')) ?>
                </span>

                <?= $block->getBlockHtml('formkey') ?>

                <div class="fieldset coupon space-y-4">
                    <div class="flex gap-2 justify-center md:justify-start">
                        <label for="coupon_code" class="label sr-only">
                            <?= $escaper->escapeHtml(__('Enter voucher code')) ?>
                        </label>
                        <div class="control w-full">
                            <input type="text"
                                   class="w-full form-input disabled:opacity-75 disabled:bg-gray-100 disabled:pointer-events-none"
                                   id="coupon_code"
                                   name="coupon_code"
                                   value="<?= $escaper->escapeHtmlAttr($block->getCouponCode()) ?>"
                                   x-model="formData.coupon_code"
                                   x-ref="couponInput"
                                   placeholder="<?= $escaper->escapeHtmlAttr(__('Enter voucher code')) ?>"
                                    <?php if ($hasCouponCode): ?>
                                       disabled
                                    <?php else: ?>
                                        required
                                    <?php endif; ?>
                            />
                        </div>

                        <div>
                            <?php if (!$hasCouponCode): ?>
                            <div class="primary">
                                <button class="btn btn-primary bg-primary-banyo" type="submit" value="<?= $escaper->escapeHtmlAttr(__('Apply Discount')) ?>">
                                    <span><?= $escaper->escapeHtml(__('Apply')) ?></span>
                                </button>
                            </div>
                            <?php else: ?>
                                <div class="primary">
                                    <input type="hidden" name="remove" id="remove-coupon" value="1"/>
                                    <button type="submit" class="btn btn-primary"
                                            value="<?= $escaper->escapeHtmlAttr(__('Cancel Coupon')) ?>">
                                        <?= $escaper->escapeHtml(__('Cancel Coupon')) ?>
                                    </button>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?= $recaptcha ? $recaptcha->getInputHtml(ReCaptcha::RECAPTCHA_FORM_ID_COUPON_CODE) : '' ?>
                    <?php if ($recaptcha && $recaptcha->getLegalNoticeHtml(ReCaptcha::RECAPTCHA_FORM_ID_COUPON_CODE)): ?>
                        <?= $recaptcha->getLegalNoticeHtml(ReCaptcha::RECAPTCHA_FORM_ID_COUPON_CODE) ?>
                    <?php endif; ?>
                </div>
            </form>
        </div>
    </div>
</div>

