<?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)
 */

/** @var Mage_Catalog_Block_Product_List_Related $this */
?>
<?php if($this->getItems()->getSize()): ?>
<div class="block block-related">
    <div class="block-title">
        <strong><span><?= $this->__('Related Products') ?></span></strong>
    </div>
    <div class="block-content">
        <p class="block-subtitle"><?= $this->__('Check items to add to the cart or') ?>&nbsp;<a href="#" onclick="selectAllRelated(this); return false;"><?= $this->__('select all') ?></a></p>
        <ol class="mini-products-list" id="block-related">
        <?php foreach($this->getItems() as $_item): ?>
            <li class="item">
                <?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
                    <?php if (!$_item->getRequiredOptions()): ?>
                        <input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?= $_item->getId() ?>" name="related_products[]" value="<?= $_item->getId() ?>" />
                    <?php endif ?>
                <?php endif ?>
                <div class="product">
                    <a href="<?= $_item->getProductUrl() ?>" title="<?= $this->escapeHtml($_item->getName()) ?>" class="product-image"><img src="<?= $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(50) ?>" width="50" height="50" alt="<?= $this->escapeHtml($_item->getName()) ?>" /></a>
                    <div class="product-details">
                        <p class="product-name"><a href="<?= $_item->getProductUrl() ?>"><?= $this->escapeHtml($_item->getName()) ?></a></p>
                        <?= $this->getPriceHtml($_item, true, '-related') ?>
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <a href="#"
                               class="link-wishlist"
                               onclick="customFormSubmit(
                                       '<?= $this->getAddToWishlistUrlCustom($_item, false) ?>',
                                       '<?= $this->escapeHtml(json_encode(['form_key' => $this->getFormKey()])) ?>',
                                       'post')">
                                <?= $this->__('Add to Wishlist') ?>
                            </a>
                        <?php endif ?>
                    </div>
                </div>
            </li>
        <?php endforeach ?>
        </ol>
    </div>
    <script type="text/javascript">
        document.querySelectorAll('.related-checkbox').forEach(elem => {
            elem.addEventListener('click', addRelatedToProduct);
        });

        let relatedProductsCheckFlag = false;

        function selectAllRelated(txt) {
            if (relatedProductsCheckFlag === false) {
                document.querySelectorAll('.related-checkbox').forEach(elem => {
                    elem.checked = true;
                });
                relatedProductsCheckFlag = true;
                txt.innerHTML = "<?= Mage::helper('core')->jsQuoteEscape($this->__('unselect all')) ?>";
            } else {
                document.querySelectorAll('.related-checkbox').forEach(elem => {
                    elem.checked = false;
                });
                relatedProductsCheckFlag = false;
                txt.innerHTML = "<?= Mage::helper('core')->jsQuoteEscape($this->__('select all')) ?>";
            }
            addRelatedToProduct();
        }

        function addRelatedToProduct() {
            const checkboxes = document.querySelectorAll('.related-checkbox');
            const values = [];

            checkboxes.forEach(checkbox => {
                if (checkbox.checked) values.push(checkbox.value);
            });

            const relatedProductsField = document.getElementById('related-products-field');
            if (relatedProductsField) {
                relatedProductsField.value = values.join(',');
            }
        }
    </script>
</div>
<?php endif ?>
