false, 'message' => 'Hanya menerima POST']); exit; } $houseId = intval($_POST['house_id'] ?? 0); $status = $conn->real_escape_string($_POST['status'] ?? 'green'); $centerId = intval($_POST['center_id'] ?? 0); // Validasi status $validStatuses = ['green', 'red', 'yellow']; if (!in_array($status, $validStatuses)) { echo json_encode(['success' => false, 'message' => 'Status tidak valid']); exit; } // Validasi house_id if ($houseId < 1) { echo json_encode(['success' => false, 'message' => 'house_id tidak valid']); exit; } // ======================== // Upsert status rumah // ======================== // Cek apakah rumah sudah ada $check = $conn->query("SELECT id FROM houses WHERE id = $houseId"); if ($check && $check->num_rows > 0) { // Update $sql = "UPDATE houses SET status='$status' WHERE id=$houseId"; } else { // Insert (ambil posisi dari fixed positions yang sama seperti di JS) // Posisi disimpan juga agar konsisten $lat = floatval($_POST['lat'] ?? 0); $lng = floatval($_POST['lng'] ?? 0); $sql = "INSERT INTO houses (id, latitude, longitude, status) VALUES ($houseId, $lat, $lng, '$status')"; } if ($conn->query($sql)) { // ======================== // Catat ke aid_logs // ======================== if ($status === 'yellow' && $centerId > 0) { $logSql = "INSERT INTO aid_logs (house_id, religious_center_id, status, timestamp) VALUES ($houseId, $centerId, 'helped', NOW())"; $conn->query($logSql); } elseif ($status !== 'yellow') { // Batalkan bantuan — catat sebagai reverted $logSql = "INSERT INTO aid_logs (house_id, religious_center_id, status, timestamp) VALUES ($houseId, $centerId, 'reverted', NOW())"; $conn->query($logSql); } echo json_encode([ 'success' => true, 'house_id' => $houseId, 'status' => $status ]); } else { echo json_encode(['success' => false, 'message' => $conn->error]); } $conn->close();