<?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_Wishlist_Block_Customer_Wishlist $this */
?>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
    <div class="my-wishlist">
        <div class="page-title title-buttons">
            <?php if ($this->helper('wishlist')->isRssAllow() && $this->hasWishlistItems()): ?>
                <a href="<?= $this->helper('wishlist')->getRssUrl($this->getWishlistInstance()->getId()) ?>" class="link-rss"><?= $this->__('RSS Feed') ?></a>
            <?php endif ?>
            <h1><?= $this->getTitle() ?></h1>
        </div>
        <?= $this->getMessagesBlock()->toHtml() ?>

        <form id="wishlist-view-form" action="<?= $this->getUrl('*/*/update', ['wishlist_id' => $this->getWishlistInstance()->getId()]) ?>" method="post">
            <?= $this->getChildHtml('top') ?>
            <fieldset>
                <?php if ($this->hasWishlistItems()): ?>
                        <?= $this->getBlockHtml('formkey') ?>
                        <?php $this->getChild('items')->setItems($this->getWishlistItems()); ?>
                        <?= $this->getChildHtml('items') ?>
                <?php else: ?>
                    <p class="wishlist-empty"><?= $this->__('You have no items in your wishlist.') ?></p>
                <?php endif ?>
                <div class="buttons-set buttons-set2">
                    <?= $this->getChild('control_buttons')->toHtml() ?>
                </div>
            </fieldset>
        </form>

        <form id="wishlist-allcart-form" action="<?= $this->getUrl('*/*/allcart') ?>" method="post">
            <?= $this->getBlockHtml('formkey') ?>
            <div class="no-display">
                <input type="hidden" name="wishlist_id" id="wishlist_id" value="<?= $this->getWishlistInstance()->getId() ?>" />
                <input type="hidden" name="qty" id="qty" value="" />
            </div>
        </form>

        <script type="text/javascript">
            const wishlistForm = new Validation(document.getElementById('wishlist-view-form'));
            const wishlistAllCartForm = new Validation(document.getElementById('wishlist-allcart-form'));

            function calculateQty() {
                const itemQtys = [];

                // Select all elements with class 'qty' inside the wishlist view form
                document.querySelectorAll('#wishlist-view-form .qty').forEach(input => {
                    const idxStr = input.name;
                    const idx = idxStr.replace(/[^\d.]/g, '');
                    itemQtys[idx] = input.value;
                });

                // Set the stringified array to the qty input
                const qtyInput = document.querySelector('#qty');
                if (qtyInput) {
                    qtyInput.value = JSON.stringify(itemQtys);
                }
            }

            function addAllWItemsToCart() {
                calculateQty();
                if (wishlistAllCartForm && wishlistAllCartForm.form) {
                    wishlistAllCartForm.form.submit();
                }
            }
        </script>
    </div>
    <?= $this->getChildHtml('bottom') ?>
    <div class="buttons-set">
        <p class="back-link"><a href="<?= $this->escapeUrl($this->getBackUrl()) ?>"><small>&laquo; </small><?= $this->__('Back') ?></a></p>
    </div>
<?php endif ?>
