<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>User Management - Admin Panel</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&amp;display=swap" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&amp;family=Plus+Jakarta+Sans:wght@600;700&amp;display=swap" rel="stylesheet"/>
<script id="tailwind-config">
        tailwind.config = {
          darkMode: "class",
          theme: {
            extend: {
              colors: {
                  primary: "#006948",
                  "primary-container": "#00855d",
                  "on-primary": "#ffffff",
                  "surface": "#F8FAFC",
                  "on-surface": "#0F172A",
                  "surface-container": "#F1F5F9",
                  "surface-container-highest": "#E2E8F0",
                  "surface-container-lowest": "#ffffff",
                  "outline-variant": "#CBD5E1"
              }
            }
          }
        }
</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 & Admin</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="/<?= ($user['role'] == 'admin') ? 'admin' : '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 bg-primary text-on-primary rounded-lg mx-2 transition-all opacity-80" href="/users">
                    <span class="material-symbols-outlined">manage_accounts</span>
                    <span>User Management</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">
    <!-- TopNavBar (Mobile Only) -->
    <header class="md:hidden flex justify-between items-center p-4 bg-surface-container-lowest border-b border-outline-variant">
        <h1 class="text-xl font-bold text-primary">User Management</h1>
        <a href="/<?= ($user['role'] == 'admin') ? 'admin' : 'kesantrian' ?>" class="text-gray-600">
            <span class="material-symbols-outlined">arrow_back</span>
        </a>
    </header>

    <div class="pt-8 px-6 pb-24 w-full mx-auto">
        <?php if (session()->getFlashdata('message')): ?>
            <div class="mb-4 bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative">
                <?= session()->getFlashdata('message') ?>
            </div>
        <?php endif; ?>

        <div class="flex justify-between items-center mb-8">
            <div>
                <h2 class="text-3xl font-bold text-on-surface">User Management</h2>
                <p class="text-gray-600 mt-1">Manage all accounts in the system.</p>
            </div>
            <button onclick="document.getElementById('modal-create').classList.remove('hidden')" class="bg-primary text-white py-2 px-4 rounded-lg font-medium hover:bg-primary-container transition-colors flex items-center gap-2">
                <span class="material-symbols-outlined text-[20px]">add</span> Add User
            </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-surface-container text-gray-700 font-medium">
                            <th class="p-4 border-b border-gray-200">Nama Lengkap</th>
                            <th class="p-4 border-b border-gray-200">Username</th>
                            <th class="p-4 border-b border-gray-200">Role</th>
                            <th class="p-4 border-b border-gray-200">No. HP</th>
                            <th class="p-4 border-b border-gray-200">Aksi</th>
                        </tr>
                    </thead>
                    <tbody class="text-gray-800">
                        <?php foreach($users as $u): ?>
                        <tr class="border-b border-gray-100 hover:bg-gray-50 transition-colors">
                            <td class="p-4 font-semibold"><?= esc($u['nama_lengkap']) ?></td>
                            <td class="p-4"><?= esc($u['username']) ?></td>
                            <td class="p-4">
                                <span class="px-2 py-1 bg-gray-200 text-gray-700 rounded-full text-xs font-semibold">
                                    <?= ucfirst($u['role']) ?>
                                </span>
                            </td>
                            <td class="p-4 text-gray-500"><?= esc($u['no_hp'] ?? '-') ?></td>
                            <td class="p-4">
                                <div class="flex items-center gap-2">
                                    <button onclick="openEditModal(<?= htmlspecialchars(json_encode($u), ENT_QUOTES, 'UTF-8') ?>)" class="text-blue-600 hover:bg-blue-50 p-1 rounded transition-colors"><span class="material-symbols-outlined text-[20px]">edit</span></button>
                                    <a href="/users/delete/<?= $u['id'] ?>" onclick="return confirm('Hapus user ini?')" class="text-red-600 hover:bg-red-50 p-1 rounded transition-colors"><span class="material-symbols-outlined text-[20px]">delete</span></a>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</main>

<!-- Create Modal -->
<div id="modal-create" class="fixed inset-0 bg-black/50 hidden z-[100] flex items-center justify-center p-4">
    <div class="bg-white rounded-xl shadow-lg w-full max-w-md p-6">
        <h3 class="text-xl font-bold mb-4">Tambah User Baru</h3>
        <form action="/users/store" method="post" class="flex flex-col gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Nama Lengkap</label>
                <input type="text" name="nama_lengkap" required class="w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/20">
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Username</label>
                <input type="text" name="username" required class="w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/20">
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Password</label>
                <input type="password" name="password" required class="w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/20">
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Role</label>
                <select name="role" required class="w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/20">
                    <option value="satpam">Satpam</option>
                    <option value="kesantrian">Kesantrian</option>
                    <option value="admin">Admin</option>
                    <option value="wali">Wali Santri</option>
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">No. HP (Opsional)</label>
                <input type="text" name="no_hp" class="w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/20">
            </div>
            <div class="flex justify-end gap-3 mt-4">
                <button type="button" onclick="document.getElementById('modal-create').classList.add('hidden')" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50">Batal</button>
                <button type="submit" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary-container">Simpan</button>
            </div>
        </form>
    </div>
</div>

<!-- Edit Modal -->
<div id="modal-edit" class="fixed inset-0 bg-black/50 hidden z-[100] flex items-center justify-center p-4">
    <div class="bg-white rounded-xl shadow-lg w-full max-w-md p-6">
        <h3 class="text-xl font-bold mb-4">Edit User</h3>
        <form id="form-edit" method="post" class="flex flex-col gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Nama Lengkap</label>
                <input type="text" name="nama_lengkap" id="edit_nama" required class="w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/20">
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Username</label>
                <input type="text" name="username" id="edit_username" required class="w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/20">
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Password Baru (Biarkan kosong jika tidak diubah)</label>
                <input type="password" name="password" class="w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/20">
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Role</label>
                <select name="role" id="edit_role" required class="w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/20">
                    <option value="satpam">Satpam</option>
                    <option value="kesantrian">Kesantrian</option>
                    <option value="admin">Admin</option>
                    <option value="wali">Wali Santri</option>
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">No. HP (Opsional)</label>
                <input type="text" name="no_hp" id="edit_nohp" class="w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/20">
            </div>
            <div class="flex justify-end gap-3 mt-4">
                <button type="button" onclick="document.getElementById('modal-edit').classList.add('hidden')" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50">Batal</button>
                <button type="submit" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary-container">Simpan Perubahan</button>
            </div>
        </form>
    </div>
</div>

<script>
function openEditModal(user) {
    document.getElementById('form-edit').action = '/users/update/' + user.id;
    document.getElementById('edit_nama').value = user.nama_lengkap;
    document.getElementById('edit_username').value = user.username;
    document.getElementById('edit_role').value = user.role;
    document.getElementById('edit_nohp').value = user.no_hp || '';
    document.getElementById('modal-edit').classList.remove('hidden');
}
</script>
</body>
</html>
