<?php
// Koneksi ke database
$koneksi = new mysqli("localhost", "root", "", "sistempakarmamdani");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'get': // Mendapatkan data berdasarkan ID
                $id = $_POST['id'];
                $result = $koneksi->query("SELECT * FROM rule WHERE idrule = '$id'");
                $data = $result->fetch_assoc();
                echo json_encode($data);
                break;

            case 'edit': // Memperbarui data
                $id = $_POST['idrule'];
                $koderule = $_POST['koderule'];
                $kodegejala = $_POST['kodegejala'];
                $kodepenyakit = $_POST['kodepenyakit'];
                $koneksi->query("UPDATE rule SET koderule='$koderule', kodegejala='$kodegejala', kodepenyakit='$kodepenyakit' WHERE idrule='$id'");
                echo json_encode(["success" => true]);
                break;

            case 'delete': // Menghapus data
                $id = $_POST['id'];
                $koneksi->query("DELETE FROM rule WHERE idrule='$id'");
                echo json_encode(["success" => true]);
                break;
        }
        exit;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Data Rule</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-4">
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <a href="index.php?halaman=ruletambah" class="btn btn-sm btn-primary shadow-sm float-right pull-right">
            <i class="fas fa-plus fa-sm text-white-50"></i> Tambah Rule
        </a>
    </div>
    <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">Data Rule</h6>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered" id="table">
                            <thead class="bg-white text-dark">
                                <tr>
                                    <th>No</th>
                                    <th>Kode Rule</th>
                                    <th>Gejala</th>
                                    <th>Penyakit</th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $nomor = 1; ?>
                                <?php $ambil = $koneksi->query("SELECT * FROM rule LEFT JOIN penyakit ON rule.kode_penyakit = penyakit.kode_penyakit"); ?>
                                <?php while ($data = $ambil->fetch_assoc()) { ?>
                                    <tr>
                                        <td><?php echo $nomor ?></td>
                                        <td align="left"><?php echo $data["koderule"] ?></td>
                                        <td><?php echo $data["kodegejala"] ?></td>
                                        <td><?php echo $data["kode_penyakit"] . ' - ' . $data['namapenyakit'] ?></td>
                                    <td>
                                        <a href="index.php?halaman=ruleedit&id=<?php echo $data['idrule']; ?>" class="btn btn-warning btn-sm m-1">Ubah</a>
                                        <a href="javascript:void(0)" class="btn btn-danger btn-sm m-1 btn-delete" data-id="<?php echo $data['idrule']; ?>">Hapus</a>
                                    </td>
                                    </tr>
                                    <?php $nomor++; ?>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal Edit -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editModalLabel">Edit Rule</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form id="editForm">
                <div class="modal-body">
                    <input type="hidden" name="idrule" id="edit-idrule">
                    <div class="form-group">
                        <label>Kode Rule</label>
                        <input type="text" class="form-control" name="koderule" id="edit-koderule" required>
                    </div>
                    <div class="form-group">
                        <label>Kode Gejala</label>
                        <input type="text" class="form-control" name="kodegejala" id="edit-kodegejala" required>
                    </div>
                    <div class="form-group">
                        <label>Kode Penyakit</label>
                        <input type="text" class="form-control" name="kodepenyakit" id="edit-kodepenyakit" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                    <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
$(document).ready(function () {
    // Event untuk membuka modal edit
    $(document).on('click', '.btn-edit', function () {
        const id = $(this).data('id');
        $.ajax({
            url: 'ruleedit.php',
            method: 'POST',
            data: { id: id, action: 'get' },
            dataType: 'json',
            success: function (data) {
                $('#edit-idrule').val(data.idrule);
                $('#edit-koderule').val(data.koderule);
                $('#edit-kodegejala').val(data.kodegejala);
                $('#edit-kodepenyakit').val(data.kodepenyakit);
                $('#editModal').modal('show');
            }
        });
    });

    // Event untuk menyimpan perubahan (edit)
    $('#editForm').submit(function (e) {
        e.preventDefault();
        const formData = $(this).serialize() + '&action=edit';
        $.ajax({
            url: '',
            method: 'POST',
            data: formData,
            success: function (response) {
                alert('Data berhasil diubah!');
                $('#editModal').modal('hide');
                location.reload();
            }
        });
    });

    // Event untuk menghapus data
    $(document).on('click', '.btn-delete', function () {
        const id = $(this).data('id');
        if (confirm('Apakah Anda yakin ingin menghapus data ini?')) {
            $.ajax({
                url: '',
                method: 'POST',
                data: { id: id, action: 'delete' },
                success: function (response) {
                    location.reload();
                }
            });
        }
    });
});
</script>
</body>
</html>