<?php
$currentPage = $_GET['page'] ?? 'dashboard';
$pengaturan  = $pengaturan ?? [];
$namaSekolah = $pengaturan['nama_sekolah'] ?? APP_FULL_NAME;
$userNama    = $_SESSION['user_nama'] ?? 'Admin';
$userRole    = $_SESSION['user_role'] ?? 'admin';
$userInitial = strtoupper(substr($userNama, 0, 1));

// Helper: check if current page starts with prefix
if (!function_exists('menuActive')) {
    function menuActive($prefix, $current) {
        return strpos($current, $prefix) === 0 ? 'active' : '';
    }
    function menuOpen($prefix, $current) {
        return strpos($current, $prefix) === 0 ? 'open' : '';
    }
    function submenuShow($prefix, $current) {
        return strpos($current, $prefix) === 0 ? 'show' : '';
    }
}
?>

<!-- Sidebar Overlay (mobile) -->
<div class="sidebar-overlay" id="sidebarOverlay"></div>

<!-- Sidebar -->
<nav class="sidebar" id="sidebar">
  <!-- Brand -->
  <div class="sidebar-brand">
    <div class="brand-logo">
      <?php if (!empty($pengaturan['logo'])): ?>
        <img src="<?= BASE_URL ?>/uploads/<?= e($pengaturan['logo']) ?>"
             alt="Logo" style="width:100%;height:100%;object-fit:cover;border-radius:8px;">
      <?php else: ?>
        <i class="bi bi-mortarboard-fill"></i>
      <?php endif; ?>
    </div>
    <div class="brand-text">
      <?= APP_NAME ?>
      <small><?= e($namaSekolah) ?></small>
    </div>
  </div>

  <!-- Navigation -->
  <div class="sidebar-nav">

    <!-- Dashboard -->
    <div class="nav-section-title">Menu Utama</div>
    <div class="nav-item">
      <a href="<?= BASE_URL ?>/index.php?page=dashboard"
         class="nav-link <?= isActive('dashboard', $currentPage) ?>">
        <span class="nav-icon"><i class="bi bi-speedometer2"></i></span>
        Dashboard
      </a>
    </div>

    <!-- ===== AKADEMIK ===== -->
    <div class="nav-section-title">Akademik</div>

    <!-- Data Siswa -->
    <div class="nav-item has-submenu <?= menuOpen('siswa', $currentPage) ?>">
      <a href="#" class="nav-link <?= menuActive('siswa', $currentPage) ?>">
        <span class="nav-icon"><i class="bi bi-people-fill"></i></span>
        Data Siswa
      </a>
      <div class="submenu <?= submenuShow('siswa', $currentPage) ?>">
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=siswa"
             class="nav-link <?= isActive('siswa', $currentPage) ?>">
            <i class="bi bi-list-ul me-1"></i> Daftar Siswa
          </a>
        </div>
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=siswa_tambah"
             class="nav-link <?= isActive('siswa_tambah', $currentPage) ?>">
            <i class="bi bi-person-plus me-1"></i> Tambah Siswa
          </a>
        </div>
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=siswa_import"
             class="nav-link <?= isActive('siswa_import', $currentPage) ?>">
            <i class="bi bi-file-earmark-excel me-1"></i> Import Excel
          </a>
        </div>
      </div>
    </div>

    <!-- Data Guru -->
    <div class="nav-item has-submenu <?= menuOpen('guru', $currentPage) ?>">
      <a href="#" class="nav-link <?= menuActive('guru', $currentPage) ?>">
        <span class="nav-icon"><i class="bi bi-person-badge-fill"></i></span>
        Data Guru
      </a>
      <div class="submenu <?= submenuShow('guru', $currentPage) ?>">
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=guru"
             class="nav-link <?= isActive('guru', $currentPage) ?>">
            <i class="bi bi-list-ul me-1"></i> Daftar Guru
          </a>
        </div>
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=guru_tambah"
             class="nav-link <?= isActive('guru_tambah', $currentPage) ?>">
            <i class="bi bi-person-plus me-1"></i> Tambah Guru
          </a>
        </div>
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=guru_import"
             class="nav-link <?= isActive('guru_import', $currentPage) ?>">
            <i class="bi bi-file-earmark-excel me-1"></i> Import Excel
          </a>
        </div>
      </div>
    </div>

    <!-- Data Kelas -->
    <div class="nav-item has-submenu <?= menuOpen('kelas', $currentPage) ?>">
      <a href="#" class="nav-link <?= menuActive('kelas', $currentPage) ?>">
        <span class="nav-icon"><i class="bi bi-building-fill"></i></span>
        Data Kelas
      </a>
      <div class="submenu <?= submenuShow('kelas', $currentPage) ?>">
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=kelas"
             class="nav-link <?= isActive('kelas', $currentPage) ?>">
            <i class="bi bi-list-ul me-1"></i> Daftar Kelas
          </a>
        </div>
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=kelas_tambah"
             class="nav-link <?= isActive('kelas_tambah', $currentPage) ?>">
            <i class="bi bi-plus-circle me-1"></i> Tambah Kelas
          </a>
        </div>
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=kelas_anggota"
             class="nav-link <?= isActive('kelas_anggota', $currentPage) ?>">
            <i class="bi bi-people me-1"></i> Anggota Kelas
          </a>
        </div>
      </div>
    </div>

    <!-- Mata Pelajaran -->
    <div class="nav-item">
      <a href="<?= BASE_URL ?>/index.php?page=mapel"
         class="nav-link <?= isActive('mapel', $currentPage) ?>">
        <span class="nav-icon"><i class="bi bi-book-fill"></i></span>
        Mata Pelajaran
      </a>
    </div>

    <!-- Nilai Siswa -->
    <div class="nav-item has-submenu <?= menuOpen('nilai', $currentPage) ?>">
      <a href="#" class="nav-link <?= menuActive('nilai', $currentPage) ?>">
        <span class="nav-icon"><i class="bi bi-journal-text"></i></span>
        Nilai Siswa
      </a>
      <div class="submenu <?= submenuShow('nilai', $currentPage) ?>">
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=nilai"
             class="nav-link <?= isActive('nilai', $currentPage) ?>">
            <i class="bi bi-list-ul me-1"></i> Daftar Nilai
          </a>
        </div>
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=nilai_input"
             class="nav-link <?= isActive('nilai_input', $currentPage) ?>">
            <i class="bi bi-pencil-square me-1"></i> Input Nilai
          </a>
        </div>
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=nilai_import"
             class="nav-link <?= isActive('nilai_import', $currentPage) ?>">
            <i class="bi bi-file-earmark-excel me-1"></i> Import Excel
          </a>
        </div>
      </div>
    </div>

    <!-- ===== LAINNYA ===== -->
    <div class="nav-section-title">Lainnya</div>

    <!-- Pencarian -->
    <div class="nav-item">
      <a href="<?= BASE_URL ?>/index.php?page=pencarian"
         class="nav-link <?= isActive('pencarian', $currentPage) ?>">
        <span class="nav-icon"><i class="bi bi-search"></i></span>
        Pencarian
      </a>
    </div>

    <!-- Pengaturan -->
    <div class="nav-item has-submenu <?= menuOpen('pengaturan', $currentPage) ?>">
      <a href="#" class="nav-link <?= menuActive('pengaturan', $currentPage) ?>">
        <span class="nav-icon"><i class="bi bi-gear-fill"></i></span>
        Pengaturan
      </a>
      <div class="submenu <?= submenuShow('pengaturan', $currentPage) ?>">
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=pengaturan_sekolah"
             class="nav-link <?= isActive('pengaturan_sekolah', $currentPage) ?>">
            <i class="bi bi-building me-1"></i> Profil Sekolah
          </a>
        </div>
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=pengaturan_sandi"
             class="nav-link <?= isActive('pengaturan_sandi', $currentPage) ?>">
            <i class="bi bi-key me-1"></i> Ubah Sandi
          </a>
        </div>
        <div class="nav-item">
          <a href="<?= BASE_URL ?>/index.php?page=pengaturan_backup"
             class="nav-link <?= isActive('pengaturan_backup', $currentPage) ?>">
            <i class="bi bi-cloud-download me-1"></i> Backup Data
          </a>
        </div>
      </div>
    </div>

    <!-- Logout -->
    <div class="nav-item mt-2">
      <a href="<?= BASE_URL ?>/index.php?page=logout"
         class="nav-link text-danger"
         data-confirm="Apakah Anda yakin ingin keluar?">
        <span class="nav-icon"><i class="bi bi-box-arrow-right"></i></span>
        Keluar
      </a>
    </div>

  </div><!-- /sidebar-nav -->

  <!-- Sidebar Footer -->
  <div class="sidebar-footer">
    <div class="sidebar-user">
      <div class="user-avatar"><?= $userInitial ?></div>
      <div class="user-info">
        <div class="user-name"><?= e($userNama) ?></div>
        <div class="user-role"><?= ucfirst($userRole) ?></div>
      </div>
    </div>
  </div>
</nav>
