<?php
//@codingStandardsIgnoreFile
/** @var \Mirasvit\Rma\Block\Adminhtml\Rma\Edit\Form\Items $block */
$rma = $block->getRma();
?>

<script type="text/javascript">
    require([
        "jquery"
    ], function ($) {
        'use strict';

        function isNumeric(obj) {
            return !jQuery.isArray(obj) && (obj - parseFloat(obj) + 1) >= 0;
        }

        function checkQtyOrdered(currentQty) {
            var ordered = parseInt($(currentQty.name.substring(0, 8) + '[qtyAvailable]').innerHTML.trim());
            var defaultQty = 0;
            if ($('rma_id').value != '') {
                ordered += parseInt($(currentQty.name.substring(0, 8) + '[rma_item_qty]').value);
                defaultQty = ordered;
            }
            if (isNumeric(currentQty.value) && isNumeric(ordered)) {
                if (currentQty.value > ordered) {
                    currentQty.value = defaultQty;
                    alert("<?php echo __('You can not return more, than available. Please, try again.') ?>");
                }
            }
        }

        $('[data-role=show-all-items]').on('click', function (e) {
            e.preventDefault();
            $(e.currentTarget).hide();
            $('[data-role=items-table] tr').show();
        })
    });
</script>

<input name="is_offline" value="1" type="hidden">
<input name="store_id" value="<?= $block->getRmaStoreId() ?>" type="hidden">
<fieldset class="fieldset admin__fieldset rma-edit__items">
    <legend class="admin__legend legend">
        <span><?= __('Items') ?></span>
    </legend>
    <table class="admin__control-table" data-role="items-table">
        <thead>
        <tr>
            <th><?= __('Product Name') ?></th>
            <th><?= __('Qty to Return') ?></th>
            <th><?= __('Reason to Return') ?></th>
            <th><?= __('Item Condition') ?></th>
            <th><?= __('Resolution') ?></th>
        </tr>
        </thead>
        <tbody>

        <?php $i = 0 ?>
        <?php foreach ($block->getRmaItems() as $item): ?>
            <?php $i++; ?>
            <tr>
                <td>
                    <?= $block->escapeHtml($item->getName()) ?>
                </td>
                <td>
                    <input name="items[<?= $i ?>][is_offline]" value="1" type="hidden">
                    <input name="items[<?= $i ?>][item_id]" value="<?= $item->getId() ?>" type="hidden">
                    <input name="items[<?= $i ?>][order_item_id]"
                           value="<?= $item->getOrderItemId() ?>" type="hidden">
                    <input id="items[<?= $i ?>][rma_item_qty]"
                           value="<?= $item->getQtyRequested() ? $item->getQtyRequested() : 0 ?>"
                           type="hidden">

                    <?php $qtyRequested = $item->getQtyRequested() ? $item->getQtyRequested() : 0; ?>
                    <div class="">
                        <input name='items[<?= $i ?>][qty_requested]'
                               type="number"
                               maxlength="3"
                               min="0"
                               max="<?= $qtyRequested ?>"
                               class="admin__control-text required-entry"
                               value='<?= $qtyRequested ?>'
                               onKeyUp="checkQtyOrdered(this)">
                    </div>
                </td>
                <td>
                    <select name="items[<?= $i ?>][reason_id]" class="admin__control-select">
                        <option value=""><?= __('--') ?></option>
                        <?php foreach ($block->getReasonList() as $reason): ?>
                            <option value="<?= $reason->getId() ?>"
                                <?= $reason->getId() == $item->getReasonId() ? 'selected' : '' ?>>
                                <?= $reason->getName() ?>
                            </option>
                        <?php endforeach ?>
                    </select>
                </td>
                <td>
                    <select name="items[<?= $i ?>][condition_id]" class="admin__control-select">
                        <option value=""><?= __('--') ?></option>
                        <?php foreach ($block->getConditionList() as $condition): ?>
                            <option value="<?= $condition->getId() ?>"
                                <?= $condition->getId() == $item->getConditionId() ? 'selected' : '' ?>>
                                <?= $condition->getName() ?>
                            </option>
                        <?php endforeach ?>
                    </select>
                </td>
                <td>
                    <select name="items[<?= $i ?>][resolution_id]" class="admin__control-select">
                        <option value=""><?= __('--') ?></option>
                        <?php foreach ($block->getResolutionList() as $resolution): ?>
                            <option value="<?= $resolution->getId() ?>"
                                <?= $resolution->getId() == $item->getResolutionId() ? 'selected' : '' ?>>
                                <?= $resolution->getName() ?>
                            </option>
                        <?php endforeach ?>
                    </select>
                </td>
            </tr>
        <?php endforeach ?>
        </tbody>
    </table>
</fieldset>