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

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

$id = "";
$tingkatan = "";
$harilat1 = "";
$jamlat1 = "";
$harilat2 = "";
$jamlat2 = "";
$harilat3 = "";
$jamlat3 = "";
$harilat4 = "";
$jamlat4 = "";
$harilat5 = "";
$jamlat5 = "";

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

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

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

    $id = $_GET["id"];

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

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

    $tingkatan = $row["tingkatan"];
    $harilat1 = $row["harilat1"];
    $jamlat1 = $row["jamlat1"];
    $harilat2 = $row["harilat2"];
    $jamlat2 = $row["jamlat2"];
    $harilat3 = $row["harilat3"];
    $jamlat3 = $row["jamlat3"];
    $harilat4 = $row["harilat4"];
    $jamlat4 = $row["jamlat4"];
    $harilat5 = $row["harilat5"];
    $jamlat5 = $row["jamlat5"];
} else {

    $id = $_POST["id"];
    $tingkatan = $_POST["tingkatan"];
    $harilat1 = $_POST["harilat1"];
    $jamlat1 = $_POST["jamlat1"];
    $harilat2 = $_POST["harilat2"];
    $jamlat2 = $_POST["jamlat2"];
    $harilat3 = $_POST["harilat3"];
    $jamlat3 = $_POST["jamlat3"];
    $harilat4 = $_POST["harilat4"];
    $jamlat4 = $_POST["jamlat4"];
    $harilat5 = $_POST["harilat5"];
    $jamlat5 = $_POST["jamlat5"];

    do {
        $sql = "UPDATE latihan
        SET tingkatan = '$tingkatan', 
        harilat1='$harilat1', 
        jamlat1='$jamlat1', 
        harilat2='$harilat2', 
        jamlat2='$jamlat2', 
        harilat3='$harilat3', 
        jamlat3='$jamlat3', 
        harilat4='$harilat4', 
        jamlat4='$jamlat4', 
        harilat5='$harilat5', 
        jamlat5='$jamlat5'
        WHERE id=$id";

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

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

        $successMessage = "Data berhasil diupdate";

        header("location:latihancch.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 Jadwal Latihan</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">Tingkatan</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="tingkatan" value="<?php echo $tingkatan; ?>"
                            readonly>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Hari Latihan 1</label>
                    <div class="col-sm-6">
                        <select name="harilat1" class="form-control" value="<?php echo $harilat1; ?>">
                            <option value="">---Pilih Hari---</option>
                            <option <?php if ($harilat1 == 'Senin') {
                                echo "selected";
                            } ?> value="Senin">Senin</option>
                            <option <?php if ($harilat1 == 'Selasa') {
                                echo "selected";
                            } ?> value="Selasa">Selasa</option>
                            <option <?php if ($harilat1 == 'Rabu') {
                                echo "selected";
                            } ?> value="Rabu">Rabu</option>
                            <option <?php if ($harilat1 == 'Kamis') {
                                echo "selected";
                            } ?> value="Kamis">Kamis</option>
                            <option <?php if ($harilat1 == 'Jumat') {
                                echo "selected";
                            } ?> value="Jumat">Jumat</option>
                            <option <?php if ($harilat1 == 'Sabtu') {
                                echo "selected";
                            } ?> value="Sabtu">Sabtu</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Jam Latihan 1</label>
                    <div class="col-sm-6">
                        <input type="time" class="form-control" name="jamlat1" value="<?php echo $jamlat1; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Hari Latihan 2</label>
                    <div class="col-sm-6">
                        <select name="harilat2" class="form-control" value="<?php echo $harilat2; ?>">
                            <option value="">---Pilih Hari---</option>
                            <option <?php if ($harilat2 == 'Senin') {
                                echo "selected";
                            } ?> value="Senin">Senin</option>
                            <option <?php if ($harilat2 == 'Selasa') {
                                echo "selected";
                            } ?> value="Selasa">Selasa</option>
                            <option <?php if ($harilat2 == 'Rabu') {
                                echo "selected";
                            } ?> value="Rabu">Rabu</option>
                            <option <?php if ($harilat2 == 'Kamis') {
                                echo "selected";
                            } ?> value="Kamis">Kamis</option>
                            <option <?php if ($harilat2 == 'Jumat') {
                                echo "selected";
                            } ?> value="Jumat">Jumat</option>
                            <option <?php if ($harilat2 == 'Sabtu') {
                                echo "selected";
                            } ?> value="Sabtu">Sabtu</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Jam Latihan 2</label>
                    <div class="col-sm-6">
                        <input type="time" class="form-control" name="jamlat2" value="<?php echo $jamlat2; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Hari Latihan 3</label>
                    <div class="col-sm-6">
                        <select name="harilat3" class="form-control" value="<?php echo $harilat3; ?>">
                            <option value="">---Pilih Hari---</option>
                            <option <?php if ($harilat3 == 'Senin') {
                                echo "selected";
                            } ?> value="Senin">Senin</option>
                            <option <?php if ($harilat3 == 'Selasa') {
                                echo "selected";
                            } ?> value="Selasa">Selasa</option>
                            <option <?php if ($harilat3 == 'Rabu') {
                                echo "selected";
                            } ?> value="Rabu">Rabu</option>
                            <option <?php if ($harilat3 == 'Kamis') {
                                echo "selected";
                            } ?> value="Kamis">Kamis</option>
                            <option <?php if ($harilat3 == 'Jumat') {
                                echo "selected";
                            } ?> value="Jumat">Jumat</option>
                            <option <?php if ($harilat3 == 'Sabtu') {
                                echo "selected";
                            } ?> value="Sabtu">Sabtu</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Jam Latihan 3</label>
                    <div class="col-sm-6">
                        <input type="time" class="form-control" name="jamlat3" value="<?php echo $jamlat3; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Hari Latihan 4</label>
                    <div class="col-sm-6">
                        <select name="harilat4" class="form-control" value="<?php echo $harilat4; ?>">
                            <option value="">---Pilih Hari---</option>
                            <option <?php if ($harilat4 == 'Senin') {
                                echo "selected";
                            } ?> value="Senin">Senin</option>
                            <option <?php if ($harilat4 == 'Selasa') {
                                echo "selected";
                            } ?> value="Selasa">Selasa</option>
                            <option <?php if ($harilat4 == 'Rabu') {
                                echo "selected";
                            } ?> value="Rabu">Rabu</option>
                            <option <?php if ($harilat4 == 'Kamis') {
                                echo "selected";
                            } ?> value="Kamis">Kamis</option>
                            <option <?php if ($harilat4 == 'Jumat') {
                                echo "selected";
                            } ?> value="Jumat">Jumat</option>
                            <option <?php if ($harilat4 == 'Sabtu') {
                                echo "selected";
                            } ?> value="Sabtu">Sabtu</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Jam Latihan 4</label>
                    <div class="col-sm-6">
                        <input type="time" class="form-control" name="jamlat4" value="<?php echo $jamlat4; ?>">
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Hari Latihan 5</label>
                    <div class="col-sm-6">
                        <select name="harilat5" class="form-control" value="<?php echo $harilat5; ?>">
                            <option value="">---Pilih Hari---</option>
                            <option <?php if ($harilat5 == 'Senin') {
                                echo "selected";
                            } ?> value="Senin">Senin</option>
                            <option <?php if ($harilat5 == 'Selasa') {
                                echo "selected";
                            } ?> value="Selasa">Selasa</option>
                            <option <?php if ($harilat5 == 'Rabu') {
                                echo "selected";
                            } ?> value="Rabu">Rabu</option>
                            <option <?php if ($harilat5 == 'Kamis') {
                                echo "selected";
                            } ?> value="Kamis">Kamis</option>
                            <option <?php if ($harilat5 == 'Jumat') {
                                echo "selected";
                            } ?> value="Jumat">Jumat</option>
                            <option <?php if ($harilat5 == 'Sabtu') {
                                echo "selected";
                            } ?> value="Sabtu">Sabtu</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-3">
                    <label class="col-sm-3 col-form-label">Jam Latihan 5</label>
                    <div class="col-sm-6">
                        <input type="time" class="form-control" name="jamlat5" value="<?php echo $jamlat5; ?>">
                    </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=" latihancch.php" role="button">Batal</a>
                    </div>
                </div>
            </form>

        </div>
    </body>

    </html>