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

/**
 * @var $block \Magento\CurrencySymbol\Block\Adminhtml\System\Currency\Rate\Matrix
 * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
 */

$_oldRates = $block->getOldRates();
$_newRates = $block->getNewRates();
$_rates = ($_newRates) ? $_newRates : $_oldRates;
?>
<?php if (empty($_rates)): ?>
    <div class="message message-warning warning"><p>
            <?= $block->escapeHtml(
                __('You must first configure currency options before being able to see currency rates.')
            ) ?></p>
    </div>
<?php else: ?>
    <form name="rateForm" id="rate-form" method="post" action="<?= $block->escapeUrl($block->getRatesFormAction()) ?>">
        <?= $block->getBlockHtml('formkey') ?>
        <div class="admin__control-table-wrapper">
            <table class="admin__control-table">
                <thead>
                    <tr>
                        <th>&nbsp;</th>
                        <?php $_i = 0; foreach ($block->getAllowedCurrencies() as $_currencyCode): ?>
                            <th><span><?= $block->escapeHtml($_currencyCode) ?></span></th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <?php $_j = 0; foreach ($block->getDefaultCurrencies() as $_currencyCode): ?>
                <tr>
                    <?php if (isset($_rates[$_currencyCode]) && is_array($_rates[$_currencyCode])): ?>
                        <?php foreach ($_rates[$_currencyCode] as $_rate => $_value): ?>
                            <?php if (++$_j == 1): ?>
                                <td><span class="admin__control-support-text"><?= $block->escapeHtml($_currencyCode) ?>
                                    </span></td>
                                <td>
                                    <input type="text"
                                           name="rate[<?= $block->escapeHtmlAttr($_currencyCode)
                                            ?>][<?= $block->escapeHtmlAttr($_rate) ?>]"
                                           value="<?= ($_currencyCode == $_rate) ? '1.0000' :
                                               ($_value>0 ? $block->escapeHtmlAttr($_value) :
                                                   (isset($_oldRates[$_currencyCode][$_rate]) ?
                                                       $block->escapeHtmlAttr($_oldRates[$_currencyCode][$_rate]) : ''))
                                                    ?>"
                                           class="admin__control-text"
                                            <?= ($_currencyCode == $_rate) ? ' disabled' : '' ?> />
                                    <?php if (isset($_newRates) && $_currencyCode != $_rate &&
                                        isset($_oldRates[$_currencyCode][$_rate])): ?>
                                    <div class="admin__field-note"><?= $block->escapeHtml(__('Old rate:')) ?>
                                        <strong><?= $block->escapeHtml($_oldRates[$_currencyCode][$_rate]) ?></strong>
                                    </div>
                                    <?php endif; ?>
                                </td>
                            <?php else: ?>
                                <td>
                                    <input type="text"
                                           name="rate[<?= $block->escapeHtmlAttr($_currencyCode)
                                            ?>][<?= $block->escapeHtmlAttr($_rate) ?>]"
                                           value="<?= ($_currencyCode == $_rate) ? '1.0000' :
                                               ($_value>0 ? $block->escapeHtmlAttr($_value) :
                                                   (isset($_oldRates[$_currencyCode][$_rate]) ?
                                                       $block->escapeHtmlAttr($_oldRates[$_currencyCode][$_rate]) : ''))
                                                    ?>"
                                           class="admin__control-text"
                                           <?= ($_currencyCode == $_rate) ? ' disabled' : '' ?> />
                                    <?php if (isset($_newRates) && $_currencyCode != $_rate &&
                                        isset($_oldRates[$_currencyCode][$_rate])): ?>
                                    <div class="admin__field-note"><?= $block->escapeHtml(__('Old rate:')) ?>
                                        <strong><?= $block->escapeHtml($_oldRates[$_currencyCode][$_rate]) ?></strong>
                                    </div>
                                    <?php endif; ?>
                                </td>
                            <?php endif; ?>
                        <?php endforeach; ?>
                        <?php $_j = 0; ?>
                    <?php endif; ?>
                </tr>
                <?php endforeach; ?>
            </table>
        </div>
    </form>
<?php endif; ?>
<?php $scriptString = <<<script
require(['jquery', "mage/mage"], function(jQuery){

    jQuery('#rate-form').mage('form').mage('validation');

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