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

/** @var $block \Magento\Eav\Block\Adminhtml\Attribute\Edit\Options\Options */

$stores = $block->getStoresSortedBySortOrder();
?>
<fieldset class="admin__fieldset fieldset">
    <legend class="legend">
        <span><?= $block->escapeHtml(__('Manage Options (Values of Your Attribute)')) ?></span>
    </legend><br />
    <div class="admin__control-table-wrapper" id="manage-options-panel" data-index="attribute_options_select_container">
        <table class="admin__control-table" data-index="attribute_options_select">
            <thead>
                <tr id="attribute-options-table">
                    <th class="col-draggable"></th>
                    <th class="col-default control-table-actions-th">
                        <span><?= $block->escapeHtml(__('Is Default')) ?></span>
                    </th>
                    <?php
                    foreach ($stores as $_store) :?>
                        <th<?php if ($_store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID) :?> class="_required"<?php endif; ?>>
                            <span><?= $block->escapeHtml(__($_store->getName())) ?></span>
                        </th>
                    <?php endforeach;
                    $storetotal = count($stores) + 3;
                    ?>
                    <th class="col-delete">&nbsp;</th>
                </tr>
            </thead>
            <tbody data-role="options-container" class="ignore-validate"></tbody>
            <tfoot>
            <tr>
                <th colspan="<?= (int)$storetotal ?>" class="validation">
                    <input type="hidden" class="required-dropdown-attribute-entry" name="dropdown_attribute_validation"/>
                    <input type="hidden" class="required-dropdown-attribute-unique" name="dropdown_attribute_validation_unique"/>
                </th>
            </tr>
            <tr>
                <th colspan="<?= (int) $storetotal ?>" class="col-actions-add">
                    <?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()) :?>
                        <button id="add_new_option_button" data-action="add_new_row"
                                title="<?= $block->escapeHtml(__('Add Option')) ?>"
                                type="button" class="action- scalable add">
                            <span><?= $block->escapeHtml(__('Add Option')) ?></span>
                        </button>
                    <?php endif; ?>
                </th>
            </tr>
            </tfoot>
        </table>
        <input type="hidden" id="option-count-check" value="" />
    </div>
    <script id="row-template" type="text/x-magento-template">
        <tr <% if (data.rowClasses) { %>class="<%- data.rowClasses %>"<% } %>>
            <td class="col-draggable">
                <?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()) :?>
                    <div data-role="draggable-handle" class="draggable-handle"
                         title="<?= $block->escapeHtml(__('Sort Option')) ?>">
                    </div>
                <?php endif; ?>
                <input data-role="order" type="hidden" name="option[order][<%- data.id %>]"  value="<%- data.sort_order %>" <?php if ($block->getReadOnly() || $block->canManageOptionDefaultOnly()) :?> disabled="disabled"<?php endif; ?>/>
            </td>
            <td class="col-default control-table-actions-cell">
                <input class="input-radio" type="<%- data.intype %>" name="default[]" value="<%- data.id %>" <%- data.checked %><?php if ($block->getReadOnly()) :?>disabled="disabled"<?php endif;?>/>
            </td>
            <?php foreach ($stores as $_store) :?>
            <td class="col-<%- data.id %>"><input name="option[value][<%- data.id %>][<?= (int) $_store->getId() ?>]" value="<%- data.store<?= /* @noEscape */ (int) $_store->getId() ?> %>" class="input-text<?php if ($_store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID) :?> required-option required-unique<?php endif; ?>" type="text" <?php if ($block->getReadOnly() || $block->canManageOptionDefaultOnly()) :?> disabled="disabled"<?php endif;?>/></td>
            <?php endforeach; ?>
            <td id="delete_button_container_<%- data.id %>" class="col-delete">
                <input type="hidden" class="delete-flag" name="option[delete][<%- data.id %>]" value="" />
                <?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()) :?>
                    <button id="delete_button_<%- data.id %>" title="<?= $block->escapeHtml(__('Delete')) ?>" type="button"
                        class="action- scalable delete delete-option"
                        >
                        <span><?= $block->escapeHtml(__('Delete')) ?></span>
                    </button>
                <?php endif;?>
            </td>
        </tr>
    </script>
    <?php
    $values = [];
    foreach ($block->getOptionValues() as $value) {
        $value = $value->getData();
        $values[] = is_array($value) ? array_map(function ($str) {
            return htmlspecialchars_decode($str, ENT_QUOTES);
        }, $value) : $value;
    }
    ?>
    <script type="text/x-magento-init">
        {
            "*": {
                "Magento_Catalog/js/options": {
                    "attributesData": <?= /* @noEscape */ json_encode($values, JSON_HEX_QUOT) ?>,
                    "isSortable":  <?= (int)(!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()) ?>,
                    "isReadOnly": <?= (int)$block->getReadOnly() ?>
                },
                "Magento_Catalog/catalog/product/attribute/unique-validate": {
                    "element": "required-dropdown-attribute-unique",
                    "message": "<?= $block->escapeHtml(__("The value of Admin must be unique.")) ?>"
                }
            }
        }
    </script>
</fieldset>
