<?php

// ===============================
// HARD CODE MENU SIP4GN
// ===============================
function activeUrl(string $url): bool
{
    $currentPath = trim(parse_url(current_url(), PHP_URL_PATH), '/');
    return $currentPath === trim($url, '/');
}
$menus = [

    // ================= DASHBOARD =================
    [
        'title' => 'Dashboard',
        'icon' => 'fas fa-tachometer-alt',
        'url' => 'dashboard',
    ],

    // ================= PPDB =================
    [
        'title' => 'PPDB / Pendaftaran',
        'icon' => 'fas fa-user-plus',
        'children' => [
            ['title' => 'Create User Account', 'icon' => 'fas fa-edit', 'url' => 'ppdb/account'],
            ['title' => 'Pendaftaran Siswa Baru', 'icon' => 'fas fa-edit', 'url' => 'ppdb'],
            ['title' => 'Seleksi & Verifikasi', 'icon' => 'fas fa-check-circle', 'url' => 'ppdb/verification'],
            ['title' => 'Pembayaran PPDB', 'icon' => 'fas fa-money-bill-wave', 'url' => 'ppdb/payment'],
            ['title' => 'Hasil Kelulusan', 'icon' => 'fas fa-flag-checkered', 'url' => 'ppdb/result'],
            ['title' => 'Daftar Ulang', 'icon' => 'fas fa-flag-checkered', 'url' => 'ppdb/daftarulang'],
        ]
    ],

    // ================= MASTER DATA =================
    [
        'title' => 'Master Data',
        'icon' => 'fas fa-database',
        'children' => [
            ['title' => 'Data Siswa', 'icon' => 'fas fa-user-graduate', 'url' => 'master/student'],
            ['title' => 'Data Guru & Staff', 'icon' => 'fas fa-chalkboard-teacher', 'url' => 'master/teacher'],
            ['title' => 'Kelas', 'icon' => 'fas fa-layer-group', 'url' => 'master/kelas'],
            ['title' => 'Rombel', 'icon' => 'fas fa-school', 'url' => 'master/rombel'],
            ['title' => 'Jurusan / Program', 'icon' => 'fas fa-project-diagram', 'url' => 'master/major'],
            ['title' => 'Jadwal Pelajaran', 'icon' => 'fas fa-project-diagram', 'url' => 'master/jadwal'],
            ['title' => 'Mata Pelajaran', 'icon' => 'fas fa-book', 'url' => 'master/subject'],
            ['title' => 'Tahun Ajaran', 'icon' => 'fas fa-calendar', 'url' => 'master/year'],
        ]
    ],

    // ================= AKADEMIK =================
    [
        'title' => 'Akademik & Kurikulum',
        'icon' => 'fas fa-graduation-cap',
        'children' => [
            ['title' => 'Jadwal Pelajaran', 'icon' => 'fas fa-calendar-alt', 'url' => 'academic/schedule'],
            ['title' => 'Pembagian Guru Mapel', 'icon' => 'fas fa-user-tag', 'url' => 'academic/teacher-subject'],
            ['title' => 'Silabus & RPP', 'icon' => 'fas fa-file-alt', 'url' => 'academic/rpp'],
            ['title' => 'Penilaian & Nilai', 'icon' => 'fas fa-star', 'url' => 'academic/grade'],
            ['title' => 'Raport Digital', 'icon' => 'fas fa-file-pdf', 'url' => 'academic/report'],
        ]
    ],

    // ================= ABSENSI =================
    [
        'title' => 'Absensi & Kehadiran',
        'icon' => 'fas fa-user-check',
        'children' => [
            ['title' => 'Absensi Siswa', 'icon' => 'fas fa-users', 'url' => 'attendance/student'],
            ['title' => 'Absensi Guru', 'icon' => 'fas fa-user-tie', 'url' => 'attendance/teacher'],
            ['title' => 'Integrasi Finger / RFID', 'icon' => 'fas fa-fingerprint', 'url' => 'attendance/device'],
            ['title' => 'Laporan Absensi', 'icon' => 'fas fa-file', 'url' => 'attendance/report'],
        ]
    ],

    // ================= E-LEARNING =================
    [
        'title' => 'E-Learning',
        'icon' => 'fas fa-laptop',
        'children' => [
            ['title' => 'Materi & Modul', 'icon' => 'fas fa-book-reader', 'url' => 'elearning/materi'],
            ['title' => 'Tugas Online', 'icon' => 'fas fa-tasks', 'url' => 'elearning/task'],
            ['title' => 'Ujian Online (CBT)', 'icon' => 'fas fa-edit', 'url' => 'elearning/exam'],
            ['title' => 'Nilai E-Learning', 'icon' => 'fas fa-star-half-alt', 'url' => 'elearning/grade'],
        ]
    ],

    // ================= KEUANGAN =================
    [
        'title' => 'Keuangan Sekolah',
        'icon' => 'fas fa-wallet',
        'children' => [
            ['title' => 'SPP & Tagihan', 'icon' => 'fas fa-file-invoice', 'url' => 'finance/billing'],
            ['title' => 'Pembayaran Siswa', 'icon' => 'fas fa-credit-card', 'url' => 'finance/payment'],
            ['title' => 'Beasiswa & Subsidi', 'icon' => 'fas fa-gift', 'url' => 'finance/scholarship'],
            ['title' => 'Kas Sekolah', 'icon' => 'fas fa-cash-register', 'url' => 'finance/cash'],
            ['title' => 'Laporan Keuangan', 'icon' => 'fas fa-chart-line', 'url' => 'finance/report'],
        ]
    ],

    // ================= BK =================
    [
        'title' => 'Bimbingan Konseling',
        'icon' => 'fas fa-heart',
        'children' => [
            ['title' => 'Kasus & Pelanggaran', 'icon' => 'fas fa-exclamation-circle', 'url' => 'bk/case'],
            ['title' => 'Konseling', 'icon' => 'fas fa-comments', 'url' => 'bk/counseling'],
            ['title' => 'Point Disiplin', 'icon' => 'fas fa-star', 'url' => 'bk/point'],
        ]
    ],

    // ================= KESISWAAN =================
    [
        'title' => 'Kesiswaan',
        'icon' => 'fas fa-users',
        'children' => [
            ['title' => 'OSIS & Organisasi', 'icon' => 'fas fa-users-cog', 'url' => 'student/organization'],
            ['title' => 'Ekstrakurikuler', 'icon' => 'fas fa-futbol', 'url' => 'student/extracurricular'],
            ['title' => 'Prestasi Siswa', 'icon' => 'fas fa-trophy', 'url' => 'student/achievement'],
        ]
    ],

    // ================= BOARDING =================
    [
        'title' => 'Asrama / Boarding',
        'icon' => 'fas fa-home',
        'children' => [
            ['title' => 'Data Penghuni Asrama', 'icon' => 'fas fa-bed', 'url' => 'boarding/student'],
            ['title' => 'Absensi Asrama', 'icon' => 'fas fa-calendar-check', 'url' => 'boarding/attendance'],
            ['title' => 'Perizinan Keluar', 'icon' => 'fas fa-door-open', 'url' => 'boarding/permission'],
        ]
    ],

    // ================= KESEHATAN =================
    [
        'title' => 'Kesehatan Siswa',
        'icon' => 'fas fa-briefcase-medical',
        'children' => [
            ['title' => 'Rekam Medis Siswa', 'icon' => 'fas fa-file-medical', 'url' => 'health/record'],
            ['title' => 'Klinik Sekolah', 'icon' => 'fas fa-clinic-medical', 'url' => 'health/clinic'],
        ]
    ],

    // ================= PERPUSTAKAAN =================
    [
        'title' => 'Perpustakaan',
        'icon' => 'fas fa-book-open',
        'children' => [
            ['title' => 'Koleksi Buku', 'icon' => 'fas fa-book', 'url' => 'library/books'],
            ['title' => 'Peminjaman', 'icon' => 'fas fa-hand-holding', 'url' => 'library/loan'],
            ['title' => 'Pengembalian', 'icon' => 'fas fa-undo', 'url' => 'library/return'],
            ['title' => 'Denda', 'icon' => 'fas fa-money-bill', 'url' => 'library/fine'],
        ]
    ],

    // ================= ORANG TUA =================
    [
        'title' => 'Komunikasi & Orang Tua',
        'icon' => 'fas fa-comments',
        'children' => [
            ['title' => 'Pengumuman', 'icon' => 'fas fa-bullhorn', 'url' => 'communication/announcement'],
            ['title' => 'WhatsApp Gateway', 'icon' => 'fas fa-phone', 'url' => 'communication/wa'],
            ['title' => 'Portal Orang Tua', 'icon' => 'fas fa-user-friends', 'url' => 'communication/parent'],
        ]
    ],

    // ================= REPORT =================
    [
        'title' => 'Reporting & Analytics',
        'icon' => 'fas fa-chart-bar',
        'children' => [
            ['title' => 'Akademik', 'icon' => 'fas fa-file', 'url' => 'report/academic'],
            ['title' => 'Absensi', 'icon' => 'fas fa-file', 'url' => 'report/attendance'],
            ['title' => 'Keuangan', 'icon' => 'fas fa-file', 'url' => 'report/finance'],
            ['title' => 'Kinerja Guru', 'icon' => 'fas fa-file', 'url' => 'report/teacher'],
        ]
    ],

    // ================= SYSTEM =================
    [
        'title' => 'User & Security',
        'icon' => 'fas fa-user-shield',
        'children' => [
            ['title' => 'User Management', 'icon' => 'fas fa-users-cog', 'url' => 'security/users'],
            ['title' => 'Role & Permission', 'icon' => 'fas fa-key', 'url' => 'security/role'],
            ['title' => 'Activity Log', 'icon' => 'fas fa-history', 'url' => 'security/log'],
        ]
    ],

];

?>

<!DOCTYPE html>
<html>

<head>
    <title><?= $title ?? 'SIMSEKO - Sistem Informasi Sekolah Enterprise One' ?></title>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.8/css/dataTables.bootstrap4.min.css">

    <link rel="stylesheet" href="<?= base_url('adminlte/plugins/fontawesome-free/css/all.min.css') ?>">
    <link rel="stylesheet" href="<?= base_url('adminlte/dist/css/adminlte.min.css') ?>">
    <link rel="stylesheet" href="<?= base_url('adminlte/plugins/datatables-bs4/css/dataTables.bootstrap4.min.css') ?>">

    <style>
        .nav-link.active {
            background: #0d6efd !important;
            color: #fff !important;
        }
    </style>
</head>

<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">

        <nav class="main-header navbar navbar-expand navbar-white navbar-light">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" data-widget="pushmenu" href="#">
                        <i class="fas fa-bars"></i>
                    </a>
                </li>
            </ul>
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a href="<?= site_url('logout') ?>" class="nav-link text-danger">Logout</a>
                </li>
            </ul>
        </nav>

        <aside class="main-sidebar sidebar-dark-primary elevation-4">
            <a href="#" class="brand-link text-center">
                <span class="brand-text font-weight-bold">SIMSEKO</span>
            </a>

            <div class="sidebar">
                <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" data-accordion="false">

                    <?php foreach ($menus as $menu):
                        $hasChild = isset($menu['children']);
                        $childActive = false;

                        if ($hasChild) {
                            foreach ($menu['children'] as $c) {
                                if (activeUrl($c['url'])) {
                                    $childActive = true;
                                    break;
                                }
                            }
                        }
                        ?>

                        <?php if (!$hasChild): ?>
                            <li class="nav-item">
                                <a href="<?= site_url($menu['url']) ?>"
                                    class="nav-link <?= activeUrl($menu['url']) ? 'active' : '' ?>">
                                    <i class="nav-icon <?= $menu['icon'] ?>"></i>
                                    <p><?= esc($menu['title']) ?></p>
                                </a>
                            </li>

                        <?php else: ?>
                            <li class="nav-item <?= $childActive ? 'menu-open' : '' ?>">
                                <a href="#" class="nav-link">
                                    <i class="nav-icon <?= $menu['icon'] ?>"></i>
                                    <p>
                                        <?= esc($menu['title']) ?>
                                        <i class="right fas fa-angle-left"></i>
                                    </p>
                                </a>

                                <ul class="nav nav-treeview">
                                    <?php foreach ($menu['children'] as $child): ?>
                                        <li class="nav-item">
                                            <a href="<?= site_url($child['url']) ?>"
                                                class="nav-link <?= activeUrl($child['url']) ? 'active' : '' ?>">
                                                <i class="nav-icon <?= $child['icon'] ?>"></i>
                                                <p><?= esc($child['title']) ?></p>
                                            </a>
                                        </li>
                                    <?php endforeach ?>
                                </ul>
                            </li>

                        <?php endif ?>

                    <?php endforeach ?>

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

        <div class="content-wrapper">
            <section class="content pt-3">
                <div class="container-fluid">
                    <?= $this->renderSection('content') ?>
                </div>
            </section>
        </div>

    </div>
    <script src="<?= base_url('adminlte/plugins/jquery/jquery.min.js') ?>"></script>
    <script src="<?= base_url('adminlte/plugins/bootstrap/js/bootstrap.bundle.min.js') ?>"></script>
    <script src="<?= base_url('adminlte/plugins/datatables/jquery.dataTables.min.js') ?>"></script>
    <script src="<?= base_url('adminlte/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js') ?>"></script>
    <script src="<?= base_url('adminlte/dist/js/adminlte.min.js') ?>"></script>

    <?= $this->renderSection('scripts') ?>
    <!-- JQUERY (WAJIB PERTAMA) -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <!-- BOOTSTRAP 4 -->
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js"></script>

    <!-- DATATABLES -->
    <script src="https://cdn.datatables.net/1.13.8/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.8/js/dataTables.bootstrap4.min.js"></script>

</body>

</html>