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

/** @var \Magento\Sales\Block\Adminhtml\Order\Invoice\Create\Items $block */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>
<section class="admin__page-section">
    <div class="admin__page-section-title">
        <?php $_itemsGridLabel = $block->getForcedShipmentCreate() ? 'Items to Invoice and Ship' : 'Items to Invoice';?>
        <span class="title"><?= $block->escapeHtml(__('%1', $_itemsGridLabel)) ?></span>
    </div>
    <div class="admin__page-section-content grid">
        <div class="admin__table-wrapper">
            <table class="data-table admin__table-primary order-invoice-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>
                        <th class="col-qty-invoice"><span><?= $block->escapeHtml(__('Qty to Invoice')) ?></span></th>
                        <th class="col-subtotal"><span><?= $block->escapeHtml(__('Subtotal')) ?></span></th>
                        <th class="col-tax"><span><?= $block->escapeHtml(__('Tax Amount')) ?></span></th>
                        <th class="col-discount"><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="3">&nbsp;</td>
                            <td><?= $block->getUpdateButtonHtml() ?></td>
                            <td colspan="4">&nbsp;</td>
                        </tr>
                    </tfoot>
                <?php endif; ?>
                <?php $_items = $block->getInvoice()->getAllItems() ?>
                <?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>
    </div>
</section>

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

<?php if (!empty($orderTotalBar)): ?>
<section class="admin__page-section">
    <?= /* @noEscape */ $orderTotalBar ?>
</section>
<?php endif; ?>

<section class="admin__page-section">
    <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(__('Invoice History')) ?></span>
            </div>
            <div id="history_form" class="admin__page-section-item-content order-history-form">
                <div class="admin__field">
                    <label for="invoice_comment_text" class="admin__field-label">
                        <span><?= $block->escapeHtml(__('Invoice Comments')) ?></span>
                    </label>
                    <div class="admin__field-control">
                        <textarea id="invoice_comment_text"
                                  name="invoice[comment_text]"
                                  class="admin__control-textarea"
                                  rows="3"
                                  cols="5"><?= $block->escapeHtml($block->getInvoice()->getCommentText())?></textarea>
                    </div>
                </div>
            </div>
        </div>

        <div id="invoice_totals" class="admin__page-section-item order-totals">
            <div class="admin__page-section-item-title">
                <span class="title"><?= $block->escapeHtml(__('Invoice Totals')) ?></span>
            </div>
            <div class="admin__page-section-item-content order-totals-actions">
                <?= $block->getChildHtml('invoice_totals') ?>
                <?php if ($block->isCaptureAllowed()): ?>
                    <?php if ($block->canCapture()): ?>
                        <div class="admin__field">
                          <label for="invoice_do_capture" class="admin__field-label">
                              <?= $block->escapeHtml(__('Amount')) ?>
                          </label>
                          <select class="admin__control-select" name="invoice[capture_case]">
                              <option value="online"><?= $block->escapeHtml(__('Capture Online')) ?></option>
                              <option value="offline"><?= $block->escapeHtml(__('Capture Offline')) ?></option>
                              <option value="not_capture"><?= $block->escapeHtml(__('Not Capture')) ?></option>
                          </select>
                        </div>
                    <?php elseif ($block->isGatewayUsed()):?>
                        <input type="hidden" name="invoice[capture_case]" value="offline"/>
                        <div>
                            <?= $block->escapeHtml(__(
                                'The invoice will be created offline without the payment gateway.'
                            )) ?>
                        </div>
                    <?php endif; ?>
                <?php endif; ?>
                <div class="admin__field admin__field-option field-append">
                    <input id="notify_customer" name="invoice[comment_customer_notify]" value="1" type="checkbox"
                           class="admin__control-checkbox" />
                    <label class="admin__field-label" for="notify_customer">
                        <?= $block->escapeHtml(__('Append Comments')) ?>
                    </label>
                </div>
                <?php if ($block->canSendInvoiceEmail()): ?>
                <div class="admin__field admin__field-option field-email">
                    <input id="send_email" name="invoice[send_email]" value="1" type="checkbox"
                           class="admin__control-checkbox" />
                    <label class="admin__field-label" for="send_email">
                        <?= $block->escapeHtml(__('Email Copy of Invoice')) ?>
                    </label>
                </div>
                <?php endif; ?>
                <?= $block->getChildHtml('submit_before') ?>
                <div class="actions">
                    <?= $block->getChildHtml('submit_button') ?>
                    <?= $block->getChildHtml('submit_after') ?>
                </div>
            </div>
        </div>
    </div>
</section>

<?php
$enableSubmitButton = (int) !$block->getDisableSubmitButton();
$scriptString = <<<script

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

//<![CDATA[
var submitButtons = jQuery('.submit-button');
var updateButtons = jQuery('.update-button');
var enableSubmitButtons = {$enableSubmitButton};
var fields = jQuery('.qty-input');

function enableButtons(buttons) {
    buttons.removeClass('disabled').prop('disabled', false);
}

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

disableButtons(updateButtons);

fields.on('keyup', 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 {
        if (enableSubmitButtons) {
            enableButtons(submitButtons);
        }
        disableButtons(updateButtons);
    }
}

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) ?>
