<?php
// File: admin/setting.php (Diperbarui dengan SELECT Pangkat/Golongan dan Jabatan)

// Tentukan Judul dan Ikon Halaman untuk Topbar
$page_title = 'Pengaturan Aplikasi';
$page_icon = 'bi-gear-fill';

include '../config/database.php';
session_start();

// Cek autentikasi dan otorisasi (dipertahankan)
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header('Location: ../login.php');
    exit();
}

$action = $_POST['action'] ?? $_GET['action'] ?? '';

// =========================================================================================
// == FUNGSI CRUD TAHUN AJARAN (DIPERTAHANKAN) ==
// =========================================================================================
if ($action === 'tambah_tahun') {
    if ($_POST['status'] == 'aktif') {
        $pdo->query("UPDATE tahun_ajaran SET status = 'tidak aktif'");
    }
    $stmt = $pdo->prepare("INSERT INTO tahun_ajaran (tahun_ajaran, semester_aktif, status) VALUES (?, ?, ?)");
    $stmt->execute([$_POST['tahun_ajaran'], $_POST['semester_aktif'], $_POST['status']]);
    $_SESSION['message'] = '<div class="alert alert-success">Tahun ajaran berhasil ditambahkan.</div>';
    header("Location: setting.php?tab=tahun");
    exit();
}
if ($action === 'update_tahun') {
    if ($_POST['status'] == 'aktif') {
        // Logika perbaikan: nonaktifkan yang lain kecuali yang sedang diupdate
        $pdo->prepare("UPDATE tahun_ajaran SET status = 'tidak aktif' WHERE id_tahun_ajaran != ?")->execute([$_POST['id_tahun_ajaran']]);
    }
    $stmt = $pdo->prepare("UPDATE tahun_ajaran SET tahun_ajaran = ?, semester_aktif = ?, status = ? WHERE id_tahun_ajaran = ?");
    $stmt->execute([$_POST['tahun_ajaran'], $_POST['semester_aktif'], $_POST['status'], $_POST['id_tahun_ajaran']]);
    $_SESSION['message'] = '<div class="alert alert-success">Tahun ajaran berhasil diperbarui.</div>';
    header("Location: setting.php?tab=tahun");
    exit();
}
if ($action === 'hapus_tahun' && isset($_GET['id'])) {
    $stmt = $pdo->prepare("DELETE FROM tahun_ajaran WHERE id_tahun_ajaran = ?");
    $stmt->execute([$_GET['id']]);
    $_SESSION['message'] = '<div class="alert alert-success">Tahun ajaran berhasil dihapus.</div>';
    header("Location: setting.php?tab=tahun");
    exit();
}
// =========================================================================================
// == FUNGSI CRUD LEMBAGA (DIPERTAHANKAN) ==
// =========================================================================================
if ($action === 'simpan_lembaga') {
    try {
        $logo_filename = $_POST['logo_lama'];
        if (isset($_FILES['logo']) && $_FILES['logo']['error'] == 0) {
            $target_dir = "../assets/img/logo/";
            if (!is_dir($target_dir)) mkdir($target_dir, 0755, true);
            $logo_filename = "logo_" . time() . "." . pathinfo($_FILES["logo"]["name"], PATHINFO_EXTENSION);
            move_uploaded_file($_FILES["logo"]["tmp_name"], $target_dir . $logo_filename);
        }
        $stmt = $pdo->prepare(
            "UPDATE pengaturan SET lembaga = ?, nama_sekolah = ?, npsn = ?, nsm = ?, akreditasi = ?, alamat = ?, kota = ?, telepon = ?, email = ?, website = ?, logo = ?, hari_sekolah = ? WHERE id = 1"
        );
        $stmt->execute([
            $_POST['lembaga'], $_POST['nama_sekolah'], $_POST['npsn'], $_POST['nsm'], $_POST['akreditasi'], 
            $_POST['alamat'], $_POST['kota'], $_POST['telepon'], $_POST['email'], $_POST['website'], $logo_filename, $_POST['hari_sekolah']
        ]);
        $_SESSION['message'] = '<div class="alert alert-success">Data lembaga berhasil diperbarui.</div>';
    } catch (PDOException $e) {
        $_SESSION['message'] = '<div class="alert alert-danger">Gagal memperbarui data lembaga: ' . $e->getMessage() . '</div>';
    }
    header("Location: setting.php?tab=lembaga");
    exit();
}
// =========================================================================================
// == FUNGSI BACKUP & RESTORE (DIPERTAHANKAN) ==
// =========================================================================================
if ($action === 'restore_database') {
    if (isset($_FILES['backup_file']) && $_FILES['backup_file']['error'] == UPLOAD_ERR_OK) {
        if (strtolower(pathinfo($_FILES['backup_file']['name'], PATHINFO_EXTENSION)) == 'sql') {
            try {
                $sql_content = file_get_contents($_FILES['backup_file']['tmp_name']);
                $pdo->exec($sql_content);
                $_SESSION['message'] = '<div class="alert alert-success">Database berhasil dipulihkan.</div>';
            } catch (PDOException $e) {
                $_SESSION['message'] = '<div class="alert alert-danger">Gagal memulihkan database: ' . $e->getMessage() . '</div>';
            }
        } else {
            $_SESSION['message'] = '<div class="alert alert-warning">Gagal, hanya file .sql yang diizinkan.</div>';
        }
    } else {
        $_SESSION['message'] = '<div class="alert alert-danger">Gagal mengunggah file backup.</div>';
    }
    header("Location: setting.php?tab=backup");
    exit();
}
// =========================================================================================
// == FUNGSI CRUD MANAJEMEN PENGGUNA (DIPERBARUI untuk 'tim') ==
// =========================================================================================
if ($action === 'tambah_user') {
    try {
        $hashed_password = password_hash($_POST['password'], PASSWORD_DEFAULT);
        $stmt = $pdo->prepare(
            "INSERT INTO users (nama_lengkap, nip, jabatan, pangkat, unit_kerja, username, password, role) 
             VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
        );
        $stmt->execute([
            $_POST['nama_lengkap'], $_POST['nip'], $_POST['jabatan'], $_POST['pangkat'], 
            $_POST['unit_kerja'], $_POST['username'], $hashed_password, $_POST['role']
        ]);
        $_SESSION['message'] = '<div class="alert alert-success">Pengguna baru berhasil ditambahkan.</div>';
    } catch (PDOException $e) {
        $_SESSION['message'] = ($e->errorInfo[1] == 1062)
            ? '<div class="alert alert-danger">Gagal: Username sudah digunakan.</div>'
            : '<div class="alert alert-danger">Gagal menambahkan pengguna: ' . $e->getMessage() . '</div>';
    }
    header("Location: setting.php?tab=users");
    exit();
}
if ($action === 'update_user') {
    try {
        $params = [
            $_POST['nama_lengkap'], $_POST['nip'], $_POST['jabatan'], $_POST['pangkat'], 
            $_POST['unit_kerja'], $_POST['username'], $_POST['role']
        ];
        $sql = "UPDATE users SET nama_lengkap = ?, nip = ?, jabatan = ?, pangkat = ?, unit_kerja = ?, username = ?, role = ?";
        if (!empty($_POST['password'])) {
            $hashed_password = password_hash($_POST['password'], PASSWORD_DEFAULT);
            $sql .= ", password = ?";
            $params[] = $hashed_password;
        }
        $sql .= " WHERE id = ?";
        $params[] = $_POST['id'];
        
        $stmt = $pdo->prepare($sql);
        $stmt->execute($params);
        $_SESSION['message'] = '<div class="alert alert-success">Data pengguna berhasil diperbarui.</div>';
    } catch (PDOException $e) {
        $_SESSION['message'] = ($e->errorInfo[1] == 1062)
            ? '<div class="alert alert-danger">Gagal: Username sudah digunakan.</div>'
            : '<div class="alert alert-danger">Gagal memperbarui pengguna: ' . $e->getMessage() . '</div>';
    }
    header("Location: setting.php?tab=users");
    exit();
}
if ($action === 'hapus_user' && isset($_GET['id'])) {
    if ($_GET['id'] == 1) { 
        $_SESSION['message'] = '<div class="alert alert-warning">Pengguna Admin default tidak dapat dihapus.</div>';
    } else {
        $stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
        $stmt->execute([$_GET['id']]);
        $_SESSION['message'] = '<div class="alert alert-success">Pengguna berhasil dihapus.</div>';
    }
    header("Location: setting.php?tab=users");
    exit();
}
// =========================================================================================
// == LOGIKA SINKRONISASI KEPMAD DIHAPUS DARI SINI (Diasumsikan ada di kepmad.php) ==
// =========================================================================================

$active_tab = $_GET['tab'] ?? 'tahun';
include 'header.php'; 

$message = $_SESSION['message'] ?? '';
unset($_SESSION['message']);

// Ambil data untuk semua tab
$pengaturan = $pdo->query("SELECT * FROM pengaturan WHERE id = 1")->fetch(PDO::FETCH_ASSOC);
$tahun_ajaran_list = $pdo->query("SELECT * FROM tahun_ajaran ORDER BY tahun_ajaran DESC")->fetchAll(PDO::FETCH_ASSOC);
$users_list = $pdo->query("SELECT * FROM users ORDER BY nama_lengkap ASC")->fetchAll(PDO::FETCH_ASSOC);

// Definisi Opsi untuk SELECT
$pangkat_options = [
    '-' => '-',
    'Penata (III/c)' => 'Penata (III/c)',
    'Penata TK.I (III/d)' => 'Penata TK.I (III/d)',
    'Pembina (IV/a)' => 'Pembina (IV/a)',
    'Pembina TK.I (IV/b)' => 'Pembina TK.I (IV/b)'
];

$jabatan_options = [
    'Guru Tetap Yayasan' => 'Guru Tetap Yayasan',
    'Guru Pertama' => 'Guru Pertama',
    'Guru Muda' => 'Guru Muda',
    'Guru Madya' => 'Guru Madya'
];

$role_options = [
    'admin' => 'Admin',
    'penilai' => 'Penilai',
    
    'kepsek' => 'Kepala Sekolah',
    'tim' => 'Tim'
];

?>

<hr> <?= $message ?>

<nav>
    <div class="nav nav-tabs" id="nav-tab" role="tablist">
        <a class="nav-link <?= $active_tab == 'tahun' ? 'active' : '' ?>" href="?tab=tahun">Tahun Penilaian</a>
        <a class="nav-link <?= $active_tab == 'lembaga' ? 'active' : '' ?>" href="?tab=lembaga">Lembaga Sekolah</a>
        <a class="nav-link <?= $active_tab == 'backup' ? 'active' : '' ?>" href="?tab=backup">Backup & Restore</a>
        <a class="nav-link <?= $active_tab == 'users' ? 'active' : '' ?>" href="?tab=users">Manajemen Pengguna</a>
    </div>
</nav>

<div class="tab-content" id="nav-tabContent">
    <div class="tab-pane fade <?= $active_tab == 'tahun' ? 'show active' : '' ?>" id="nav-tahun">
        <div class="card shadow-sm border-top-0">
            <div class="card-body">
                <h5 class="card-title">Manajemen Tahun Penilaian</h5><hr>
                <p>Atur tahun penilaian PKKM yang akan digunakan di seluruh aplikasi. Pastikan hanya ada satu baris yang berstatus <strong>Aktif</strong>.</p>
                <button type="button" class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#tambahTahunModal"><i class="bi bi-plus-circle"></i> Tambah Tahun Penilaian</button>
                <div class="table-responsive">
                    <table class="table table-striped table-bordered">
                        <thead class="table-light"><tr><th>Tahun </th><th>Semester</th>
                            <th>Status</th><th>Aksi</th></tr></thead>
                        <tbody>
                            <?php foreach ($tahun_ajaran_list as $ta) : ?>
                                <tr class="<?= $ta['status'] == 'aktif' ? 'table-success' : '' ?>">
                                    <td><?= htmlspecialchars($ta['tahun_ajaran']) ?></td>
                                    <td><?= htmlspecialchars($ta['semester_aktif']) ?></td>
                                    <td><span class="badge bg-<?= $ta['status'] == 'aktif' ? 'success' : 'secondary' ?>"><?= $ta['status'] == 'aktif' ? 'Aktif' : 'Tidak Aktif' ?></span></td>
                                    <td>
                                        <button class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#editTahunModal<?= $ta['id_tahun_ajaran'] ?>"><i class="bi bi-pencil-square"></i> Edit</button>
                                        <a href="setting.php?action=hapus_tahun&id=<?= $ta['id_tahun_ajaran'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus?')"><i class="bi bi-trash"></i> Hapus</a>
                                    </td>
                                </tr>
                                <div class="modal fade" id="editTahunModal<?= $ta['id_tahun_ajaran'] ?>" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <form method="POST" action="setting.php">
                                                <input type="hidden" name="action" value="update_tahun"><input type="hidden" name="id_tahun_ajaran" value="<?= $ta['id_tahun_ajaran'] ?>">
                                                <div class="modal-header"><h5 class="modal-title">Edit Tahun Penilaian</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
                                                <div class="modal-body">
                                                    <div class="mb-3"><label class="form-label">Tahun Penilaian</label><input type="text" class="form-control" name="tahun_ajaran" value="<?= htmlspecialchars($ta['tahun_ajaran']) ?>" required></div>
                                                    <div class="mb-3"><label class="form-label">Semester Aktif</label><select class="form-select" name="semester_aktif" required><option value="1" <?= $ta['semester_aktif'] == '1' ? 'selected' : '' ?>>Semester 1</option><option value="2" <?= $ta['semester_aktif'] == '2' ? 'selected' : '' ?>>Semester 2</option></select></div>
                                                    <div class="mb-3"><label class="form-label">Status</label><select class="form-select" name="status" required><option value="aktif" <?= $ta['status'] == 'aktif' ? 'selected' : '' ?>>Aktif</option><option value="tidak aktif" <?= $ta['status'] == 'tidak aktif' ? 'selected' : '' ?>>Tidak Aktif</option></select></div>
                                                </div>
                                                <div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button><button type="submit" class="btn btn-primary">Simpan Perubahan</button></div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
  
    <div class="tab-pane fade <?= $active_tab == 'lembaga' ? 'show active' : '' ?>" id="nav-lembaga">
        <div class="card shadow-sm border-top-0">
             <form method="POST" action="setting.php" enctype="multipart/form-data" class="card-body">
                <input type="hidden" name="action" value="simpan_lembaga"><input type="hidden" name="logo_lama" value="<?= htmlspecialchars($pengaturan['logo']) ?>">
                <h5 class="card-title">Data Institusi Sekolah</h5><hr>
                <div class="row">
                    <div class="col-md-6 mb-3"><label class="form-label">Lembaga</label><input type="text" class="form-control" name="lembaga" value="<?= htmlspecialchars($pengaturan['lembaga']) ?>"></div>
                    <div class="col-md-6 mb-3"><label class="form-label">Nama Sekolah</label><input type="text" class="form-control" name="nama_sekolah" value="<?= htmlspecialchars($pengaturan['nama_sekolah']) ?>"></div>
                    <div class="col-md-6 mb-3"><label class="form-label">NPSN</label><input type="text" class="form-control" name="npsn" value="<?= htmlspecialchars($pengaturan['npsn']) ?>"></div>
                    <div class="col-md-6 mb-3"><label class="form-label">NSM</label><input type="text" class="form-control" name="nsm" value="<?= htmlspecialchars($pengaturan['nsm']) ?>"></div>
                    <div class="col-md-6 mb-3"><label class="form-label">Akreditasi</label><select class="form-select" name="akreditasi">
                        <option value="A" <?= $pengaturan['akreditasi'] == 'A' ? 'selected' : '' ?>>A</option>
                        <option value="B" <?= $pengaturan['akreditasi'] == 'B' ? 'selected' : '' ?>>B</option>
                        <option value="C" <?= $pengaturan['akreditasi'] == 'C' ? 'selected' : '' ?>>C</option>
                        <option value="Belum Akreditasi" <?= $pengaturan['akreditasi'] == 'Belum Akreditasi' ? 'selected' : '' ?>>Belum Akreditasi</option></select></div>
                    <div class="col-md-6 mb-3"><label class="form-label">Nomor Telepon</label><input type="tel" class="form-control" name="telepon" value="<?= htmlspecialchars($pengaturan['telepon']) ?>"></div>
                    <div class="col-md-6 mb-3"><label class="form-label">Email</label><input type="email" class="form-control" name="email" value="<?= htmlspecialchars($pengaturan['email']) ?>"></div>
                    <div class="col-md-6 mb-3"><label class="form-label">Website</label><input type="url" class="form-control" name="website" value="<?= htmlspecialchars($pengaturan['website']) ?>" placeholder="https://www.namasekolah.sch.id"></div>
                    <div class="col-md-6 mb-3"><label class="form-label">Kota/Kabupaten</label><input type="text" class="form-control" name="kota" value="<?= htmlspecialchars($pengaturan['kota']) ?>"></div>
                    <div class="col-md-6 mb-3"><label class="form-label">Hari Sekolah</label><select class="form-select" name="hari_sekolah"><option value="5" <?= ($pengaturan['hari_sekolah'] ?? 5) == 5 ? 'selected' : '' ?>>5 Hari Sekolah</option><option value="6" <?= ($pengaturan['hari_sekolah'] ?? 5) == 6 ? 'selected' : '' ?>>6 Hari Sekolah</option></select></div>
                    <div class="col-md-6 mb-3"><label class="form-label">Logo Sekolah</label><input class="form-control" type="file" name="logo" accept="image/*"><?php if (!empty($pengaturan['logo'])): ?><small class="form-text text-muted">Logo saat ini: <img src="../assets/img/logo/<?= htmlspecialchars($pengaturan['logo']) ?>" height="40" class="mt-2"></small><?php endif; ?></div>
                    <div class="col-12 mb-3"><label class="form-label">Alamat Sekolah</label><textarea class="form-control" name="alamat" rows="2"><?= htmlspecialchars($pengaturan['alamat']) ?></textarea></div>
                </div>
                <button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> Simpan Data Lembaga</button>
            </form>
        </div>
    </div>
    
    <div class="tab-pane fade <?= $active_tab == 'backup' ? 'show active' : '' ?>" id="nav-backup">
       <div class="card shadow-sm border-top-0">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <h5><i class="bi bi-database-down"></i> Backup Database</h5><hr>
                        <p>Unduh seluruh data aplikasi (guru, siswa, nilai, dll) dalam satu file .sql.</p>
                        <a href="backup.php" class="btn btn-success"><i class="bi bi-download"></i> Backup Database Sekarang</a>
                    </div>
                    <div class="col-md-6 border-start">
                        <h5><i class="bi bi-database-up"></i> Restore Database</h5><hr>
                        <div class="alert alert-danger fw-bold"><i class="bi bi-exclamation-triangle-fill"></i> PERHATIAN: Memulihkan database akan MENGHAPUS semua data saat ini.</div>
                        <form method="POST" action="setting.php" enctype="multipart/form-data" onsubmit="return confirm('APAKAH ANDA YAKIN? Semua data saat ini akan hilang dan diganti.');">
                            <input type="hidden" name="action" value="restore_database">
                            <div class="mb-3">
                                <label for="backup_file" class="form-label">Pilih File Backup (.sql)</label>
                                <input class="form-control" type="file" name="backup_file" id="backup_file" accept=".sql" required>
                            </div>
                            <button type="submit" class="btn btn-danger"><i class="bi bi-upload"></i> Restore dari File</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="tab-pane fade <?= $active_tab == 'users' ? 'show active' : '' ?>" id="nav-users">
        <div class="card shadow-sm border-top-0">
            <div class="card-body">
                <h5 class="card-title">Manajemen Pengguna</h5><hr>
                <p>Kelola siapa saja yang dapat mengakses sistem beserta perannya masing-masing.</p>
                <button type="button" class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#tambahUserModal"><i class="bi bi-person-plus-fill"></i> Tambah Pengguna</button>
                <div class="table-responsive">
                    <table class="table table-striped table-bordered">
                        <thead class="table-light"><tr><th>Nama Lengkap</th><th>NIP</th><th>Username</th><th>Peran (Role)</th><th>Aksi</th></tr></thead>
                        <tbody>
                            <?php foreach ($users_list as $user): ?>
                            <tr>
                                <td><?= htmlspecialchars($user['nama_lengkap']) ?></td>
                                <td><?= htmlspecialchars($user['nip']) ?></td>
                                <td><?= htmlspecialchars($user['username']) ?></td>
                                <td><span class="badge bg-info text-dark"><?= ucfirst($user['role']) ?></span></td>
                                <td>
                                    <button class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#editUserModal<?= $user['id'] ?>"><i class="bi bi-pencil-square"></i> Edit</button>
                                    <?php if ($user['id'] != 1): ?>
                                    <a href="setting.php?action=hapus_user&id=<?= $user['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus pengguna ini?')"><i class="bi bi-trash"></i> Hapus</a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <div class="modal fade" id="editUserModal<?= $user['id'] ?>" tabindex="-1">
                                <div class="modal-dialog modal-lg">
                                    <div class="modal-content">
                                        <form method="POST" action="setting.php">
                                            <input type="hidden" name="action" value="update_user"><input type="hidden" name="id" value="<?= $user['id'] ?>">
                                            <div class="modal-header"><h5 class="modal-title">Edit Pengguna</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
                                            <div class="modal-body">
                                                <div class="row">
                                                    <div class="col-md-6 mb-3"><label class="form-label">Nama Lengkap</label><input type="text" class="form-control" name="nama_lengkap" value="<?= htmlspecialchars($user['nama_lengkap']) ?>" required></div>
                                                    <div class="col-md-6 mb-3"><label class="form-label">NIP</label><input type="text" class="form-control" name="nip" value="<?= htmlspecialchars($user['nip']) ?>"></div>
                                                    
                                                    <div class="col-md-6 mb-3">
                                                        <label class="form-label">Jabatan</label>
                                                        <select class="form-select" name="jabatan">
                                                            <option value="" disabled>Pilih Jabatan...</option>
                                                            <?php foreach ($jabatan_options as $key => $value): ?>
                                                                <option value="<?= htmlspecialchars($key) ?>" <?= ($user['jabatan'] ?? '') == $key ? 'selected' : '' ?>>
                                                                    <?= htmlspecialchars($value) ?>
                                                                </option>
                                                            <?php endforeach; ?>
                                                        </select>
                                                    </div>
                                                    <div class="col-md-6 mb-3">
                                                        <label class="form-label">Pangkat/Golongan</label>
                                                        <select class="form-select" name="pangkat">
                                                            <?php foreach ($pangkat_options as $key => $value): ?>
                                                                <option value="<?= htmlspecialchars($key) ?>" <?= ($user['pangkat'] ?? '') == $key ? 'selected' : '' ?>>
                                                                    <?= htmlspecialchars($value) ?>
                                                                </option>
                                                            <?php endforeach; ?>
                                                        </select>
                                                    </div>
                                                    <div class="col-12 mb-3"><label class="form-label">Unit Kerja</label><input type="text" class="form-control" name="unit_kerja" value="<?= htmlspecialchars($user['unit_kerja']) ?>"></div>
                                                    <hr>
                                                    <div class="col-md-6 mb-3"><label class="form-label">Username</label><input type="text" class="form-control" name="username" value="<?= htmlspecialchars($user['username']) ?>" required></div>
                                                    <div class="col-md-6 mb-3"><label class="form-label">Password Baru (kosongkan jika tidak diubah)</label><input type="password" class="form-control" name="password"></div>
                                                    <div class="col-md-6 mb-3"><label class="form-label">Role</label><select class="form-select" name="role" required>
                                                        <?php foreach ($role_options as $key => $value): ?>
                                                            <option value="<?= htmlspecialchars($key) ?>" <?= $user['role'] == $key ? 'selected' : '' ?>>
                                                                <?= htmlspecialchars($value) ?>
                                                            </option>
                                                        <?php endforeach; ?>
                                                    </select></div>
                                                </div>
                                            </div>
                                            <div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button><button type="submit" class="btn btn-primary">Simpan Perubahan</button></div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="tambahTahunModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="POST" action="setting.php">
                <input type="hidden" name="action" value="tambah_tahun">
                <div class="modal-header"><h5 class="modal-title">Tambah Tahun Ajaran Baru</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
                <div class="modal-body">
                    <div class="mb-3"><label class="form-label">Tahun Ajaran</label><input type="text" class="form-control" name="tahun_ajaran" placeholder="Contoh: 2024/2025" required></div>
                    <div class="mb-3"><label class="form-label">Semester Aktif</label><select class="form-select" name="semester_aktif" required><option value="1">Semester 1</option><option value="2">Semester 2</option></select></div>
                    <div class="mb-3"><label class="form-label">Status</label><select class="form-select" name="status" required><option value="aktif">Aktif</option><option value="tidak aktif" selected>Tidak Aktif</option></select></div>
                </div>
                <div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button><button type="submit" class="btn btn-primary">Tambah Tahun Ajaran</button></div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="tambahUserModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <form method="POST" action="setting.php">
                <input type="hidden" name="action" value="tambah_user">
                <div class="modal-header"><h5 class="modal-title">Tambah Pengguna Baru</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6 mb-3"><label class="form-label">Nama Lengkap</label><input type="text" class="form-control" name="nama_lengkap" required></div>
                        <div class="col-md-6 mb-3"><label class="form-label">NIP</label><input type="text" class="form-control" name="nip"></div>
                        
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Jabatan</label>
                            <select class="form-select" name="jabatan">
                                <option value="" disabled selected>Pilih Jabatan...</option>
                                <?php foreach ($jabatan_options as $key => $value): ?>
                                    <option value="<?= htmlspecialchars($key) ?>"><?= htmlspecialchars($value) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Pangkat/Golongan</label>
                            <select class="form-select" name="pangkat">
                                <?php foreach ($pangkat_options as $key => $value): ?>
                                    <option value="<?= htmlspecialchars($key) ?>" <?= $key == '-' ? 'selected' : '' ?>>
                                        <?= htmlspecialchars($value) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-12 mb-3"><label class="form-label">Unit Kerja</label><input type="text" class="form-control" name="unit_kerja"></div>
                        <hr>
                        <div class="col-md-6 mb-3"><label class="form-label">Username</label><input type="text" class="form-control" name="username" required></div>
                        <div class="col-md-6 mb-3"><label class="form-label">Password</label><input type="password" class="form-control" name="password" required></div>
                        <div class="col-md-6 mb-3"><label class="form-label">Role</label><select class="form-select" name="role" required>
                            <?php foreach ($role_options as $key => $value): ?>
                                <option value="<?= htmlspecialchars($key) ?>" <?= $key == 'penilai' ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($value) ?>
                                </option>
                            <?php endforeach; ?>
                        </select></div>
                    </div>
                </div>
                <div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button><button type="submit" class="btn btn-primary">Tambah Pengguna</button></div>
            </form>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>