<?php include("cn.php");?>
<?php $title = "Book Now";?>
		<!DOCTYPE html>
		<html lang="en">
<?php
include("head-tag.php");
    $sql_1 = mysqli_query($connect,"select * from app_users where
    Id = '".$_SESSION["userid"]."' ");
    $data_1 = mysqli_fetch_assoc($sql_1);
    $data_1['Image']="../admin/".$data_1['Image'];
?>

<body>
    <div class="inner-wrapper">
	    <?php include("pre-loader.php");?>
		<?php
include("header.php");
date_default_timezone_set('Asia/Dubai');


if ($_POST) {
// check if customer exists
// $customer=[];
$name = $_REQUEST["name"];
// $username = $_POST['username'];
$hotel = $_REQUEST["Hotel"];
$room = $_REQUEST["Room"];
$flight = $_REQUEST["Flight"];
$amount = $_REQUEST["Amount"];
$comments = $_REQUEST["Comments"];
$email = $_REQUEST["email"];
$password = '123456';
$phone = $_REQUEST["phone"];
$address = $_REQUEST["address"];
$guest = $_SESSION["guestid"];
$guestemail = $data_1["Email"];

if($booknowemail == 1){
    if($customeremailnotification == 1 && $guestemailnotification == 1){
        $to = $email;
        $to .= ",";
        $to .= $guestemail;
    }
    elseif($customeremailnotification == 1){
        $to = $email;
    }
    elseif($guestemailnotification == 1){
        $to = $guestemail;
    }
else{
    $to;
    }

$subject = "E Booking";
$txt = "Name: $name\nPhone: $phone\nHotel: $hotel\nRoom: $room\nFlight: $flight\nAmount: $amount\nComments: $comments\nCustomer Email: $email";
$headers = "From: info@qimsofttech.com" . "\r\n" .
"CC: $companyemailnotifcaiton";

mail($to,$subject,$txt,$headers);
}
$raw = 0;
$appuser =0;

function email_exists($conn1, $email1)
{
    $sql = "SELECT * FROM app_users WHERE `email` ='$email1'";
    
    $raw = mysqli_query($conn1, $sql);

    if ($email1=="" || !(mysqli_num_rows($raw) > 0) ) {
        return false;
    }
    else if (mysqli_num_rows($raw) > 0) {
        $raw = mysqli_fetch_assoc($raw);
        return true;
    }
}

function clean($string)
{
    return preg_replace("/[^A-Za-z\-0-9.]/i", "", $string); // Removes special chars.
}


    //hash code for email verification
    // https://code.tutsplus.com/tutorials/how-to-implement-email-verification-for-new-members--net-3824
    $hash = md5(rand(0, 1000));

    $picture = "default.png";

    if (!email_exists($connect, $email)){
        
        $stmt = "INSERT INTO app_users ( `Name`, `Email`, `Password`, `Phone`, `Image`, `address`,`ehash`) 
                        VALUES ('$name', '$email', '$password', '$phone', '$picture', '$address', '$hash')";

        // $stmt="INSERT INTO `app_users`( `Name`, `Email`, `Password`, `Phone`, `Image`) VALUES('$name', '$email', '$password', '$phone', '$picture')";

        $result = mysqli_query($connect, $stmt); // $result = mysqli_query($conn,$stmt);
        $result1;
        $appuser = $connect->insert_id;

        //uploading image
        
        // https://stackoverflow.com/questions/54066507/sending-image-to-server-with-android-image-cropper-returns-null-with-getcroppedi
        

        // $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
        // fwrite($myfile, $stmt);
        // fclose($myfile);
        
        if (!$result) {
            $response["code"] = "202";
            $response["msg"] = "Email is already in Use";
            //$response['msg'] = $stmt->error;
        } else {
            $response["code"] = "200";
            $response["msg"] =
                "You account has been created. ...Kindly Verify your Email from the Link. Please login now with your username/email address and password.";

//             // email verification goes here


//             $to = $email; // Send email to our user
//             $subject = $companyname."Signup | Verification"; // Give the email a subject
//             $message =
//                 '
  
// Thanks for signing up!
// Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.
  
// ------------------------
// Username: ' .
//                 $email .
//                 '
// Password: ' .
//                 $password .
//                 '
// ------------------------
  
// Please click this link to activate your account:
// $companyweb.'/'verify.php?email=' .
//                 $email .
//                 "&hash=" .
//                 $hash .
//                 '
  
// '; // Our message above including the link

//             $headers = "From:".$companyemailbooking."\r\n"; // Set from headers
//             mail($to, $subject, $message, $headers); // Send our email

            // email verification goes here
//
        }
    }

if ($email!="")
{
$sql = "SELECT * FROM app_users WHERE `email` ='$email'";

$raw = mysqli_query($connect, $sql);
$raw = mysqli_fetch_assoc($raw);
$appuser =$raw['Id'];
}
else
{
$sql = "SELECT max(Id) as Id FROM app_users";

$raw = mysqli_query($connect, $sql);
$raw = mysqli_fetch_assoc($raw);
$appuser =$raw['Id'];
}

    // $charge_type = $_REQUEST['radio1'];
    $charge_type = "trip";
    $car_id = $connect->real_escape_string($_POST['hidden_carid']);
    // $rent_start_date = date('Y-m-d H:i:s', strtotime($_POST['rent_start_date']));
    // $rent_end_date = date('Y-m-d H:i:s', strtotime($_POST['rent_end_date']));
    $rent_start_date = date('Y-m-d H:i:s');
    $pickup = $_REQUEST["Pickup"];
    $dropoff = $_REQUEST["Dropoff"];
    $return_status = "NR"; // not returned
    $status = "Pending";
    $paymentstatus = "Un-paid";
    $amount = $_REQUEST['Amount'];
    $hotel = $_REQUEST['Hotel'];
    $room = $_REQUEST['Room'];
    $flight = $_REQUEST['Flight'];
    $comments = $_REQUEST['Comments'];
    

    $car_name =0;
    $car_nameplate =0;
    // $client_name =0;
    // $client_phone =0;
    


    // function dateDiff($start, $end) {
    //     $start_ts = strtotime($start);
    //     $end_ts = strtotime($end);
    //     $diff = $end_ts - $start_ts;
    //     return round($diff / 86400);
    // }
    
    // $err_date = dateDiff("$rent_start_date", "$rent_end_date");

    $sql0 = "SELECT * FROM cars WHERE car_id = '$car_id'";
    $result0 = mysqli_query($connect,$sql0);

    if (mysqli_num_rows($result0) > 0) {
        while($row0 = mysqli_fetch_assoc($result0)) {

                if ($charge_type=="min")
                {
                    $fare = $row0["ac_price"];
                }
                else if ($charge_type=="days")
                {
                $fare = $row0["ac_price_per_day"];
            }
            else if ($charge_type=="trip") {
                $fare = $amount;
                $charge_type = "trip";
            }
        }
    }
    
    // if($err_date >= 0) {

    
    $sql1 = "INSERT INTO rentedcars(`car_id`,`booking_date`, `Name`,
    `rent_start_date`,
    `charge_type`,`pickup`,`destination`,`PaymentStatus`,
    `status`,`appuser`,`return_status`,`bookingsource`,
    `customer_hotel`,`customer_room`,`customer_flight`,`comments`,`fare`,`guest_id`
    ) 
    VALUES(" . $car_id . ",'" . date('Y-m-d H:i:s') ."','" .$name."',
    '" . $rent_start_date ."',
    '" . $charge_type . "','" . $pickup . "','" . $dropoff . "','" . $paymentstatus . "',
    '" . $status . "','" . $appuser . "','" . $return_status . "','Guest',
    '" . $hotel . "','" . $room . "','" . $flight . "','" . $comments . "'," . $fare . ",'" . $guest . "')";
    
// $myfile = fopen("testfile.txt", "w");
// fwrite($myfile, $sql1);
// fclose($myfile);

    $result1 = mysqli_query($connect,$sql1);
    $id = mysqli_insert_id($connect);

    $booking_number = date("Ymd").$id;
    mysqli_query($connect, "UPDATE rentedcars set bookingnumber = '$booking_number' WHERE id = '$id'");


    // echo($result1."<br>");

	$sql4 = "SELECT * FROM  cars c WHERE c.car_id = '$car_id'";
    $result4 = $connect->query($sql4);


    if (mysqli_num_rows($result4) > 0) {
        while($row = mysqli_fetch_assoc($result4)) {
            $car_name = $row["car_name"];
            $car_nameplate = $row["car_nameplate"];
        }
    }
        if (!$result0)
        {
            die("Couldnt enter data result0 ".$connect->error);
        }
        if (!$result1)
        {
            die("Couldnt enter data result1 ".$connect->error);
        }
        if (!$result4)
        {
        die("Couldnt enter data result4: ".$connect->error);
    }
// $customer = mysqli_fetch_assoc($sql1);

?>


<?php 
}
include("scripts.php");

header("location:../guest/booking.php");
 
 ?> 
 
<div class="sidebar-overlay" id="sidebar_overlay"></div>
</div>
</body>
</html>