<?php
/**
 * Copyright © Magefan (support@magefan.com). All rights reserved.
 * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
 *
 * Glory to Ukraine! Glory to the heroes!
 */
?>
<?php
/**
 * Blog sidebar categories template
 *
 * @var $block \Magefan\Blog\Block\Sidebar\Categories
 */
?>

<?php $items = $block->getGroupedChilds(); ?>

<?php if (count($items)) { ?>
    <div class="widget block block-categories" data-bind="scope: 'categories'">
        <div class="block-title">
            <strong><?= $block->escapeHtml(__('Categories')) ?></strong>
        </div>

        <ul class="accordion"  id="accordion-2">
        <?php
            $level = 0;
            $first = true;
            foreach ($items as $item) {
                $newLevel = $item->getLevel();
                if ($level == $newLevel && !$first) {
                    echo '</li>';
                }

                if ($level < $newLevel) {
                    echo '<ul>';
                }

                if ($level > $newLevel) {
                    for ($i = $level; $i > $newLevel; $i--) {
                        echo '</ul></li>';
                    }
                }
                ?>
                <li>
                    <a title="<?= $block->escapeHtml($item->getTitle()) ?>" href="<?= $block->escapeUrl($item->getCategoryUrl()) ?>">
                        <?= $block->escapeHtml($item->getTitle()) ?>
                    </a>
                    <?php if ($block->showPostsCount()) { ?>
                        (<?= $block->escapeHtml($item->getPostsCount()) ?>)
                    <?php } ?>
                    <?php
                    $level = $newLevel;
                    $first = false;
                }
            ?>
        </ul>
    </div>
<?php } ?>
