<?php
$currentPage = $_GET['page'] ?? 'dashboard';
?>

<aside class="app-sidebar" id="appSidebar" role="navigation">
  <div class="px-2 pb-3">
    <ul class="nav flex-column">

      <!-- 1. DASHBOARD -->
      <?php if (checkPermission('dashboard.view')): ?>
      <li>
        <a class="nav-link<?= ($currentPage === 'dashboard') ? ' active' : '' ?>" 
           href="/eagle_client_istanbulcups/index.php?page=dashboard">
           <i class="fa fa-chart-line"></i> Tableau de bord
        </a>
      </li>
      <?php endif; ?>


      <!-- 2. RÉFÉRENTIEL -->
      <?php if (hasAnyPermission(['produits.view','categories.view','unites.view','depots.view'])): ?>
      <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#referentielMenu">
          <span><i class="fa fa-database"></i> Référentiel</span>
          <i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="referentielMenu" class="sidebar-dropdown">
          <?php if (checkPermission('categories.view')): ?>
          <a class="nav-link" href="?page=categories"><i class="fa fa-tags"></i> Catégories</a>
          <?php endif; ?>
          <?php if (checkPermission('unites.view')): ?>
          <a class="nav-link" href="?page=unite"><i class="fa fa-ruler"></i> Unités</a>
          <?php endif; ?>
          <?php if (checkPermission('produits.view')): ?>
          <a class="nav-link" href="?page=produits"><i class="fa fa-box"></i> Produits</a>
          <?php endif; ?>
          <?php if (checkPermission('depots.view')): ?>
          <a class="nav-link" href="?page=depots"><i class="fa fa-warehouse"></i> Dépôts</a>
          <?php endif; ?>
        </ul>
      </li>
      <?php endif; ?>


      <!-- 3. APPROVISIONNEMENT -->
      <?php if (hasAnyPermission(['achats.view','appro.view','achats.commandes','achats.receptions','achats.paiements'])): ?>
      <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#approMenu">
          <span><i class="fa fa-truck-loading"></i> Approvisionnement</span>
          <i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="approMenu" class="sidebar-dropdown">

          <?php if (checkPermission('achats.view')): ?>
          <a class="nav-link" href="?page=achats">
            <i class="fa fa-shopping-cart"></i> Achats directs
          </a>
          <?php endif; ?>

          <?php if (checkPermission('appro.view')): ?>
          <a class="nav-link" href="?page=appros">
            <i class="fa fa-boxes"></i> Entrée de stock
          </a>
          <?php endif; ?>

          <?php if (checkPermission('achats.commandes')): ?>
          <a class="nav-link" href="?page=commandes_fournisseurs">
            <i class="fa fa-file-alt"></i> Commandes fournisseurs
          </a>
          <?php endif; ?>

          <?php if (checkPermission('achats.receptions')): ?>
          <a class="nav-link" href="?page=receptions_fournisseurs">
            <i class="fa fa-truck"></i> Réceptions fournisseurs
          </a>
          <?php endif; ?>

          <?php if (checkPermission('achats.paiements')): ?>
          <a class="nav-link" href="?page=paiements_fournisseurs">
            <i class="fa fa-money-bill-wave"></i> Paiements fournisseurs
          </a>
          <?php endif; ?>

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


      <!-- 4. POS -->
      <?php if (checkPermission('pos.view','ventes.list')): ?>
      <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#ventesMenu">
          <span><i class="fa fa-cash-register"></i> POS</span>
          <i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="ventesMenu" class="sidebar-dropdown">
           <?php if (checkPermission('pos.view')): ?>
          <a class="nav-link" href="?page=pos">
            <i class="fa fa-plus"></i> Nouvelle vente
          </a>
          <?php endif; ?>

          <?php if (checkPermission('ventes.list')): ?>
          <a class="nav-link" href="?page=pos-liste">
            <i class="fa fa-list"></i> Ventes du jour
          </a>
          <?php endif; ?>

          <!-- FUTUR -->
          <!-- <a class="nav-link" href="?page=commandes_clients">
            <i class="fa fa-file-invoice"></i> Commandes clients
          </a> -->
        </ul>
      </li>
      <?php endif; ?>

      <!-- VENTES AVANCÉES -->
      <?php if (hasAnyPermission([
        'ventes.view',
        'devis.view',
        'commandes_clients.view',
        'livraisons.view',
        'factures.view',
        'encaissements.view'
      ])): ?>
      <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#cycleClientMenu">
          <span><i class="fa fa-file-invoice"></i> Facturation</span>
          <i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="cycleClientMenu" class="sidebar-dropdown">

         <?php if (checkPermission('ventes.view')): ?>
            <a class="nav-link" href="?page=ventes">
              <i class="fa fa-shopping-cart"></i> Vente direct
            </a>
          <?php endif; ?>

          <?php if (checkPermission('devis.view')): ?>
            <a class="nav-link" href="?page=devis">
              <i class="fa fa-file-signature"></i> Devis
            </a>
          <?php endif; ?>

          <?php if (checkPermission('commandes_clients.view')): ?>
            <a class="nav-link" href="?page=commandes_clients">
              <i class="fa fa-shopping-basket"></i> Commandes clients
            </a>
          <?php endif; ?>

          <?php if (checkPermission('livraisons.view')): ?>
            <a class="nav-link" href="?page=livraisons">
              <i class="fa fa-truck"></i> Livraisons
            </a>
          <?php endif; ?>

          <?php if (checkPermission('factures.view')): ?>
            <a class="nav-link" href="?page=factures">
              <i class="fa fa-file-invoice-dollar"></i> Factures
            </a>
          <?php endif; ?>

          <?php if (checkPermission('encaissements.view')): ?>
            <a class="nav-link" href="?page=encaissements_clients">
              <i class="fa fa-money-bill-wave"></i> Encaissements
            </a>
          <?php endif; ?>

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


      <!-- 5. PRODUCTION -->
      <?php if (checkPermission('productions.view')): ?>
      <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#productionMenu">
          <span><i class="fa fa-industry"></i> Production</span>
          <i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="productionMenu" class="sidebar-dropdown">
          <a class="nav-link" href="?page=matiere-premiere">
            <i class="fa fa-boxes"></i> Matières premières
          </a>
          <a class="nav-link" href="?page=fiches-techniques">
            <i class="fa fa-file-alt"></i> Fiches techniques
          </a>
          <a class="nav-link" href="?page=simulation">
            <i class="fa fa-calculator"></i> Simulation
          </a>
          <a class="nav-link" href="?page=productions">
            <i class="fa fa-cogs"></i> Ordres de production
          </a>
          <a class="nav-link" href="?page=produits-finis">
            <i class="fa fa-cube"></i> Produits finis
          </a>
        </ul>
      </li>
      <?php endif; ?>


      <!-- 6. STOCK & LOGISTIQUE -->
      <?php if (hasAnyPermission(['mouvements.view','inventaire.view','retours.view'])): ?>
      <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#stockMenu">
          <span><i class="fa fa-boxes"></i> Stock & Logistique</span>
          <i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="stockMenu" class="sidebar-dropdown">
          <a class="nav-link" href="?page=mouvements">
            <i class="fa fa-exchange-alt"></i> Mouvements
          </a>
          <a class="nav-link" href="?page=inventaire">
            <i class="fa fa-clipboard-list"></i> Inventaire
          </a>
          <a class="nav-link" href="?page=retours">
            <i class="fa fa-undo"></i> Retours
          </a>
        </ul>
      </li>
      <?php endif; ?>


      <!-- 7. TRÉSORERIE -->
      <?php if (checkPermission('tresorerie.view')): ?>
      <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#tresorerieMenu">
          <span><i class="fa fa-wallet"></i> Trésorerie</span>
          <i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="tresorerieMenu" class="sidebar-dropdown">
          <a class="nav-link" href="?page=tresorerie"><i class="fa fa-eye"></i> Vue globale</a>
          <a class="nav-link" href="?page=caisse_especes"><i class="fa fa-money-bill"></i> Caisse Espèces</a>
          <a class="nav-link" href="?page=caisse_banque"><i class="fa fa-university"></i> Caisse Banque</a>
          <a class="nav-link" href="?page=caisse_mobile"><i class="fa fa-mobile-alt"></i> Caisse Mobile</a>
          <a class="nav-link" href="?page=autres_operations"><i class="fa fa-exchange-alt"></i> Autres opérations</a>
        </ul>
      </li>
      <?php endif; ?>


      <!-- 8. FINANCES -->
      <?php if (hasAnyPermission(['creances.view','dettes.view'])): ?>
      <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#financesMenu">
          <span><i class="fa fa-chart-line"></i> Finances</span>
          <i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="financesMenu" class="sidebar-dropdown">
          <a class="nav-link" href="?page=creances"><i class="fa fa-hand-holding-usd"></i> Créances clients</a>
          <a class="nav-link" href="?page=dettes"><i class="fa fa-file-invoice-dollar"></i> Dettes fournisseurs</a>
        </ul>
      </li>
      <?php endif; ?>

      <!-- COMPTABILITÉ (FUTUR) -->
      <?php if (hasAnyPermission(['compta.view','compta.journal.view','compta.bilan.view'])): ?>
      <li class="sidebar-item">
          <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#comptaMenu">
              <span><i class="fa fa-book"></i> Comptabilité</span>
              <i class="fa fa-chevron-down small toggle-icon"></i>
          </a>
          <ul id="comptaMenu" class="sidebar-dropdown">
              <a class="nav-link" href="?page=compta_journal">
                  <i class="fa fa-clipboard"></i> Journal
              </a>
              <a class="nav-link" href="?page=compta_grand_livre">
                  <i class="fa fa-book-open"></i> Grand Livre
              </a>
              <a class="nav-link" href="?page=compta_balance">
                  <i class="fa fa-balance-scale"></i> Balance
              </a>
              <a class="nav-link" href="?page=compta_bilan">
                  <i class="fa fa-chart-bar"></i> Bilan & Résultat
              </a>
          </ul>
      </li>
      <?php endif; ?>

      <!-- RESSOURCES HUMAINES (FUTUR) -->
    <?php if (hasAnyPermission(['rh.view','rh.paie.view','rh.presence.view'])): ?>
    <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#rhMenu">
            <span><i class="fa fa-user-tie"></i> Ressources Humaines</span>
            <i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="rhMenu" class="sidebar-dropdown">
            <a class="nav-link" href="?page=rh_employes">
                <i class="fa fa-id-badge"></i> Employés
            </a>
            <a class="nav-link" href="?page=rh_presences">
                <i class="fa fa-calendar-check"></i> Présences
            </a>
            <a class="nav-link" href="?page=rh_paie">
                <i class="fa fa-money-check"></i> Paie
            </a>
        </ul>
    </li>
    <?php endif; ?>


      <!-- 9. RELATIONS -->
      <?php if (hasAnyPermission(['clients.view','fournisseurs.view','employes.view'])): ?>
      <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#relationsMenu">
          <span><i class="fa fa-users"></i> Relations</span>
          <i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="relationsMenu" class="sidebar-dropdown">
          <a class="nav-link" href="?page=clients"><i class="fa fa-user"></i> Clients</a>
          <a class="nav-link" href="?page=fournisseurs"><i class="fa fa-truck"></i> Fournisseurs</a>
          <a class="nav-link" href="?page=employes"><i class="fa fa-id-badge"></i> Employés</a>
        </ul>
      </li>
      <?php endif; ?>


      <!-- 10. RAPPORTS -->
      <?php if (hasAnyPermission(['rapports.achats.view','rapports.ventes.view','rapports.stocks.view','rapports.caisse.view'])): ?>
      <li>
        <a class="nav-link" href="?page=rapports_ventes">
          <i class="fa fa-chart-pie"></i> Rapports
        </a>
      </li>
      <?php endif; ?>


      <!-- 11. ADMINISTRATION -->
      <?php if (hasAnyPermission(['users.manage','roles.manage','settings.view'])): ?>
      <li class="sidebar-item">
        <a href="#" class="nav-link sidebar-toggle d-flex justify-content-between align-items-center" data-target="#adminMenu" aria-expanded="false">
          <span><i class="fa fa-cogs"></i> Administration</span><i class="fa fa-chevron-down small toggle-icon"></i>
        </a>
        <ul id="adminMenu" class="sidebar-dropdown">
          <?php if (checkPermission('users.manage')): ?><a class="nav-link" href="/eagle_client_istanbulcups/index.php?page=utilisateurs"><i class="fa fa-user-shield"></i> Utilisateurs</a><?php endif; ?>
          <?php if (checkPermission('roles.manage')): ?><a class="nav-link" href="/eagle_client_istanbulcups/index.php?page=roles"><i class="fa fa-key"></i> Rôles & Permissions</a><?php endif; ?>
          <?php if (checkPermission('settings.view')): ?><a class="nav-link" href="/eagle_client_istanbulcups/index.php?page=parametres"><i class="fa fa-cog"></i> Paramètres</a><?php endif; ?>
        </ul>
      </li>
      <?php endif; ?>


    </ul>
  </div>
</aside>