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

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php
$_product = $block->getProduct();
$buttonTitle = __('Add to Cart');
$helper =  $this->helper('MGS\ThemeSettings\Helper\Config');
?>

<?php if ($_product->isSaleable()): ?>
    <?php if ($helper->getFullActionName() == 'catalog_product_view'): ?>
        <?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\View")->setTemplate("MGS_Aquickview::product/view/time.phtml")->toHtml();?>
    <?php endif; ?>

    <div class="box-tocart">
        <div class="fieldset">
            <?php if ($block->shouldRenderQuantity()): ?>
                <div class="field qty">
                    <label class="label" for="qty"><span><?= $block->escapeHtml(__('Qty')) ?></span></label>
                    <div class="control">
                        <span class="edit-qty minus" onclick="arrowMinusQty('qty')">-</span>
                        <input type="number"
                            name="qty"
                            id="qty"
                            min="0"
                            value="<?= $block->getProductDefaultQty() * 1 ?>"
                            title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
                            class="input-text qty"
                            data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                        />
                        <span class="edit-qty plus" onclick="arrowPlusQty('qty')">+</span>
                    </div>
                </div>
            <?php endif; ?>

            <div class="actions">
                <button type="submit" title="<?= $block->escapeHtmlAttr($buttonTitle) ?>" class="action primary tocart" id="product-addtocart-button" disabled>
                    <span><?= $block->escapeHtml($buttonTitle) ?></span>
                </button>
            </div>
            <?= $block->getChildHtml('', true) ?>
        </div>
    </div>
<?php endif; ?>

<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/js/validate-product": {}
        }
    }
</script>

<?php if ($block->shouldRenderQuantity()): ?>
    <script>
        function arrowPlusQty(qtyInput) {
            const qty = document.querySelector('#' + qtyInput);
            const stickyCartQty = document.querySelector('#sticky-cart-' + qtyInput);
            if ( !qty || qty.hasAttribute('disabled')) {
                return;
            }

            let qtyValue = qty.value;
            qtyValue = parseInt(qtyValue);
            qtyValue++;
            qty.value = qtyValue;
            if(stickyCartQty){
                stickyCartQty.value = qtyValue;
            }
        }

        function arrowMinusQty(qtyInput) {
            const qty = document.querySelector('#' + qtyInput);
            const stickyCartQty = document.querySelector('#sticky-cart-' + qtyInput);

            if ( !qty || qty.hasAttribute('disabled')) {
                return;
            }

            let qtyValue = qty.value;
            qtyValue = parseInt(qtyValue);

            if (qtyValue > 1) {
                qtyValue--;
                qty.value = qtyValue;
                if(stickyCartQty){
                    stickyCartQty.value = qtyValue;
                }
            }
        }
    </script>
<?php endif; ?>
