<?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\Checkbox $block */
/** @var Magento\Framework\Escaper $escaper */

$option = $block->getOption();
$selections = $option->getSelections();
$default = $option->getDefaultSelection();
$productId = $block->getProduct()->getId();
$optionId = $option->getId();
?>
<?php if ($block->showSingle()): ?>
    <?= /* @noEscape */ $block->getSelectionQtyTitlePrice($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 ?>]"
           form="mageplaza-fbt-form"
           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 else: ?>
    <?php foreach ($selections as $selection): ?>
        <?php $selectionId = $selection->getSelectionId(); ?>
        <?php $optionSelectionId = $optionId . '-' . $selectionId ?>
    <div class="field field-reserved">
        <div class="choice mt-1 flex items-center gap-x-2 my-1">
            <input class="bundle-option-<?= (int)$optionId ?>
                form-checkbox"
                   id="bundle-option-<?= $escaper->escapeHtmlAttr($optionSelectionId) ?>"
                   name="bundle_option[<?=(int)$productId ?>][<?= (int)$optionId ?>][<?= (int)$selectionId ?>]"
                   form="mageplaza-fbt-form"
                   type="checkbox"
                <?php if ($block->isSelected($selection)) { echo  'checked="checked"'; } ?>
                <?php if (!$selection->isSaleable()): ?>
                    disabled
                <?php endif; ?>
                <?= $option->getRequired()
                    ? 'data-validate=\'{"validate-one-required-by-name":"bundle_option[' . (int)$productId . '][' . (int)$optionId  . ']"}\''
                    : '' ?>
                   value="<?= (int)$selectionId ?>"
                   data-option-id="<?= $escaper->escapeHtmlAttr($optionSelectionId) ?>"
                   x-on:change="calculateTotalPrice()"
            />
            <label class="label"
                   for="bundle-option-<?= $escaper->escapeHtmlAttr($optionSelectionId) ?>">
                <span><?= /* @noEscape */ $block->getSelectionQtyTitlePrice($selection) ?></span>
            </label>
        </div>
    </div>

    <?php endforeach; ?>
    <div id="bundle-option-<?= (int)$optionId ?>-container"></div>
<?php endif; ?>
