<?php
/**
 * Menu panel admin + visibilitas menurut AdminAccess::canAccess().
 */
$u = uri_string();
$is = static function (string $prefix) use ($u): bool {
    return $u === $prefix || str_starts_with($u, $prefix . '/');
};
$exact = static function (string $path) use ($u): bool {
    return $u === $path || $u === $path . '/';
};
$dashActive = $exact('admin');
$linkBase = 'flex items-center gap-3 rounded-xl px-3 py-2.5 text-sm font-medium transition-colors';
$linkIdle = 'text-gray-700 hover:bg-gray-100';
$linkActive = 'bg-gray-100 text-gray-900';
$subWrap = 'mt-1 space-y-0.5 border-l border-gray-200 py-1 pl-3 ml-3';
$subLink = 'block rounded-md px-2 py-2 text-xs font-medium transition-colors';
$subIdle = 'text-gray-600 hover:bg-gray-50 hover:text-gray-900';
$subActive = 'bg-gray-100 text-gray-900';

$presensiOpen = $is('admin/presensi');
$presensiData = $exact('admin/presensi') || str_starts_with($u, 'admin/presensi/detail');
$presLapangan = str_starts_with($u, 'admin/presensi/lapangan');
$presLembur   = str_starts_with($u, 'admin/presensi/lembur');
$presJadwal   = str_starts_with($u, 'admin/presensi/jadwal');
$presLibur    = str_starts_with($u, 'admin/presensi/libur');
$presAktiv    = str_starts_with($u, 'admin/presensi/aktivitas');

$perusahaanOpen = $is('admin/perusahaan');
$laporanOpen    = $is('admin/laporan');
$laporanRingkas = $exact('admin/laporan');
$laporanCuti    = str_starts_with($u, 'admin/laporan/cuti');

$panelOpen = $is('admin/panel');
$panelUserList = $exact('admin/panel/users') || str_starts_with($u, 'admin/panel/users/reset') || str_starts_with($u, 'admin/panel/users/edit');
$panelUserCreate = str_starts_with($u, 'admin/panel/users/create');
$panelGroupsIndex = $exact('admin/panel/groups');
$panelGroupCreate = str_starts_with($u, 'admin/panel/groups/create');
$panelGroupEdit = str_starts_with($u, 'admin/panel/groups/edit');

$utilOpen = $is('admin/util');
?>
<aside id="admin-sidebar" class="fixed inset-y-0 left-0 z-50 flex h-screen w-[290px] shrink-0 flex-col border-r border-gray-200 bg-white shadow-sm lg:static lg:shadow-none">
    <div class="sidebar-brand flex h-16 shrink-0 items-center gap-3 border-b border-gray-200 px-5">
        <img src="<?= base_url('assets/images/bpr-logo.png') ?>" alt="Logo BPR" width="144" height="36" decoding="async" class="sidebar-logo-expanded h-9 w-auto max-w-[9rem] shrink-0 object-contain object-left">
        <img src="<?= base_url('assets/images/bij_logo.png') ?>" alt="BIJ" width="36" height="36" decoding="async" class="sidebar-logo-collapsed hidden h-9 w-9 shrink-0 rounded-none border-0 bg-transparent object-contain shadow-none ring-0 outline-none">
    </div>
    <nav class="custom-scrollbar flex-1 overflow-y-auto overflow-x-hidden px-3 py-5 text-sm">
        <p class="nav-section-label mb-2 px-3 text-[11px] font-semibold uppercase tracking-wider text-gray-400">Menu</p>

        <a href="<?= site_url('admin') ?>" title="Beranda" class="<?= $linkBase ?> <?= $dashActive ? $linkActive : $linkIdle ?>">
            <i class="fa-solid fa-house w-5 shrink-0 text-center text-gray-500"></i>
            <span class="sidebar-text">Beranda</span>
        </a>

        <?php if (canAccess('presensi')) : ?>
            <details class="group mb-1" <?= $presensiOpen ? 'open' : '' ?>>
                <summary class="<?= $linkBase ?> cursor-pointer list-none text-gray-700 marker:hidden hover:bg-gray-100 [&::-webkit-details-marker]:hidden">
                    <i class="fa-solid fa-clock w-5 shrink-0 text-center text-gray-500"></i>
                    <span class="sidebar-text flex-1 text-left">Presensi</span>
                    <i class="fa-solid fa-chevron-down text-[10px] text-gray-400 transition group-open:rotate-180"></i>
                </summary>
                <div class="<?= $subWrap ?>">
                    <a href="<?= site_url('admin/presensi') ?>" title="Data Presensi" class="<?= $subLink ?> <?= $presensiData ? $subActive : $subIdle ?>">Data Presensi</a>
                    <a href="<?= site_url('admin/presensi/lapangan') ?>" title="Tugas luar / lapangan" class="<?= $subLink ?> <?= $presLapangan ? $subActive : $subIdle ?>">Tugas Luar / Lapangan</a>
                    <a href="<?= site_url('admin/presensi/lembur') ?>" title="Jadwal lembur" class="<?= $subLink ?> <?= $presLembur ? $subActive : $subIdle ?>">Jadwal Lembur</a>
                    <?php if (canAccess('presensi_jadwal')) : ?>
                        <a href="<?= site_url('admin/presensi/jadwal') ?>" title="Management jadwal" class="<?= $subLink ?> <?= $presJadwal ? $subActive : $subIdle ?>">Management Jadwal</a>
                    <?php endif ?>
                    <?php if (canAccess('presensi_libur')) : ?>
                        <a href="<?= site_url('admin/presensi/libur') ?>" title="Hari libur" class="<?= $subLink ?> <?= $presLibur ? $subActive : $subIdle ?>">Hari Libur</a>
                    <?php endif ?>
                    <a href="<?= site_url('admin/presensi/aktivitas') ?>" title="Rekaman aktivitas" class="<?= $subLink ?> <?= $presAktiv ? $subActive : $subIdle ?>">Rekaman Aktivitas</a>
                </div>
            </details>
        <?php endif ?>

        <?php if (canAccess('perusahaan')) : ?>
            <details class="group mb-1" <?= $perusahaanOpen ? 'open' : '' ?>>
                <summary class="<?= $linkBase ?> cursor-pointer list-none text-gray-700 marker:hidden hover:bg-gray-100 [&::-webkit-details-marker]:hidden">
                    <i class="fa-solid fa-building w-5 shrink-0 text-center text-gray-500"></i>
                    <span class="sidebar-text flex-1 text-left">Perusahaan</span>
                    <i class="fa-solid fa-chevron-down text-[10px] text-gray-400 transition group-open:rotate-180"></i>
                </summary>
                <div class="<?= $subWrap ?>">
                    <a href="<?= site_url('admin/perusahaan/kantor') ?>" title="Lokasi kerja" class="<?= $subLink ?> <?= str_starts_with($u, 'admin/perusahaan/kantor') ? $subActive : $subIdle ?>">Lokasi Kerja</a>
                    <a href="<?= site_url('admin/perusahaan/unit_kerja') ?>" title="Unit kerja" class="<?= $subLink ?> <?= str_starts_with($u, 'admin/perusahaan/unit_kerja') ? $subActive : $subIdle ?>">Unit Kerja</a>
                    <a href="<?= site_url('admin/perusahaan/jabatan') ?>" title="Jabatan" class="<?= $subLink ?> <?= str_starts_with($u, 'admin/perusahaan/jabatan') ? $subActive : $subIdle ?>">Jabatan</a>
                    <a href="<?= site_url('admin/perusahaan/golongan') ?>" title="Golongan" class="<?= $subLink ?> <?= str_starts_with($u, 'admin/perusahaan/golongan') ? $subActive : $subIdle ?>">Golongan</a>
                    <a href="<?= site_url('admin/perusahaan/berita') ?>" title="Berita dan pengumuman" class="<?= $subLink ?> <?= str_starts_with($u, 'admin/perusahaan/berita') ? $subActive : $subIdle ?>">Berita / Pengumuman</a>
                </div>
            </details>
        <?php endif ?>

        <?php if (canAccess('pegawai')) : ?>
            <details class="group mb-1" <?= ($is('admin/pegawai') || $is('admin/cuti')) ? 'open' : '' ?>>
                <summary class="<?= $linkBase ?> cursor-pointer list-none text-gray-700 marker:hidden hover:bg-gray-100 [&::-webkit-details-marker]:hidden">
                    <i class="fa-solid fa-users w-5 shrink-0 text-center text-gray-500"></i>
                    <span class="sidebar-text flex-1 text-left">Pegawai</span>
                    <i class="fa-solid fa-chevron-down text-[10px] text-gray-400 transition group-open:rotate-180"></i>
                </summary>
                <div class="<?= $subWrap ?>">
                    <a href="<?= site_url('admin/pegawai') ?>" title="Data pegawai" class="<?= $subLink ?> <?= $is('admin/pegawai') && ! $is('admin/cuti') ? $subActive : $subIdle ?>">Data Pegawai</a>
                    <?php if (canAccess('cuti')) : ?>
                        <a href="<?= site_url('admin/cuti') ?>" title="Data cuti" class="<?= $subLink ?> <?= $is('admin/cuti') ? $subActive : $subIdle ?>">Data Cuti</a>
                    <?php endif ?>
                </div>
            </details>
        <?php endif ?>

        <?php if (canAccess('laporan')) : ?>
            <details class="group mb-1" <?= $laporanOpen ? 'open' : '' ?>>
                <summary class="<?= $linkBase ?> cursor-pointer list-none text-gray-700 marker:hidden hover:bg-gray-100 [&::-webkit-details-marker]:hidden">
                    <i class="fa-solid fa-print w-5 shrink-0 text-center text-gray-500"></i>
                    <span class="sidebar-text flex-1 text-left">Laporan</span>
                    <i class="fa-solid fa-chevron-down text-[10px] text-gray-400 transition group-open:rotate-180"></i>
                </summary>
                <div class="<?= $subWrap ?>">
                    <a href="<?= site_url('admin/laporan') ?>" title="Statistik dan ringkasan" class="<?= $subLink ?> <?= $laporanRingkas ? $subActive : $subIdle ?>">Statistik / Ringkasan</a>
                    <a href="<?= site_url('admin/laporan/cuti') ?>" title="Laporan cuti pegawai" class="<?= $subLink ?> <?= $laporanCuti ? $subActive : $subIdle ?>">Cuti Pegawai</a>
                </div>
            </details>
        <?php endif ?>

        <?php if (canAccess('panel')) : ?>
            <details class="group mb-1" <?= $panelOpen ? 'open' : '' ?>>
                <summary class="<?= $linkBase ?> cursor-pointer list-none text-gray-700 marker:hidden hover:bg-gray-100 [&::-webkit-details-marker]:hidden">
                    <i class="fa-solid fa-user-shield w-5 shrink-0 text-center text-gray-500"></i>
                    <span class="sidebar-text flex-1 text-left">Akses Pengguna</span>
                    <i class="fa-solid fa-chevron-down text-[10px] text-gray-400 transition group-open:rotate-180"></i>
                </summary>
                <div class="<?= $subWrap ?>">
                    <a href="<?= site_url('admin/panel/users') ?>" title="Daftar pengguna admin" class="<?= $subLink ?> <?= $panelUserList && ! $panelUserCreate ? $subActive : $subIdle ?>">Daftar Pengguna</a>
                    <a href="<?= site_url('admin/panel/users/create') ?>" title="Tambah pengguna admin" class="<?= $subLink ?> <?= $panelUserCreate ? $subActive : $subIdle ?>">Tambah Pengguna</a>
                    <a href="<?= site_url('admin/panel/groups') ?>" title="Grup akses" class="<?= $subLink ?> <?= ($panelGroupsIndex || $panelGroupCreate || $panelGroupEdit) && ! $panelGroupCreate && ! $panelGroupEdit ? $subActive : $subIdle ?>">Kelola Grup</a>
                    <a href="<?= site_url('admin/panel/groups/create') ?>" title="Tambah grup" class="<?= $subLink ?> <?= $panelGroupCreate ? $subActive : $subIdle ?>">Tambah Grup</a>
                </div>
            </details>
        <?php endif ?>

        <?php if (canAccess('utilitas')) : ?>
            <details class="group mb-1" <?= $utilOpen ? 'open' : '' ?>>
                <summary class="<?= $linkBase ?> cursor-pointer list-none text-gray-700 marker:hidden hover:bg-gray-100 [&::-webkit-details-marker]:hidden">
                    <i class="fa-solid fa-gears w-5 shrink-0 text-center text-gray-500"></i>
                    <span class="sidebar-text flex-1 text-left">Utilitas</span>
                    <i class="fa-solid fa-chevron-down text-[10px] text-gray-400 transition group-open:rotate-180"></i>
                </summary>
                <div class="<?= $subWrap ?>">
                    <a href="<?= site_url('admin/util/backup') ?>" title="Backup database" class="<?= $subLink ?> <?= str_starts_with($u, 'admin/util/backup') ? $subActive : $subIdle ?>">Daftar File Backup</a>
                </div>
            </details>
        <?php endif ?>

        <?php
        $apk = env('ANDROID_APP_APK_URL', 'https://bij.mwp.co.id/assets/uploads/bij_apps_v_1_1_0.apk');
        ?>
        <div class="mt-4 border-t border-gray-200 pt-4">
            <p class="nav-section-label mb-2 px-3 text-[11px] font-semibold uppercase tracking-wider text-gray-400">Tautan</p>
            <?php if (is_string($apk) && $apk !== '' && canAccess('apk_link')) : ?>
                <a href="<?= esc($apk) ?>" target="_blank" rel="noopener noreferrer" title="Unduh aplikasi Android" class="<?= $linkBase ?> text-emerald-700 hover:bg-emerald-50">
                    <i class="fa-brands fa-android w-5 shrink-0 text-center"></i>
                    <span class="sidebar-text">Aplikasi Android</span>
                </a>
            <?php endif ?>
            <?php if (session()->get('admin_mobile_token')) : ?>
                <p class="sidebar-text mb-1 truncate px-3 text-xs text-gray-500"><?= esc(session('admin_username') ?? '') ?></p>
                <a href="<?= site_url('admin/logout') ?>" title="Keluar dari panel" class="<?= $linkBase ?> text-red-600 hover:bg-red-50">
                    <i class="fa-solid fa-right-from-bracket w-5 shrink-0 text-center"></i>
                    <span class="sidebar-text">Sign Out</span>
                </a>
            <?php else : ?>
                <a href="<?= site_url('admin/login') ?>" title="Masuk ke panel admin" class="<?= $linkBase ?> border border-gray-200 bg-gray-50 text-gray-900 hover:bg-white">
                    <i class="fa-solid fa-key w-5 shrink-0 text-center text-gray-500"></i>
                    <span class="sidebar-text">Login</span>
                </a>
            <?php endif ?>
        </div>
    </nav>
</aside>
