<?php
    use App\Helpers\globals;

    $uri      = current_url(true);
    $segment1 = $uri->setSilent()->getSegment(1);
    $segment2 = $uri->setSilent()->getSegment(2);
    $segment3 = $uri->setSilent()->getSegment(3);
?>
<aside class="page-sidebar">
    <div class="page-logo">
        <a href="<?= base_url() ?>" class="page-logo-link press-scale-down d-flex align-items-center position-relative" data-toggle="modal" data-target="#modal-shortcut">
            <img src="<?= view_cell('Users::logo') ?>" alt="<?= env('app.name') ?>" aria-roledescription="logo">
            <span class="page-logo-text mr-1"><strong><?= env('app.name') ?></strong></span>
        </a>
    </div>
    <!-- BEGIN PRIMARY NAVIGATION -->
    <nav id="js-primary-nav" class="primary-nav" role="navigation">
        <div class="nav-filter">
            <div class="position-relative">
                <input type="text" id="nav_filter_input" placeholder="Filter menu" class="form-control" tabindex="0">
                <a href="#" onclick="return false;" class="btn-primary btn-search-close js-waves-off" data-action="toggle" data-class="list-filter-active" data-target=".page-sidebar">
                    <i class="fal fa-chevron-up"></i>
                </a>
            </div>
        </div>
        <div class="info-card">
            <img src="<?= view_cell('Users::avatar') ?>" class="profile-image rounded-circle" alt="Dr. Codex Lantern">
            <div class="info-card-text">
                <a href="<?= base_url('account/profile') ?>" class="d-flex align-items-center text-white">
                    <span class="text-truncate text-truncate-sm d-inline-block">
                        <?= view_cell('Users::username') ?>
                    </span>
                </a>
                <span class="d-inline-block text-truncate text-truncate-md"><?= view_cell('Users::nipx') ?></span>
            </div>
            <img src="<?= view_cell('Users::bgprofile') ?>" class="cover" alt="cover">
            <a href="<?= base_url('account/profile') ?>" onclick="return false;" class="pull-trigger-btn" data-action="toggle" data-class="list-filter-active" data-target=".page-sidebar" data-focus="nav_filter_input">
                <i class="fal fa-angle-down"></i>
            </a>
        </div>
        <!-- 
		TIP: The menu items are not auto translated. You must have a residing lang file associated with the menu saved inside dist/media/data with reference to each 'data-i18n' attribute.
		-->
        <ul id="js-nav-menu" class="nav-menu">
            
            <li class="nav-title">Navigation</li>

            <li <?= !$segment2 ? 'class="active"' : '' ?>>
                <a href="<?= base_url() ?>" title="Home" data-filter-tags="home">
                    <i class="fal fa-home"></i>
                    <span class="nav-link-text">Home</span>
                </a>
            </li>

            <!-- MODUL/MENU DINAMIS -->
            <?php 
                $usr_modul = Globals::get_modul(); 

                foreach ($usr_modul as $k) {
                    $menu_dua = Globals::set_menu($k['fname']);                                
                    $menu_data = prepareList($menu_dua);


            ?>

            <?php if (!empty($menu_data)) { ?>

            <!-- START LOOP MODUL NAME -->
            <li <?= $segment1 === $k['fname'] ? 'class="active open"' : '' ?>>
                <a href="#" title="<?= $k['nama_modul'] ?>" data-filter-tags="<?= $k['nama_modul'] ?>">
                    <i class="<?= $k['ikon'] ?>"></i>
                    <span class="nav-link-text"><?= $k['nama_modul'] ?></span>
                </a>

                <ul>
                <!-- START LOOP MENU -->
                <?php
                    foreach ($menu_data as $m) { 
                        if (!empty($m['children'])) {
                            $ar_method = array_column($m['children'], 'method');
                            $active1 = in_array($segment2, $ar_method)?' class="active open"':''; ?>
                                                               
                        <li <?= $active1 ?>>
                            <a href="javascript:void(0);" title="<?= $m['nama_menu'] ?>" data-filter-tags="<?= $m['nama_menu'] ?>">
                                <span class="nav-link-text"><?= $m['nama_menu'] ?></span>
                            </a>
                            <ul>
                                <?php  foreach ($m['children'] as $c) {
                                    $active2 = $segment3==$c['method']?' class="active"':'';
                                ?>
                                <li <?= $active2 ?>>
                                    <a href="<?= base_url($k['fname'].'/'.$m['method'].'/'.$c['method']) ?>" title="<?= $c['nama_menu'] ?>" data-filter-tags="<?= $c['nama_menu'] ?>">
                                        <span class="nav-link-text"><?= $c['nama_menu'] ?></span>
                                    </a>
                                </li>
                                <?php } ?>
                            </ul>
                        </li>

                        <?php } else { ?>

                        <li <?= $segment2 === $m['method'] ? 'class="active"' : '' ?>>
                            <a href="<?= base_url($m['fname'].'/'.$m['method']) ?>" title="<?= $m['nama_menu'] ?>" data-filter-tags="<?= $m['nama_menu'] ?>">
                                <span class="nav-link-text"><?= $m['nama_menu'] ?></span>
                            </a>
                        </li>
                <?php }} ?>
                </ul>
            </li>
            <!-- END LOOP MODUL NAME -->
            
        <?php } else { ?>
            <li <?= $segment1 === $k['fname'] ? 'class="active"' : '' ?>>
                <a href="<?= base_url($k['fname']) ?>" title="<?= $k['nama_modul'] ?>" data-filter-tags="<?= $k['nama_modul'] ?>">
                    <i class="<?= $k['ikon'] ?>"></i>
                    <span class="nav-link-text"><?= $k['nama_modul'] ?></span>
                </a>
            </li>
        <?php }} ?>
        </ul>
        <div class="filter-message js-filter-message bg-success-600"></div>
    </nav>
    <!-- END PRIMARY NAVIGATION -->
</aside>