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

$id = filterRequest("id");

$fields = [];
$params = [':id' => $id];

$possibleFields = [
    'start_location', 'end_location', 'date', 'time', 'price',
    'passenger_id', 'status', 'carType', 'passengerRate',
    'price_for_passenger', 'distance', 'duration'
];

foreach ($possibleFields as $field) {
    if (isset($_POST[$field])) {
        $value = filterRequest($field);
        $fields[] = "`$field` = :$field";
        $params[":$field"] = $value;
    }
}

if (empty($fields)) {
    jsonError("No fields provided for update");
    exit;
}

$setClause = implode(", ", $fields);
$sql = "UPDATE `waitingRides` SET $setClause WHERE `id` = :id";

$stmt = $con->prepare($sql);
$stmt->execute($params);

if ($stmt->rowCount() > 0) {
    jsonSuccess(null, "Waiting ride data updated successfully");
} else {
    jsonError("Failed to update waiting ride data");
}
?>