<?php
// includes/sidebar.php
$current_page = basename($_SERVER['PHP_SELF']);
$current_module = $_GET['module'] ?? 'dashboard';
$current_page_param = $_GET['page'] ?? '';
$current_unit_id = (int)($_GET['unit_id'] ?? 0);

$current_year = isset($_GET['tahun']) ? (int)$_GET['tahun'] : 2026;
$current_month = isset($_GET['bulan']) ? (int)$_GET['bulan'] : 4;

$userRole = $_SESSION['user_role'] ?? 'viewer';

$laporanBase = 'index.php?module=laporan_bulanan&page=unit_form&bulan=' . $current_month . '&tahun=' . $current_year;

$rajalLinks = [
    ['unit_id' => 1,  'label' => 'IGD'],
    ['unit_id' => 3,  'label' => 'HD / Hemodialisa'],
    ['unit_id' => 4,  'label' => 'Poliklinik'],
    ['unit_id' => 22, 'label' => 'OK / Kamar Operasi'],
    // Tambahkan jika ID VK/Kamar Bersalin sudah pasti:
    // ['unit_id' => XX, 'label' => 'VK / Kamar Bersalin'],
];

$ranapLinks = [
    ['unit_id' => 6,  'label' => 'Pav. Canigara'],
    ['unit_id' => 7,  'label' => 'Pav. Cempaka'],
    ['unit_id' => 8,  'label' => 'Pav. Cendana'],
    ['unit_id' => 9,  'label' => 'Ruang Bayi'],
    ['unit_id' => 10, 'label' => 'NICU'],
    ['unit_id' => 11, 'label' => 'ICU'],
];

$menus = [
    ['module' => 'dashboard', 'icon' => 'fa-chart-pie', 'label' => 'Dashboard', 'roles' => ['admin', 'editor', 'viewer']],

    ['header' => 'Master Data'],
    ['module' => 'manage_user', 'icon' => 'fa-users-cog', 'label' => 'Manage User', 'roles' => ['admin']],
    ['module' => 'data_perawat', 'icon' => 'fa-user-nurse', 'label' => 'Data Perawat', 'roles' => ['admin', 'editor', 'viewer']],
    ['module' => 'data_perawat_per_unit', 'icon' => 'fa-users', 'label' => 'Data Perawat per Unit', 'roles' => ['admin', 'editor']],
    ['module' => 'data_staf_baru', 'icon' => 'fa-user-plus', 'label' => 'Data Staf Baru', 'roles' => ['admin', 'editor']],
    ['module' => 'jadwal_orientasi_staf_baru', 'icon' => 'fa-calendar-week', 'label' => 'Jadwal Orientasi Staf Baru', 'roles' => ['admin', 'editor']],
    ['module' => 'rotasi_perawat', 'icon' => 'fa-exchange-alt', 'label' => 'Rotasi / Resign', 'roles' => ['admin', 'editor']],
    ['module' => 'beban_kerja', 'icon' => 'fa-tasks', 'label' => 'Beban Kerja', 'roles' => ['admin', 'editor']],

    ['header' => 'Laporan'],
    ['module' => 'laporan_bulanan', 'icon' => 'fa-file-alt', 'label' => 'Laporan Bulanan', 'roles' => ['admin', 'kepala_unit', 'kasubdiv', 'kadiv', 'viewer']],
    ['module' => 'permasalahan_tindak_lanjut', 'icon' => 'fa-triangle-exclamation', 'label' => 'Permasalahan & TL', 'roles' => ['admin', 'editor', 'kepala_unit', 'kasubdiv', 'kadiv', 'viewer']],
    ['module' => 'kegiatan_harian', 'icon' => 'fa-clock', 'label' => 'Kegiatan Harian', 'roles' => ['admin', 'editor', 'viewer']],
    ['module' => 'patient_safety', 'icon' => 'fa-shield-alt', 'label' => 'Patient Safety', 'roles' => ['admin', 'editor']],
    ['module' => 'laporan_rka', 'icon' => 'fa-chart-line', 'label' => 'Laporan RKA', 'roles' => ['admin', 'editor']],
    ['module' => 'laporan_katim', 'icon' => 'fa-clipboard-list', 'label' => 'Laporan Katim', 'roles' => ['admin', 'editor']],

    ['header' => 'Analisis Pelayanan'],
    ['module' => 'analisis_kunjungan', 'icon' => 'fa-chart-line', 'label' => 'Analisis Kunjungan', 'roles' => ['admin']],
    ['module' => 'bpjs_margin_operasi', 'icon' => 'fa-chart-pie', 'label' => 'Margin Operasi BPJS', 'roles' => ['admin', 'superadmin']],

    ['header' => 'Utilities'],
    ['module' => 'daftar_spo', 'icon' => 'fa-file-alt', 'label' => 'Daftar SPO', 'roles' => ['admin', 'editor', 'viewer']],
    ['module' => 'generator_surat', 'icon' => 'fa-file-signature', 'label' => 'Generator Surat', 'roles' => ['admin', 'editor']],
    ['module' => 'distribusi_alat', 'icon' => 'fa-screwdriver-wrench', 'label' => 'WO Kerusakan & Barang', 'roles' => ['superadmin', 'admin', 'editor', 'viewer', 'kepala_unit', 'kasubdiv', 'kadiv']],
    ['module' => 'peminjaman_alat', 'icon' => 'fa-handshake-angle', 'label' => 'Peminjaman Alat', 'roles' => ['superadmin', 'admin', 'editor', 'viewer', 'kepala_unit', 'kasubdiv', 'kadiv']],
    ['module' => 'p3k', 'icon' => 'fa-first-aid', 'label' => 'Penugasan P3K', 'roles' => ['superadmin', 'admin', 'editor', 'viewer', 'kepala_unit', 'kasubdiv', 'kadiv']],
    
   ['module' => 'rdk', 'icon' => 'fa-stethoscope', 'label' => 'RDK / Refleksi Kasus', 'roles' => ['admin', 'editor', 'viewer', 'kepala_unit', 'kasubdiv', 'kadiv']],
];
?>
<aside class="sidebar fixed top-0 left-0 h-screen w-64 text-white shadow-2xl overflow-y-auto z-20">
    <div class="p-6 border-b border-gray-700">
        <img src="<?= LOGO_URL ?>" alt="BaliMed" class="h-10 mx-auto mb-2 brightness-0 invert">
        <h2 class="text-lg font-bold text-center">Keperawatan</h2>
        <p class="text-xs text-gray-400 text-center">RS BaliMed Denpasar</p>
    </div>

    <nav class="p-4">
        <ul class="space-y-1">
            <?php foreach ($menus as $menu): ?>
                <?php if (isset($menu['header'])): ?>
                    <li class="text-xs text-gray-400 uppercase tracking-wider px-4 py-3 mt-2">
                        <?= $menu['header'] ?>
                    </li>
                <?php else: ?>
                    <?php if (in_array($userRole, $menu['roles'], true)): ?>
                        <li>
                            <a href="index.php?module=<?= $menu['module'] ?>"
                               class="nav-item flex items-center space-x-3 px-4 py-3 rounded-lg text-gray-300 hover:text-white transition-all <?= ($current_module == $menu['module']) ? 'active bg-gradient-to-r from-blue-600 to-indigo-600 text-white' : '' ?>">
                                <i class="fas <?= $menu['icon'] ?> w-5"></i>
                                <span class="text-sm font-medium"><?= $menu['label'] ?></span>
                            </a>
                        </li>
                    <?php endif; ?>
                <?php endif; ?>
            <?php endforeach; ?>

            <?php if ($userRole === 'admin'): ?>
                <li class="text-xs text-gray-400 uppercase tracking-wider px-4 py-3 mt-2">
                    Shortcut Laporan Unit
                </li>

                <li>
                    <button type="button"
                            onclick="toggleSubmenu('submenu-rajal','icon-rajal')"
                            class="w-full nav-item flex items-center justify-between px-4 py-3 rounded-lg text-gray-300 hover:text-white transition-all">
                        <span class="flex items-center space-x-3">
                            <i class="fas fa-notes-medical w-5"></i>
                            <span class="text-sm font-medium">Unit Rajal</span>
                        </span>
                        <i id="icon-rajal" class="fas fa-chevron-down text-xs transition-transform"></i>
                    </button>

                    <ul id="submenu-rajal" class="hidden ml-6 mt-1 space-y-1">
                        <?php foreach ($rajalLinks as $item): ?>
                            <?php
                            $url = $laporanBase . '&unit_id=' . (int)$item['unit_id'];
                            $isActive = ($current_module === 'laporan_bulanan'
                                && $current_page_param === 'unit_form'
                                && $current_unit_id === (int)$item['unit_id']);
                            ?>
                            <li>
                                <a href="<?= e($url) ?>"
                                   class="block px-4 py-2 rounded-lg text-sm transition-all <?= $isActive ? 'bg-blue-600 text-white' : 'text-gray-300 hover:bg-slate-700 hover:text-white' ?>">
                                    <?= e($item['label']) ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </li>

                <li>
                    <button type="button"
                            onclick="toggleSubmenu('submenu-ranap','icon-ranap')"
                            class="w-full nav-item flex items-center justify-between px-4 py-3 rounded-lg text-gray-300 hover:text-white transition-all">
                        <span class="flex items-center space-x-3">
                            <i class="fas fa-bed w-5"></i>
                            <span class="text-sm font-medium">Unit Ranap</span>
                        </span>
                        <i id="icon-ranap" class="fas fa-chevron-down text-xs transition-transform"></i>
                    </button>

                    <ul id="submenu-ranap" class="hidden ml-6 mt-1 space-y-1">
                        <?php foreach ($ranapLinks as $item): ?>
                            <?php
                            $url = $laporanBase . '&unit_id=' . (int)$item['unit_id'];
                            $isActive = ($current_module === 'laporan_bulanan'
                                && $current_page_param === 'unit_form'
                                && $current_unit_id === (int)$item['unit_id']);
                            ?>
                            <li>
                                <a href="<?= e($url) ?>"
                                   class="block px-4 py-2 rounded-lg text-sm transition-all <?= $isActive ? 'bg-blue-600 text-white' : 'text-gray-300 hover:bg-slate-700 hover:text-white' ?>">
                                    <?= e($item['label']) ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </li>
            <?php endif; ?>

            <li class="pt-4 mt-4 border-t border-gray-700">
                <a href="logout.php" class="nav-item flex items-center space-x-3 px-4 py-3 rounded-lg text-gray-300 hover:text-white hover:bg-red-600 transition-all">
                    <i class="fas fa-sign-out-alt w-5"></i>
                    <span class="text-sm font-medium">Logout</span>
                </a>
            </li>
        </ul>
    </nav>
</aside>

<script>
function toggleSubmenu(submenuId, iconId) {
    const submenu = document.getElementById(submenuId);
    const icon = document.getElementById(iconId);
    if (!submenu || !icon) return;

    submenu.classList.toggle('hidden');
    icon.classList.toggle('rotate-180');
}

// Auto-open submenu if current page is unit_form
document.addEventListener('DOMContentLoaded', function () {
    const currentModule = '<?= e($current_module) ?>';
    const currentPage = '<?= e($current_page_param) ?>';
    const currentUnitId = <?= (int)$current_unit_id ?>;

    if (currentModule === 'laporan_bulanan' && currentPage === 'unit_form') {
        const rajalIds = [1, 3, 4, 22];
        const ranapIds = [6, 7, 8, 9, 10, 11];

        if (rajalIds.includes(currentUnitId)) {
            document.getElementById('submenu-rajal')?.classList.remove('hidden');
            document.getElementById('icon-rajal')?.classList.add('rotate-180');
        }

        if (ranapIds.includes(currentUnitId)) {
            document.getElementById('submenu-ranap')?.classList.remove('hidden');
            document.getElementById('icon-ranap')?.classList.add('rotate-180');
        }
    }
});
</script>