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

/** @var $block \Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Websites */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>
<fieldset id="grop_fields" class="fieldset">
    <legend class="legend"><span><?= $block->escapeHtml(__('Product In Websites')) ?></span></legend>
    <br>
    <?php if ($block->getProductId()):?>
    <div class="messages">
        <div class="message message-notice">
            <?= $block->escapeHtml(__('To hide an item in catalog or search results, set the status to "Disabled".')) ?>
        </div>
    </div>
    <?php endif; ?>
    <div class="store-scope">
        <?= $block->getHintHtml() ?>
        <div class="store-tree">
            <?php $_websites = $block->getWebsiteCollection() ?>
            <?php foreach ($_websites as $_website):?>
            <div class="website-name">
                <input name="product[website_ids][]"
                       value="<?= (int) $_website->getId() ?>"
                    <?php if ($block->isReadonly()):?>
                        disabled="disabled"
                    <?php endif;?>
                       class="checkbox website-checkbox"
                       id="product_website_<?= (int) $_website->getId() ?>"
                       type="checkbox"
                    <?php if ($block->hasWebsite($_website->getId()) ||
                        !$block->getProductId() && count($_websites) === 1):?>
                        checked="checked"
                    <?php endif; ?>
                />
                <label for="product_website_<?= (int) $_website->getId() ?>">
                    <?= $block->escapeHtml($_website->getName()) ?>
                </label>
            </div>
            <dl class="webiste-groups" id="product_website_<?= (int) $_website->getId() ?>_data">
                <?php foreach ($block->getGroupCollection($_website) as $_group):?>
                <dt><?= $block->escapeHtml($_group->getName()) ?></dt>
                <dd>
                    <ul>
                        <?php foreach ($block->getStoreCollection($_group) as $_store):?>
                        <li>
                            <?= $block->escapeHtml($_store->getName()) ?>
                            <?php if ($block->getWebsites() && !$block->hasWebsite($_website->getId())):?>
                                <span class="website-<?= (int) $_website->getId() ?>-select">
                                <?= $block->escapeHtml(
                                    __('(Copy data from: %1)', $block->getChooseFromStoreHtml($_store)),
                                    ['select', 'option', 'optgroup']
                                ) ?>
                                </span>
                                <?= /* @noEscape */ $secureRenderer->renderStyleAsTag(
                                    'display:none',
                                    'span.website-' . (int)$_website->getId() . '-select'
                                ) ?>
                            <?php endif; ?>
                        </li>
                        <?php endforeach; ?>
                    </ul>
                </dd>
                <?php endforeach; ?>
            </dl>
            <?php endforeach; ?>
        </div>
    </div>
</fieldset>

<?php $scriptString = <<<script
require(["prototype"], function(){

    //<![CDATA[
    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 = $('product_website_' + element.value + '_data').getElementsBySelector('select');
        var selectBlocks = $('product_website_' + element.value + '_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();
            }
        }
    }
    window.toggleStoreFromChoosers = toggleStoreFromChoosers;
    //]]>

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