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

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

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

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

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

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

    $id = $_GET["id"];

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

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

    $kode = $row["kode"];
    $nama_lomba = $row["nama_lomba"];
    $tanggal_lomba = $row["tanggal_lomba"];
    $lokasi_lomba = $row["lokasi_lomba"];
} else {

    $id = $_POST["id"];
    $kode = $_POST["kode"];
    $nama_lomba = $_POST["nama_lomba"];
    $tanggal_lomba = $_POST["tanggal_lomba"];
    $lokasi_lomba = $_POST["lokasi_lomba"];

    do {
        $sql = "UPDATE lomba
        SET kode = '$kode', 
        nama_lomba='$nama_lomba', 
        tanggal_lomba='$tanggal_lomba', 
        lokasi_lomba='$lokasi_lomba'
        WHERE id=$id";

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

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

        $successMessage = "Data berhasil diupdate";

        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">
        <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 Data Lomba</h2>

            <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">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>

                <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>