<?php include("../cn.php");
include("../info.php");
// https://stackoverflow.com/questions/9541029/insert-current-date-in-datetime-format-mysql

$driver = $_REQUEST["id"];
$bookingno = $_REQUEST["bookingno"];
$amount = $_REQUEST["amount"];
$dropoff = $_REQUEST["destination"];

$user="";

$sql = mysqli_query($connect, "SELECT rentedcars.*, cars.car_name AS car_title, cars.car_nameplate AS car_number, 
driver.driver_name as driver_name
FROM rentedcars 
INNER JOIN cars ON cars.car_id = rentedcars.car_id
LEFT JOIN driver ON driver.driver_id = rentedcars.driver_id
WHERE `BookingNumber`='".$bookingno."'");

$sql_1 = mysqli_query($connect,"select * from app_users where
Id = '".$_SESSION["userid"]."' ");
$data_1 = mysqli_fetch_assoc($sql_1);

$data = mysqli_fetch_assoc($sql);
$user = $data['AppUser'];

$sql1 = mysqli_query($connect, "SELECT * FROM app_users 
WHERE `Id`='".$user."'");

$customer = mysqli_fetch_assoc($sql1);

$user = $data['driver_id'];

$driver_name = $data["driver_name"];
$name = $customer["Name"];
$email = $customer["Email"];
$phone = $customer["Phone"];
$tripendedat = $data["EndTime"];
$hotel = $data["customer_hotel"];
$room = $data["customer_room"];
$flight = $data["customer_flight"]; 
$car_title = $data["car_title"];
$car_number = $data["car_number"];
$pickup = $data["pickup"];
$tripstartedat = $data['StartTime'];
$comments = $_REQUEST["Comments"];
$password = '123456';
$address = $_REQUEST["address"];
$driver = $_SESSION["driverid"];
$payoption = $_POST["pay_option"];
$driveremail = $data_1["Email"];
$paymentreference = $_POST["PaymentReference"];
$addifare = $_POST["addifare"];

if($payoption == "card"){
    $paymentmethod = "Credit Card";
}
elseif($payoption == "room"){
    $paymentmethod = "Room";
}
elseif($payoption == "cash"){
    $paymentmethod = "Cash";
}
elseif($payoption == "Hotel"){
    $paymentmethod = "Hotel";
}


$sql = "UPDATE rentedcars SET  Destination = '$dropoff', total_amount = '$amount', fare = '$amount',`PaymentStatus`='$paymentmethod', `PaymentReference`='$paymentreference', `addifare`='$addifare' WHERE  `BookingNumber`='$bookingno'";

if($collectcashemail == 1){
    if($customeremailnotification == 1 && $driveremailnotification == 1){
        $to = $email;
        $to .= ",";
        $to .= $driveremail;
    }
    elseif($customeremailnotification == 1){
        $to = $email;
    }
    elseif($driveremailnotification == 1){
        $to = $driveremail;
    }
else{
    $to;
    }

$subject = "Payment Mode";
$txt = "Booking Number: $bookingno\nDriver Name: $driver_name\nCustomer Name: $name\nEmail: $email\nPhone: $phone\nAmount: $amount\nHotel: $hotel\nRoom: $room\nFlight: $flight\nCar: $car_title\nCar Number: $car_number\nPick Up Location: $pickup\nDrop Off Location: $destination\nTrip Started At: $starttime";
$headers = "From: $companyemailnotifcaiton" . "\r\n" .
"CC: $companyemailnotifcaiton";

mail($to,$subject,$txt,$headers);
}

// $myfile = fopen("testfile.txt", "w");
// fwrite($myfile, $sql);
// fclose($myfile);

$result = mysqli_query($connect, $sql);

$query = "UPDATE driver SET driver_availability = 'yes' WHERE driver_id = $driver";
$result1 = mysqli_query($connect, $query);

// $response["count"] = mysqli_affected_rows($result);

if ($result && $result1)
{
$response["code"] = "200";
$response["msg"] = "Receipt Recorded Successfully";
}
else
{
    $response["code"] = "202";
    $response["msg"] = "Receipt Failed";
}

echo 
'<h1>Amount (AED.): '.$amount.
'<br/> Received against <br/> Booking No. '.
$bookingno.'</h1>'.
'You will be redirected for new booking  within 3 Seconds.<br/> <a href="../index.php">Click here to Go Now</a>';

header( "Refresh:0; url=../bookingdetail.php?id=".$bookingno, true, 303);
// echo json_encode($response);

?>