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

// @codingStandardsIgnoreFile

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

$stores = $block->getStoresSortedBySortOrder();
?>
<fieldset class="fieldset">
    <legend class="legend">
        <span><?php echo $block->escapeHtml(__('Manage Options (Values of Your Attribute)'))?></span>
    </legend>
    <div 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><?php echo $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><?php echo $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="<?php echo (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="<?php echo (int) $storetotal; ?>" class="col-actions-add">
                    <?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()):?>
                        <button id="add_new_option_button" data-action="add_new_row"
                                title="<?php echo $block->escapeHtml(__('Add Option')); ?>"
                                type="button" class="action- scalable add">
                             <span><?php echo $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>
            <td class="col-draggable">
                <?php if (!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()): ?>
                    <div data-role="draggable-handle" class="draggable-handle"
                         title="<?php echo $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 %>][<?php echo (int) $_store->getId() ?>]"
                    value="<%- data.store<?php echo (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="<?php echo $block->escapeHtml(__('Delete')) ?>"
                        type="button"
                        class="action- scalable delete delete-option">
                        <span><?php echo $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("htmlspecialchars_decode", $value) : $value;
    }
    ?>
    <script type="text/x-magento-init">
        {
            "*": {
                "Magento_Catalog/js/options": {
                    "attributesData": <?php /* @noEscape */ echo json_encode($values, JSON_HEX_QUOT); ?>,
                    "isSortable":  <?php echo (int)(!$block->getReadOnly() && !$block->canManageOptionDefaultOnly()) ?>,
                    "isReadOnly": <?php echo (int)$block->getReadOnly(); ?>
                },
                "Magento_Catalog/catalog/product/attribute/unique-validate": {
                    "element": "required-dropdown-attribute-unique",
                    "message": "<?php echo $block->escapeHtml(__('The value of Admin must be unique.')) ?>"
                }
            }
        }
    </script>
</fieldset>
