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

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis

/**
 * Product view template
 *
 * @var $block \Magento\Catalog\Block\Product\View
 */
?>
<?php $_helper = $this->helper(Magento\Catalog\Helper\Output::class); ?>
<?php $_product = $block->getProduct(); ?>

<div class="product-add-form">
    <form data-product-sku="<?= $block->escapeHtmlAttr($_product->getSku()) ?>"
          action="<?= $block->escapeUrl($block->getSubmitUrl($_product)) ?>" method="post"
          id="product_addtocart_form"<?php if ($_product->getOptions()) :?> enctype="multipart/form-data"<?php endif; ?>>
        <input type="hidden" name="product" value="<?= (int)$_product->getId() ?>" />
        <input type="hidden" name="selected_configurable_option" value="" />
        <input type="hidden" name="related_product" id="related-products-field" value="" />
        <input type="hidden" name="item"  value="<?= (int)$block->getRequest()->getParam('id') ?>" />
        <?= $block->getBlockHtml('formkey') ?>
        <?= $block->getChildHtml('form_top') ?>
        <?php if (!$block->hasOptions()) :?>
            <?= $block->getChildHtml('product_info_form_content') ?>
        <?php else :?>
            <?php if ($_product->isSaleable() && $block->getOptionsContainer() == 'container1') :?>
                <?= $block->getChildChildHtml('options_container') ?>
            <?php endif;?>
        <?php endif; ?>

        <?php if ($_product->isSaleable() && $block->hasOptions() && $block->getOptionsContainer() == 'container2') :?>
            <?= $block->getChildChildHtml('options_container') ?>
        <?php endif;?>
        <?= $block->getChildHtml('form_bottom') ?>
    </form>
</div>

<script>
    require([
        'jquery',
        'priceBox'
    ], function($){
        var dataPriceBoxSelector = '[data-role=priceBox]',
            dataProductIdSelector = '[data-product-id=<?= $block->escapeHtml($_product->getId()) ?>]',
            priceBoxes = $(dataPriceBoxSelector + dataProductIdSelector);

        priceBoxes = priceBoxes.filter(function(index, elem){
            return !$(elem).find('.price-from').length;
        });
        priceBoxes.priceBox({'priceConfig': <?= /* @noEscape */ $block->getJsonConfig() ?>});
<?php //MNM ?>
        $('.table-wrapper.grouped #super-product-table').each(function() {
            $formQtyInput = $(this).closest('form').find('input[name="qty"]');
            $products = $(this).find('.item');
            $aponowlinks = $('.aponow');

            $products.eq(0).addClass('selected');

            $products.each(function(index) {
    			var $el = $(this);
    			let pid = $(this).data('pid');

    		    $el.mouseenter(function() {$el.addClass('highlight')});
    			$el.mouseleave(function() {$el.removeClass('highlight')});

				$el.click(function() {
					$products.removeClass('selected').find('input').val(0);

					$el.addClass('selected').find('input').val($formQtyInput.val());

					$('#gallery_images > div').addClass('hidden').eq(index).removeClass('hidden');

					$aponowlinks.hide();
					$aponowlinks.filter('[data-pid="'+pid+'"]').eq(0).show();
				});
            });

            let select_index = 0

            let address = window.location.search
  			let parameterList = new URLSearchParams(address)

  			if(parameterList.has('pid'))
  			{
    			let pid = parameterList.get('pid')

				if(pid)
				{
					$products.each(function(index) {
						if($(this).data('pid') == pid)
						{
							select_index = index
						}
					})
				}
			}

            $products.eq(select_index).click();
            $formQtyInput.change(function() {
            	$products.filter('.selected').find('input').val($(this).val());
            });
        });
<?php //MNM ?>
    });
</script>
