60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
<?php
|
|
include "koneksi.php";
|
|
header('Content-Type: application/json');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
echo json_encode(["status" => "error", "message" => "Method tidak diizinkan"]);
|
|
exit;
|
|
}
|
|
|
|
if (!isset($_POST['id_occupant']) || empty($_POST['id_occupant'])) {
|
|
echo json_encode(["status" => "error", "message" => "ID tidak ditemukan"]);
|
|
exit;
|
|
}
|
|
|
|
$id = $_POST['id_occupant'];
|
|
|
|
if (!is_numeric($id)) {
|
|
echo json_encode(["status" => "error", "message" => "ID tidak valid"]);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
// Ambil status saat ini
|
|
$get = $pdo->prepare("SELECT status FROM occupant WHERE id_occupant = ?");
|
|
$get->execute([$id]);
|
|
$row = $get->fetch();
|
|
|
|
if (!$row) {
|
|
echo json_encode(["status" => "error", "message" => "Data tidak ditemukan"]);
|
|
exit;
|
|
}
|
|
|
|
// Toggle: approved <-> rejected AND active <-> suspend
|
|
if ($row['status'] === 'approved') {
|
|
$newStatus = 'rejected';
|
|
$newAccess = 'suspend';
|
|
}
|
|
else {
|
|
$newStatus = 'approved';
|
|
$newAccess = 'active';
|
|
}
|
|
|
|
$stmt = $pdo->prepare("UPDATE occupant SET status = ?, access = ? WHERE id_occupant = ?");
|
|
$stmt->execute([$newStatus, $newAccess, $id]);
|
|
|
|
echo json_encode([
|
|
"status" => "success",
|
|
"new_status" => $newStatus,
|
|
"new_access" => $newAccess
|
|
]);
|
|
|
|
}
|
|
catch (PDOException $e) {
|
|
echo json_encode([
|
|
"status" => "error",
|
|
"message" => "Gagal mengupdate: " . $e->getMessage()
|
|
]);
|
|
}
|
|
?>
|