<?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);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\ProductPrice;
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Model\Product\Option;
use Magento\Catalog\Pricing\Price\CustomOptionPrice;

/** @var Magento\Catalog\Block\Product\View\Options\Type\Text $block */
/** @var Magento\Framework\Escaper $escaper */
/** @var ViewModelRegistry $viewModels */

$product = $block->getProduct();

/** @var Option $option */
$option = $block->getOption();
$optionId = $option->getId();
/** @var ProductPrice $productPriceViewModel */
$productPriceViewModel = $viewModels->require(ProductPrice::class);
$optionPrice = $productPriceViewModel->getCustomOptionPrice($option, CustomOptionPrice::PRICE_CODE, $product);
if ($productPriceViewModel->displayPriceInclAndExclTax()) {
    $optionBasePrice = $productPriceViewModel->getCustomOptionPriceExclTax($option, CustomOptionPrice::PRICE_CODE, $product);
}
?>

<div class="flex flex-col sm:flex-row border-t border-gray-300 py-2 last:mb-6 last:border-b w-full items-center">
    <label class="label text-gray-700 text-left w-full sm:w-1/2"
           for="options_<?= $escaper->escapeHtmlAttr($optionId) ?>_text"
           x-html="updateOptionPrice('<?= $escaper->escapeJs($optionId) ?>', $el)"
    >
        <span><?= $escaper->escapeHtml($option->getTitle()) ?></span>
        <?php if ($option->getIsRequire()): ?>
            <span class="sup text-sm">*</span>
        <?php endif; ?>
        <span><?= /* @noEscape */ $block->getFormattedPrice() ?></span>
    </label>

    <div class="sm:ml-2 text-gray-900 text-left w-full sm:w-1/2">
        <?php if ($option->getType() == ProductCustomOptionInterface::OPTION_TYPE_FIELD): ?>
            <input type="text"
                   id="options_<?= $escaper->escapeHtmlAttr($optionId) ?>_text"
                   class="product-custom-option form-input w-full"
                   name="options[<?= $escaper->escapeHtmlAttr($optionId) ?>]"
                   value="<?= $escaper->escapeHtml($block->getDefaultValue()) ?>"
                   <?php if ($option->getMaxCharacters()): ?>
                   maxlength="<?= (int)$option->getMaxCharacters() ?>"
                   <?php endif; ?>
                   <?= $option->getIsRequire() ? 'required' : '' ?>
                   data-price-amount="<?= $escaper->escapeHtmlAttr($optionPrice) ?>"
                   data-price-type="<?= $escaper->escapeHtmlAttr($option->getPriceType()) ?>"
                   data-option-id="<?= $escaper->escapeHtmlAttr($optionId) ?>"
                   x-ref="option-<?= $escaper->escapeHtmlAttr($optionId) ?>"
                   x-on:input="updateCustomOptionValue(
                        $dispatch,
                        '<?= $escaper->escapeHtmlAttr($optionId) ?>',
                        $event.target
                   )"
            />
        <?php elseif ($option->getType() == ProductCustomOptionInterface::OPTION_TYPE_AREA): ?>
            <textarea id="options_<?= $escaper->escapeHtmlAttr($optionId) ?>_text"
                      class="product-custom-option form-textarea"
                      <?php if ($option->getMaxCharacters()): ?>
                      maxlength="<?= (int)$option->getMaxCharacters() ?>"
                      <?php endif; ?>
                      <?= $option->getIsRequire() ? 'required' : '' ?>
                      name="options[<?= $escaper->escapeHtmlAttr($optionId) ?>]"
                      rows="5"
                      cols="25"
                      data-price-amount="<?= $escaper->escapeHtmlAttr($optionPrice) ?>"
                      data-price-type="<?= $escaper->escapeHtmlAttr($option->getPriceType()) ?>"
                      data-option-id="<?= $escaper->escapeHtmlAttr($optionId) ?>"
                      x-ref="option-<?= $escaper->escapeHtmlAttr($optionId) ?>"
                      x-on:input="updateCustomOptionValue(
                        $dispatch,
                        '<?= $escaper->escapeHtmlAttr($optionId) ?>',
                        $event.target
                   )"
            ><?= $escaper->escapeHtml($block->getDefaultValue()) ?></textarea>
        <?php endif; ?>
        <?php if ($option->getMaxCharacters()): ?>
            <p class="note text-sm">
                <?= $escaper->escapeHtml(__('Maximum %1 characters', $option->getMaxCharacters())) ?>
            </p>
        <?php endif; ?>
    </div>
</div>
