<?php
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Shop by Brand for Magento 2
 */
/**
 * @var \Amasty\ShopbyBrand\Block\BrandsPopup $block
 */
/** @var \Magento\Framework\Escaper $escaper */

$parentClass = $block->isShowPopup() ? 'parent' : '';
$ultimoClasses = $block->isUltimoTheme() ? ' nav-item level0 level-top nav-item--parent' : '';
$liClass = $parentClass . $ultimoClasses;
$brandItemPadding = 20;
$brandItemBorder = 2;
?>
<?php if ($block->isShouldWrap()): ?>
<li class="level0 <?= /** @noEscape */ $liClass ?> ambrands-menu-item"
    data-ambrands-js="brands-menu-link">
<?php endif;?>
    <?php if ($block->isShowPopup()): ?>
        <?php
        $items = $block->getIndex();
        $brandLetters = $block->getAllLetters();
        $isShowCount = $block->getData('show_count');
        $isShowLogos = $block->getData('show_images');
        $imgWidth = $block->getImageWidth();
        $imgHeight = $block->getImageHeight() ? $block->getImageHeight() . 'px' : 'inherit';
        $imgHeightWithOffset = $block->getImageHeight() ? (($block->getImageHeight() + $brandItemPadding) . 'px') : 'inherit';
        $isShowFilter = $block->getData('show_filter');
        $filterDisplayAll = $block->getData('filter_display_all');
        ?>
        <a href="<?= $escaper->escapeUrl($block->getAllBrandsUrl()) ?>" class="level-top ambrands-link">
            <?php if (!$block->isPortoTheme() && !$block->isUltimoTheme()): ?>
                <span class="ui-menu-icon ui-icon ui-icon-carat-1-e ambrands-arrow"></span>
            <?php endif ?>
            <span class="ambrands-label"><?= $escaper->escapeHtml($block->getLabel()) ?></span>
            <?php if ($block->isUltimoTheme()): ?>
                <span class="caret"></span>
            <?php endif; ?>
        </a>
        <div class="ambrands-list-popup" data-ambrands-js="brands-popup">
            <?php if ($isShowLogos): ?>
                <style>
                    .ambrands-list-popup .ambrands-letter .ambrands-content .ambrands-image {
                        max-width: <?= $escaper->escapeHtml($imgWidth) ?>px;
                        max-height: <?= $escaper->escapeHtml($imgHeight) ?>;
                    }

                    .ambrands-list-popup .ambrands-brand-item .ambrands-image-block {
                        height: <?= $escaper->escapeHtml($imgHeightWithOffset) ?>;
                    }

                    .ambrands-list-popup .ambrands-brand-item {
                        width: <?= $escaper->escapeHtml($imgWidth + $brandItemPadding + $brandItemBorder) ?>px;
                        min-height: <?= $escaper->escapeHtml($imgWidth + 30) ?>px;
                    }

                    .ambrands-list-popup .ambrands-brand-item .ambrands-empty {
                        width: <?= $escaper->escapeHtml($imgWidth) ?>px;
                        line-height: <?= $escaper->escapeHtml($imgWidth) ?>px;
                        font-size: <?= $escaper->escapeHtml($imgWidth * 0.65) ?>px;
                    }
                </style>
            <?php endif; ?>

            <div class="ambrands-list-container">
            <?php if ($items): ?>
                <?php if ($isShowFilter): ?>
                    <div class="ambrands-letters-filter">
                        <a href="<?= $escaper->escapeUrl($block->getAllBrandsUrl()); ?>"
                           class="ambrands-letter -letter-all -active"
                           title="<?= $escaper->escapeHtmlAttr(__('All Brands')) ?>">
                            <?= $escaper->escapeHtml(__('All Brands')) ?>
                        </a>
                        <?php if ($filterDisplayAll): ?>
                            <?php foreach (array_merge(range('A', 'Z'), ['#']) as $letter): ?>
                                <?php $disabled = !(in_array($letter, $brandLetters)) ?>
                                <button data-ambrands-js="popup-filter-letter"
                                        class="ambrands-letter <?= ($disabled)
                                            ? '-disabled'
                                            : 'letter-' . $escaper->escapeHtmlAttr($letter) ?>"
                                        <?php if ($disabled): ?>
                                            disabled
                                        <?php endif; ?>
                                >
                                    <?= $escaper->escapeHtml($letter) ?>
                                </button>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <?php foreach ($brandLetters as $letter): ?>
                                <button data-ambrands-js="popup-filter-letter"
                                        class="ambrands-letter letter-<?= $escaper->escapeHtmlAttr($letter) ?>">
                                    <?= $escaper->escapeHtml($letter) ?>
                                </button>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                    <script type="text/x-magento-init">
                         {
                             "*": {
                                 "amBrandsFilterInit": {
                                    "element": "[data-ambrands-js='popup-filter-letter']",
                                    "target": "[data-ambrands-js='popup-brand-letter']"
                                }
                             }
                         }
                    </script>
                <?php endif; ?>
                <div class="ambrands-popup-items<?= $isShowFilter ? '' : ' am-brands-fullwidth' ?>">
                    <?php foreach ($items as $letters): ?>
                    <section class="ambrands-letters-list">
                        <?php foreach ($letters as $letter => $options): ?>
                            <div class="ambrands-letter letter-<?= $escaper->escapeHtml($letter) ?>"
                                data-ambrands-js="popup-brand-letter">
                                <h3 class="ambrands-title"><?= $escaper->escapeHtml($letter) ?></h3>
                                <div class="ambrands-content">
                                    <?php foreach ($options as $option): ?>
                                        <div class="ambrands-brand-item <?= /* @noEscape */ !$isShowLogos ? '-no-logo' : '' ?>">
                                            <a href="<?= $escaper->escapeUrl($option['url']) ?>"
                                               class="ambrands-inner"
                                               title="<?= $escaper->escapeHtmlAttr($option['label']) ?>">
                                                <?php if ($isShowLogos): ?>
                                                    <?php if ($option['img']): ?>
                                                        <span class="ambrands-image-block">
                                                        <img class="ambrands-image"
                                                             src="<?= $escaper->escapeUrl($option['img']) ?>"
                                                             title="<?= $escaper->escapeHtmlAttr($option['alt']) ?>"
                                                             alt="<?= $escaper->escapeHtmlAttr($option['alt']) ?>"
                                                             loading="lazy"
                                                        />
                                                    </span>
                                                    <?php else: ?>
                                                        <span class="ambrands-image-block">
                                                        <span class="ambrands-empty">
                                                            <?= $escaper->escapeHtml($letter) ?>
                                                        </span>
                                                    </span>
                                                    <?php endif; ?>
                                                <?php endif; ?>
                                                <span class="ambrands-label">
                                                    <?= $escaper->escapeHtml($option['label']) ?>
                                                    <?php if ($isShowCount): ?>
                                                        <span class="ambrands-count"><?= $escaper->escapeHtml($option['cnt']) ?></span>
                                                    <?php endif; ?>
                                                </span>
                                            </a>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </section>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <?= $escaper->escapeHtml(__('Please select brand attribute in Stores -> Configuration ->
                Amasty Extensions -> Improved Layered Navigation: Brands.')) ?>
            <?php endif ?>
            </div>
        </div>
    <?php else: ?>
        <a href="<?= $escaper->escapeUrl($block->getAllBrandsUrl()) ?>"
           class="level-top ambrands-link"
           title="<?= $escaper->escapeHtmlAttr($block->getLabel()) ?>">
            <?= $escaper->escapeHtml($block->getLabel()) ?>
        </a>
    <?php endif; ?>
<?php if ($block->isShouldWrap()): ?>
</li>
<?php endif; ?>
