<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php $_items = $block->getCreditmemo()->getAllItems() ?>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?php /* @escapeNotVerified */ echo __('Items to Refund') ?></span>
    </div>

    <?php if (count($_items)) : ?>
    <div class="admin__table-wrapper">
        <table class="data-table admin__table-primary order-creditmemo-tables">
            <thead>
                <tr class="headings">
                    <th class="col-product"><span><?php /* @escapeNotVerified */ echo __('Product') ?></span></th>
                    <th class="col-price"><span><?php /* @escapeNotVerified */ echo __('Price') ?></span></th>
                    <th class="col-ordered-qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></th>
                    <?php if ($block->canReturnToStock()) : ?>
                    <th class="col-return-to-stock"><span><?php /* @escapeNotVerified */ echo __('Return to Stock') ?></span></th>
                    <?php endif; ?>
                    <th class="col-refund"><span><?php /* @escapeNotVerified */ echo __('Qty to Refund') ?></span></th>
                    <th class="col-subtotal"><span><?php /* @escapeNotVerified */ echo __('Subtotal') ?></span></th>
                    <th class="col-tax-amount"><span><?php /* @escapeNotVerified */ echo __('Tax Amount') ?></span></th>
                    <th class="col-discont"><span><?php /* @escapeNotVerified */ echo __('Discount Amount') ?></span></th>
                    <th class="col-total last"><span><?php /* @escapeNotVerified */ echo __('Row Total') ?></span></th>
                </tr>
            </thead>
            <?php if ($block->canEditQty()): ?>
            <tfoot>
                <tr>
                    <td colspan="3">&nbsp;</td>
                    <td colspan="3">
                        <?php echo $block->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 /* @escapeNotVerified */ echo $i%2 ? 'even' : 'odd' ?>">
                    <?php echo $block->getItemHtml($_item) ?>
                    <?php echo $block->getItemExtraInfoHtml($_item->getOrderItem()) ?>
                </tbody>
            <?php endforeach; ?>
        </table>
    </div>
    <?php else : ?>
    <div class="no-items">
        <?php /* @escapeNotVerified */ echo __('No Items To Refund') ?>
    </div>
    <?php endif; ?>
</section>

<?php $orderTotalBar = $block->getChildHtml('order_totalbar'); ?>

<?php if (!empty($orderTotalBar)): ?>
<section class="fieldset-wrapper">
    <?php /* @escapeNotVerified */ echo $orderTotalBar; ?>
</section>
<?php endif; ?>

<section class="admin__page-section">
    <input type="hidden" name="creditmemo[do_offline]" id="creditmemo_do_offline" value="0" />
    <div class="admin__page-section-title">
        <span class="title"><?php /* @escapeNotVerified */ echo __('Order Total') ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-comments-history">
            <div class="admin__page-section-item-title">
                <span class="title"><?php /* @escapeNotVerified */ echo __('Credit Memo Comments') ?></span>
            </div>
            <div id="history_form" class="admin__fieldset-wrapper-content">
                <div class="admin__field">
                    <label class="normal admin__field-label"
                           for="creditmemo_comment_text">
                        <span><?php /* @escapeNotVerified */ echo __('Comment Text') ?></span></label>
                    <div class="admin__field-control">
                        <textarea id="creditmemo_comment_text"
                                  class="admin__control-textarea"
                                  name="creditmemo[comment_text]"
                                  rows="3"
                                  cols="5"><?php /* @escapeNotVerified */ echo $block->getCreditmemo()->getCommentText(); ?></textarea>
                    </div>
                </div>
            </div>
        </div>
        <div class="admin__page-section-item order-totals creditmemo-totals">
            <div class="admin__page-section-item-title">
                <span class="title"><?php /* @escapeNotVerified */ echo __('Refund Totals') ?></span>
            </div>
            <?php echo $block->getChildHtml('creditmemo_totals') ?>
            <div class="order-totals-actions">
                <div class="field choice admin__field admin__field-option field-append-comments">
                    <input id="notify_customer"
                           class="admin__control-checkbox"
                           name="creditmemo[comment_customer_notify]"
                           value="1"
                           type="checkbox" />
                    <label for="notify_customer" class="admin__field-label">
                        <span><?php /* @escapeNotVerified */ echo __('Append Comments') ?></span>
                    </label>
                </div>
                <?php if ($block->canSendCreditmemoEmail()):?>
                <div class="field choice admin__field admin__field-option field-email-copy">
                    <input id="send_email"
                           class="admin__control-checkbox"
                           name="creditmemo[send_email]"
                           value="1"
                           type="checkbox" />
                    <label for="send_email" class="admin__field-label">
                        <span><?php /* @escapeNotVerified */ echo __('Email Copy of Credit Memo') ?></span>
                    </label>
                </div>
                <?php endif; ?>
                <?php echo $block->getChildHtml('submit_before') ?>
                <div class="actions">
                    <?php echo $block->getChildHtml('submit_offline') ?>
                    <?php echo $block->getChildHtml('submit_button') ?>
                    <?php echo $block->getChildHtml('submit_after') ?>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
require(['jquery', 'prototype'], function(jQuery){

//<![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');});
    }
}

submitCreditMemo = function() {
    if ($('creditmemo_do_offline')) $('creditmemo_do_offline').value=0;
    // Temporary solution will be replaced after refactoring order functionality
    jQuery('#edit_form').triggerHandler('save');
}

submitCreditMemoOffline = function() {
    if ($('creditmemo_do_offline')) $('creditmemo_do_offline').value=1;
    // Temporary solution will be replaced after refactoring order functionality
    jQuery('#edit_form').triggerHandler('save');
}

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;
    }
}

window.bindSendEmail = bindSendEmail;
window.checkButtonsRelation = checkButtonsRelation;
//]]>

});
</script>
