<?php
require_once __DIR__ . '/../../connect.php';

// Fetch the input data
$id = filterRequest("id");  // Trip ID to identify the trip
$status = "cancelled";  // New status

// Check if the trip ID is provided
if (empty($id)) {
    jsonError("Trip ID is missing.");
    exit;
}

// Update the status of the trip to "cancelled"
$sql = "
UPDATE `mishwaritrips`
SET `status` = :status
WHERE `id` = :id AND `status` != 'cancelled'
";

$stmt = $con->prepare($sql);
$stmt->bindParam(':status', $status, PDO::PARAM_STR);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);  // Bind the ID parameter

// Execute the update
if ($stmt->execute()) {
    // Check if the update was successful
    if ($stmt->rowCount() > 0) {
        // Trip status updated successfully
        jsonSuccess(null, "Trip cancelled successfully.");
    } else {
        // No rows updated, meaning the trip might not have been found or was already cancelled
        jsonError("No trip found to cancel.");
    }
} else {
    // Print failure if the update query failed
    $errorInfo = $stmt->errorInfo();
    error_log('SQL Error: ' . implode(", ", $errorInfo));
    jsonError("Failed to cancel the trip.");
}
?>