<?php
/**
 * Maho
 *
 * @category    design
 * @package     default_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_Adminhtml_Block_Sales_Order_Shipment_Create_Items $this */
?>
<div class="grid np">
  <div class="hor-scroll">
    <table cellspacing="0" class="data order-tables">
        <col />
        <col width="1" />
        <col width="1" />
        <?php if (!$this->canShipPartiallyItem()): ?>
        <col width="20" />
        <?php endif ?>
        <thead>
            <tr class="headings">
                <th><?= $this->helper('sales')->__('Product') ?></th>
                <th class="a-center"><?= $this->helper('sales')->__('Qty') ?></th>
                <th<?php if ($this->isShipmentRegular()): ?> class="last"<?php endif ?>><span class="nobr"><?= $this->helper('sales')->__('Qty to Ship') ?></span></th>

                <?php if (!$this->canShipPartiallyItem()): ?>
                <th class="a-center last"><span class="nobr"><?= $this->helper('sales')->__('Ship') ?></span></th>
                <?php endif ?>

            </tr>
        </thead>
        <?php $_items = $this->getShipment()->getAllItems() ?>
        <?php $_i=0;foreach ($_items as $_item): if ($_item->getOrderItem()->getIsVirtual() || $_item->getOrderItem()->getParentItem()): continue; endif; $_i++ ?>
        <tbody class="<?= $_i%2?'odd':'even' ?>">
            <?= $this->getItemHtml($_item) ?>
            <?= $this->getItemExtraInfoHtml($_item->getOrderItem()) ?>
        </tbody>
        <?php endforeach ?>
    </table>
  </div>
</div>
<br />
<div class="box-left entry-edit">
    <div class="entry-edit-head"><h4><?= $this->__('Shipment Comments') ?></h4></div>
    <fieldset>
        <div id="order-history_form">
            <span class="field-row">
                <label class="normal" for="shipment_comment_text"><?= Mage::helper('sales')->__('Shipment Comments') ?></label>
                <textarea id="shipment_comment_text" name="shipment[comment_text]" rows="3" cols="5" style="height:6em; width:99%;"><?= $this->getShipment()->getCommentText() ?></textarea>
            </span>
            <div class="clear"></div>
        </div>
    </fieldset>
</div>

<div class="box-right entry-edit">
    <div class="order-totals">
        <div class="order-totals-bottom">
          <?php if ($this->canCreateShippingLabel()): ?>
          <p>
              <label class="normal" for="create_shipping_label"><?= Mage::helper('sales')->__('Create Shipping Label') ?></label>
              <input id="create_shipping_label" name="shipment[create_shipping_label]" value="1" type="checkbox"  onclick="toggleCreateLabelCheckbox();" />
          </p>
          <?php endif ?>
          <p>
              <label class="normal" for="notify_customer"><?= Mage::helper('sales')->__('Append Comments') ?></label>
              <input id="notify_customer" name="shipment[comment_customer_notify]" value="1" type="checkbox" />
          </p>
          <?php if ($this->canSendShipmentEmail()): ?>
          <p>
              <label class="normal" for="send_email"><?= Mage::helper('sales')->__('Email Copy of Shipment') ?></label>
              <input id="send_email" name="shipment[send_email]" value="1" type="checkbox" />
          </p>
          <?php endif ?>
          <div class="a-right">
          <?= $this->getChildHtml('submit_button') ?>
          </div>
        </div>
    </div>
</div>
<div class="clear"></div>
<script type="text/javascript">
//<![CDATA[
var sendEmailCheckbox = $('send_email');
if (sendEmailCheckbox) {
    var notifyCustomerCheckbox = $('notify_customer');
    var shipmentCommentText = $('shipment_comment_text');
    Event.observe(sendEmailCheckbox, 'change', bindSendEmail);
    bindSendEmail();
}
function bindSendEmail() {
    if (sendEmailCheckbox.checked == true) {
        notifyCustomerCheckbox.disabled = false;
        //shipmentCommentText.disabled = false;
    }
    else {
        notifyCustomerCheckbox.disabled = true;
        //shipmentCommentText.disabled = true;
    }
}
function toggleCreateLabelCheckbox() {
    var checkbox = $('create_shipping_label');
    var submitButton = checkbox.up('.order-totals').select('.submit-button span')[0];
    if (checkbox.checked) {
        submitButton.innerText += '...';
    } else {
        submitButton.innerText = submitButton.innerText.replace(/\.\.\.$/, '');
    }
}
function submitShipment(btn) {
    var checkbox = $(btn).up('.order-totals').select('#create_shipping_label')[0];

    if (!validQtyItems()) {
        return;
    }

    if (checkbox && checkbox.checked) {
        packaging.showWindow();
    } else if(editForm.submit()) {
        disableElements('submit-button');
    }
}

function validQtyItems() {
    var valid = true;
    var errorMessage = '<?= Mage::helper('core')->jsQuoteEscape($this->helper('sales')->__('Invalid value(s) for Qty to Ship')) ?>';
    $$('.qty-item').each(function(item) {
        var val = parseFloat(item.value);
        if (isNaN(val) || val < 0) {
            valid = false;
            alert(errorMessage);
            throw $break;
        }
    });
    return valid;
}
//]]>
</script>
