<?php
include __DIR__ . '/../koneksi.php';

if (!isset($_GET['id'])) {
    echo "ID tidak ditemukan";
    exit;
}

$id = $_GET['id'];

$query = mysqli_query($conn, "SELECT * FROM santri WHERE id = '$id'");
$data = mysqli_fetch_assoc($query);

if (!$data) {
    echo "Data santri tidak ditemukan";
    exit;
}

// ambil data kelas untuk dropdown
$kelas = mysqli_query($conn, "SELECT * FROM kelas");
?>

<h2>Edit Data Santri</h2>

<form method="post" action="santri_proses_edit.php">
    <input type="hidden" name="id" value="<?= $data['id']; ?>">

    <label>NIS</label><br>
    <input type="text" name="nis" value="<?= $data['nis']; ?>" required><br><br>

    <label>Nama</label><br>
    <input type="text" name="nama" value="<?= $data['nama']; ?>" required><br><br>

    <label>Jenis Kelamin</label><br>
    <select name="jenis_kelamin" required>
        <option value="Laki-laki" <?= $data['jenis_kelamin']=='Laki-laki'?'selected':''; ?>>Laki-laki</option>
        <option value="Perempuan" <?= $data['jenis_kelamin']=='Perempuan'?'selected':''; ?>>Perempuan</option>
    </select><br><br>

    <label>Kelas</label><br>
    <select name="id_kelas" required>
        <?php while ($k = mysqli_fetch_assoc($kelas)) { ?>
            <option value="<?= $k['id_kelas']; ?>"
                <?= $k['id_kelas'] == $data['id_kelas'] ? 'selected' : ''; ?>>
                <?= $k['nama_kelas']; ?>
            </option>
        <?php } ?>
    </select><br><br>

    <label>Alamat</label><br>
    <textarea name="alamat" required><?= $data['alamat']; ?></textarea><br><br>

    <label>Tahun Masuk</label><br>
    <input type="number" name="tahun_masuk" value="<?= $data['tahun_masuk']; ?>" required><br><br>

    <button type="submit">Simpan Perubahan</button>
    <a href="santri.php">Batal</a>
</form>
