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

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php if ($_product->isSaleable()): ?>
	<div class="box-tocart">
		<?php // If is deals product. Show countdown time ?>
		<?php if($_product->getSpecialPrice() && $_product->getSpecialToDate() && $_product->getSpecialFromDate()): ?>
			
			<?php $themeHelper = $this->helper('MGS\ThemeSettings\Helper\Config'); ?>
			<?php $dateTimeFormat = \Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT; ?>
			<?php $now = $themeHelper->getCurrentDateTime(); ?>
			<?php $specialFromDate = date($dateTimeFormat, strtotime($_product->getSpecialFromDate())); ?>
			<?php $specialToDate = date($dateTimeFormat, strtotime($_product->getSpecialToDate())); ?>
			
			<?php if($specialFromDate <= $now && $specialToDate > $now): ?>
				<div class="product-detail-deals margin-top25">
					<?= $this->getLayout()
									->createBlock('Magento\Framework\View\Element\Template')
									->setProduct($_product->getId())
									->setTimeNow($now)
									->setSpecialFromDate($specialFromDate)
									->setSpecialToDate($specialToDate)
									->setTemplate('MGS_ThemeSettings::products/deals/countdown_detail.phtml')
									->toHtml() ?>
				</div>
			<?php endif ?>
			
		<?php endif ?>
		<?php // End Deals ?>
		<div class="fieldset">

			
			<?php if ($block->shouldRenderQuantity()): ?>
			<div class="field qty">
				<div class="qty-pr">
					<span class="edit-qty minus disable-select-text" onclick="minusQty('qty')"><i class="icon-minus"></i></span>
					<input type="number"
						   name="qty"
						   id="qty"
						   maxlength="12"
						   value="<?= $block->getProductDefaultQty() * 1 ?>"
						   title="<?= __('Qty') ?>" class="input-text qty"
						   data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
						   />
					<span class="edit-qty plus disable-select-text" onclick="plusQty('qty')"><i class="icon-plus"></i></span>
				</div>
			</div>
			<?php endif; ?>
			<div class="actions">
				<button type="submit"
						title="<?= __('Add To Cart') ?>"
						class="action primary tocart"
						id="product-addtocart-button">
					<?= __('Add To Cart') ?>
				</button>
			</div>
		</div>
	</div>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/product/view/validation": {
                "radioCheckboxClosest": ".nested"
            }
        }
    }
</script>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "catalogAddToCart": {
                "bindSubmit": false
            }
        }
    }
</script>
<?php endif; ?>

<?php if ($block->shouldRenderQuantity()): ?>
	<script type="text/javascript">
		function plusQty(qtyInput){
			require([
			'jquery'
			], function($){
				if($('#'+qtyInput).attr('disabled')!='disabled'){
					qty = $('#'+qtyInput).attr('value');
					qty = parseInt(qty);
					qty++;
					$('#'+qtyInput).val(qty);
					$('#sticky_qty').val(qty);
				}
			});
			
		}
		function minusQty(qtyInput){
			require([
			'jquery'
			], function($){
				if($('#'+qtyInput).attr('disabled')!='disabled'){
					qty = $('#'+qtyInput).attr('value');
					qty = parseInt(qty);
					
					if(qty>1){
						qty--;
						$('#'+qtyInput).val(qty);
						$('#sticky_qty').val(qty);
					}
				}
			});
		}
	</script>
<?php endif; ?>