<?php
// Cek mode: Apakah TAMBAH BARU atau EDIT?
$act = isset($_GET['act']) ? $_GET['act'] : '';
$idk = isset($_GET['idk']) ? $_GET['idk'] : '';

if ($act == "edit" && !empty($idk)) {
    $judul = "Edit Data Kelas";
    // Ambil data lama
    $query = mysqli_query($koneksi, "SELECT * FROM kelas WHERE idk='$idk'");
    $data  = mysqli_fetch_assoc($query);
    
    // Validasi jika data tidak ditemukan
    if ($data) {
        $nama_val = $data['nama'];
    } else {
        echo "<script>alert('Data kelas tidak ditemukan!'); window.location='media.php?module=kelas';</script>";
        exit;
    }

    // Arahkan form ke proses EDIT di simpan.php
    $form_action = "./module/simpan.php?act=edit_kelas";
} else {
    $judul = "Tambah Kelas Baru";
    $nama_val = "";
    
    // Arahkan form ke proses INPUT di simpan.php
    $form_action = "./module/simpan.php?act=input_kelas";
}
?>

<div class="row mb-3">
    <div class="col-lg-12">
        <h3 class="page-header"><strong><?= $judul ?></strong></h3>
    </div>
</div>

<div class="row">
    <div class="col-lg-6">
        <div class="panel panel-primary">
            <div class="panel-heading"><?= $judul ?></div>
            <div class="panel-body">
                
                <form method="post" action="<?= $form_action ?>">
                    
                    <?php if ($act == "edit") { ?>
                        <input type="hidden" name="idk" value="<?= $idk ?>">
                    <?php } ?>

                    <div class="form-group">
                        <label>Nama Kelas</label>
                        <input type="text" name="nama" class="form-control" value="<?= $nama_val ?>" required placeholder="Contoh: VII, VIII, IX">
                    </div>

                    <button type="submit" class="btn btn-success">
                        <i class="fa fa-save"></i> Simpan
                    </button>
                    <a href="media.php?module=kelas" class="btn btn-danger">Batal</a>
                </form>

            </div>
        </div>
    </div>
</div>