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

$id = filterRequest("id");

// Create an empty array to store the column-value pairs
$columnValues = array();

// Check if each column is set in the request and add it to the array
if (isset($_POST["amount"])) {
    $amount = filterRequest("amount");
    $columnValues[] = "`amount` = '$amount'";
}

if (isset($_POST["payment_method"])) {
    $payment_method = filterRequest("payment_method");
    $columnValues[] = "`payment_method` = '$payment_method'";
}

if (isset($_POST["passengerID"])) {
    $passengerID = filterRequest("passengerID");
    $columnValues[] = "`passengerID` = '$passengerID'";
}

if (isset($_POST["rideId"])) {
    $rideId = filterRequest("rideId");
    $columnValues[] = "`rideId` = '$rideId'";
}

if (isset($_POST["driverID"])) {
    $driverID = filterRequest("driverID");
    $columnValues[] = "`driverID` = '$driverID'";
}

if (isset($_POST["created_at"])) {
    $created_at = filterRequest("created_at");
    $columnValues[] = "`created_at` = '$created_at'";
}

if (isset($_POST["updated_at"])) {
    $updated_at = filterRequest("updated_at");
    $columnValues[] = "`updated_at` = '$updated_at'";
}

if (isset($_POST["isGiven"])) {
    $isGiven = filterRequest("isGiven");
    $columnValues[] = "`isGiven` = '$isGiven'";
}

// Construct the SET clause of the update query using the column-value pairs
$setClause = implode(", ", $columnValues);

$sql = "UPDATE `payments` SET $setClause WHERE `id` = '$id'";

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

if ($stmt->rowCount() > 0) {
    // Print a success message
    jsonSuccess($message = "Payment data updated successfully");
} else {
    // Print a failure message
    jsonError($message = "Failed to update payment data");
}
?>