<?php
session_start();
if(!isset($_SESSION['username'])){
    header("Location: Login.php");
    exit();
}
require "koneksi.php";
$con = open_connection();

// Ambil data kelas buat dropdown
$daftar_kelas = mysqli_query($con, "SELECT kode_kelas, nama_kelas FROM kelas ORDER BY nama_kelas ASC");

// Proses simpan data kalau form disubmit
if(isset($_POST['simpan'])){
    $nama = $_POST['nama'];
    $nisn = $_POST['nisn'];
    $kode_kelas = $_POST['kode_kelas'];

    $query = "INSERT INTO murid (nama, nisn, kode_kelas) VALUES ('$nama', '$nisn', '$kode_kelas')";
    mysqli_query($con, $query);

    catat_log($con, "Data murid baru ditambahkan: " . $nama, "murid");

    header("Location: murid.php?pesan=Data murid berhasil ditambahkan");
    exit();
}
?>
<?php require "header.php"; ?>

    <div class="container mt-4">
        <h3 class="page-title">Tambah Murid</h3>

        <form method="POST" class="mt-3">
            <div class="mb-3">
                <label class="form-label">Nama Murid</label>
                <input type="text" class="form-control" name="nama" required>
            </div>
            <div class="mb-3">
                <label class="form-label">NISN</label>
                <input type="text" class="form-control" name="nisn" required>
            </div>
            <div class="mb-3">
                <label class="form-label">Kelas</label>
                <select class="form-control" name="kode_kelas" required>
                    <option value="">-- Pilih Kelas --</option>
                    <?php while($k = mysqli_fetch_assoc($daftar_kelas)): ?>
                        <option value="<?php echo $k['kode_kelas']; ?>"><?php echo htmlspecialchars($k['nama_kelas']); ?></option>
                    <?php endwhile; ?>
                </select>
            </div>
            <button type="submit" name="simpan" class="btn btn-tambah">Simpan</button>
            <a href="murid.php" class="btn btn-secondary">Batal</a>
        </form>
    </div>
<?php include 'footer.php';?>