<?php
/**
 * OpenMage
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available at https://opensource.org/license/afl-3-0-php
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
 * @copyright   Copyright (c) 2021-2024 The OpenMage Contributors (https://www.openmage.org)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/** @var Mage_Adminhtml_Block_Sales_Order_Creditmemo_Create_Items $this */
?>
<?php $_items = $this->getCreditmemo()->getAllItems() ?>
<?php if (count($_items)) : ?>
<div class="grid np">
  <div class="hor-scroll">
    <table cellspacing="0" class="data order-tables">
        <col />
        <col width="1" />
        <col width="1" />
        <col width="1" />
        <?php if ($this->canReturnToStock()) : ?><col width="1" /><?php endif ?>
        <col width="1" />
        <col width="1" />
        <col width="1" />
        <col width="1" />
        <thead>
            <tr class="headings">
                <th><?php echo $this->helper('sales')->__('Product') ?></th>
                <th><?php echo $this->helper('sales')->__('Price') ?></th>
                <th class="a-center"><?php echo $this->helper('sales')->__('Qty') ?></th>
                <?php if ($this->canReturnToStock()) : ?>
                <th><span class="nobr"><?php echo $this->helper('sales')->__('Return to Stock') ?></span></th>
                <?php endif ?>
                <th><span class="nobr"><?php echo $this->helper('sales')->__('Qty to Refund') ?></span></th>
                <th><?php echo $this->helper('sales')->__('Subtotal') ?></th>
                <th><span class="nobr"><?php echo $this->helper('sales')->__('Tax Amount') ?></span></th>
                <th><span class="nobr"><?php echo $this->helper('sales')->__('Discount Amount') ?></span></th>
                <th class="last"><span class="nobr"><?php echo $this->helper('sales')->__('Row Total') ?></span></th>
            </tr>
        </thead>
        <?php if ($this->canEditQty()): ?>
        <tfoot>
            <tr>
                <td colspan="3">&nbsp;</td>
                <td colspan="3" class="a-center">
                    <?php echo $this->getUpdateButtonHtml() ?>
                </td>
                <td colspan="3" class="last">&nbsp;</td>
            </tr>
        </tfoot>
        <?php endif ?>
        <?php $i=0;foreach ($_items as $_item): ?>
            <?php if ($_item->getOrderItem()->getParentItem()) continue; else $i++; ?>
            <tbody class="<?php echo $i%2?'even':'odd' ?>">
                <?php echo $this->getItemHtml($_item) ?>
                <?php echo $this->getItemExtraInfoHtml($_item->getOrderItem()) ?>
            </tbody>
        <?php endforeach ?>
    </table>
  </div>
</div>
<br />
<?php else: ?>
    <div class="entry-edit">
        <fieldset><div class="a-center"><?php echo $this->helper('sales')->__('No Items To Refund') ?></div></fieldset>
    </div>
<?php endif ?>

<div class="clear"></div>
<?php echo $this->getChildHtml('order_totalbar') ?>
<div class="clear"></div>

<input type="hidden" name="creditmemo[do_offline]" id="creditmemo_do_offline" value="0" />

<div class="box-left entry-edit">
    <div class="entry-edit-head"><h4><?php echo Mage::helper('sales')->__('Credit Memo Comments') ?></h4></div>
    <fieldset id="history_form">
    <label class="normal" for="creditmemo_comment_text"><?php echo Mage::helper('sales')->__('Credit Memo Comments') ?></label><br/>
    <textarea id="creditmemo_comment_text" name="creditmemo[comment_text]" rows="3" cols="5" style="width:98%;"><?php echo $this->getCreditmemo()->getCommentText(); ?></textarea>
    </fieldset>
</div>
<div class="box-right entry-edit">
    <div class="entry-edit-head"><h4><?php echo Mage::helper('sales')->__('Refund Totals') ?></h4></div>
    <div class="order-totals">
        <?php echo $this->getChildHtml('creditmemo_totals') ?>
        <div class="order-totals-bottom">
          <div class="divider"></div>
          <p>
              <label class="normal" for="notify_customer"><?php echo Mage::helper('sales')->__('Append Comments') ?></label>
              <input id="notify_customer" name="creditmemo[comment_customer_notify]" value="1" type="checkbox" />
          </p>
          <?php if ($this->canSendCreditmemoEmail()):?>
          <p>
              <label class="normal" for="send_email"><?php echo Mage::helper('sales')->__('Email Copy of Credit Memo') ?></label>
              <input id="send_email" name="creditmemo[send_email]" value="1" type="checkbox" />
          </p>
          <?php endif ?>
              <?php echo $this->getChildHtml('submit_before') ?>
              <?php echo $this->getChildHtml('submit_offline') ?>
              <?php echo $this->getChildHtml('submit_button') ?>
              <?php echo $this->getChildHtml('submit_after') ?>
          </div>
    </div>
</div>
<div class="clear"></div>

<script type="text/javascript">
//<![CDATA[
var submitButtons = $$('.submit-button');
var updateButtons = $$('.update-button');
var fields = $$('.qty-input');

updateButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});

for(var i=0;i<fields.length;i++){
    fields[i].observe('change', checkButtonsRelation)
    fields[i].baseValue = fields[i].value;
}

function checkButtonsRelation() {
    var hasChanges = false;
    fields.each(function (elem) {
        if (elem.baseValue != elem.value) {
            hasChanges = true;
        }
    }.bind(this));
    if (hasChanges) {
        submitButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});
        updateButtons.each(function (elem) {elem.disabled=false;elem.removeClassName('disabled');});
    }
    else {
        submitButtons.each(function (elem) {elem.disabled=false;elem.removeClassName('disabled');});
        updateButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});
    }
}

function submitCreditMemo() {
    if ($('creditmemo_do_offline')) $('creditmemo_do_offline').value=0;
    editForm.submit()
}
function submitCreditMemoOffline() {
    if ($('creditmemo_do_offline')) $('creditmemo_do_offline').value=1;
    editForm.submit()
}

var sendEmailCheckbox = $('send_email');
if (sendEmailCheckbox) {
    var notifyCustomerCheckbox = $('notify_customer');
    var creditmemoCommentText = $('creditmemo_comment_text');
    Event.observe(sendEmailCheckbox, 'change', bindSendEmail);
    bindSendEmail();
}
function bindSendEmail()
{
    if (sendEmailCheckbox.checked == true) {
        notifyCustomerCheckbox.disabled = false;
        //creditmemoCommentText.disabled = false;
    }
    else {
        notifyCustomerCheckbox.disabled = true;
        //creditmemoCommentText.disabled = true;
    }
}
//]]>
</script>
