<?php
/**
 * Maho
 *
 * @category    design
 * @package     rwd_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2023 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/** @var Mage_Checkout_Block_Cart_Coupon $this */
?>
<form id="discount-coupon-form" action="<?= $this->getFormActionUrl() ?>" method="post">
    <div class="discount">
        <h2><?= $this->__('Discount Codes') ?></h2>
        <div class="discount-form">
            <label for="coupon_code"><?= $this->__('Discount Codes') ?></label>
            <input type="hidden" name="remove" id="remove-coupone" value="0" />
            <div class="field-wrapper">
                <input class="input-text" type="text" id="coupon_code" name="coupon_code" value="<?= $this->escapeHtml($this->getCouponCode()) ?>" />
                <div class="button-wrapper">
                    <button type="button" title="<?= Mage::helper('core')->quoteEscape($this->__('Apply')) ?>" class="button2" onclick="discountForm.submit(false)" value="<?= $this->__('Apply') ?>"><?= $this->__('Apply') ?></button>
                    <?php if(strlen($this->getCouponCode())): ?>
                        <button type="button" title="<?= Mage::helper('core')->quoteEscape($this->__('Cancel')) ?>" class="button2 cancel-coupon" onclick="discountForm.submit(true)" value="<?= $this->__('Cancel') ?>"><?= $this->__('Cancel') ?>
                        </button>
                    <?php endif ?>
                </div>
            </div>
        </div>
    </div>
</form>
<script type="text/javascript">
    const discountForm = new VarienForm('discount-coupon-form');
    discountForm.submit = function(isRemove) {
        const couponCode = document.getElementById('coupon_code');
        const removeCoupon = document.getElementById('remove-coupone');

        if (isRemove) {
            couponCode.classList.remove('required-entry');
            removeCoupon.value = "1";
        } else {
            couponCode.classList.add('required-entry');
            removeCoupon.value = "0";
        }

        return this.form.submit();
    }
</script>
