<?php
/**
 * Maho
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2024 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)
 */
?>
<?php /** @var Mage_Downloadable_Block_Catalog_Product_Links $this */ ?>
<?php $_linksPurchasedSeparately = $this->getLinksPurchasedSeparately(); ?>
<?php if ($this->getProduct()->isSaleable() && $this->hasLinks()):?>
    <dl>
        <?php $_links = $this->getLinks(); ?>
        <?php $_isRequired = $this->getLinkSelectionRequired(); ?>
        <dt><label<?php if ($_isRequired) echo ' class="required"' ?>><?php if ($_isRequired) echo '<em>*</em>' ?><?= $this->escapeHtml($this->getLinksTitle()) ?></label></dt>
        <dd<?php /* if ($_option->decoratedIsLast){?> class="last"<?php } */ ?>>
            <ul id="downloadable-links-list" class="options-list">
            <?php foreach ($_links as $_link): ?>
                <li>
                    <?php if ($_linksPurchasedSeparately): ?>
                        <input type="checkbox" class="checkbox<?php if($_isRequired):?> validate-one-required-by-name<?php endif ?> product-downloadable-link" onclick="dConfig.reloadPrice()" name="links[]" id="links_<?= $_link->getId() ?>" value="<?= $_link->getId() ?>" <?= $this->getLinkCheckedValue($_link) ?> />
                    <?php endif ?>
                    <span class="label">
                        <label for="links_<?= $_link->getId() ?>">
                            <?= $this->escapeHtml($_link->getTitle()) ?>
                        </label>
                            <?php if ($_link->getSampleFile() || $_link->getSampleUrl()): ?>
                                &nbsp;(<a href="<?= $this->getLinkSamlpeUrl($_link) ?>" <?= $this->getIsOpenInNewWindow()?'onclick="this.target=\'_blank\'"':'' ?>><?= Mage::helper('downloadable')->__('sample') ?></a>)
                            <?php endif ?>
                            <?php if ($_linksPurchasedSeparately): ?>
                                <?= $this->getFormattedLinkPrice($_link) ?>
                            <?php endif ?>
                    </span>
                    <?php if ($_isRequired): ?>
                        <script type="text/javascript">
                            var linkElement = document.getElementById('links_<?= $_link->getId() ?>');
                            linkElement.advaiceContainer = 'links-advice-container';
                            linkElement.callbackFunction = 'validateDownloadableCallback';
                        </script>
                    <?php endif ?>
                </li>
            <?php endforeach ?>
            </ul>

            <?php if ($_isRequired): ?>
                <span id="links-advice-container"></span>
            <?php endif ?>
        </dd>
    </dl>
    <script type="text/javascript">
        class ProductDownloadable {
            constructor(config) {
                this.config = config;
                this.reloadPrice();
                document.addEventListener('DOMContentLoaded', () => this.reloadPrice());
            }

            reloadPrice() {
                let price = 0;
                const config = this.config;

                document.querySelectorAll('.product-downloadable-link').forEach(elm => {
                    if (config[elm.value] && elm.checked) {
                        price += parseFloat(config[elm.value]);
                    }
                });

                try {
                    const _displayZeroPrice = optionsPrice.displayZeroPrice;
                    optionsPrice.displayZeroPrice = false;
                    optionsPrice.changePrice('downloadable', price);
                    optionsPrice.reload();
                    optionsPrice.displayZeroPrice = _displayZeroPrice;
                } catch (e) {}
            }
        }

        function validateDownloadableCallback(elmId, result) {
            const container = document.getElementById('downloadable-links-list');
            if (result == 'failed') {
                container.classList.remove('validation-passed');
                container.classList.add('validation-failed');
            } else {
                container.classList.remove('validation-failed');
                container.classList.add('validation-passed');
            }
        }

        const dConfig = new ProductDownloadable(<?= $this->getJsonConfig() ?>);
    </script>
<?php endif ?>
