<?php
/**
 * Sidebar Menu Component
 *
 * Renders the navigation menu based on user permissions.
 */

$menu = fetchGroupMenus();
$groups = [];
if (_SLM::$session['user']['profile']['slm']['type'] === 'profile') {
    $groups = filterMenuWithPermissions($menu, _SLM::$session["user"]["profile"]["acl"]);
}
$view_active = empty($_GET['v']) ? ($_vista ?? 'home.main') : $_GET['v'];
$module_active = [];
?>
<ul class="nk-menu">
    <?php if (!empty($groups)): ?>
        <?php foreach ($groups as $group_index => $group): ?>

            <?php if (isset($group['title']) && !$group['display']): ?>
                <?php continue; ?>
            <?php endif; ?>

            <?php if ($group_index > 0 && isset($group['title']) && $group['display']): ?>
                <li class="nk-menu-heading">
                    <h6 class="overline-title text-primary-alt"><?= $group['title'] ?></h6>
                </li>
            <?php endif; ?>

            <?php foreach ($group['menu'] as $menu): ?>

                <?php if (isset($menu['module'])): ?>

                    <li class="nk-menu-item has-sub">
                        <a href="#" class="nk-menu-link nk-menu-toggle">
                            <span class="nk-menu-icon">
                                <em class="icon ni ni-<?= $menu['icon'] ?? 'menu-circled' ?>"></em>
                            </span>
                            <span class="nk-menu-text"><?= $menu['name'] ?></span>
                        </a>
                        <ul class="nk-menu-sub">
                            <?php foreach ($menu['module'] as $module_item): ?>
                                <?php if ($view_active === $module_item['folder']['folder']) {
                                    $module_active["name"] = $module_item['folder']["name"];
                                    $module_active["icon"] = $menu["icon"];
                                    $module_active["module"] = $module_item;
                                } ?>
                                <li class="nk-menu-item <?= !$module_item['display'][0] ? 'd-none' : '' ?> <?= $view_active == $module_item['folder']['folder'] ? 'active current-page' : '' ?>">
                                    <a href="./?v=<?= $module_item['folder']['folder'] ?>" class="nk-menu-link">
                                        <span class="nk-menu-text"><?= $module_item['folder']['name'] ?></span>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </li>

                <?php else: ?>
                    <li class="nk-menu-item <?= $group['display'] ? '' : 'd-none' ?> <?= $view_active == $menu['folder'] ? 'active current-page' : '' ?>">
                        <a href="./?v=<?= $menu['folder'] ?>" class="nk-menu-link">
                            <span class="nk-menu-icon">
                                <em class="icon ni ni-<?= $menu['icon'] ?? 'menu-circled' ?>"></em>
                            </span>
                            <span class="nk-menu-text"><?= $menu['name'] ?></span>
                        </a>
                    </li>
                <?php endif; ?>

            <?php endforeach; ?>

        <?php endforeach; ?>
    <?php endif; ?>
</ul>
