<?php
// includes/sidebar.php
$user = currentUser();
$role = $user['role'];
$cur  = basename($_SERVER['PHP_SELF']);
$path = $_SERVER['PHP_SELF'];

function navLink($url, $icon, $label, $cur, $match) {
    $active = ($cur === $match || strpos($cur, $match) !== false) ? 'active' : '';
    echo "<a href=\"$url\" class=\"nav-link $active\"><span class=\"icon\">$icon</span> $label</a>";
}
?>

<!-- Overlay gelap saat sidebar terbuka di mobile -->
<div class="sidebar-overlay" id="sidebarOverlay" onclick="closeSidebar()"></div>

<!-- Sidebar -->
<div class="sidebar" id="sidebar">
  <div class="sidebar-brand">
    <div class="brand-name">🛒 <?= NAMA_TOKO ?></div>
    <div class="brand-sub">Sistem POS</div>
  </div>

  <nav class="sidebar-nav">
    <?php if ($role === 'admin'): ?>
    <div class="nav-section">Admin</div>
    <?php navLink(BASE_URL.'admin/dashboard.php',  '📊', 'Dashboard',        $cur, 'dashboard.php'); ?>
    <?php navLink(BASE_URL.'admin/barang.php',     '📦', 'Barang',           $cur, 'barang.php'); ?>
    <?php navLink(BASE_URL.'admin/stok.php',       '🗂️', 'Manajemen Stok',   $cur, 'stok.php'); ?>
    <?php navLink(BASE_URL.'admin/laporan.php',    '📋', 'Laporan',          $cur, 'laporan.php'); ?>
    <?php navLink(BASE_URL.'admin/akun.php',       '👥', 'Akun',             $cur, 'akun.php'); ?>
    <div class="nav-section">Kasir</div>
    <?php endif; ?>
    <?php navLink(BASE_URL.'kasir/dashboard.php',  '🏠', 'Beranda Kasir',    $cur, 'kasir/dashboard.php'); ?>
    <?php navLink(BASE_URL.'kasir/penjualan.php',  '🛒', 'Penjualan',        $cur, 'penjualan.php'); ?>
    <?php navLink(BASE_URL.'kasir/riwayat.php',    '🕐', 'Riwayat Transaksi',$cur, 'riwayat.php'); ?>
  </nav>

  <div class="sidebar-user">
    <div class="uname">👤 <?= htmlspecialchars($user['nama']) ?></div>
    <div class="urole"><?= ucfirst($role) ?></div>
    <a href="<?= BASE_URL ?>logout.php">🚪 Keluar</a>
  </div>
</div>

<!-- ── Bottom Navigation Mobile ── -->
<div class="mobile-nav">
  <?php if ($role === 'admin'): ?>

    <?php
    // Admin: Home | Laporan | [JUAL] | Barang+Stok | Akun
    $isHome    = $cur==='dashboard.php' && strpos($path,'admin')!==false;
    $isLaporan = $cur==='laporan.php';
    $isJual    = $cur==='penjualan.php';
    $isBarang  = $cur==='barang.php' || $cur==='stok.php';
    $isAkun    = $cur==='akun.php' || $cur==='profil.php';
    ?>

    <!-- Home / Dashboard -->
    <a href="<?= BASE_URL ?>admin/dashboard.php"
       class="mobile-nav-btn <?= $isHome ? 'active':'' ?>">
      <div class="mni-wrap"><span class="mni">🏠</span></div>
      Home
    </a>

    <!-- Laporan -->
    <a href="<?= BASE_URL ?>admin/laporan.php"
       class="mobile-nav-btn <?= $isLaporan ? 'active':'' ?>">
      <div class="mni-wrap"><span class="mni">📋</span></div>
      Laporan
    </a>

    <!-- PENJUALAN — FAB Tengah -->
    <a href="<?= BASE_URL ?>kasir/penjualan.php"
       class="mobile-nav-btn center-fab <?= $isJual ? 'active':'' ?>">
      <div class="fab-circle">🛒</div>
      <span class="fab-label">Jual</span>
    </a>

    <!-- Barang & Stok -->
    <a href="<?= BASE_URL ?>admin/barang.php"
       class="mobile-nav-btn <?= $isBarang ? 'active':'' ?>">
      <div class="mni-wrap"><span class="mni">📦</span></div>
      Barang
    </a>

    <!-- Akun / Profil -->
    <a href="<?= BASE_URL ?>kasir/profil.php"
       class="mobile-nav-btn <?= $cur==='profil.php' ? 'active':'' ?>">
      <div class="mni-wrap"><span class="mni">👤</span></div>
      Akun
    </a>

  <?php else: ?>

    <?php
    // Kasir: Beranda | Laporan | [JUAL] | Riwayat | Akun
    $isHome    = $cur==='dashboard.php' && strpos($path,'kasir')!==false;
    $isRiwayat = $cur==='riwayat.php';
    $isJual    = $cur==='penjualan.php';
    ?>

    <!-- Beranda Kasir -->
    <a href="<?= BASE_URL ?>kasir/dashboard.php"
       class="mobile-nav-btn <?= $isHome ? 'active':'' ?>">
      <div class="mni-wrap"><span class="mni">🏠</span></div>
      Beranda
    </a>

    <!-- Riwayat -->
    <a href="<?= BASE_URL ?>kasir/riwayat.php"
       class="mobile-nav-btn <?= $isRiwayat ? 'active':'' ?>">
      <div class="mni-wrap"><span class="mni">🕐</span></div>
      Riwayat
    </a>

    <!-- PENJUALAN — FAB Tengah -->
    <a href="<?= BASE_URL ?>kasir/penjualan.php"
       class="mobile-nav-btn center-fab <?= $isJual ? 'active':'' ?>">
      <div class="fab-circle">🛒</div>
      <span class="fab-label">Jual</span>
    </a>

    <!-- Placeholder kiri akun (simetri) -->
    <div class="mobile-nav-btn" style="visibility:hidden"></div>

    <!-- Akun / Profil -->
    <a href="<?= BASE_URL ?>kasir/profil.php"
       class="mobile-nav-btn <?= $cur==='profil.php' ? 'active':'' ?>">
      <div class="mni-wrap"><span class="mni">👤</span></div>
      Akun
    </a>

  <?php endif; ?>
</div>
