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

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

$nama_peserta = "";
$sandi = "";
$status = "";

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


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_peserta = $_POST["nama_peserta"];
    $sandi = $_POST["sandi"];
    $status = $_POST["status"];

    do {
        if (
            empty($nama_peserta) || empty($sandi) || empty($status)
        ) {
            $errorMessage = "Mohon mengisi semua kolom";
            break;
        }

        $sql = "INSERT INTO userpeserta (nama_peserta, sandi, status)" .
            "VALUES ('$nama_peserta', '$sandi', '$status')";

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

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

        $nama_peserta = "";
        $successMessage = "Proses Pendaftaran Berhasil";
        
        header("location: loginpageuser.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>Form Buat Akun</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-3">
                    <label class="col-sm-3 col-form-label">Nama</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" placeholder="Masukkan Nama" name="nama_peserta"
                            value="<?php echo $nama_peserta; ?>">
                        <p><i>Mohon memasukkan nama lengkap tanpa disingkat <br>*Jika orang tua maka bisa memasukkan
                                nama anak secara lengkap tanpa disingkat</i></p>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Passsword</label>
                    <div class="col-sm-6">
                        <input type="password" class="form-control" placeholder="Minimal 10 karakter" name="sandi"
                            value="<?php echo $sandi; ?>">
                        <p><i>*Mohon membuat password yang mudah diingat oleh Anda</i></p>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Status</label>
                    <div class="col-sm-6">
                        <select name="status" class="form-control" value="<?php echo $status; ?>">
                            <option value="">---Pilih Status---</option>
                            <option value="siswa">Siswa</option>
                            <option value="ortu">Orang Tua</option>
                        </select>
                        <p><i>*Pilih status Anda sebagai orang tua atau siswa</i></p>
                    </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="loginpageuser.php" role="button">Batal</a>
                    </div>
                </div>
            </form>

        </div>
    </body>

    </html>