<?php
/**
 * Maho
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2023 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/** @var Mage_Catalog_Block_Product_View_Options_Type_Date $this */
?>
<?php $_option = $this->getOption() ?>
<?php $_optionId = $_option->getId() ?>
<dt><label<?php if ($_option->getIsRequire()) echo ' class="required"' ?>><?php if ($_option->getIsRequire()) echo '<em>*</em>' ?><?=  $this->escapeHtml($_option->getTitle()) ?></label>
    <?= $this->getFormatedPrice() ?></dt>
<dd<?php if ($_option->decoratedIsLast){?> class="last"<?php }?>>

<?php if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE_TIME
    || $_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE): ?>

    <?= $this->getDateHtml() ?>

    <?php if (!$this->useCalendar()): ?>
    <script type="text/javascript">
        document.getElementById('options_<?= $_optionId ?>_month').addEventListener('change', dateOption.reloadMonth.bind(dateOption));
        document.getElementById('options_<?= $_optionId ?>_year').addEventListener('change', dateOption.reloadMonth.bind(dateOption));
    </script>
    <?php endif ?>

<?php endif ?>

<?php if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE_TIME
    || $_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_TIME): ?>
    <span class="time-picker"><?= $this->getTimeHtml() ?></span>
<?php endif ?>

    <input type="hidden" name="validate_datetime_<?= $_optionId ?>" class="validate-datetime-<?= $_optionId ?>" value="" />
    <script type="text/javascript">
    <?php if ($_option->getIsRequire()): ?>
        Validation.add(
            'validate-datetime-<?= $_optionId ?>',
            '<?= $this->jsQuoteEscape( Mage::helper('catalog')->__('This is a required option') ) ?>',
            function(v) {
                const dateTimeParts = document.querySelectorAll('.datetime-picker[id^="options_<?= $_optionId ?>"]');
                for (let i = 0; i < dateTimeParts.length; i++) {
                    if (dateTimeParts[i].value === "") return false;
                }
                return true;
            }
        );
    <?php else: ?>
        Validation.add(
            'validate-datetime-<?= $_optionId ?>',
            '<?= $this->jsQuoteEscape( Mage::helper('catalog')->__('Field is not complete') ) ?>',
            function(v) {
                const dateTimeParts = document.querySelectorAll('.datetime-picker[id^="options_<?= $_optionId ?>"]');
                let hasWithValue = false;
                let hasWithNoValue = false;
                const pattern = /day_part$/i;
                for (let i = 0; i < dateTimeParts.length; i++) {
                    if (!pattern.test(dateTimeParts[i].id)) {
                        if (dateTimeParts[i].value === "") {
                            hasWithValue = true;
                        } else {
                            hasWithNoValue = true;
                        }
                    }
                }
                return hasWithValue ^ hasWithNoValue;
            }
        );
    <?php endif ?>
    </script>
</dd>
