<?php
require 'koneksi.php';
$con = open_connection();

$murid = mysqli_query($con, "SELECT * FROM murid");
$guru = mysqli_query($con, "SELECT * FROM guru");
$aspek = mysqli_query($con, "SELECT * FROM aspek_perkembangan");

if(isset($_POST['simpan']))
{
    $id_murid = $_POST['id_murid'];
    $id_aspek = $_POST['id_aspek'];
    $id_guru = $_POST['id_guru'];
    $deskripsi = $_POST['deskripsi'];
    $tanggal = $_POST['tanggal_penilaian'];

    mysqli_query($con,"
        INSERT INTO perkembangan
        (id_murid,id_aspek,id_guru,deskripsi,tanggal_penilaian)
        VALUES
        ('$id_murid','$id_aspek','$id_guru','$deskripsi','$tanggal')
    ");

    $nama_murid = mysqli_fetch_assoc(
        mysqli_query($con, "SELECT nama FROM murid WHERE id_murid='$id_murid'")
    )['nama'] ?? '';
    catat_log($con, "Data perkembangan diperbarui untuk: " . $nama_murid, "perkembangan");

    header("Location: perkembangan.php");
}
?>
<?php require 'header.php';?>

<div class="container mt-4">

<h2>Tambah Data Perkembangan</h2>

<form method="POST">

    <div class="mb-3">
        <label>Murid</label>
        <select name="id_murid" class="form-control" required>
            <option value="">Pilih Murid</option>

            <?php while($m = mysqli_fetch_assoc($murid)){ ?>
                <option value="<?= $m['id_murid']; ?>">
                    <?= $m['nama']; ?>
                </option>
            <?php } ?>

        </select>
    </div>

    <div class="mb-3">
        <label>Aspek Perkembangan</label>
        <select name="id_aspek" class="form-control" required>

            <option value="">Pilih Aspek</option>

            <?php while($a = mysqli_fetch_assoc($aspek)){ ?>
                <option value="<?= $a['id_aspek']; ?>">
                    <?= $a['nama_aspek']; ?>
                </option>
            <?php } ?>

        </select>
    </div>

    <div class="mb-3">
        <label>Guru</label>
        <select name="id_guru" class="form-control" required>

            <option value="">Pilih Guru</option>

            <?php while($g = mysqli_fetch_assoc($guru)){ ?>
                <option value="<?= $g['id_guru']; ?>">
                    <?= $g['nama']; ?>
                </option>
            <?php } ?>

        </select>
    </div>

    <div class="mb-3">
        <label>Tanggal Penilaian</label>
        <input type="date"
            name="tanggal_penilaian"
            class="form-control"
            required>
    </div>

    <div class="mb-3">
        <label>Deskripsi</label>
        <textarea name="deskripsi"
                class="form-control"
                rows="4"
                required></textarea>
    </div>

    <button type="submit"
            name="simpan"
            class="btn btn-success">
        Simpan
    </button>

</form>

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