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

use Magento\Catalog\Model\Product\Option;

/**
 * @var $block \Magento\Catalog\Block\Product\View\Options\Type\Select\Checkable
 */
$option = $block->getOption();
if ($option) : ?>
    <?php
    $configValue = $block->getPreconfiguredValue($option);
    $optionType = $option->getType();
    $arraySign = $optionType === Option::OPTION_TYPE_CHECKBOX ? '[]' : '';
    $count = 1;
    ?>

<div class="options-list nested" id="options-<?= $block->escapeHtmlAttr($option->getId()) ?>-list">
    <?php if ($optionType === Option::OPTION_TYPE_RADIO && !$option->getIsRequire()) :?>
    <div class="field choice admin__field admin__field-option">
        <input type="radio"
               id="options_<?= $block->escapeHtmlAttr($option->getId()) ?>"
               class="radio admin__control-radio product-custom-option"
               name="options[<?= $block->escapeHtmlAttr($option->getId()) ?>]"
               data-selector="options[<?= $block->escapeHtmlAttr($option->getId()) ?>]"
               onclick="<?= $block->getSkipJsReloadPrice() ? '' : 'opConfig.reloadPrice()' ?>"
               value=""
               checked="checked"
        />
        <label class="label admin__field-label" for="options_<?= $block->escapeHtmlAttr($option->getId()) ?>">
                        <span>
                            <?= $block->escapeHtml(__('None'))  ?>
                        </span>
        </label>
    </div>
<?php endif; ?>

    <?php foreach ($option->getValues() as $value) : ?>
        <?php
        $checked = '';
        $count++;
        if ($arraySign) {
            $checked = is_array($configValue) && in_array($value->getOptionTypeId(), $configValue) ? 'checked' : '';
        } else {
            $checked = $configValue == $value->getOptionTypeId() ? 'checked' : '';
        }
        $dataSelector = 'options[' . $option->getId() . ']';
        if ($arraySign) {
            $dataSelector .= '[' . $value->getOptionTypeId() . ']';
        }
        ?>

        <div class="field choice admin__field admin__field-option <?= /* @noEscape */ $option->getIsRequire() ? 'required': '' ?>">
            <input type="<?= $block->escapeHtmlAttr($optionType) ?>"
                   class="<?= $optionType === Option::OPTION_TYPE_RADIO
                       ? 'radio admin__control-radio'
                       : 'checkbox admin__control-checkbox' ?> <?= $option->getIsRequire()
                       ? 'required': '' ?>
                       product-custom-option
                        <?= $block->getSkipJsReloadPrice() ? '' : 'opConfig.reloadPrice()' ?>"
                   name="options[<?= $block->escapeHtmlAttr($option->getId()) ?>]<?= /* @noEscape */ $arraySign ?>"
                   id="options_<?= $block->escapeHtmlAttr($option->getId() . '_' . $count) ?>"
                   value="<?= $block->escapeHtmlAttr($value->getOptionTypeId()) ?>"
                <?= $block->escapeHtml($checked) ?>
                   data-selector="<?= $block->escapeHtmlAttr($dataSelector) ?>"
                   price="<?= $block->escapeHtmlAttr($block->getCurrencyByStore($value)) ?>"
            />
            <label class="label admin__field-label"
                   for="options_<?= $block->escapeHtmlAttr($option->getId() . '_' . $count) ?>">
                <span>
                    <?= $block->escapeHtml($value->getTitle()) ?>
                </span>
                <?= /* @noEscape */ $block->formatPrice($value) ?>
            </label>
        </div>
    <?php endforeach; ?>
    </div>
<?php endif; ?>
