<?php
$base = isset($inSubfolder) ? '../' : '';
$current = basename($_SERVER['PHP_SELF']);
function navActive($file, $current) { return $file === $current ? 'active' : ''; }
function groupHasActive($files, $current) { return in_array($current, $files); }

$groupTransaksi = ['transaksi_pemasukan.php','transaksi_pengeluaran.php','pembayaran_spp.php','pembayaran_kategori.php'];
$groupSpp       = ['siswa.php','tagihan_spp.php','tingkat_kelas.php','naik_pindah_kelas.php'];
$groupLaporan   = ['buku_besar.php','laporan_kategori.php','laporan_laba_rugi.php','laporan_spp.php','laporan_per_siswa.php','laporan_siswa_kategori.php','laporan_belum_bayar.php'];
$groupMaster    = ['kategori.php','akun_kas.php','tahun_ajaran.php'];
$groupSistem    = ['users.php','pengaturan.php'];
?>
    <div class="sidebar-overlay" id="sidebarOverlay"></div>
    <aside class="sidebar" id="sidebar">
        <div class="sidebar-brand">
            <i class="bi bi-wallet2"></i> <span>KeuanganSekolah</span>
        </div>
        <nav class="sidebar-nav">
            <a href="<?= $base ?>index.php" class="<?= navActive('index.php', $current) ?>"><i class="bi bi-speedometer2"></i> Dashboard</a>

            <!-- TRANSAKSI -->
            <button class="nav-group-toggle <?= groupHasActive($groupTransaksi, $current) ? 'open' : '' ?>" data-bs-toggle="collapse" data-bs-target="#grpTransaksi">
                <span><i class="bi bi-arrow-left-right"></i> Transaksi</span>
                <i class="bi bi-chevron-down chevron"></i>
            </button>
            <div class="collapse <?= groupHasActive($groupTransaksi, $current) ? 'show' : '' ?>" id="grpTransaksi">
                <a href="<?= $base ?>transaksi_pemasukan.php" class="sub-link <?= navActive('transaksi_pemasukan.php', $current) ?>"><i class="bi bi-arrow-down-circle text-success"></i> Pemasukan Umum</a>
                <a href="<?= $base ?>transaksi_pengeluaran.php" class="sub-link <?= navActive('transaksi_pengeluaran.php', $current) ?>"><i class="bi bi-arrow-up-circle text-danger"></i> Pengeluaran</a>
                <a href="<?= $base ?>pembayaran_spp.php" class="sub-link <?= navActive('pembayaran_spp.php', $current) ?>"><i class="bi bi-cash-coin"></i> Pembayaran SPP</a>
                <a href="<?= $base ?>pembayaran_kategori.php" class="sub-link <?= navActive('pembayaran_kategori.php', $current) ?>"><i class="bi bi-tag-fill"></i> Pembayaran per Kategori</a>
            </div>

            <!-- SPP SISWA -->
            <button class="nav-group-toggle <?= groupHasActive($groupSpp, $current) ? 'open' : '' ?>" data-bs-toggle="collapse" data-bs-target="#grpSpp">
                <span><i class="bi bi-mortarboard"></i> SPP Siswa</span>
                <i class="bi bi-chevron-down chevron"></i>
            </button>
            <div class="collapse <?= groupHasActive($groupSpp, $current) ? 'show' : '' ?>" id="grpSpp">
                <a href="<?= $base ?>siswa.php" class="sub-link <?= navActive('siswa.php', $current) ?>"><i class="bi bi-people"></i> Data Siswa</a>
                <a href="<?= $base ?>tagihan_spp.php" class="sub-link <?= navActive('tagihan_spp.php', $current) ?>"><i class="bi bi-receipt"></i> Tagihan SPP</a>
                <a href="<?= $base ?>tingkat_kelas.php" class="sub-link <?= navActive('tingkat_kelas.php', $current) ?>"><i class="bi bi-diagram-3"></i> Tingkat &amp; Kelas</a>
                <a href="<?= $base ?>naik_pindah_kelas.php" class="sub-link <?= navActive('naik_pindah_kelas.php', $current) ?>"><i class="bi bi-arrow-up-circle"></i> Naik &amp; Pindah Kelas</a>
            </div>

            <!-- LAPORAN -->
            <button class="nav-group-toggle <?= groupHasActive($groupLaporan, $current) ? 'open' : '' ?>" data-bs-toggle="collapse" data-bs-target="#grpLaporan">
                <span><i class="bi bi-file-earmark-bar-graph"></i> Laporan</span>
                <i class="bi bi-chevron-down chevron"></i>
            </button>
            <div class="collapse <?= groupHasActive($groupLaporan, $current) ? 'show' : '' ?>" id="grpLaporan">
                <a href="<?= $base ?>buku_besar.php" class="sub-link <?= navActive('buku_besar.php', $current) ?>"><i class="bi bi-journal-text"></i> Buku Besar</a>
                <a href="<?= $base ?>laporan_kategori.php" class="sub-link <?= navActive('laporan_kategori.php', $current) ?>"><i class="bi bi-bar-chart"></i> Laporan per Kategori</a>
                <a href="<?= $base ?>laporan_laba_rugi.php" class="sub-link <?= navActive('laporan_laba_rugi.php', $current) ?>"><i class="bi bi-graph-up-arrow"></i> Laporan Laba Rugi</a>
                <a href="<?= $base ?>laporan_spp.php" class="sub-link <?= navActive('laporan_spp.php', $current) ?>"><i class="bi bi-card-checklist"></i> Laporan SPP</a>
                <a href="<?= $base ?>laporan_per_siswa.php" class="sub-link <?= navActive('laporan_per_siswa.php', $current) ?>"><i class="bi bi-person-lines-fill"></i> Laporan per Siswa</a>
                <a href="<?= $base ?>laporan_siswa_kategori.php" class="sub-link <?= navActive('laporan_siswa_kategori.php', $current) ?>"><i class="bi bi-funnel"></i> Laporan Siswa per Kategori</a>
                <a href="<?= $base ?>laporan_belum_bayar.php" class="sub-link <?= navActive('laporan_belum_bayar.php', $current) ?>"><i class="bi bi-exclamation-triangle"></i> Siswa Belum Bayar</a>
            </div>

            <!-- MASTER DATA -->
            <button class="nav-group-toggle <?= groupHasActive($groupMaster, $current) ? 'open' : '' ?>" data-bs-toggle="collapse" data-bs-target="#grpMaster">
                <span><i class="bi bi-database"></i> Master Data</span>
                <i class="bi bi-chevron-down chevron"></i>
            </button>
            <div class="collapse <?= groupHasActive($groupMaster, $current) ? 'show' : '' ?>" id="grpMaster">
                <a href="<?= $base ?>kategori.php" class="sub-link <?= navActive('kategori.php', $current) ?>"><i class="bi bi-tags"></i> Kategori</a>
                <a href="<?= $base ?>akun_kas.php" class="sub-link <?= navActive('akun_kas.php', $current) ?>"><i class="bi bi-bank"></i> Akun Kas / Bank</a>
                <a href="<?= $base ?>tahun_ajaran.php" class="sub-link <?= navActive('tahun_ajaran.php', $current) ?>"><i class="bi bi-calendar3"></i> Tahun Ajaran</a>
            </div>

            <?php if (($_SESSION['role'] ?? '') === 'admin'): ?>
            <!-- SISTEM -->
            <button class="nav-group-toggle <?= groupHasActive($groupSistem, $current) ? 'open' : '' ?>" data-bs-toggle="collapse" data-bs-target="#grpSistem">
                <span><i class="bi bi-gear-wide-connected"></i> Sistem</span>
                <i class="bi bi-chevron-down chevron"></i>
            </button>
            <div class="collapse <?= groupHasActive($groupSistem, $current) ? 'show' : '' ?>" id="grpSistem">
                <a href="<?= $base ?>users.php" class="sub-link <?= navActive('users.php', $current) ?>"><i class="bi bi-person-gear"></i> Pengguna</a>
                <a href="<?= $base ?>pengaturan.php" class="sub-link <?= navActive('pengaturan.php', $current) ?>"><i class="bi bi-gear"></i> Pengaturan Sekolah</a>
            </div>
            <?php endif; ?>
        </nav>
    </aside>
    <main class="main-content">
        <?php showFlash(); ?>