<?php
if (session_status() === PHP_SESSION_NONE) session_start();

$currentFile = basename($_SERVER['PHP_SELF'] ?? 'index.php');
$scriptDir = str_replace('\\', '/', dirname($_SERVER['PHP_SELF'] ?? ''));
$inModules = basename($scriptDir) === 'modules';
$base = $inModules ? '..' : '.';
$userName = isset($_SESSION['name']) ? esc($_SESSION['name']) : 'Admin Desa';

$pageMap = [
    'index.php' => ['Dashboard', 'ri-dashboard-fill'],
    'infografis.php' => ['Data Penduduk', 'ri-group-fill'],
    'kartu_keluarga.php' => ['Data Kartu Keluarga', 'ri-booklet-fill'],
    'statistik_kependudukan.php' => ['Statistik Kependudukan', 'ri-bar-chart-2-fill'],
    'wilayah.php' => ['Wilayah (Dusun/RT/RW)', 'ri-map-pin-2-fill'],
    'mutasi_penduduk.php' => ['Mutasi Penduduk', 'ri-user-shared-fill'],
    'pengajuan_menu.php' => ['Pengajuan Surat', 'ri-inbox-archive-fill'],
    'pengajuan_surat_menu.php' => ['Administrasi Surat', 'ri-file-list-3-fill'],
    'pengajuan_surat_list.php' => ['Daftar Pengajuan Surat', 'ri-list-check-3'],
    'pengajuan_list.php' => ['Pengajuan Penghasilan Orang Tua', 'ri-money-dollar-circle-fill'],
    'pengajuan_list_usaha.php' => ['Pengajuan Surat Usaha', 'ri-store-2-fill'],
    'pengajuan_list_tidak_mampu.php' => ['Pengajuan Surat Tidak Mampu', 'ri-hand-coin-fill'],
    'pengajuan_list_domisili.php' => ['Pengajuan Surat Domisili', 'ri-map-pin-2-fill'],
    'pengajuan_list_hilang.php' => ['Pengajuan Surat Hilang', 'ri-error-warning-fill'],
    'pengajuan_list_pindah.php' => ['Pengajuan Surat Pindah', 'ri-truck-fill'],
    'pengajuan_list_ijin_keramaian.php' => ['Pengajuan Izin Keramaian', 'ri-megaphone-fill'],
    'pengajuan_list_merantau.php' => ['Pengajuan Surat Merantau', 'ri-suitcase-3-fill'],
    'pengajuan_list_kuasa.php' => ['Pengajuan Surat Kuasa', 'ri-file-shield-2-fill'],
    'pengajuan_list_pengantar_kayu.php' => ['Pengajuan Pengantar Kayu', 'ri-tree-fill'],
    'surat_keterangan.php' => ['Administrasi Surat', 'ri-file-text-fill'],
    'surat_tidak_mampu.php' => ['Surat Tidak Mampu', 'ri-hand-coin-fill'],
    'surat_domisili.php' => ['Surat Domisili', 'ri-map-pin-2-fill'],
    'surat_hilang.php' => ['Surat Hilang', 'ri-error-warning-fill'],
    'surat_ijin_keramaian.php' => ['Surat Izin Keramaian', 'ri-megaphone-fill'],
    'surat_pindah.php' => ['Surat Pindah', 'ri-truck-fill'],
    'surat_merantau.php' => ['Surat Merantau', 'ri-suitcase-3-fill'],
    'surat_usaha.php' => ['Surat Usaha', 'ri-store-2-fill'],
    'surat_kuasa.php' => ['Surat Kuasa', 'ri-file-shield-2-fill'],
    'surat_pengantar_kayu.php' => ['Surat Pengantar Kayu', 'ri-tree-fill'],
    'surat_penghasilan.php' => ['Surat Penghasilan', 'ri-money-dollar-box-fill'],
    'surat_penghasilan_ortu.php' => ['Surat Penghasilan Orang Tua', 'ri-money-dollar-circle-fill'],
    'surat_undangan.php' => ['Surat Undangan', 'ri-mail-send-fill'],
    'surat_ahli_waris.php' => ['Surat Ahli Waris', 'ri-group-2-fill'],
    'surat_gadai.php' => ['Surat Gadai', 'ri-exchange-dollar-fill'],
    'surat_belum_menikah.php' => ['Surat Belum Menikah', 'ri-heart-2-fill'],
    'surat_belum_bekerja.php' => ['Surat Belum Bekerja', 'ri-briefcase-4-fill'],
    'surat_pengantar_hewan.php' => ['Surat Pengantar Hewan', 'ri-bear-smile-fill'],
    'surat_kelakuan_baik.php' => ['Surat Kelakuan Baik', 'ri-shield-check-fill'],
    'surat_nikah.php' => ['Surat Nikah', 'ri-user-heart-fill'],
    'surat_pernyataan_kesanggupan.php' => ['Surat Pernyataan Kesanggupan', 'ri-file-text-fill'],
    'surat_pembagian_waris.php' => ['Surat Pembagian Waris', 'ri-scales-3-fill'],
    'profil.php' => ['Profil Desa', 'ri-government-fill'],
    'informasi.php' => ['Informasi Desa', 'ri-information-fill'],
    'sotk.php' => ['SOTK Desa', 'ri-organization-chart'],
    'lembaga_desa.php' => ['Lembaga Desa', 'ri-community-fill'],
    'lembaga.php' => ['Lembaga', 'ri-community-fill'],
    'apbdes.php' => ['APBDes', 'ri-bar-chart-grouped-fill'],
    'idm.php' => ['IDM Desa', 'ri-line-chart-fill'],
    'ppid.php' => ['PPID', 'ri-folder-info-fill'],
    'admin_layanan_mandiri.php' => ['Layanan Mandiri', 'ri-customer-service-2-fill'],
    'berita.php' => ['Berita', 'ri-newspaper-fill'],
    'galeri.php' => ['Galeri', 'ri-image-2-fill'],
    'pengumuman.php' => ['Pengumuman', 'ri-notification-3-fill'],
    'pustaka.php' => ['Pustaka Desa', 'ri-book-2-fill'],
    'potensi.php' => ['Potensi Desa', 'ri-lightbulb-flash-fill'],
    'bumdes.php' => ['BUMDes', 'ri-store-2-fill'],
    'umkm.php' => ['UMKM Desa', 'ri-shopping-bag-3-fill'],
    'kopdes.php' => ['KOPDES', 'ri-building-4-fill'],
    'bantuan.php' => ['Bantuan Sosial', 'ri-gift-2-fill'],
    'pengaduan_chat.php' => ['Pengaduan', 'ri-chat-3-fill'],
];
[$pageTitle, $pageIcon] = $pageMap[$currentFile] ?? ['Admin Desa Mambu', 'ri-apps-2-fill'];

if (!function_exists('nav_active')) {
    function nav_active(array $files, string $currentFile): string { return in_array($currentFile, $files, true) ? ' active' : ''; }
}
?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= esc($pageTitle) ?> · Admin Desa Mambu</title>

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/remixicon@4.6.0/fonts/remixicon.css" rel="stylesheet">

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>

<link rel="stylesheet" href="<?= $base ?>/assets/css/admin.css?v=20260905d">
<script defer src="<?= $base ?>/assets/js/admin.js?v=20260905d"></script>
</head>
<body>
<div class="admin-shell">
    <aside class="app-sidebar" id="adminSidebar" aria-label="Navigasi admin">
        <div class="sidebar-brand">
            <img class="sidebar-crest" src="<?= $base ?>/modules/assets/img/logo.png" alt="Logo Polewali Mandar">
            <div>
                <strong>PEMERINTAH<br>DESA MAMBU</strong>
                <span>Kec. Luyo, Kab. Polewali Mandar</span>
            </div>
        </div>

        <nav class="sidebar-nav">
            <div class="side-section">Utama</div>
            <a class="side-link<?= nav_active(['index.php'], $currentFile) ?>" href="<?= $base ?>/index.php">
                <i class="ri-dashboard-fill"></i><span>Dashboard</span>
            </a>

            <div class="side-section">Data Desa</div>
            <a class="side-link<?= nav_active(['infografis.php'], $currentFile) ?>" href="<?= $base ?>/modules/infografis.php">
                <i class="ri-bar-chart-box-fill"></i><span>Infografis</span>
            </a>
            <a class="side-link<?= nav_active(['kartu_keluarga.php'], $currentFile) ?>" href="<?= $base ?>/modules/kartu_keluarga.php">
                <i class="ri-booklet-fill"></i><span>Data Kartu Keluarga</span>
            </a>
            <a class="side-link<?= nav_active(['mutasi_penduduk.php'], $currentFile) ?>" href="<?= $base ?>/modules/mutasi_penduduk.php">
                <i class="ri-user-shared-fill"></i><span>Mutasi Penduduk</span>
            </a>
            <a class="side-link<?= nav_active(['statistik_kependudukan.php'], $currentFile) ?>" href="<?= $base ?>/modules/statistik_kependudukan.php">
                <i class="ri-bar-chart-2-fill"></i><span>Statistik Kependudukan</span>
            </a>
            <a class="side-link<?= nav_active(['wilayah.php'], $currentFile) ?>" href="<?= $base ?>/modules/wilayah.php">
                <i class="ri-map-pin-2-fill"></i><span>Wilayah (Dusun/RT/RW)</span>
            </a>
            <a class="side-link<?= nav_active(['idm.php'], $currentFile) ?>" href="<?= $base ?>/modules/idm.php">
                <i class="ri-line-chart-fill"></i><span>IDM Desa</span>
            </a>
            <a class="side-link<?= nav_active(['informasi.php'], $currentFile) ?>" href="<?= $base ?>/modules/informasi.php">
                <i class="ri-home-4-fill"></i><span>Informasi Desa</span>
            </a>
            <a class="side-link<?= nav_active(['sotk.php'], $currentFile) ?>" href="<?= $base ?>/modules/sotk.php">
                <i class="ri-organization-chart"></i><span>SOTK</span>
            </a>
            <a class="side-link<?= nav_active(['lembaga_desa.php','lembaga.php'], $currentFile) ?>" href="<?= $base ?>/modules/lembaga_desa.php">
                <i class="ri-community-fill"></i><span>Lembaga Desa</span>
            </a>
            <a class="side-link<?= nav_active(['potensi.php'], $currentFile) ?>" href="<?= $base ?>/modules/potensi.php">
                <i class="ri-lightbulb-flash-fill"></i><span>Potensi Desa</span>
            </a>

            <div class="side-section">Konten Publik</div>
            <a class="side-link<?= nav_active(['berita.php'], $currentFile) ?>" href="<?= $base ?>/modules/berita.php">
                <i class="ri-newspaper-fill"></i><span>Berita</span>
            </a>
            <a class="side-link<?= nav_active(['galeri.php'], $currentFile) ?>" href="<?= $base ?>/modules/galeri.php">
                <i class="ri-image-2-fill"></i><span>Galeri</span>
            </a>
            <a class="side-link<?= nav_active(['pengumuman.php'], $currentFile) ?>" href="<?= $base ?>/modules/pengumuman.php">
                <i class="ri-notification-3-fill"></i><span>Pengumuman</span>
            </a>
            <a class="side-link<?= nav_active(['pustaka.php'], $currentFile) ?>" href="<?= $base ?>/modules/pustaka.php">
                <i class="ri-book-2-fill"></i><span>Pustaka Desa</span>
            </a>
            <a class="side-link<?= nav_active(['ppid.php'], $currentFile) ?>" href="<?= $base ?>/modules/ppid.php">
                <i class="ri-file-list-3-fill"></i><span>PPID</span>
            </a>

            <div class="side-section">Keuangan & Ekonomi</div>
            <a class="side-link<?= nav_active(['apbdes.php'], $currentFile) ?>" href="<?= $base ?>/modules/apbdes.php">
                <i class="ri-money-dollar-circle-fill"></i><span>APBDes</span>
            </a>
            <a class="side-link<?= nav_active(['bumdes.php'], $currentFile) ?>" href="<?= $base ?>/modules/bumdes.php">
                <i class="ri-store-2-fill"></i><span>BUMDes</span>
            </a>
            <a class="side-link<?= nav_active(['umkm.php'], $currentFile) ?>" href="<?= $base ?>/modules/umkm.php">
                <i class="ri-shopping-bag-3-fill"></i><span>UMKM Desa</span>
            </a>
            <a class="side-link<?= nav_active(['kopdes.php'], $currentFile) ?>" href="<?= $base ?>/modules/kopdes.php">
                <i class="ri-building-4-fill"></i><span>KOPDES Merah Putih</span>
            </a>
            <a class="side-link<?= nav_active(['bantuan.php'], $currentFile) ?>" href="<?= $base ?>/modules/bantuan.php">
                <i class="ri-gift-2-fill"></i><span>Bantuan Sosial</span>
            </a>

            <div class="side-section">Pelayanan Desa</div>
            <a class="side-link<?= nav_active(['pengajuan_menu.php','pengajuan_surat_menu.php','pengajuan_surat_list.php','pengajuan_list.php','pengajuan_list_usaha.php','pengajuan_list_tidak_mampu.php','pengajuan_list_domisili.php','pengajuan_list_hilang.php','pengajuan_list_pindah.php','pengajuan_list_ijin_keramaian.php','pengajuan_list_merantau.php','pengajuan_list_kuasa.php','pengajuan_list_pengantar_kayu.php'], $currentFile) ?>" href="<?= $base ?>/modules/pengajuan_menu.php">
                <i class="ri-inbox-archive-fill"></i><span>Pengajuan Surat</span>
            </a>
            <a class="side-link<?= nav_active(['surat_keterangan.php','surat_tidak_mampu.php','surat_domisili.php','surat_hilang.php','surat_ijin_keramaian.php','surat_pindah.php','surat_merantau.php','surat_usaha.php','surat_kuasa.php','surat_pengantar_kayu.php','surat_penghasilan.php','surat_penghasilan_ortu.php','surat_undangan.php','surat_ahli_waris.php','surat_gadai.php','surat_belum_menikah.php','surat_belum_bekerja.php','surat_pengantar_hewan.php','surat_kelakuan_baik.php','surat_nikah.php','surat_pernyataan_kesanggupan.php','surat_pembagian_waris.php'], $currentFile) ?>" href="<?= $base ?>/modules/surat_keterangan.php">
                <i class="ri-file-text-fill"></i><span>Surat Desa</span>
            </a>
            <a class="side-link<?= nav_active(['admin_layanan_mandiri.php'], $currentFile) ?>" href="<?= $base ?>/modules/admin_layanan_mandiri.php">
                <i class="ri-customer-service-2-fill"></i><span>Layanan Mandiri</span>
            </a>
            <a class="side-link<?= nav_active(['pengaduan_chat.php'], $currentFile) ?>" href="<?= $base ?>/modules/pengaduan_chat.php">
                <i class="ri-chat-3-fill"></i><span>Pengaduan</span>
            </a>
        </nav>

        <div class="sidebar-motto">
            <em>“Data Akurat<br>Desa Maju<br>Masyarakat Sejahtera”</em>
            <span>✧ v1.0.0</span>
            <small>© <?= date('Y') ?> Desa Mambu<br>Sistem Informasi Kependudukan</small>
        </div>
    </aside>

    <div class="sidebar-overlay" id="sidebarOverlay"></div>

    <section class="app-main">
        <header class="app-topbar">
            <button class="topbar-menu" id="adminMenuToggle" type="button" aria-label="Buka menu">
                <i class="ri-menu-2-line"></i>
            </button>

            <form class="top-search" id="globalSearchForm" action="<?= $base ?>/modules/infografis.php" method="get">
                <i class="ri-search-line"></i>
                <input type="hidden" name="a" value="list">
                <input name="q_nama" type="search" placeholder="Cari data penduduk, NIK, nama, atau nomor KK...">
                <button type="submit" aria-label="Cari"><i class="ri-search-line"></i></button>
            </form>

            <div class="topbar-actions">
                <button class="icon-btn" type="button" aria-label="Notifikasi">
                    <i class="ri-notification-3-line"></i><span class="notif-dot">3</span>
                </button>
                <button class="icon-btn" type="button" aria-label="Pesan">
                    <i class="ri-message-3-line"></i>
                </button>
                <div class="topbar-user">
                    <span class="user-avatar"><i class="ri-user-3-fill"></i></span>
                    <span><strong><?= esc($userName) ?></strong><small>Operator</small></span>
                    <i class="ri-arrow-down-s-line"></i>
                </div>
                <a class="logout-compact" href="<?= $base ?>/logout.php" title="Keluar"><i class="ri-logout-box-r-line"></i></a>
            </div>
        </header>

        <div class="content-wrap">
            <div class="content-meta">
                <div class="breadcrumb"><a href="<?= $base ?>/index.php"><i class="ri-home-5-fill"></i> Dashboard</a><i class="ri-arrow-right-s-line"></i><span><?= esc($pageTitle) ?></span></div>
                <div class="date-badge"><i class="ri-calendar-2-line"></i><span id="adminDateText">Memuat tanggal...</span></div>
            </div>

            <main class="admin-content" data-page="<?= esc($currentFile) ?>">
