<?php
session_start();
include '../koneksi.php';

// Cek jika jadwal yang akan diedit telah dipilih
if (!isset($_GET['id'])) {
    header("Location: jadwal.php"); // Jika tidak ada ID yang dipilih, kembalikan ke halaman data jadwal
    exit();
}

$id_jadwal = $_GET['id'];

// Ambil data jadwal berdasarkan ID
$query = "SELECT * FROM tbl_jadwal WHERE id_jadwal = '$id_jadwal'";
$result = mysqli_query($koneksi, $query);
$jadwal = mysqli_fetch_assoc($result);

// Jika form di-submit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $hari = $_POST['hari'];
    $jam_mulai = $_POST['jam_mulai'];
    $jam_selesai = $_POST['jam_selesai'];
    $id_dokter = $_POST['id_dokter'];

    // Query untuk update data jadwal
    $update_query = "UPDATE tbl_jadwal SET hari='$hari', jam_mulai='$jam_mulai', jam_selesai='$jam_selesai', id_dokter='$id_dokter' WHERE id_jadwal='$id_jadwal'";
    
    if (mysqli_query($koneksi, $update_query)) {
        // Redirect ke halaman jadwal setelah update
        header("Location: jadwal.php");
        exit();
    } else {
        echo "Error updating record: " . mysqli_error($koneksi);
    }
}

?>

<?php include 'header.php'; ?>

<div id="wrapper">
    <div id="content-wrapper" class="d-flex flex-column">
        <div id="content">
            <div class="container-fluid">
                <h1 class="h3 mb-4 text-gray-800">Edit Data Jadwal</h1>

                <div class="card shadow mb-4">
                    <div class="card-header py-3">
                        <h6 class="m-0 font-weight-bold text-primary">Edit Jadwal</h6>
                    </div>
                    <div class="card-body">
                        <form action="" method="post">
                            <div class="form-group mb-3">
                                <label for="hari">Hari</label>
                                <input type="text" class="form-control" id="hari" name="hari" value="<?php echo $jadwal['hari']; ?>" required>
                            </div>

                            <div class="form-group mb-3">
                                <label for="jam_mulai">Jam Praktek</label>
                                <input type="time" class="form-control" id="jam_mulai" name="jam_mulai" value="<?php echo $jadwal['jam_mulai']; ?>" required>
                            </div>

                            <div class="form-group mb-3">
                                <label for="jam_selesai">Jam Selesai</label>
                                <input type="time" class="form-control" id="jam_selesai" name="jam_selesai" value="<?php echo $jadwal['jam_selesai']; ?>" required>
                            </div>

                            <div class="form-group">
                                <label for="id_dokter">Dokter</label>
                                <select class="form-control" id="id_dokter" name="id_dokter" required>
                                    <?php
                                    $dokter_query = mysqli_query($koneksi, "SELECT id_dokter, nama_dokter FROM tbl_dokter");
                                    while ($dokter = mysqli_fetch_assoc($dokter_query)) {
                                        $selected = ($dokter['id_dokter'] == $jadwal['id_dokter']) ? 'selected' : '';
                                        echo "<option value='" . $dokter['id_dokter'] . "' $selected>" . $dokter['nama_dokter'] . "</option>";
                                    }
                                    ?>
                                </select>
                            </div>

                            <input type="submit" class="btn btn-primary" value="Simpan Perubahan">
                            <a href="jadwal.php" class="btn btn-secondary">Batal</a>
                        </form>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
