<?php

/**
 * Copyright © 2020 Wyomind. All rights reserved.
 * See LICENSE.txt for license details.
 */

/**
 * Copyright © 2020 Wyomind. All rights reserved.
 * See LICENSE.txt for license details.
 */

?>

<?php $_product = $block->getProduct() ?>

<?php if ($block->displayProductStockStatus()) : ?>
    <div class="stock available" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
        <?php
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        /** @var $stockStatus  \Wyomind\MsiStockStatus\Helper\StockStatus */
        $framework = $objectManager->create("\Wyomind\Framework\Helper\Module");
        $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
        $storeId = $storeManager->getStore()->getStoreId();
        if ($framework->getStoreConfig("msistockstatus/settings/status/in_product", $storeId) && $framework->getStoreConfig("msistockstatus/settings/status/replace_default", $storeId)) :
            ?>
            <div class="msiss-message-container configurable notice">
                <div class="msiss-message"
                    x-data="configurableStockStatus()"
                     x-init="watchForConfigurableProductSelection()"
                >
                    <p id="msiss-stock-status"><?php echo __("* Please configure the options to get the availability "); ?></p>
                </div>
                <script>
                    function configurableStockStatus() {
                        return {
                            watchForConfigurableProductSelection() {
                                window.addEventListener('configurable-selection-changed', (event) => {
                                    let productId = event.detail.productIndex;

                                    fetch(
                                        "<?= $block->getUrl('msistockstatus/stock/update') ?>",
                                        {
                                            method: "POST",
                                            mode: "cors",
                                            credentials: "include",
                                            body: new URLSearchParams({
                                                form_key: hyva.getFormKey(),
                                                type: 'message',
                                                productId: productId
                                            }),
                                        }
                                    ).then(response => {
                                        if (response.ok) {
                                            return response.json();
                                        } else {
                                            console.warn("stock status update failed.");
                                        }
                                    }).then(data => {
                                        if (data.html) {
                                            document.getElementById('msiss-stock-status').innerHTML = data.html;
                                        }
                                    })
                                })
                            },
                        }
                    }
                </script>
            </div>
        <?php                                                                                                                                                                                                                 else: ?>
            <?php if ($_product->isAvailable()) : ?>
                <div class="stock available" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
                    <span><?= $block->escapeHtml(__('In stock')) ?></span>
                </div>
            <?php else : ?>
                <div class="stock unavailable" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
                    <span><?= $block->escapeHtml(__('Out of stock')) ?></span>
                </div>
            <?php endif; ?>
        <?php endif; ?>

    </div>

<?php endif; ?>
