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

/**
 * Category left navigation
 *
 * @var $block \Magento\Catalog\Block\Navigation
 */
?>
<?php if (!$block->getCategory()) {
    //return;
} ?>
<?php
//MNM
    $_helper = $this->helper(Magento\Catalog\Helper\Category::class);
    $_categories = $_helper->getStoreCategories();

    function hasVisibleChildren($cat)
    {
        foreach($cat->getChildrenCategories() as $c)
        {
            //MNM - (bad/slow) workaround to retrieve getIncludeInMenu
            $c = $c->load($c->getId());

            if($c->getIncludeInMenu())
            {
                return true;
            }
        }
        return false;
    }
//MNM
?>
<?php $_count = is_array($_categories) ? count($_categories) : $_categories->count(); ?>
<?php if ($_count) :?>
    <div class="block filter">
        <div class="title">
            <strong><?= $block->escapeHtml(__('Shop By')) ?></strong>
        </div>
        <div class="content">
            <strong class="subtitle"><?= $block->escapeHtml(__('Shopping Options')) ?></strong>
            <dl class="options" id="narrow-by-list2">
                <dt><?= $block->escapeHtml(__('Categories')) ?></dt>
                <dd>
                    <ol class="items" id="category-sidemenu-accordion">
                        <?php /** @var \Magento\Catalog\Model\Category $_category */ ?>
                        <?php foreach ($_categories as $_category) :?>
                            <?php if ($_category->getIsActive()) :?>
                                <li class="item">
                                	<div>
                                        <a href="<?= $block->escapeUrl($block->getCategoryUrl($_category)) ?>"
                                            <?php if ($block->isCategoryActive($_category)) :?>
                                                class="current"
                                            <?php endif; ?>
                                        ><?= $block->escapeHtml($_category->getName()) ?></a>
                                        <?php if ($_category->hasChildren()) :?>
                                        	<div class="open-this<?php if (!$block->isCategoryActive($_category)) :?> collapsed<?php endif; ?>"
                                        		data-toggle="collapse"
                                        		data-parent="#category-sidemenu-accordion"
                                        		href="#sidemenu-category-<?= $_category->getId() ?>">
                                        	</div>
                                        <?php endif; ?>
                                    </div>
                                    <?php if ($_category->hasChildren()) :?>
                                    	<ul class="children level-1 collapse<?php if ($block->isCategoryActive($_category)) :?> in<?php endif; ?>"
                                    		id="sidemenu-category-<?= $_category->getId() ?>">
                                    		<?php foreach ($_category->getChildrenCategories() as $_childCategory) :?>
                                    			<li class="item">
                                    				<div>
                                        				<a href="<?= $block->escapeUrl($block->getCategoryUrl($_childCategory)) ?>"
                                        				    <?php if ($block->isCategoryActive($_childCategory)) :?>
                                                                class="current"
                                                            <?php endif; ?>
                                                        ><?= $block->escapeHtml($_childCategory->getName()) ?></a>
                                                    	<?php if ($_childCategory->hasChildren() && hasVisibleChildren($_childCategory)) :?>
                                                        	<div class="open-this<?php if (!$block->isCategoryActive($_childCategory)) :?> collapsed<?php endif; ?>"
                                                        		data-toggle="collapse"
                                                        		data-parent="#sidemenu-category-<?= $_category->getId() ?>"
                                                        		href="#sidemenu-category-<?= $_childCategory->getId() ?>">
                                                        	</div>
                                                        <?php endif; ?>
                                                    </div>
                                                    <?php if ($_childCategory->hasChildren()) :?>
                                                    	<ul class="children level-2 collapse<?php if ($block->isCategoryActive($_childCategory)) :?> in<?php endif; ?>"
                                                    		id="sidemenu-category-<?= $_childCategory->getId() ?>">
                                                    		<?php foreach ($_childCategory->getChildrenCategories() as $_childChildCategory) : ?>
                                                        		<?php if($_childChildCategory->load($_childChildCategory->getId())->getIncludeInMenu() == '1'): ?>
                                                        			<li class="item">
                                                        				<a href="<?= $block->escapeUrl($block->getCategoryUrl($_childChildCategory)) ?>"
                                                        				    <?php if ($block->isCategoryActive($_childChildCategory)) :?>
                                                                                class="current"
                                                                            <?php endif; ?>
                                                                        ><?= $block->escapeHtml($_childChildCategory->getName()) ?></a>
                                                        			</li>
                                                        		<?php endif; ?>
                                                    		<?php endforeach; ?>
                                                    	</ul>
                                                    <?php endif; ?>
                                    			</li>
                                    		<?php endforeach; ?>
                                    	</ul>
                                    <?php endif; ?>
                                </li>
                            <?php endif; ?>
                        <?php endforeach ?>
                    </ol>
                </dd>
            </dl>
        </div>
    </div>
<?php endif; ?>
