<?php
session_start();

include '../koneksi.php';

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

$id_dokter = $_GET['id'];

// Ambil data dokter berdasarkan ID
$query = "SELECT * FROM tbl_dokter WHERE id_dokter = '$id_dokter'";
$result = mysqli_query($koneksi, $query);
$dokter = mysqli_fetch_assoc($result);

// Jika form di-submit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_dokter = $_POST['nama_dokter'];
    $spesialis = $_POST['spesialis'];
    $keterangan = $_POST['keterangan'];
    $id_poli = $_POST['id_poli'];

    // Query untuk update data dokter
    $update_query = "UPDATE tbl_dokter SET nama_dokter='$nama_dokter', spesialis='$spesialis', keterangan='$keterangan', id_poli='$id_poli' WHERE id_dokter='$id_dokter'";
    
    if (mysqli_query($koneksi, $update_query)) {
        // Redirect ke halaman dokter setelah update
        header("Location: dokter.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 Dokter</h1>

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

                            <div class="form-group mb-3">
                                <label for="spesialis">Spesialis</label>
                                <input type="text" class="form-control" id="spesialis" name="spesialis" value="<?php echo $dokter['spesialis']; ?>" required>
                            </div>

                            <div class="form-group mb-3">
                                <label for="keterangan">Keterangan</label>
                                <input type="text" class="form-control" id="keterangan" name="keterangan" value="<?php echo $dokter['keterangan']; ?>" required>
                            </div>

                            <div class="form-group">
                                <label for="id_poli">Poli</label>
                                <select class="form-control" id="id_poli" name="id_poli" required>
                                    <?php
                                    $poli_query = mysqli_query($koneksi, "SELECT id_poli, nama_poli FROM tbl_poli");
                                    while ($poli = mysqli_fetch_assoc($poli_query)) {
                                        $selected = ($dokter['id_poli'] == $poli['id_poli']) ? 'selected' : '';
                                        echo "<option value='" . $poli['id_poli'] . "' $selected>" . $poli['nama_poli'] . "</option>";
                                    }
                                    ?>
                                </select>
                            </div>

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

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

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