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

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

$kode = "";
$nama_lomba = "";
$tanggal_lomba = "";
$lokasi_lomba = "";

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


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $kode = $_POST["kode"];
    $nama_lomba = $_POST["nama_lomba"];
    $tanggal_lomba = $_POST["tanggal_lomba"];
    $lokasi_lomba = $_POST["lokasi_lomba"];

    do {
        if (
            empty($kode) || empty($nama_lomba) || empty($tanggal_lomba) || empty($lokasi_lomba)
        ) {
            $errorMessage = "Mohon mengisi semua kolom";
            break;
        }
        $sql = "INSERT INTO lomba (kode, nama_lomba, tanggal_lomba, lokasi_lomba)" .
            "VALUES ('$kode', '$nama_lomba', '$tanggal_lomba', '$lokasi_lomba')";
        $result = $connection->query($sql);

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

        $kode = "";
        $nama_lomba = "";
        $tanggal_lomba = "";
        $lokasi_lomba = "";

        $successMessage = "Berhasil Menambahkan lomba";

        header("location: datalombacch.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 Lomba </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>Tambah Data Lomba</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">Kode</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="kode" value="<?php echo $kode; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Nama Lomba</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="nama_lomba" value="<?php echo $nama_lomba; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Tanggal Lomba</label>
                    <div class="col-sm-6">
                        <input type="date" class="form-control" name="tanggal_lomba"
                            value="<?php echo $tanggal_lomba; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Lokasi Lomba</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="lokasi_lomba"
                            value="<?php echo $lokasi_lomba; ?>">
                    </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="datalombacch.php" role="button">Batal</a>
                    </div>
                </div>
            </form>

        </div>
    </body>

    </html>