<?php
$current_mod = $_GET['mod'] ?? $GLOBALS['request_data']['mod'] ?? 'dashboard';
$current_page = $_GET['page'] ?? $GLOBALS['request_data']['page'] ?? 'list';
$current_route = "mod={$current_mod}&page={$current_page}";

// Security setup for Sidebar
$role_list = implode(',', $_SESSION['all_role_ids'] ?? [0]);
$is_super_admin = in_array(1, $_SESSION['all_role_ids'] ?? []);
?>

<!-- Main Sidebar Container -->
<aside class="main-sidebar sidebar-dark-primary elevation-4">
    <a href="index.php" class="brand-link">
        <span class="brand-text font-weight-light ml-3"><b>WIMAS</b> Portal</span>
    </a>

    <div class="sidebar">
        <nav class="mt-2">
            <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
                
                <li class="nav-item">
                    <a href="index.php?s=<?php echo encrypt_url('mod=dashboard&page=list'); ?>" class="nav-link <?php echo ($current_mod == 'dashboard') ? 'active' : ''; ?>">
                        <i class="nav-icon fas fa-tachometer-alt"></i>
                        <p>Dashboard</p>
                    </a>
                </li>

                <?php
                $main_menu_sql = "SELECT * FROM TBL_MAIN_MENU WHERE STATUS = 'ACTIVE' ORDER BY SORT_ORDER ASC";
                $main_menu_result = $db->query($main_menu_sql);

                while ($main = mysqli_fetch_assoc($main_menu_result)) {
                    $main_id = $main['MAIN_MENU_ID'];
                    
                    // NEW: Security Filter for Sidebar Links
                    if ($is_super_admin) {
                        // God Mode: See everything
                        $child_menu_sql = "SELECT * FROM TBL_CHILD_MENU WHERE MAIN_MENU_ID = $main_id AND STATUS = 'ACTIVE' ORDER BY SORT_ORDER ASC";
                    } else {
                        // Normal Users: Only see what they have CAN_VIEW permission for
                        $child_menu_sql = "SELECT c.* FROM TBL_CHILD_MENU c
                                           JOIN TBL_ROLE_PERMISSIONS p ON c.CHILD_MENU_ID = p.MENU_ID
                                           WHERE c.MAIN_MENU_ID = $main_id 
                                           AND c.STATUS = 'ACTIVE' 
                                           AND p.ROLE_ID IN ($role_list)
                                           AND p.CAN_VIEW = 1
                                           GROUP BY c.CHILD_MENU_ID
                                           ORDER BY c.SORT_ORDER ASC";
                    }
                    
                    $child_menu_result = $db->query($child_menu_sql);
                    
                    if (mysqli_num_rows($child_menu_result) > 0) {
                        
                        $children_html = '';
                        $is_parent_active = false;

                        while ($child = mysqli_fetch_assoc($child_menu_result)) {
                            $encrypted_link = "index.php?s=" . encrypt_url($child['MENU_URL']);
                            $is_child_active = ($child['MENU_URL'] === $current_route);
                            if ($is_child_active) $is_parent_active = true; 
                            
                            $active_class = $is_child_active ? 'active' : '';
                            
                            $children_html .= '<li class="nav-item">';
                            $children_html .= '<a href="' . $encrypted_link . '" class="nav-link ' . $active_class . '">';
                            $children_html .= '<i class="' . $child['MENU_ICON'] . ' nav-icon"></i>';
                            $children_html .= '<p>' . $child['MENU_NAME'] . '</p>';
                            $children_html .= '</a>';
                            $children_html .= '</li>';
                        }

                        $parent_open_class = $is_parent_active ? 'menu-open' : '';
                        $parent_active_class = $is_parent_active ? 'active' : '';

                        echo '<li class="nav-item has-treeview ' . $parent_open_class . '">';
                        echo '<a href="#" class="nav-link ' . $parent_active_class . '">';
                        echo '<i class="nav-icon ' . $main['MENU_ICON'] . '"></i>';
                        echo '<p>' . $main['MENU_NAME'] . ' <i class="fas fa-angle-left right"></i></p>';
                        echo '</a>';
                        echo '<ul class="nav nav-treeview">';
                        echo $children_html;
                        echo '</ul>';
                        echo '</li>';
                    } else if ($is_super_admin) {
                        // Empty parents only show for Super Admins
                        echo '<li class="nav-item">';
                        echo '<a href="#" class="nav-link">';
                        echo '<i class="nav-icon ' . $main['MENU_ICON'] . '"></i>';
                        echo '<p>' . $main['MENU_NAME'] . ' <span class="right badge badge-warning">Empty</span></p>';
                        echo '</a>';
                        echo '</li>';
                    }
                }
                ?>
            </ul>
        </nav>
    </div>
</aside>