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

/** @var $block \Magento\Catalog\Block\Product\View */
$qty = 1;
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()) :?>
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()) :?>
          <div class="field qty up-down flex-layout no-wrap">
				<div class="control">
					<input type="number"
						   name="qty"
						   id="qty"
						   maxlength="12"
						   value="<?php /* @escapeNotVerified */ echo $qty * 1 ?>"
						   title="<?php /* @escapeNotVerified */ echo __('Quantity') ?>" class="input-text qty"
						   data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
						   />
				</div>
				<div class="click-up-down flex-layout no-wrap column">
					<a class="qty-up-fixed-onclick qty-up" href="#"><span class="icon-chevron-up"></span></a> 
					<a class="qty-down-fixed-onclick qty-down" href="#"><span class="icon-chevron-down"></span></a> 
				</div>
			</div>
        <?php endif; ?>
        <div class="actions">
            <button type="submit"
                    title="<?= $block->escapeHtmlAttr($buttonTitle) ?>"
                    class="action primary tocart"
                    id="product-addtocart-button">
                <span><?= $block->escapeHtml($buttonTitle) ?></span>
            </button>
            <?= $block->getChildHtml('', true) ?>
        </div>
    </div>
</div>
<?php endif; ?>
<?php if ($block->isRedirectToCartEnabled()) : ?>
	<script type="text/x-magento-init">
		{
			"#product_addtocart_form": {
				"Magento_Catalog/product/view/validation": {
					"radioCheckboxClosest": ".nested"
				}
			}
		}
	</script>
<?php else : ?>
	<script>
		require([
			'jquery',
			'mage/mage',
			'Magento_Catalog/product/view/validation',
			'Magento_Catalog/js/catalog-add-to-cart'
		], function ($) {
			'use strict';
			
			$('#product_addtocart_form').mage('validation', {
				radioCheckboxClosest: '.nested',
				submitHandler: function (form) {
					var widget = $(form).catalogAddToCart({
						bindSubmit: false
					});

					widget.catalogAddToCart('submitForm', $(form));

					return false;
				}
			});
		});
	</script>
<?php endif; ?>

<script>
	require([
		'jquery'
	], function ($) {
		'use strict';
		var number_click = <?php echo $qty;?>;
		$(".qty-down-fixed-onclick").click(function() {
			var val_input = $(this).closest('div.field').find('#qty').val();
			val_input = parseInt(val_input);
			if(val_input <= number_click){
				val_input = number_click;
			}
			else{
				val_input = val_input - number_click;
			}
			$('div.field div.control #qty').val(val_input);
			return false;
		});
		$(".qty-up-fixed-onclick").click(function() {
			var val_input = $(this).closest('div.field').find('#qty').val();
			val_input = parseInt(val_input);
			val_input = val_input + number_click;
			$('div.field div.control #qty').val(val_input);
			return false;
		});
	});
</script>
