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

/** @var $block Magento\Catalog\Block\Adminhtml\Product\Edit\Action\Attribute\Tab\Websites */
?>

<div class="fieldset-wrapper" id="add-products-to-website-wrapper">
    <fieldset class="fieldset" id="grop_fields">
        <legend class="legend">
            <span><?= $block->escapeHtml(__('Add Product To Websites')) ?></span>
        </legend>
        <br>
        <div class="store-scope">
            <div class="store-tree" id="add-products-to-website-content">
                <?php foreach ($block->getWebsiteCollection() as $_website) :?>
                    <div class="website-name">
                        <input name="add_website_ids[]"
                               value="<?= $block->escapeHtmlAttr($_website->getId()) ?>"
                            <?php if ($block->getWebsitesReadonly()) :?>
                                disabled="disabled"
                            <?php endif;?>
                               class="checkbox website-checkbox"
                               id="add_product_website_<?= $block->escapeHtmlAttr($_website->getId()) ?>"
                               type="checkbox" />
                        <label for="add_product_website_<?= $block->escapeHtmlAttr($_website->getId()) ?>"><?= $block->escapeHtml($_website->getName()) ?></label>
                    </div>
                    <dl class="webiste-groups" id="add_product_website_<?= $block->escapeHtmlAttr($_website->getId()) ?>_data">
                        <?php foreach ($block->getGroupCollection($_website) as $_group) :?>
                            <dt><?= $block->escapeHtml($_group->getName()) ?></dt>
                            <dd class="group-stores">
                                <ul>
                                <?php foreach ($block->getStoreCollection($_group) as $_store) :?>
                                    <li>
                                        <?= $block->escapeHtml($_store->getName()) ?>
                                    </li>
                                <?php endforeach; ?>
                                </ul>
                            </dd>
                        <?php endforeach; ?>
                    </dl>
                <?php endforeach; ?>
            </div>
        </div>
    </fieldset>
</div>

<div class="fieldset-wrapper" id="remove-products-to-website-wrapper">
    <fieldset class="fieldset" id="grop_fields">
        <legend class="legend">
            <span><?= $block->escapeHtml(__('Remove Product From Websites')) ?></span>
        </legend>
        <br>
        <div class="messages">
            <div class="message message-notice">
                <div><?= $block->escapeHtml(__('To hide an item in catalog or search results, set the status to "Disabled".')) ?></div>
            </div>
        </div>
        <div class="store-scope">
            <div class="store-tree" id="remove-products-to-website-content">
                <?php foreach ($block->getWebsiteCollection() as $_website) :?>
                    <div class="website-name">
                        <input name="remove_website_ids[]"
                               value="<?= $block->escapeHtmlAttr($_website->getId()) ?>"
                            <?php if ($block->getWebsitesReadonly()) :?>
                                disabled="disabled"
                            <?php endif;?>
                               class="checkbox website-checkbox"
                               id="remove_product_website_<?= $block->escapeHtmlAttr($_website->getId()) ?>"
                               type="checkbox" />
                        <label for="remove_product_website_<?= $block->escapeHtmlAttr($_website->getId()) ?>"><?= $block->escapeHtml($_website->getName()) ?></label>
                    </div>
                    <dl class="webiste-groups"
                        id="remove_product_website_<?= $block->escapeHtmlAttr($_website->getId()) ?>_data">
                        <?php foreach ($block->getGroupCollection($_website) as $_group) :?>
                            <dt><?= $block->escapeHtml($_group->getName()) ?></dt>
                            <dd class="group-stores">
                                <ul>
                                <?php foreach ($block->getStoreCollection($_group) as $_store) :?>
                                    <li>
                                        <?= $block->escapeHtml($_store->getName()) ?>
                                    </li>
                                <?php endforeach; ?>
                                </ul>
                            </dd>
                        <?php endforeach; ?>
                    </dl>
                <?php endforeach; ?>
            </div>
        </div>
    </fieldset>
</div>

<script>
require([
    'prototype'
], function () {
    var productWebsiteCheckboxes = $$('.website-checkbox');

    for(var i=0;i<productWebsiteCheckboxes.length;i++){
        Event.observe(productWebsiteCheckboxes[i], 'click', toggleStoreFromChoosers);
    }

    function toggleStoreFromChoosers(event){
        var element = Event.element(event);
        var selects = $(element.id+'_data').getElementsBySelector('select');
        var selectBlocks = $(element.id+'_data').getElementsByClassName('website-'+element.value+'-select');
        for (var i=0; i<selects.length; i++) {
            selects[i].disabled=!element.checked;
        }
        for (var i=0; i<selectBlocks.length; i++) {
            if (element.checked) {
                selectBlocks[i].show();
            }
            else {
                selectBlocks[i].hide();
            }
        }
    }
});
</script>
