<?php
require_once __DIR__ . '/../../connect.php';
$driverId = filterRequest("driverId");

$sql = "
SELECT
    mi.`id`,
    mi.`driverId`,
    mi.`phone`,
    mi.`gender`,
    mi.`name`,
    mi.`name_english`,
    mi.`address`,
    mi.`religion`,
    mi.`age`,
    mi.`education`,
    mi.`license_type`,
    mi.`national_number`,
    mi.`car_plate`,
    mi.`make`,
    mi.`model`,
    mi.`color`,
    mi.`color_hex`,
    mi.`token`,
    mi.`rating`,
    mi.`countRide`,
    mi.`passengerId`,
    mi.`timeSelected`,
    mi.`createdAt`,
    mi.`status`,
    p.phone AS passengerPhone,
    p.first_name AS passengerName,
    p.last_name AS passengerLastName,
    p.gender AS passengergender,
    d.name_arabic,
    tkp.token AS passengerToken
FROM
    `mishwaritrips` mi
LEFT JOIN driver d ON
    d.id = mi.driverId
LEFT JOIN passengers p ON
    p.id = mi.passengerId
LEFT JOIN tokens tkp ON
    tkp.passengerID = mi.passengerId
WHERE
   mi. driverId = :driverId
   AND mi.createdAt >= CURDATE() - INTERVAL 4 DAY
     AND mi.timeSelected > NOW()
ORDER BY
   mi. `createdAt`
DESC
LIMIT 1

     ";
$stmt = $con->prepare($sql);
$stmt->bindParam(':driverId', $driverId, PDO::PARAM_STR);
$stmt->execute();

if ($stmt->rowCount() > 0) {
    // Fetch the record
    $row = $stmt->fetchAll(PDO::FETCH_ASSOC);

    jsonSuccess($row);
    
}
    else{
    // Print a failure message
    jsonError($message = "No wallet record found");
}
?>