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

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

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

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

$hasil = mysqli_query($connection, "SELECT max(id) as maxPeserta FROM peserta");
$data = mysqli_fetch_array($hasil);
$maxpeserta = $data["maxPeserta"];
$maxpeserta++;
$thn = "2023";
$kodenis = $thn . sprintf("%04s", $maxpeserta);
$pola = "98811065";
$kodeva = $pola . sprintf("%04s", $maxpeserta);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_peserta = $_POST["nama_peserta"];
    $anakke = $_POST["anakke"];
    $nis = $_POST["nis"];
    $jenjang = $_POST["jenjang"];
    $jenis_kelamin = $_POST["jenis_kelamin"];
    $agama = $_POST["agama"];
    $jmlhsdr = $_POST["jmlhsdr"];
    $tgl_lahir = $_POST["tgl_lahir"];
    $tmpt_lahir = $_POST["tmpt_lahir"];
    $nomorortu = $_POST["nomorortu"];
    $nomorpeserta = $_POST["nomorpeserta"];
    $alamat = $_POST["alamat"];
    $tingkatan = $_POST["tingkatan"];
    $asalsekolah = $_POST["asalsekolah"];
    $virtualacc = $_POST["virtualacc"];
    $cad4 = $_POST["cad4"];

    do {
        if (
            empty($nama_peserta) || empty($anakke) || empty($jenjang) || empty($jenis_kelamin) ||
            empty($agama) || empty($jmlhsdr) || empty($tgl_lahir) || empty($tmpt_lahir) || empty($nomorpeserta) || empty($alamat) ||
            empty($tingkatan) || empty($asalsekolah)
        ) {
            $errorMessage = "Mohon mengisi semua kolom";
            break;
        }

        $sql = "INSERT INTO peserta (nama_peserta, anakke, nis, jenjang, jenis_kelamin, agama, jmlhsdr, tgl_lahir, tmpt_lahir,nomorpeserta, nomorortu, alamat, tingkatan, asalsekolah, virtualacc, cad4)" .
            "VALUES ('$nama_peserta','$anakke', '$kodenis', '$jenjang', '$jenis_kelamin', '$agama', '$jmlhsdr', '$tgl_lahir', '$tmpt_lahir', '$nomorpeserta', '$nomorortu', '$alamat', '$tingkatan', '$asalsekolah', '$kodeva', '0')";
        $sql2 = "INSERT INTO raport (nama_peserta)" . "VALUES ('$nama_peserta')";

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

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

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

        $successMessage = "Berhasil Menambahkan Peserta";

        header("location: landingpage.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>
    <style>
        body {
            background-color: #EBEBEB;
        }
    </style>

    <body>
        <div class="container">
            <h2>Pendaftaran 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">
                <div class="row mb-1">
                    <label class="col-sm-3 col-form-label-lg" >Nama Lengkap</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-1">
                    <label class="col-sm-3 col-form-label-lg">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-1">
                    <label class="col-sm-3 col-form-label-lg">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-1">
                    <label class="col-sm-3 col-form-label-lg">Jenjang</label>
                    <div class="col-sm-6">
                    <select name="jenjang" class="form-control" value="<?php echo $jenjang; ?>">
                            <option value="">---Pilih Jenjang---</option>
                            <option value="SD">SD</option>
                            <option value="SMP">SMP</option>
                            <option value="SMA">SMA</option>
                            <option value="SMK">SMK</option>
                            <option value="UMUM">UMUM</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-1">
                    <label class="col-sm-3 col-form-label-lg">Jenis Kelamin</label>
                    <div class="col-sm-6">
                        <input type="radio" name="jenis_kelamin" value="Laki-laki"> <b>Laki-laki</b> &emsp;&emsp;
                        <input type="radio" name="jenis_kelamin" value="Perempuan"> <b>Perempuan</b>
                    </div>
                </div>
                <div class="row mb-1">
                    <label class="col-sm-3 col-form-label-lg">Agama</label>
                    <div class="col-sm-6">
                        <select name="agama" class="form-control" value="<?php echo $agama; ?>">
                            <option value="">---Pilih Agama---</option>
                            <option value="Islam">Islam</option>
                            <option value="Kristen">Kristen</option>
                            <option value="Katholik">Katholik</option>
                            <option value="Hindu">Hindu</option>
                            <option value="Buddha">Buddha</option>
                            <option value="Konghucu">Konghucu</option>
                            <option value="Lainnya">Lainnya</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-1">
                    <label class="col-sm-3 col-form-label-lg">Tanggal Lahir</label>
                    <div class="col-sm-2">
                        <input type="date" class="form-control" name="tgl_lahir" value="<?php echo $tgl_lahir; ?>">
                    </div>
                </div>
                <div class="row mb-1">
                    <label class="col-sm-3 col-form-label-lg">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-1">
                    <label class="col-sm-3 col-form-label-lg">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-1">
                    <label class="col-sm-3 col-form-label-lg">Nomor HP Wali/Orang Tua</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="nomorortu" value="<?php echo $nomorortu; ?>">
                    </div>
                </div>
                <div class="row mb-1">
                    <label class="col-sm-3 col-form-label-lg">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-1">
                    <label class="col-sm-3 col-form-label-lg">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-1">
                    <label class="col-sm-3 col-form-label-lg">Tingkatan</label>
                    <div class="col-sm-6">
                        <select name="tingkatan" class="form-control" value="<?php echo $tingkatan; ?>">
                            <option value="">---Pilih Tingkatan---</option>
                            <option value="Beginner">Beginner</option>
                            <option value="Intermediate">Intermediate</option>
                            <option value="Advanced">Advanced</option>
                            <option value="Expert">Expert</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-1">
                    <label class="col-sm-3 col-form-label-lg">Asal Sekolah/Instansi</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="asalsekolah" value="<?php echo $asalsekolah; ?>">
                    </div>
                </div>
                <img src="img/brosur.svg" width="25%" style="display:block; margin:auto;"> <br>
                <?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="landingpage.php" role="button">Batal</a>
                    </div>
                </div>
            </form>

        </div>
    </body>

    </html>