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

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


    $appuser = $_SESSION['userid'];
    $charge_type = $_REQUEST['radio1'];
    $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']));
    $pickup = $_REQUEST["Pickup"];
    $dropoff = $_REQUEST["Dropoff"];
    $return_status = "NR"; // not returned
    $status = "Pending";
    $paymentstatus = "Un-paid";

            $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 {
                $fare = $row0["ac_price_per_day"];
                $charge_type = "days";
            }
        }
    }
    
    if($err_date >= 0) {

    
    $sql1 = "INSERT INTO rentedcars(`car_id`,`booking_date`, `Name`,
    `rent_start_date`,`rent_end_date`,`fare`,
    `charge_type`,`pickup`,`destination`,`PaymentStatus`,
    `status`,`appuser`,`return_status`,`bookingsource`) 
    VALUES(" . $car_id . ",'" . date('Y-m-d H:i:s') ."','" .$_SESSION['name']."',
    '" . $rent_start_date ."','" . $rent_end_date . "'," . $fare . ",
    '" . $charge_type . "','" . $pickup . "','" . $dropoff . "','" . $paymentstatus . "',
    '" . $status . "','" . $appuser . "','" . $return_status . "','web')";
    
//     $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);
    }

?>
										<div>
											<div>
												<h1 style="color: green;"><span class="glyphicon glyphicon-ok-circle"></span> Booking Confirmed.</h1> </div>
										</div>
										<br>
										<h2> Thank you for using Online Booking System! We wish you have a safe ride. </h2>
										<h3> <strong>Your Order Number:</strong> <span style="color: blue;"><?php echo "$booking_number"; ?></span> </h3>
										<div>
											<h5>Please read the following information about your order.</h5>
											<div class="box">
												<div class="col-md-10" style="float: none; margin: 0 auto; text-align: center;">
													<h3 style="color: orange;">Your booking has been received and placed into out order processing system.</h3>
													<br>
													<h4>Please make a note of your <strong>order number</strong> now and keep in the event you need to communicate with us about your order.</h4>
													<br>
													<h3 style="color: orange;">Booking Detail</h3>
													<br> </div>
												<div class="col-md-10" style="float: none; margin: 0 auto; ">
													<h4> <strong>Vehicle Name: </strong> <?php echo $car_name; ?></h4>
													<br>
													<h4> <strong>Vehicle Number:</strong> <?php echo $car_nameplate; ?></h4>
													<br>
													<?php     
                if($charge_type == "days"){
                ?>
														<h4> <strong>Fare:</strong> Rs. <?php echo $fare; ?>/day</h4>
														<?php } else {
                    ?>
															<h4> <strong>Fare:</strong> Rs. <?php echo $fare."/".$charge_type; ?></h4>
															<?php } ?>
																<br>
																<h4> <strong>Booking Date: </strong> <?php echo date("Y-m-d H:i:s"); ?> </h4>
																<br>
																<h4> <strong>Pickup From: </strong> <?php echo $pickup; ?></h4>
																<br>
																<h4> <strong>Destination: </strong> <?php echo $dropoff; ?></h4>
																<br>
																<h4> <strong>Start Date: </strong> <?php echo $rent_start_date; ?></h4>
																<br>
																<h4> <strong>Return Date: </strong> <?php echo $rent_end_date; ?></h4>
																<br>
																<br> </div>
											</div>
											<div class="col-md-12" style="float: none; margin: 0 auto; text-align: center;">
												<h6>Warning! <strong>Do not reload this page</strong> or the above display will be lost. If you want a hardcopy of this page, please print it now.</h6> </div>
										</div>
				</body>
				<?php } else { ?>
						<div>
							<div style="text-align: center;"> You have selected an incorrect date.
								<br>
								<br> </div>
							<?php } ?>
							

    
    <?php include("scripts.php");?>
    
<div class="sidebar-overlay" id="sidebar_overlay"></div>
</body>
</html>