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

$id = filterRequest("id");

// Create an empty array to store the column-value pairs
$columnValues = array();
$params = [':id' => $id];

// 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";
    $params[':amount'] = $amount;
}

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

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

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

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

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

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

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

// Construct the SET clause of the update query using the column-value pairs
$sql = "UPDATE `payments` SET $setClause WHERE `id` = :id";

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

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");
}
?>