<?php
function generateSidebarMenu($menus) {
    // Récupérer le nom de la compagnie depuis compagnie_table
    $compagnie_name = 'Compagnie';
    try {
        $pdo = new PDO('mysql:host=localhost;dbname=hiba;charset=utf8', 'root', '');
        $stmtCompagnie = $pdo->query("SELECT compagnie_name FROM compagnie_table LIMIT 1");
        $rowCompagnie = $stmtCompagnie->fetch(PDO::FETCH_ASSOC);
        if ($rowCompagnie && !empty($rowCompagnie['compagnie_name'])) {
            $compagnie_name = $rowCompagnie['compagnie_name'];
        }
    } catch (Exception $e) {
        // fallback: garder le nom par défaut
    }

    $html = '<ul class="navbar-nav sidebar sidebar-light accordion" id="accordionSidebar">';
    
    // Logo et marque
    $html .= '
      <a class="sidebar-brand d-flex align-items-center justify-content-center" href="dashboard.php">
        <div class="sidebar-brand-icon">
          <img src="img/logo/logo2.png">
        </div>
        <div class="sidebar-brand-text mx-3">'.htmlspecialchars($compagnie_name).'</div>
      </a>
      <hr class="sidebar-divider my-0">';
    
    foreach ($menus as $key => $menu) {
        if (isset($menu['type']) && $menu['type'] === 'heading') {
            // Section de titre
            $html .= '<hr class="sidebar-divider">
                      <div class="sidebar-heading">'.$menu['title'].'</div>';
        } else {
            // Item de menu
            $activeClass = (isset($menu['active']) && $menu['active']) ? 'active' : '';
            
            if (isset($menu['submenu'])) {
                // Menu avec sous-menu
                $collapseId = 'collapse'.ucfirst($key);
                $isExpanded = (isset($menu['active']) && $menu['active']) ? 'true' : 'false';
                $showClass = (isset($menu['active']) && $menu['active']) ? 'show' : '';
                
                $html .= '
                <li class="nav-item '.$activeClass.'">
                  <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#'.$collapseId.'"
                    aria-expanded="'.$isExpanded.'" aria-controls="'.$collapseId.'">
                    <i class="'.$menu['icon'].'"></i>
                    <span>'.$menu['title'].'</span>
                  </a>
                  <div id="'.$collapseId.'" class="collapse '.$showClass.'" aria-labelledby="heading'.ucfirst($key).'" data-parent="#accordionSidebar">
                    <div class="bg-white py-2 collapse-inner rounded">
                      <h6 class="collapse-header">'.$menu['title'].'</h6>';
                
                foreach ($menu['submenu'] as $submenu) {
                    $subActiveClass = (isset($submenu['active']) && $submenu['active']) ? 'active' : '';
                    $html .= '<a class="collapse-item '.$subActiveClass.'" href="'.$submenu['link'].'">'.$submenu['title'].'</a>';
                }
                
                $html .= '</div></div></li>';
            } else {
                // Menu simple
                $html .= '
                <li class="nav-item '.$activeClass.'">
                  <a class="nav-link" href="'.$menu['link'].'">
                    <i class="'.$menu['icon'].'"></i>
                    <span>'.$menu['title'].'</span>
                  </a>
                </li>';
            }
        }
    }
    
    // Version
    $html .= '<hr class="sidebar-divider">
              <div class="version" id="version-ruangadmin"></div>
            </ul>';
    
    return $html;
}
?>