<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_FrequentlyBought
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */


declare(strict_types=1);


/** @var Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Select $block */
/** @var Magento\Framework\Escaper $escaper */
$option = $block->getOption();
$selections = $option->getSelections();
$default = $option->getDefaultSelection();
list($defaultQty, $canChangeQty) = $block->getDefaultValues();
$productId = $block->getProduct()->getId();
$optionId = $option->getId();
$activeSelectOptions = [];
?>
<?php if ($block->showSingle()): ?>
    <?= /* @noEscape */ $block->getSelectionTitlePrice($selections[0]) ?>
    <?= /* @noEscape */ $block->getTierPriceRenderer()->renderTierPrice($selections[0]) ?>
    <input type="hidden"
           class="bundle-option-<?= (int)$optionId ?>  product bundle option"
           name="bundle_option[<?=(int)$productId ?>][<?= (int)$optionId ?>]"
           value="<?= (int)$selections[0]->getSelectionId() ?>"
           id="bundle-option-<?= (int)$optionId ?>-<?= (int)$selections[0]->getSelectionId() ?>"
           checked
           form="mageplaza-fbt-form"
           data-option-id="<?= (int)$optionId ?>"
           data-selection-id="<?= (int)$selections[0]->getSelectionId() ?>"
    />
    <?php if ((int)$selections[0]->getSelectionId()) {
        array_push($activeSelectOptions, (int)$selections[0]->getSelectionId());
    } ?>
<?php else: ?>
    <select id="bundle-option-<?= (int)$optionId ?>"
            name="bundle_option[<?=(int)$productId ?>][<?= (int)$optionId ?>]"
            class="form-select max-w-full mb-4"
            form="mageplaza-fbt-form"
            x-on:change="
                activeSelectOptions['<?= (int)$optionId ?>'] = [$event.target.value];
                $nextTick(() => {calculateTotalPrice()});
            "
        <?= $option->getRequired() ? 'required' : '' ?>
    >
        <option value=""><?= $escaper->escapeHtml(__('Choose a selection...')) ?></option>
        <?php foreach ($selections as $selection): ?>
            <?php $selectionId = $selection->getSelectionId(); ?>
            <?php $optionSelectionId = $optionId . '-' . $selectionId ?>
            <option value="<?= (int) $selectionId ?>"
                <?php if ($block->isSelected($selection)) {
                    array_push($activeSelectOptions, $selectionId);
                    echo 'selected = "selected"';
                } ?>
                <?php if (!$selection->isSaleable()): ?>
                    disabled
                <?php endif; ?>
                data-option-id="<?= $escaper->escapeHtmlAttr($optionSelectionId) ?>"
            >
                <?= /* @noEscape */ $block->getSelectionTitlePrice($selection, false) ?>
            </option>
        <?php endforeach; ?>
    </select>

    <div id="option-tier-prices-<?= (int)$optionId ?>"
         class="option-tier-prices"
    >
        <?php foreach ($selections as $selection): ?>
            <?php $selectionId = $selection->getSelectionId(); ?>
            <?php $optionSelectionId = $optionId . '-' . $selectionId ?>
            <div data-role="selection-tier-prices"
                 class="selection-tier-prices" x-cloak
                 x-show="activeSelectOptions[<?= (int) $optionId ?>] &&
                    activeSelectOptions[<?= (int) $optionId ?>].includes(
                        '<?= (int)$selectionId ?>'
                    )"
                    >
                <?php /* @noEscape */ echo $block->getSelectionTitlePrice($selection, false) ?>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<div class="field qty qty-holder mt-2">
    <label class="label font-semibold mr-4"
           for="bundle-option-<?= (int)$optionId ?>-qty-input">
        <span>
            <?= $escaper->escapeHtml(__('Quantity')) ?>
        </span>
    </label>

    <input
        id="bundle-option-<?= (int)$optionId ?>-qty-input"
        class="form-input w-20 disabled:opacity-75"
        type="number"
        min="0"
        name="bundle_option_qty[<?=(int)$productId ?>][<?= (int)$optionId ?>]"
        value="<?= $escaper->escapeHtmlAttr($defaultQty) ?>"
        :value="getQtyValue(<?= (int)$optionId ?>)"
        form="mageplaza-fbt-form"
        x-on:change="
            setQtyValue(<?= (int)$optionId ?>, $event.target.value);
            calculateTotalPrice($dispatch)"
        <?php if (!$canChangeQty): ?> disabled <?php endif; ?>
        :disabled="getQtyDisabled(<?= (int)$optionId ?>)"
    />
</div>

<?php
/**
 * Set the initial values on activeSelectOptions
 * Because we can't tell which options are selected when configuring a product that is in
 * the cart (through checkout/cart/configure) in the main Alpine component
 */
?>
<div @set-selected-options.window="activeSelectOptions['<?= (int)$optionId ?>'] =
        <?= $escaper->escapeHtmlAttr(json_encode($activeSelectOptions)) ?>">
</div>
