<?php
// Mengambil data berdasarkan ID untuk ditampilkan di form edit
$id = $_GET['id'];
$ambil = $koneksi->query("SELECT * FROM penyakit_gejala WHERE id_penyakit_gejala='$id'");
$data = $ambil->fetch_assoc();
?>

<div class="row">
    <div class="col-md-12 mb-4">
        <div class="card shadow mb-4">
            <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                <h6 class="m-0 font-weight-bold">Edit Data Gejala Penyakit</h6>
            </div>
            <div class="card-body">
                <form method="post">
                    <div class="form-group">
                        <label>Gejala</label>
                        <select name="kodegejala" class="form-control select2" required>
                            <option value="">--Pilih--</option>
                            <?php
                            $ambilgejala = $koneksi->query("SELECT * FROM gejala");
                            while ($rowg = $ambilgejala->fetch_assoc()) { ?>
                                <option value="<?= $rowg['kodegejala'] ?>" <?= ($rowg['kodegejala'] == $data['kode_gejala']) ? 'selected' : '' ?>>
                                    <?= $rowg['kodegejala'] ?> - <?= $rowg['gejala'] ?>
                                </option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Penyakit</label>
                        <select name="kode_penyakit" class="form-control select2" required>
                            <option value="">--Pilih--</option>
                            <?php
                            $ambilpenyakit = $koneksi->query("SELECT * FROM penyakit");
                            while ($rowp = $ambilpenyakit->fetch_assoc()) { ?>
                                <option value="<?= $rowp['kode_penyakit'] ?>" <?= ($rowp['kode_penyakit'] == $data['kode_penyakit']) ? 'selected' : '' ?>>
                                    <?= $rowp['kode_penyakit'] ?> - <?= $rowp['namapenyakit'] ?>
                                </option>
                            <?php } ?>
                        </select>
                    </div>

                    <div class="form-group">
                        <label>Nilai Interpretasi</label>
                        <select name="nilai" class="form-control" required>
                            <option value="">--Pilih--</option>
                            <?php
                            $ambilnilai = $koneksi->query("SELECT * FROM interpretasi_fuzzy");
                            while ($rown = $ambilnilai->fetch_assoc()) { ?>
                                <option value="<?= $rown['nilai_interpretasi'] ?>" <?= ($rown['nilai_interpretasi'] == $data['nilai']) ? 'selected' : '' ?>>
                                    <?= $rown['kategori'] ?> - <?= $rown['nilai_interpretasi'] ?>
                                </option>
                            <?php } ?>
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary" name="update">Update</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php
if (isset($_POST['update'])) {
    $kode_gejala = mysqli_real_escape_string($koneksi, $_POST['kodegejala']);
    $kode_penyakit = mysqli_real_escape_string($koneksi, $_POST['kode_penyakit']);
    $nilai = mysqli_real_escape_string($koneksi, $_POST['nilai']);

    $koneksi->query("UPDATE penyakit_gejala SET kode_gejala='$kode_gejala', kode_penyakit='$kode_penyakit', nilai='$nilai' WHERE id_penyakit_gejala='$id'");
    echo "<script>alert('Data Berhasil Diupdate');</script>";
    echo "<script>location ='index.php?halaman=nilaigejalapenyakitdaftar';</script>";
}
?>