<?php
/**
 * @category Mageants ConfigurablePreselect
 * @package Mageants_ConfigurablePreselect
 * @copyright Copyright (c) 2017 Mageants
 * @author Mageants Team <info@mageants.com>
 */
?>
<?php
/** @var $block \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable*/
$product = $block->getProduct();
$attributes = $block->decorateArray($block->getAllowAttributes());
?>
<?php if ($product->isSaleable() && count($attributes)):?>
    <?php foreach ($attributes as $attribute): ?>
        <div class="field configurable required test">
            <label class="label" for="attribute<?= /* @noEscape */ $attribute->getAttributeId() ?>">
                <span><?= $block->escapeHtml($attribute->getProductAttribute()->getStoreLabel()) ?></span>
            </label>
            <div class="control">
                <select name="super_attribute[<?= /* @noEscape */ $attribute->getAttributeId() ?>]"
                        data-selector="super_attribute[<?= /* @noEscape */ $attribute->getAttributeId() ?>]"
                        data-validate="{required:true}"
                        id="attribute<?= /* @noEscape */ $attribute->getAttributeId() ?>"
                        class="super-attribute-select">
                    <option value=""><?= /* @noEscape */ __('Choose an Option...') ?></option>
                </select>
            </div>
        </div>
    <?php endforeach; ?>

    <?php
    $preselectBlockObj = $block->getLayout()
                              ->createBlock(\Mageants\SimpleConfigurable\Block\Product\Renderer\Configurable::class);
    $isEnable = $preselectBlockObj->isEnable();
    ?>
    <?php if ($isEnable) {
        $preSelectedOption = $preselectBlockObj->getPreselectOptionId();
        $preSelectedOption = $preselectBlockObj->convertAttributeCodeToId($preSelectedOption);
        ?>
        <script type="text/x-magento-init">
            {
                "#product_addtocart_form": {
                    "Magento_ConfigurableProduct/js/configurable": {
                        "spConfig": <?= /* @noEscape */ $block->getJsonConfig() ?>,
                        "gallerySwitchStrategy": "<?= /* @noEscape */  __($block->getVar(
                            'gallery_switch_strategy',
                            'Magento_ConfigurableProduct'
                        ) ?: 'replace'); ?>",
                        "isExtensionEnable" : "<?= /* @noEscape */ __($isEnable); ?>" ,
                        <?php if (!empty($preSelectedOption)) {?>
                        "preSelectedOption": <?= /* @noEscape */ $preSelectedOption ?>,
                        <?php } ?>
                        "replaceUrl" : "<?= /* @noEscape */ __($preselectBlockObj->replaceUrl()) ?>",
                        "is_PDP_page" : true
                    }
                }
            }
        </script>
    <?php } else {  ?>
    <script type="text/x-magento-init">
        {
            "#product_addtocart_form": {
                "Magento_ConfigurableProduct/js/configurable": {
                    "spConfig": <?= /* @noEscape */ $block->getJsonConfig() ?>,
                    "gallerySwitchStrategy": "<?= /* @noEscape */ __($block->getVar(
                        'gallery_switch_strategy',
                        'Magento_ConfigurableProduct'
                    ) ?: 'replace'); ?>",
                    "isExtensionEnable" : <?= /* @noEscape */ __($isEnable); ?> 
                }
            }
        }
    </script>
<?php } ?>
<?php endif;?>