<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "membership";

$connection = new mysqli($servername, $username, $password, $database);

$id = "";
$nama_peserta = "";
$nis = "";
$anakke = "";
$jmlhsdr = "";
$jenjang = "";
$jenis_kelamin = "";
$agama = "";
$tgl_lahir = "";
$tmpt_lahir = "";
$nomorpeserta = "";
$nomorortu = "";
$alamat = "";
$tingkatan = "";
$asalsekolah = "";
$virtualacc = "";

$errorMessage = "";
$successMessage = "";

if ($_SERVER['REQUEST_METHOD'] == 'GET') {

    if (!isset($_GET["id"])) {
        header("location: datapesertaadm.php");
        exit;
    }

    $id = $_GET["id"];

    $sql = "SELECT * FROM peserta WHERE id=$id";
    $result = $connection->query($sql);
    $row = $result->fetch_assoc();

    if (!$row) {
        header("location: datapesertaadm.php");
        exit;
    }

    $nama_peserta = $row["nama_peserta"];
    $nis = $row["nis"];
    $anakke = $row["anakke"];
    $jmlhsdr = $row["jmlhsdr"];
    $jenjang = $row["jenjang"];
    $jenis_kelamin = $row["jenis_kelamin"];
    $agama = $row["agama"];
    $tgl_lahir = $row["tgl_lahir"];
    $tmpt_lahir = $row["tmpt_lahir"];
    $nomorpeserta = $row["nomorpeserta"];
    $nomorortu = $row["nomorortu"];
    $alamat = $row["alamat"];
    $tingkatan = $row["tingkatan"];
    $asalsekolah = $row["asalsekolah"];
    $virtualacc = $row ["virtualacc"];

} else {

    $id = $_POST["id"];
    $nama_peserta = $_POST["nama_peserta"];
    $nis = $_POST["nis"];
    $anakke = $_POST["anakke"];
    $jmlhsdr = $_POST["jmlhsdr"];
    $jenjang = $_POST["jenjang"];
    $jenis_kelamin = $_POST["jenis_kelamin"];
    $agama = $_POST["agama"];
    $tgl_lahir = $_POST["tgl_lahir"];
    $tmpt_lahir = $_POST["tmpt_lahir"];
    $nomorpeserta = $_POST["nomorpeserta"];
    $nomorortu = $_POST["nomorortu"];
    $alamat = $_POST["alamat"];
    $tingkatan = $_POST["tingkatan"];
    $asalsekolah = $_POST["asalsekolah"];
    $virtualacc = $_POST["virtualacc"];

    do {
        $sql = "UPDATE peserta
        SET nama_peserta = '$nama_peserta', 
        nis='$nis',
        anakke='$anakke',
        jmlhsdr='$jmlhsdr', 
        jenjang='$jenjang', 
        jenis_kelamin='$jenis_kelamin',
        agama='$agama', 
        tgl_lahir='$tgl_lahir', 
        tmpt_lahir='$tmpt_lahir',
        nomorpeserta='$nomorpeserta',
        nomorortu='$nomorortu', 
        alamat='$alamat', 
        tingkatan='$tingkatan', 
        asalsekolah='$asalsekolah',
        virtualacc='$virtualacc' 
        WHERE id=$id";

$sql2 = "UPDATE raport set nama_peserta = '$nama_peserta' WHERE id=$id";

$result = $connection->query($sql);
$result = $connection->query($sql2);

        if (!$result) {
            $errorMessage = "Invalid Query" . $connection->error;
            break;
        }

        $successMessage = "Data berhasil diupdate";

        header("location:  datapesertaadm.php");
        exit;

    } while (false);
}
?>

<DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Daftar Peserta </title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
    </head>

    <body>

        <div class="container">
            <h2>Edit Peserta</h2>

            <?php
            if (!empty($errorMessage)) {
                echo "
                <div class='alert alert-warning alert-dismissible fade show' role='alert'>
                <strong>$errorMessage</strong>
                <button type='button' class='btn-close' data-bs-dissmiss='alert' aria-label='Close'></button>
                </div>
            ";
            }
            ?>

            <form method="post">
                <input type="hidden" name="id" value="<?php echo $id; ?>">
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Nama</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="nama_peserta"
                            value="<?php echo $nama_peserta; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">NIS</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="nis" value="<?php echo $nis; ?>" readonly>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Anak Ke</label>
                    <div class="col-sm-1">
                        <input type="text" class="form-control" name="anakke" value="<?php echo $anakke; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Jumlah Saudara Kandung</label>
                    <div class="col-sm-1">
                        <input type="text" class="form-control" name="jmlhsdr" value="<?php echo $jmlhsdr; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Jenjang</label>
                    <div class="col-sm-6">
                    <select name="jenjang" class="form-control" value="<?php echo $jenjang; ?>">
                            <option value="">---Pilih Jenjang---</option>
                            <option <?php if ($jenjang == 'SD') {
                                echo "selected";
                            } ?> value="SD">SD
                            </option>
                            <option <?php if ($jenjang == 'SMP') {
                                echo "selected";
                            } ?> value="SMP">
                                SMP</option>
                            <option <?php if ($jenjang == 'SMA') {
                                echo "selected";
                            } ?> value="SMA">SMA
                            </option>
                            <option <?php if ($jenjang == 'SMK') {
                                echo "selected";
                            } ?> value="SMK">SMK</option>
                            <option <?php if ($jenjang == 'UMUM') {
                                echo "selected";
                            } ?> value="UMUM">UMUM</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Jenis Kelamin</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="jenis_kelamin"
                            value="<?php echo $jenis_kelamin; ?>" readonly>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Agama</label>
                    <div class="col-sm-6">
                        <select name="agama" class="form-control" value="<?php echo $agama; ?>">
                            <option value="">---Pilih Agama---</option>
                            <option <?php if ($agama == 'Islam') {
                                echo "selected";
                            } ?> value="Islam">Islam</option>
                            <option <?php if ($agama == 'Kristen') {
                                echo "selected";
                            } ?> value="Kristen">Kristen</option>
                            <option <?php if ($agama == 'Katholik') {
                                echo "selected";
                            } ?> value="Katholik">Katholik
                            </option>
                            <option <?php if ($agama == 'Hindu') {
                                echo "selected";
                            } ?> value="Hindu">Hindu</option>
                            <option <?php if ($agama == 'Buddha') {
                                echo "selected";
                            } ?> value="Buddha">Buddha</option>
                            <option <?php if ($agama == 'Konghucu') {
                                echo "selected";
                            } ?> value="Konghucu">Konghucu
                            </option>
                            <option <?php if ($agama == 'Lainnya') {
                                echo "selected";
                            } ?> value="Lainnya">Lainnya</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Tanggal Lahir</label>
                    <div class="col-sm-6">
                        <input type="date" class="form-control" name="tgl_lahir" value="<?php echo $tgl_lahir; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Tempat Lahir</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="tmpt_lahir" value="<?php echo $tmpt_lahir; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Nomor HP Peserta</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="nomorpeserta" value="<?php echo $nomorpeserta; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Nomor HP Ortu/Wali</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="nomorortu" value="<?php echo $nomorortu; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Alamat</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="alamat" value="<?php echo $alamat; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Tingkatan</label>
                    <div class="col-sm-6">
                        <select name="tingkatan" class="form-control" value="<?php echo $tingkatan; ?>">
                            <option value="">---Pilih Tingkatan---</option>
                            <option <?php if ($tingkatan == 'Beginner') {
                                echo "selected";
                            } ?> value="Beginner">Beginner
                            </option>
                            <option <?php if ($tingkatan == 'Intermediate') {
                                echo "selected";
                            } ?> value="Intermediate">
                                Intermediate</option>
                            <option <?php if ($tingkatan == 'Advanced') {
                                echo "selected";
                            } ?> value="Advanced">Advanced
                            </option>
                            <option <?php if ($tingkatan == 'Expert') {
                                echo "selected";
                            } ?> value="Expert">Expert</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Asal Sekolah</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="asalsekolah" value="<?php echo $asalsekolah; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Nomor Virtual Account</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="virtualacc" value="<?php echo $virtualacc; ?>" readonly>
                    </div>
                </div>

                <?php
                if (!empty($successMessage)) {
                    echo "
                    <div class='row mb-3'>
                    <div class='offset-sm-3 col-sm-6'>
                    <div class='alert alert-success alert-dismissible fade show' role='alert'>
                    <strong>$successMessage</strong>
                    <button type='button' class='btn-close' data-bs-dissmiss='alert' aria-label='Close'></button>
                    </div>
                    </div>
                    </div>
                    ";
                }

                ?>

                <div class="row mb-3">
                    <div class="offset-sm-3 col-sm-3 d-grid">
                        <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                    <div class="col-sm-3 d-grid">
                        <a class="btn btn-outline-primary" href=" datapesertaadm.php" role="button">Batal</a>
                    </div>
                </div>
            </form>

        </div>
    </body>

    </html>