<?php
/**
 * Maho
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2022 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 if(!$this->getDontDisplayContainer()): ?>
<script type="text/javascript">
    if (!window.toogleVisibilityOnObjects) {
        var toogleVisibilityOnObjects = function(source, objects) {
            if (typeof source == 'string') {
                source = document.getElementById(source);
            }

            if (source && source.checked) {
                // Show elements
                objects.forEach(function(item) {
                    const element = document.getElementById(item);
                    if (element) {
                        element.style.display = 'block';
                        // Remove validation-passed from input fields
                        element.querySelectorAll('.input-text').forEach(function(input) {
                            input.classList.remove('validation-passed');
                        });
                    }
                });
            } else {
                // Hide elements
                objects.forEach(function(item) {
                    const element = document.getElementById(item);
                    if (element) {
                        element.style.display = 'none';

                        // Add validation-passed to input fields
                        element.querySelectorAll('.input-text').forEach(function(input) {
                            input.classList.add('validation-passed');
                        });

                        // Clear textarea values
                        element.querySelectorAll('.giftmessage-area').forEach(function(textarea) {
                            textarea.value = '';
                        });

                        // Uncheck checkboxes
                        element.querySelectorAll('.checkbox').forEach(function(checkbox) {
                            checkbox.checked = false;
                        });

                        // Clear select values
                        element.querySelectorAll('.select').forEach(function(select) {
                            select.value = '';
                        });

                        // Add no-display class to price boxes
                        element.querySelectorAll('.price-box').forEach(function(priceBox) {
                            priceBox.classList.add('no-display');
                        });
                    }
                });
            }
        }
    }

    if (!window.toogleRequired) {
        var toogleRequired = function (source, objects) {
            source = document.getElementById(source);
            if (source && source.value.trim() !== '') {
                objects.forEach(function(item) {
                    const element = document.getElementById(item);
                    if (element) {
                        element.classList.add('required-entry');
                    }
                });
            } else {
                objects.forEach(function(item) {
                    const element = document.getElementById(item);
                    if (element) {
                        if (typeof shippingMethod !== 'undefined' && shippingMethod.validator) {
                            shippingMethod.validator.reset(item);
                        }
                        element.classList.remove('required-entry');
                    }
                });
            }
        }
    }

    if (window.shipping) {
        shipping.onSave = function(evt) {
            fetch('<?= $this->getAdditionalUrl() ?>', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            })
                .then(response => response.text())
                .then(html => {
                    const container = document.getElementById('onepage-checkout-shipping-method-additional-load');
                    container.innerHTML = html;

                    // Execute any scripts in the response
                    const scripts = container.getElementsByTagName('script');
                    for (let script of scripts) {
                        eval(script.innerHTML);
                    }

                    this.nextStep(evt);
                })
                .catch(error => console.error('Error:', error));
        }.bind(shipping);

        billing.onSave = function(evt) {
            fetch('<?= $this->getAdditionalUrl() ?>', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            })
                .then(response => response.text())
                .then(html => {
                    const container = document.getElementById('onepage-checkout-shipping-method-additional-load');
                    container.innerHTML = html;

                    // Execute any scripts in the response
                    const scripts = container.getElementsByTagName('script');
                    for (let script of scripts) {
                        eval(script.innerHTML);
                    }

                    this.nextStep(evt);
                })
                .catch(error => console.error('Error:', error));
        }.bind(billing);
    }
</script>
<?php endif ?>
<?php if ($this->isMessagesAvailable() || $this->isItemsAvailable()): ?>
<?php switch ($this->getType()): ?>
<?php case 'onepage_checkout': ?>
    <div class="gift-messages">
        <h3><?= $this->__('Do you have any gift items in your order?') ?></h3>
        <p class="control">
            <input type="checkbox" name="allow_gift_messages" id="allow_gift_messages" value="1" onclick="toogleVisibilityOnObjects(this, ['allow-gift-message-container']);"<?php if($this->getItemsHasMesssages() || $this->getEntityHasMessage()): ?> checked="checked"<?php endif ?> class="checkbox" />
            <label for="allow_gift_messages"><?= $this->__('Add gift options.') ?></label>
        </p>
    </div>
    <div class="gift-messages-form" id="allow-gift-message-container">
        <div class="inner-box">
            <?php if ($this->isMessagesAvailable()): ?>
                <h4><?= $this->__('Gift Options for the Entire Order.') ?></h4>
                <p>
                    <input type="checkbox" name="allow_gift_messages_for_order" id="allow_gift_messages_for_order" value="1" onclick="toogleVisibilityOnObjects(this, ['allow-gift-messages-for-order-container']);"<?php if($this->getEntityHasMessage()): ?> checked="checked"<?php endif ?> class="checkbox" />
                    <label for="allow_gift_messages_for_order"><?= $this->__('Add gift options for the Entire Order') ?></label>
                </p>
                <div class="allow-gift-messages-for-order-container" id="allow-gift-messages-for-order-container" style="display:none">
                    <p><?= $this->__('You can leave this box blank if you do not wish to add a gift message for whole order.') ?></p>
                    <input type="hidden" name="giftmessage[<?= $this->getEntity()->getId() ?>][type]"  value="quote" />
                    <ul class="form-list">
                        <li class="fields">
                            <div class="field">
                                <label for="gift-message-whole-from"><?= $this->__('From') ?></label>
                                <div class="input-box">
                                    <input type="text" name="giftmessage[<?= $this->getEntity()->getId() ?>][from]" id="gift-message-whole-from" title="<?= Mage::helper('core')->quoteEscape($this->__('From')) ?>"  value="<?= $this->getEscaped($this->getMessage()->getSender(), $this->getDefaultFrom()) ?>" class="input-text validation-passed" />
                                </div>
                            </div>
                            <div class="field">
                                <label for="gift-message-whole-to"><?= $this->__('To') ?></label>
                                <div class="input-box">
                                    <input type="text" name="giftmessage[<?= $this->getEntity()->getId() ?>][to]" id="gift-message-whole-to" title="<?= Mage::helper('core')->quoteEscape($this->__('To')) ?>" value="<?= $this->getEscaped($this->getMessage()->getRecipient(), $this->getDefaultTo()) ?>" class="input-text validation-passed" />
                                </div>
                            </div>
                        </li>
                        <li class="wide">
                            <label for="gift-message-whole-message"><?= $this->__('Message') ?></label>
                            <div class="input-box">
                                <textarea id="gift-message-whole-message" onchange="toogleRequired('gift-message-whole-message', ['gift-message-whole-from','gift-message-whole-to'])" class="input-text validation-passed giftmessage-area" name="giftmessage[<?= $this->getEntity()->getId() ?>][message]" title="<?= Mage::helper('core')->quoteEscape($this->__('Message')) ?>" rows="5" cols="10"><?= $this->getEscaped($this->getMessage()->getMessage()) ?></textarea>
                            </div>
                        </li>
                    </ul>
                    <script type="text/javascript">
                        toogleRequired('gift-message-whole-message', ['gift-message-whole-from','gift-message-whole-to']);
                    </script>
                </div>
             <?php endif ?>
             <?php if($this->isItemsAvailable()): ?>
                 <h4><?= $this->__('Gift Options for Individual Items') ?></h4>
                 <p>
                    <input type="checkbox" name="allow_gift_messages_for_items" id="allow_gift_messages_for_items" value="1" onclick="toogleVisibilityOnObjects(this, ['allow-gift-messages-for-items-container']);"<?php if($this->getItemsHasMesssages()): ?> checked="checked"<?php endif ?> class="checkbox" />
                    <label for="allow_gift_messages_for_items"><?= $this->__('Add gift options for Individual Items') ?></label>
                </p>
                <div id="allow-gift-messages-for-items-container">
                 <p><?= $this->__('You can leave this box blank if you do not wish to add a gift message for the item.') ?></p>
                 <ol>
                 <?php foreach($this->getItems() as $_index=>$_item): ?>
                 <?php $_product=$_item->getProduct() ?>
                 <li class="item">
                     <div class="product-img-box">
                         <p class="product-image">
                             <img src="<?= $this->helper('catalog/image')->init($_product, 'thumbnail')->resize(75) ?>" width="75" height="75" alt="<?= $this->escapeHtml($_product->getName()) ?>" title="<?= $this->escapeHtml($_product->getName()) ?>" />
                         </p>
                         <p class="number"><?= $this->__('Item %d of %d', $_index+1, $this->countItems()) ?></p>
                     </div>
                     <div class="details">
                         <div class="f-fix">
                             <h5 class="product-name"><?= $this->escapeHtml($_product->getName()) ?></h5>
                             <input type="hidden" name="giftmessage[<?= $_item->getId() ?>][type]" value="quote_item" />
                              <ul class="form-list">
                                 <li class="fields">
                                     <div class="field">
                                         <label for="gift-message-<?= $_item->getId() ?>-from"><?= $this->__('From') ?></label>
                                         <div class="input-box">
                                             <input type="text" name="giftmessage[<?= $_item->getId() ?>][from]" id="gift-message-<?= $_item->getId() ?>-from" title="<?= Mage::helper('core')->quoteEscape($this->__('From')) ?>" value="<?= $this->getEscaped($this->getMessage($_item)->getSender(), $this->getDefaultFrom()) ?>" class="input-text validation-passed" />
                                         </div>
                                     </div>
                                     <div class="field">
                                         <label for="gift-message-<?= $_item->getId() ?>-to"><?= $this->__('To') ?></label>
                                         <div class="input-box">
                                             <input type="text" name="giftmessage[<?= $_item->getId() ?>][to]" id="gift-message-<?= $_item->getId() ?>-to" title="<?= Mage::helper('core')->quoteEscape($this->__('To')) ?>" value="<?= $this->getEscaped($this->getMessage($_item)->getRecipient(), $this->getDefaultTo()) ?>" class="input-text validation-passed" />
                                         </div>
                                     </div>
                                 </li>
                                 <li class="wide">
                                     <label for="gift-message-<?= $_item->getId() ?>-message"><?= $this->__('Message') ?></label>
                                     <div class="input-box">
                                         <textarea id="gift-message-<?= $_item->getId() ?>-message" onchange="toogleRequired('gift-message-<?= $_item->getId() ?>-message', ['gift-message-<?= $_item->getId() ?>-from','gift-message-<?= $_item->getId() ?>-to'])" class="input-text validation-passed giftmessage-area" name="giftmessage[<?= $_item->getId() ?>][message]" title="<?= Mage::helper('core')->quoteEscape($this->__('Message')) ?>" rows="5" cols="40"><?= $this->getEscaped($this->getMessage($_item)->getMessage()) ?></textarea>
                                     </div>
                                 </li>
                             </ul>
                             <script type="text/javascript">
                                toogleRequired('gift-message-<?= $_item->getId() ?>-message', ['gift-message-<?= $_item->getId() ?>-from','gift-message-<?= $_item->getId() ?>-to']);
                             </script>
                         </div>
                     </div>
                 </li>
                 <?php endforeach ?>
             </ol>
             </div>
             <?php endif ?>
        </div>
    </div>
    <script type="text/javascript">
        toogleVisibilityOnObjects('allow_gift_messages', ['allow-gift-message-container']);
        toogleVisibilityOnObjects('allow_gift_messages_for_order', ['allow-gift-messages-for-order-container']);
        toogleVisibilityOnObjects('allow_gift_messages_for_items', ['allow-gift-messages-for-items-container']);
    </script>
<?php break ?>
<?php case 'multishipping_adress_checkbox': ?>

<?php break ?>
<?php case 'multishipping_adress': ?>
    <div class="gift-messages">
        <h3><?= $this->__('Do you have any gift items in your order?') ?></h3>
        <p class="control">
            <input type="checkbox" name="allow_gift_messages_<?= $this->getEntity()->getId() ?>" id="allow_gift_messages_<?= $this->getEntity()->getId() ?>" value="1" onclick="toogleVisibilityOnObjects(this, ['allow-gift-message-container-<?= $this->getEntity()->getId() ?>']);"<?php if($this->getItemsHasMesssages() || $this->getEntityHasMessage()): ?> checked="checked"<?php endif ?> class="checkbox" />
            <label for="allow_gift_messages_<?= $this->getEntity()->getId() ?>"><?= $this->__('Add gift options') ?></label>
        </p>
    </div>
    <div class="gift-messages-form" id="allow-gift-message-container-<?= $this->getEntity()->getId() ?>">
        <div class="inner-box">
            <?php if ($this->isMessagesAvailable()): ?>
            <h4><?= $this->__('Gift Options for this address.') ?></h4>
            <p>
                 <input type="checkbox" name="allow_gift_messages_for_order_<?= $this->getEntity()->getId() ?>" id="allow_gift_messages_for_order_<?= $this->getEntity()->getId() ?>" value="1" onclick="toogleVisibilityOnObjects(this, ['allow-gift-messages-for-order-container-<?= $this->getEntity()->getId() ?>']);"<?php if($this->getEntityHasMessage()): ?> checked="checked"<?php endif ?> class="checkbox" />
                 <label for="allow_gift_messages_for_order"><?= $this->__('Add gift options for the Entire Order') ?></label>
            </p>
            <div id="allow-gift-messages-for-order-container-<?= $this->getEntity()->getId() ?>">
                <p><?= $this->__('You can leave this box blank if you do not wish to add a gift message for this address.') ?></p>
                <input type="hidden" name="giftmessage[<?= $this->getEntity()->getId() ?>][type]" value="quote_address" />
                <ul class="form-list">
                    <li class="fields">
                        <div class="field">
                            <label for="gift-message-<?= $this->getEntity()->getId() ?>-from"><?= $this->__('From') ?></label>
                            <div class="input-box">
                                <input type="text" name="giftmessage[<?= $this->getEntity()->getId() ?>][from]" id="gift-message-<?= $this->getEntity()->getId() ?>-from" title="<?= Mage::helper('core')->quoteEscape($this->__('From')) ?>" value="<?= $this->getEscaped($this->getMessage()->getSender(), $this->getDefaultFrom()) ?>" class="input-text validation-passed" />
                            </div>
                        </div>
                        <div class="field">
                            <label for="gift-message-<?= $this->getEntity()->getId() ?>-to"><?= $this->__('To') ?></label>
                            <div class="input-box">
                                <input type="text" name="giftmessage[<?= $this->getEntity()->getId() ?>][to]" id="gift-message-<?= $this->getEntity()->getId() ?>-to" title="<?= Mage::helper('core')->quoteEscape($this->__('To')) ?>" value="<?= $this->getEscaped($this->getMessage()->getRecipient(), $this->getDefaultTo()) ?>" class="input-text validation-passed" />
                            </div>
                        </div>
                    </li>
                    <li class="wide">
                        <label for="gift-message-<?= $this->getEntity()->getId() ?>-message"><?= $this->__('Message') ?></label>
                        <div class="input-box">
                            <textarea id="gift-message-<?= $this->getEntity()->getId() ?>-message" onchange="toogleRequired('gift-message-<?= $this->getEntity()->getId() ?>-message', ['gift-message-<?= $this->getEntity()->getId() ?>-from','gift-message-<?= $this->getEntity()->getId() ?>-to'])" class="input-text validation-passed giftmessage-area" name="giftmessage[<?= $this->getEntity()->getId() ?>][message]" title="<?= Mage::helper('core')->quoteEscape($this->__('Message')) ?>" rows="5" cols="40"><?= $this->getEscaped($this->getMessage()->getMessage()) ?></textarea>
                        </div>
                    </li>
                </ul>
                <script type="text/javascript">
                    var shippingMethod = new VarienForm('shipping_method_form');
                    toogleRequired('gift-message-<?= $this->getEntity()->getId() ?>-message', ['gift-message-<?= $this->getEntity()->getId() ?>-from','gift-message-<?= $this->getEntity()->getId() ?>-to']);
                </script>
            </div>
            <?php endif ?>
            <?php if($this->isItemsAvailable()): ?>
            <h4><?= $this->__('Gift Options for Individual Items') ?></h4>
            <p>
                <input type="checkbox" name="allow_gift_messages_for_items_<?= $this->getEntity()->getId() ?>" id="allow_gift_messages_for_items_<?= $this->getEntity()->getId() ?>" value="1" onclick="toogleVisibilityOnObjects(this, ['allow-gift-messages-for-items-container-<?= $this->getEntity()->getId() ?>']);"<?php if($this->getItemsHasMesssages()): ?> checked="checked"<?php endif ?> class="checkbox" />
                <label for="allow_gift_messages_for_items_<?= $this->getEntity()->getId() ?>"><?= $this->__('Add gift options for Individual Items') ?></label>
            </p>
            <div id="allow-gift-messages-for-items-container-<?= $this->getEntity()->getId() ?>">
             <p><?= $this->__('You can leave this box blank if you do not wish to add a gift message for the item.') ?></p>
             <ol>
             <?php foreach($this->getItems() as $_index=>$_item): ?>
             <?php $_product=$_item->getProduct() ?>
                 <li class="item">
                     <div class="product-img-box">
                         <p class="product-image"><img src="<?= $this->helper('catalog/image')->init($_product, 'small_image')->resize(75) ?>" width="75" height="75" alt="<?= $this->escapeHtml($_product->getName()) ?>" title="<?= $this->escapeHtml($_product->getName()) ?>" /></p>
                         <p class="number"><?= $this->__('Item %d of %d', $_index+1, $this->countItems()) ?></p>
                     </div>
                     <div class="details">
                         <div class="f-fix">
                             <input type="hidden" name="giftmessage[<?= $_item->getId() ?>][type]" value="quote_address_item" />
                             <input type="hidden" name="giftmessage[<?= $_item->getId() ?>][address]" value="<?= $this->getEntity()->getId() ?>" />
                             <h5 class="product-name"><?= $this->escapeHtml($_product->getName()) ?></h5>
                             <ul class="form-list">
                                 <li class="fields">
                                     <div class="field">
                                         <label for="gift-message-<?= $_item->getId() ?>-from"><?= $this->__('From') ?></label>
                                         <div class="input-box">
                                             <input type="text" name="giftmessage[<?= $_item->getId() ?>][from]" id="gift-message-<?= $_item->getId() ?>-from" title="<?= Mage::helper('core')->quoteEscape($this->__('From')) ?>"  value="<?= $this->getEscaped($this->getMessage($_item)->getSender(), $this->getDefaultFrom()) ?>" class="input-text validation-passed" />
                                         </div>
                                     </div>
                                     <div class="field">
                                         <label for="gift-message-<?= $_item->getId() ?>-to"><?= $this->__('To') ?></label>
                                         <div class="input-box">
                                             <input type="text" name="giftmessage[<?= $_item->getId() ?>][to]" id="gift-message-<?= $_item->getId() ?>-to" title="<?= Mage::helper('core')->quoteEscape($this->__('To')) ?>" value="<?= $this->getEscaped($this->getMessage($_item)->getRecipient(), $this->getDefaultTo()) ?>" class="input-text validation-passed" />
                                         </div>
                                     </div>
                                 </li>
                                 <li class="wide">
                                     <label for="gift-message-<?= $_item->getId() ?>-message"><?= $this->__('Message') ?></label>
                                     <div class="input-box">
                                         <textarea id="gift-message-<?= $_item->getId() ?>-message" onchange="toogleRequired('gift-message-<?= $_item->getId() ?>-message', ['gift-message-<?= $_item->getId() ?>-from','gift-message-<?= $_item->getId() ?>-to'])" class="input-text validation-passed  giftmessage-area" name="giftmessage[<?= $_item->getId() ?>][message]" title="<?= Mage::helper('core')->quoteEscape($this->__('Message')) ?>" rows="5" cols="10"><?= $this->getEscaped($this->getMessage($_item)->getMessage()) ?></textarea>
                                     </div>
                                 </li>
                             </ul>
                             <script type="text/javascript">
                                 toogleRequired('gift-message-<?= $_item->getId() ?>-message', ['gift-message-<?= $_item->getId() ?>-from','gift-message-<?= $_item->getId() ?>-to']);
                             </script>
                         </div>
                     </div>
                 </li>
             <?php endforeach ?>
             </ol>
            </div>
            <?php endif ?>
        </div>
    </div>
    <script type="text/javascript">
        toogleVisibilityOnObjects('allow_gift_messages_<?= $this->getEntity()->getId() ?>', ['allow-gift-message-container-<?= $this->getEntity()->getId() ?>']);
        toogleVisibilityOnObjects('allow_gift_messages_for_order_<?= $this->getEntity()->getId() ?>', ['allow-gift-messages-for-order-container-<?= $this->getEntity()->getId() ?>']);
        toogleVisibilityOnObjects('allow_gift_messages_for_items_<?= $this->getEntity()->getId() ?>', ['allow-gift-messages-for-items-container-<?= $this->getEntity()->getId() ?>']);
    </script>
    <?php break ?>
<?php endswitch ?>
<?php endif ?>
