<?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_File $this */
?>
<?php $_option = $this->getOption(); ?>
<?php $_fileInfo = $this->getFileInfo(); ?>
<?php $_fileExists = $_fileInfo->hasData() ? true : false; ?>
<?php $_fileName = 'options_' . $_option->getId() . '_file'; ?>
<?php $_fieldNameAction = $_fileName . '_action'; ?>
<?php $_fieldValueAction = $_fileExists ? 'save_old' : 'save_new'; ?>
<?php $_fileNamed = $_fileName . '_name'; ?>
<?php $_rand = rand(); ?>

<script type="text/javascript">
    opFile<?= $_rand ?> = {
        initializeFile: function(inputBox) {
            this.inputFile = inputBox.querySelector('input[name="<?= $_fileName ?>"]');
            this.inputFileAction = inputBox.querySelector('input[name="<?= $_fieldNameAction ?>"]');
            this.fileNameBox = inputBox.closest('dd').querySelector('.<?= $_fileNamed ?>');
        },

        toggleFileChange: function(inputBox) {
            this.initializeFile(inputBox);
            inputBox.style.display = inputBox.style.display === 'none' ? 'block' : 'none';
            this.fileChangeFlag = this.fileChangeFlag ? false : true;
            if (!this.fileDeleteFlag) {
                if (this.fileChangeFlag) {
                    this.inputFileAction.value = 'save_new';
                    this.inputFile.disabled = false;
                } else {
                    this.inputFileAction.value = 'save_old';
                    this.inputFile.disabled = true;
                }
            }
        },

        toggleFileDelete: function(fileDeleteFlag, inputBox) {
            this.initializeFile(inputBox);
            this.fileDeleteFlag = fileDeleteFlag.checked ? true : false;
            if (this.fileDeleteFlag) {
                this.inputFileAction.value = '';
                this.inputFile.disabled = true;
                this.fileNameBox.style.textDecoration = 'line-through';
            } else {
                this.inputFileAction.value = this.fileChangeFlag ? 'save_new' : 'save_old';
                this.inputFile.disabled = (this.fileChangeFlag == 'save_old');
                this.fileNameBox.style.textDecoration = 'none';
            }
        }
    };
</script>

<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 ($_fileExists): ?>
        <span class="<?= $_fileNamed ?>"><?= $this->escapeHtml($_fileInfo->getTitle()) ?></span>
        <a href="javascript:void(0)" class="label" onclick="opFile<?= $_rand ?>.toggleFileChange($(this).next('.input-box'))">
            <?= Mage::helper('catalog')->__('Change') ?>
        </a>&nbsp;
        <?php if (!$_option->getIsRequire()): ?>
           <input type="checkbox" onclick="opFile<?= $_rand ?>.toggleFileDelete($(this), $(this).next('.input-box'))" />
           <span class="label"><?= Mage::helper('catalog')->__('Delete') ?></span>
        <?php endif ?>
    <?php endif ?>
    <div class="input-box" <?= $_fileExists ? 'style="display:none"' : '' ?>>
        <input type="file" name="<?= $_fileName ?>" class="product-custom-option<?= $_option->getIsRequire() ? ' required-entry' : '' ?>" <?= $_fileExists ? 'disabled="disabled"' : '' ?> onchange="opConfig.reloadPrice()" />
        <input type="hidden" name="<?= $_fieldNameAction ?>" value="<?= $_fieldValueAction ?>" />
        <?php if ($_option->getFileExtension()): ?>
        <p class="no-margin"><?= Mage::helper('catalog')->__('Allowed file extensions to upload')?>: <strong><?= $_option->getFileExtension() ?></strong></p>
        <?php endif ?>
        <?php if ($_option->getImageSizeX() > 0): ?>
        <p class="no-margin"><?= Mage::helper('catalog')->__('Maximum image width')?>: <strong><?= $_option->getImageSizeX() ?> <?= Mage::helper('catalog')->__('px.') ?></strong></p>
        <?php endif ?>
        <?php if ($_option->getImageSizeY() > 0): ?>
        <p class="no-margin"><?= Mage::helper('catalog')->__('Maximum image height')?>: <strong><?= $_option->getImageSizeY() ?> <?= Mage::helper('catalog')->__('px.') ?></strong></p>
        <?php endif ?>
    </div>
</dd>
