<?php
include '../config/connection.php';
$id = intval($_POST['id']);
$status = $connection->real_escape_string($_POST['status']);
echo $connection->query("UPDATE permintaan SET status='$status' WHERE id_permintaan='$id'") ? "success" : "error";
?>
<script>
// ===================== UPDATE STATUS DENGAN TOMBOL SIMPAN =====================
document.querySelectorAll(".simpan-status").forEach(btn => {
    btn.addEventListener("click", function() {
        const id = this.dataset.id;
        const statusSelect = document.getElementById("status-" + id);
        const status = statusSelect.value;

        fetch("", {
            method: "POST",
            headers: { "Content-Type": "application/x-www-form-urlencoded" },
            body: "ajax_update=true&id=" + encodeURIComponent(id) + "&status=" + encodeURIComponent(status)
        })
        .then(r => r.text())
        .then(res => {
            if (res.trim() === "success") {
                Swal.fire({
                    icon: "success",
                    title: "Status berhasil diperbarui!",
                    showConfirmButton: false,
                    timer: 1500
                });
            } else {
                Swal.fire({
                    icon: "error",
                    title: "Gagal memperbarui status!",
                    showConfirmButton: true
                });
            }
        })
        .catch(err => {
            console.error("Error:", err);
            Swal.fire({
                icon: "error",
                title: "Terjadi kesalahan koneksi!"
            });
        });
    });
});

</script>
