<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
    <title>Kesantrian Admin - Student Data</title>
    <script src="https://cdn.tailwindcss.com?plugins=forms,container-queries"></script>
    <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:wght,FILL@100..700,0..1&display=swap" rel="stylesheet"/>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Plus+Jakarta+Sans:wght@600;700&display=swap" rel="stylesheet"/>
    <script id="tailwind-config">
        tailwind.config = {
          darkMode: "class",
          theme: {
            extend: {
              colors: {
                  primary: "#006948",
                  "primary-container": "#00855d",
                  "on-primary": "#ffffff",
                  "surface": "#f8f9ff",
                  "on-surface": "#0b1c30",
                  "surface-container": "#e5eeff",
                  "surface-container-highest": "#d3e4fe",
                  "surface-container-lowest": "#ffffff",
                  "outline-variant": "#bccac0"
              }
            }
          }
        }
    </script>
</head>
<body class="bg-surface text-on-surface antialiased flex">

<!-- SideNavBar -->
<nav class="hidden md:flex flex-col fixed left-0 top-0 h-full py-8 bg-surface-container border-r border-outline-variant w-72 z-50">
    <div class="px-6 mb-8 flex items-center gap-4">
        <div>
            <h1 class="text-2xl font-bold text-primary m-0">Admin Panel</h1>
            <p class="text-sm text-gray-600 m-0">Kesantrian Division</p>
        </div>
    </div>
    <div class="flex-1 overflow-y-auto">
        <ul class="space-y-2">
            <li>
                <a class="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-surface-container-highest transition-all rounded-lg mx-2" href="/kesantrian">
                    <span class="material-symbols-outlined">dashboard</span>
                    <span>Dashboard</span>
                </a>
            </li>
            <li>
                <a class="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-surface-container-highest transition-all rounded-lg mx-2" href="/kesantrian/permits">
                    <span class="material-symbols-outlined">assignment_turned_in</span>
                    <span>Permits</span>
                </a>
            </li>
            <li>
                <a class="flex items-center gap-3 px-4 py-3 bg-primary text-on-primary rounded-lg mx-2 transition-all opacity-80" href="/santri">
                    <span class="material-symbols-outlined">group</span>
                    <span>Student Data</span>
                </a>
            </li>
            <li>
                <a class="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-surface-container-highest transition-all rounded-lg mx-2" href="#">
                    <span class="material-symbols-outlined">manage_accounts</span>
                    <span>User Management</span>
                </a>
            </li>
            <li>
                <a class="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-surface-container-highest transition-all rounded-lg mx-2" href="#">
                    <span class="material-symbols-outlined">assessment</span>
                    <span>Reports</span>
                </a>
            </li>
        </ul>
    </div>
    <div class="mt-auto px-6">
        <a class="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-surface-container-highest transition-all rounded-lg mx-2" href="/auth/logout">
            <span class="material-symbols-outlined">logout</span>
            <span>Logout</span>
        </a>
    </div>
</nav>

<!-- Main Content Area -->
<main class="flex-1 md:ml-72 flex flex-col min-h-screen">
    <div class="pt-8 px-6 pb-24 w-full mx-auto">
        <div class="flex justify-between items-center mb-8">
            <div>
                <h2 class="text-3xl font-bold text-on-surface">Data Santri</h2>
                <p class="text-gray-600 mt-1">Kelola data master santri.</p>
            </div>
            <button onclick="document.getElementById('modalAdd').classList.remove('hidden')" class="bg-primary text-white px-4 py-2 rounded-lg flex items-center gap-2 hover:bg-primary-container">
                <span class="material-symbols-outlined">add</span> Tambah Santri
            </button>
        </div>

        <div class="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
            <div class="overflow-x-auto">
                <table class="w-full text-left border-collapse">
                    <thead>
                        <tr class="bg-gray-50 border-b border-gray-200">
                            <th class="p-4 font-semibold text-gray-700">NIS</th>
                            <th class="p-4 font-semibold text-gray-700">Nama Santri</th>
                            <th class="p-4 font-semibold text-gray-700">Kelas</th>
                            <th class="p-4 font-semibold text-gray-700">Asrama</th>
                            <th class="p-4 font-semibold text-gray-700 text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        <?php if(empty($santri)): ?>
                            <tr>
                                <td colspan="5" class="p-4 text-center text-gray-500">Belum ada data santri.</td>
                            </tr>
                        <?php else: ?>
                            <?php foreach($santri as $s): ?>
                            <tr class="hover:bg-gray-50">
                                <td class="p-4 text-sm text-gray-900"><?= esc($s['nis']) ?></td>
                                <td class="p-4 font-medium text-gray-900"><?= esc($s['nama_santri']) ?></td>
                                <td class="p-4 text-sm text-gray-600"><?= esc($s['kelas']) ?></td>
                                <td class="p-4 text-sm text-gray-600"><?= esc($s['asrama']) ?></td>
                                <td class="p-4 text-center">
                                    <button onclick="editSantri(<?= htmlspecialchars(json_encode($s)) ?>)" class="text-blue-600 hover:text-blue-800 mx-1">
                                        <span class="material-symbols-outlined text-[20px]">edit</span>
                                    </button>
                                    <a href="/santri/delete/<?= $s['id'] ?>" onclick="return confirm('Hapus data santri ini?')" class="text-red-600 hover:text-red-800 mx-1">
                                        <span class="material-symbols-outlined text-[20px]">delete</span>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</main>

<!-- Modal Add -->
<div id="modalAdd" class="hidden fixed inset-0 bg-black bg-opacity-50 z-[60] flex items-center justify-center">
    <div class="bg-white rounded-xl p-6 w-full max-w-md">
        <h3 class="text-xl font-bold mb-4">Tambah Santri</h3>
        <form action="/santri/store" method="post" class="flex flex-col gap-4">
            <div>
                <label class="block text-sm font-medium mb-1">NIS</label>
                <input type="text" name="nis" required class="w-full border border-gray-300 rounded p-2">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">Nama Lengkap</label>
                <input type="text" name="nama_santri" required class="w-full border border-gray-300 rounded p-2">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">Kelas</label>
                <input type="text" name="kelas" required class="w-full border border-gray-300 rounded p-2">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">Asrama</label>
                <input type="text" name="asrama" required class="w-full border border-gray-300 rounded p-2">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">Wali Santri (Akun)</label>
                <select name="wali_user_id" class="w-full border border-gray-300 rounded p-2">
                    <option value="">-- Tidak Terhubung --</option>
                    <?php foreach($waliUsers as $w): ?>
                        <option value="<?= $w['id'] ?>"><?= esc($w['nama_lengkap']) ?> (<?= esc($w['username']) ?>)</option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="flex justify-end gap-2 mt-4">
                <button type="button" onclick="document.getElementById('modalAdd').classList.add('hidden')" class="px-4 py-2 border rounded">Batal</button>
                <button type="submit" class="px-4 py-2 bg-primary text-white rounded">Simpan</button>
            </div>
        </form>
    </div>
</div>

<!-- Modal Edit -->
<div id="modalEdit" class="hidden fixed inset-0 bg-black bg-opacity-50 z-[60] flex items-center justify-center">
    <div class="bg-white rounded-xl p-6 w-full max-w-md">
        <h3 class="text-xl font-bold mb-4">Edit Santri</h3>
        <form id="formEdit" method="post" class="flex flex-col gap-4">
            <div>
                <label class="block text-sm font-medium mb-1">NIS</label>
                <input type="text" name="nis" id="edit_nis" required class="w-full border border-gray-300 rounded p-2">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">Nama Lengkap</label>
                <input type="text" name="nama_santri" id="edit_nama" required class="w-full border border-gray-300 rounded p-2">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">Kelas</label>
                <input type="text" name="kelas" id="edit_kelas" required class="w-full border border-gray-300 rounded p-2">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">Asrama</label>
                <input type="text" name="asrama" id="edit_asrama" required class="w-full border border-gray-300 rounded p-2">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">Wali Santri (Akun)</label>
                <select name="wali_user_id" id="edit_wali" class="w-full border border-gray-300 rounded p-2">
                    <option value="">-- Tidak Terhubung --</option>
                    <?php foreach($waliUsers as $w): ?>
                        <option value="<?= $w['id'] ?>"><?= esc($w['nama_lengkap']) ?> (<?= esc($w['username']) ?>)</option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="flex justify-end gap-2 mt-4">
                <button type="button" onclick="document.getElementById('modalEdit').classList.add('hidden')" class="px-4 py-2 border rounded">Batal</button>
                <button type="submit" class="px-4 py-2 bg-primary text-white rounded">Simpan</button>
            </div>
        </form>
    </div>
</div>

<script>
    function editSantri(santri) {
        document.getElementById('formEdit').action = '/santri/update/' + santri.id;
        document.getElementById('edit_nis').value = santri.nis;
        document.getElementById('edit_nama').value = santri.nama_santri;
        document.getElementById('edit_kelas').value = santri.kelas;
        document.getElementById('edit_asrama').value = santri.asrama;
        document.getElementById('edit_wali').value = santri.wali_user_id;
        document.getElementById('modalEdit').classList.remove('hidden');
    }
</script>
</body>
</html>
