<?php
include "config.php"; // DB connection

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['id'])) {
        $id = mysqli_real_escape_string($con, $_POST['id']);

        // Get current is_delete value
        $checkQuery = "SELECT is_delete FROM spoffice WHERE id = '$id'";
        $result = mysqli_query($con, $checkQuery);

        if ($row = mysqli_fetch_assoc($result)) {
            $currentStatus = $row['is_delete'];
            $newStatus = ($currentStatus == 1) ? 0 : 1;

            // Toggle is_delete value
            $updateQuery = "UPDATE spoffice SET is_delete = '$newStatus' WHERE id = '$id'";

            if (mysqli_query($con, $updateQuery)) {
                                                       echo '<script>window.location.href = "sp_office.php";</script>';
             } else {
                echo "Error updating record: " . mysqli_error($con);
            }
        } else {
            echo "Record not found.";
        }
    } else {
        echo "Invalid request: ID missing.";
    }
} else {
    echo "Invalid request method.";
}
?>