<?php
// @codingStandardsIgnoreFile

/* @var $block \Magento\Backend\Block\Store\Switcher */
?>
<?php if ($websites = $block->getWebsites()): ?>

    <div class="store-switcher store-view">
        <span class="store-switcher-label"><?php echo $block->escapeHtml(__('Websites View:')) ?></span>
        <div class="actions dropdown closable">
            <input type="hidden" name="store_switcher" id="store_switcher"
                   data-role="store-view-id" data-param="<?php echo $block->escapeHtml($block->getStoreVarName()) ?>"
                   value="<?php echo $block->escapeHtml($block->getStoreId()) ?>"
                   onchange="switchScope(this);"<?php echo $block->escapeHtml($block->getUiId()) ?> />
            <input type="hidden" name="store_group_switcher" id="store_group_switcher"
                   data-role="store-group-id" data-param="<?php echo $block->escapeHtml($block->getStoreGroupVarName()) ?>"
                   value="<?php echo $block->escapeHtml($block->getStoreGroupId()) ?>"
                   onchange="switchScope(this);"<?php echo $block->escapeHtml($block->getUiId()) ?> />
            <input type="hidden" name="website_switcher" id="website_switcher"
                   data-role="website-id" data-param="<?php echo $block->escapeHtml($block->getWebsiteVarName()) ?>"
                   value="<?php echo $block->escapeHtml($block->getWebsiteId()) ?>"
                   onchange="switchScope(this);"<?php echo $block->escapeHtml($block->getUiId()) ?> />
            <button
                type="button"
                class="admin__action-dropdown"
                data-mage-init='{"dropdown":{}}'
                data-toggle="dropdown"
                aria-haspopup="true"
                id="store-change-button">
                <?php echo $block->escapeHtml($block->getCurrentSelectionName()) ?>
            </button>
            <ul class="dropdown-menu" data-role="stores-list">
                <?php if ($block->hasDefaultOption()): ?>
                    <li class="store-switcher-all <?php if ( ! ($block->getDefaultSelectionName() != $block->getCurrentSelectionName())) {
                        echo "disabled";
                    } ?> <?php if ( ! $block->hasScopeSelected()) {
                        ?> current<?php
                    } ?>">
                        <?php if ($block->getDefaultSelectionName() != $block->getCurrentSelectionName()) {
                            ?>
                            <a data-role="store-view-id" data-value="" href="#">
                                <?php echo $block->escapeHtml($block->getDefaultSelectionName()) ?>
                            </a>
                            <?php
                        } else {
                            ?>
                            <span><?php echo $block->escapeHtml($block->getDefaultSelectionName()) ?></span>
                            <?php
                        } ?>
                    </li>
                <?php endif; ?>
                <?php foreach ($websites as $website): ?>
                    <?php $showWebsite = false; ?>
                    <?php foreach ($website->getGroups() as $group): ?>
                        <?php $showGroup = false; ?>
                        <?php foreach ($block->getStores($group) as $store): ?>
                            <?php if ($showWebsite == false): ?>
                                <?php $showWebsite = true; ?>
                                <li class="store-switcher-website <?php if ( ! ($block->isWebsiteSwitchEnabled() && ! $block->isWebsiteSelected($website))) {
                                    echo "disabled";
                                } ?> <?php if ($block->isWebsiteSelected($website)) {
                                    ?> current<?php
                                } ?>">
                                    <?php if ($block->isWebsiteSwitchEnabled() && ! $block->isWebsiteSelected($website)) {
                                        ?>
                                        <a data-role="website-id" data-value="<?php echo $block->escapeHtml($website->getId()) ?>" href="#">
                                            <?php echo $block->escapeHtml($website->getName()) ?>
                                        </a>
                                        <?php
                                    } else {
                                        ?>
                                        <span><?php echo $block->escapeHtml($website->getName()) ?></span>
                                        <?php
                                    } ?>
                                </li>
                            <?php endif; ?>
                            <?php if ($showGroup == false): ?>
                                <?php $showGroup = true; ?>
                                <li class="store-switcher-store <?php if ( ! ($block->isStoreGroupSwitchEnabled() && ! $block->isStoreGroupSelected($group))) {
                                    echo "disabled";
                                } ?> <?php if ($block->isStoreGroupSelected($group)) {
                                    ?> current<?php
                                } ?>">
                                    <?php if ($block->isStoreGroupSwitchEnabled() && ! $block->isStoreGroupSelected($group)) {
                                        ?>
                                        <a data-role="store-group-id" data-value="<?php echo $block->escapeHtml($group->getId()) ?>" href="#">
                                            <?php echo $block->escapeHtml($group->getName()) ?>
                                        </a>
                                        <?php
                                    } else {
                                        ?>
                                        <span><?php echo $block->escapeHtml($group->getName()) ?></span>
                                        <?php
                                    } ?>
                                </li>
                            <?php endif; ?>
                            <li class="store-switcher-store-view <?php if ( ! ($block->isStoreSwitchEnabled() && ! $block->isStoreSelected($store))) {
                                echo "disabled";
                            } ?> <?php if ($block->isStoreSelected($store)) {
                                ?> current<?php
                            } ?>">
                                <?php if ($block->isStoreSwitchEnabled() && ! $block->isStoreSelected($store)) {
                                    ?>
                                    <a data-role="store-view-id" data-value="<?php echo $block->escapeHtml($store->getId()) ?>" href="#">
                                        <?php echo $block->escapeHtml($store->getName()) ?>
                                    </a>
                                    <?php
                                } else {
                                    ?>
                                    <span><?php echo $block->escapeHtml($store->getName()) ?></span>
                                    <?php
                                } ?>
                            </li>
                        <?php endforeach; ?>
                    <?php endforeach; ?>
                <?php endforeach; ?>
                <?php if ($block->getShowManageStoresLink() && $block->getAuthorization()->isAllowed('Magento_Backend::store')): ?>
                    <li class="dropdown-toolbar">
                        <a href="<?php echo $block->escapeUrl($block->getUrl('*/system_store')) ?>"><?php echo $block->escapeHtml(__('Stores Configuration')) ?></a>
                    </li>
                <?php endif; ?>
            </ul>
        </div>
        <?php //echo $block->getHintHtml() ?>
    </div>

    <script type="text/x-magento-init">
        {
            "*": {
                "Dotdigitalgroup_Email/js/switcher":{
                    "getUseConfirm":"<?php echo (int)$block->getUseConfirm(); ?>",
                    "getContent":"<?php echo $block->escapeHtml(__('Please confirm scope switching. All data that hasn\'t been saved will be lost.')) ?>",
                    "objVal":"<?php echo $block->escapeHtml($block->getStoreId()) ?>",
                    "isUsingIframe":"<?php echo $block->escapeHtml($block->isUsingIframe()); ?>",
                    "getSwitchUrl":"<?php echo $block->escapeUrl($block->getSwitchUrl()) ?>"
                }
            }
        }
    </script>
<?php endif; ?>
