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

/* @var \Magento\Sales\Block\Adminhtml\Order\Creditmemo\Create\Items $block */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>
<?php
/** @var Magento\Sales\ViewModel\CreditMemo\Create\UpdateTotalsButton $viewModel */
$viewModel = $block->getData('viewModel');
/** @var Magento\Sales\ViewModel\CreditMemo\Create\ItemsToRender $itemsToRenderViewModel */
$itemsToRenderViewModel = $block->getData('itemsToRenderViewModel');
$_items = $itemsToRenderViewModel->getItems();
$commentText = $block->getCreditmemo()->getCommentText();
?>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= $block->escapeHtml(__('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><?= $block->escapeHtml(__('Product')) ?></span></th>
                    <th class="col-price"><span><?= $block->escapeHtml(__('Price')) ?></span></th>
                    <th class="col-ordered-qty"><span><?= $block->escapeHtml(__('Qty')) ?></span></th>
                    <?php if ($block->canReturnToStock()): ?>
                    <th class="col-return-to-stock"><span><?= $block->escapeHtml(__('Return to Stock')) ?></span></th>
                    <?php endif; ?>
                    <th class="col-refund"><span><?= $block->escapeHtml(__('Qty to Refund')) ?></span></th>
                    <th class="col-subtotal"><span><?= $block->escapeHtml(__('Subtotal')) ?></span></th>
                    <th class="col-tax-amount"><span><?= $block->escapeHtml(__('Tax Amount')) ?></span></th>
                    <th class="col-discont"><span><?= $block->escapeHtml(__('Discount Amount')) ?></span></th>
                    <th class="col-total last"><span><?= $block->escapeHtml(__('Row Total')) ?></span></th>
                </tr>
            </thead>
            <?php if ($block->canEditQty()): ?>
            <tfoot>
                <tr>
                    <td colspan="4">&nbsp;</td>
                    <td>
                        <?= $block->getUpdateButtonHtml() ?>
                    </td>
                    <td colspan="4" class="last">&nbsp;</td>
                </tr>
            </tfoot>
            <?php endif; ?>
            <?php $i = 0; foreach ($_items as $_item): ?>
                <?php if ($_item->getOrderItem()->getParentItem()):
                    continue;
                else:
                    $i++;
                endif; ?>
                <tbody class="<?= /* @noEscape */ $i%2 ? 'even' : 'odd' ?>">
                    <?= $block->getItemHtml($_item) ?>
                    <?= $block->getItemExtraInfoHtml($_item->getOrderItem()) ?>
                </tbody>
            <?php endforeach; ?>
        </table>
    </div>
    <?php else: ?>
    <div class="no-items">
        <?= $block->escapeHtml(__('No Items To Refund')) ?>
    </div>
    <?php endif; ?>
</section>

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

<?php if (!empty($orderTotalBar)): ?>
<section class="fieldset-wrapper">
    <?= /* @noEscape */ $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"><?= $block->escapeHtml(__('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"><?= $block->escapeHtml(__('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><?= $block->escapeHtml(__('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"><?= $block->escapeHtml($commentText) ?></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"><?= $block->escapeHtml(__('Refund Totals')) ?></span>
            </div>
            <?= $block->getChildHtml('creditmemo_totals') ?>
            <div class="totals-actions"><?= /* @noEscape */ $viewModel->getUpdateTotalsButton() ?></div>
            <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><?= $block->escapeHtml(__('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><?= $block->escapeHtml(__('Email Copy of Credit Memo')) ?></span>
                    </label>
                </div>
                <?php endif; ?>
                <?= $block->getChildHtml('submit_before') ?>
                <div class="actions">
                    <?= $block->getChildHtml('submit_offline') ?>
                    <?= $block->getChildHtml('submit_button') ?>
                    <?= $block->getChildHtml('submit_after') ?>
                </div>
            </div>
        </div>
    </div>
</section>

<?php $scriptString = <<<script

require(['jquery'], function(jQuery){

//<![CDATA[
var submitButtons = jQuery('.submit-button');
var updateButtons = jQuery('.update-button, .update-totals-button');
var fields = jQuery('.qty-input, .order-subtotal-table input[type="text"]');
function enableButtons(buttons) {
    buttons.removeClass('disabled').prop('disabled', false);
}

function disableButtons(buttons) {
    buttons.addClass('disabled').prop('disabled', true);
}

disableButtons(updateButtons);

fields.on('change', checkButtonsRelation);
fields.each(function (i, elem) {
    elem.baseValue = elem.value;
});

function checkButtonsRelation() {
    var hasChanges = false;
    fields.each(function (i, elem) {
        if (elem.baseValue != elem.value) {
            hasChanges = true;
        }
    }.bind(this));
    if (hasChanges) {
        disableButtons(submitButtons);
        enableButtons(updateButtons);
    }
    else {
        enableButtons(submitButtons);
        disableButtons(updateButtons);
    }
}

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

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

var sendEmailCheckbox = jQuery('#send_email');
if (sendEmailCheckbox.length) {
    var notifyCustomerCheckbox = jQuery('#notify_customer');
    sendEmailCheckbox.on('change', bindSendEmail);
    bindSendEmail();
}

function bindSendEmail() {
    if (sendEmailCheckbox.prop('checked') == true) {
        notifyCustomerCheckbox.prop('disabled', false);
    }
    else {
        notifyCustomerCheckbox.prop('disabled', true);
    }
}

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

});
script;
?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
