<?php
include "dbconfig.php";
include 'phpmailer/PHPMailerAutoload.php';
$driver_id=$_REQUEST['driver_id'];
$booking_id=$_REQUEST['booking_id'];
date_default_timezone_set ("Europe/London"); 
$today=date("Y-m-d");

$sqlb="SELECT bookings.start_date,bookings.id AS booking_id,bookings.comments,bookings.flight_no,bookings.parking,bookings.driver_id, bookings.drop_address,bookings.drop_post_code,bookings.pickup_address,bookings.pickup_post_code, bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time, bookings.hours,bookings.mins,bookings.other,bookings.job_status,bookings.passengers,job_status.name,bookings.comments,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate AS rate,bookings.supp_cost AS supp_cost,bookings.substitute,
customers.id AS customer_id, customers.name AS customer_name,customers.address1,customers.post_code,customers.city, customers.phone as customer_phone, 
employees.first_name AS driver,vehicles.color as color,vehicles.reg as reg, vehicle_make.make as make,
company_contact.name as company_contact,company_contact.email,company_contact.mobile as passenger_phone

FROM bookings 
LEFT JOIN customers ON bookings.customer_id = customers.id 
LEFT JOIN payment_type ON bookings.payment_type = payment_type.id 
LEFT JOIN employees ON bookings.driver_id = employees.id 
LEFT JOIN company_contact ON bookings.passenger_id = company_contact.id 
LEFT JOIN vehicle_make ON bookings.vehicle_id = vehicle_make.id 
LEFT JOIN vehicles on vehicles.driver_id=employees.id
LEFT JOIN job_status ON bookings.job_status = job_status.id 
 WHERE bookings.start_date >'$today' AND bookings.id='$booking_id'";
 $t_result=mysqli_query($con,$sqlb);
$customer=mysqli_fetch_assoc($t_result);	

$count_res=mysqli_query($con,$sqlb);
$total_jobs=mysqli_num_rows($count_res);		

if($total_jobs==0)
{
echo '<script>alert("After or on the job start date we can not unallocated, you can only change the driver then.");var url11 = "list_bookings.php?"+new Date().getTime();window.location.replace(url11);</script>';
}
else
{
$sql="SELECT employees.pco_number,employees.pco_expiry,employees.address,employees.post_code,employees.phone,employees.first_name,employees.email,employees.phone,employees.driver_photo,vehicles.reg,vehicles.color,driver_bookings.driver_id
FROM vehicles
LEFT JOIN employees on vehicles.driver_id=employees.id
LEFT JOIN driver_bookings on driver_bookings.driver_id=employees.id where driver_bookings.booking_id='$booking_id'";
		//echo $sql;
		if (!mysqli_query($con,$sql)) {mysqli_error($con);}
		$result=$con->query($sql);
		$driver=mysqli_fetch_assoc($result);
		$driver_email=$driver['email'];
		$driver_phone=$driver['phone'];
		$driver_name=$driver['first_name'];
		
		
 //  $driver_email="dashraja65@gmail.com";//$to;
		  $message="hello Your Job ".$booking_id." has been Cancelled, for more information please call the office.";
		   $driver_message=$message;
		 // $driver_name=$current_driver;
		$mail2 = new PHPMailer;
		$mail2->SMTPAuth   = true;
		$mail2->setFrom('info@wingschauffeurs.com', 'Wings Chauffeurs Ltd.');
		$mail2->IsHTML(true);
		$mail2->addAddress($driver_email, $driver_name);
		$mail2->Subject  = 'Job Cancelled';
		$mail2->Body     = $driver_message;
		if(!$mail2->send()) {
		   
		   
			  /*echo '<script>alert("Email Was not Sent To Driver");</script>';*/
		   }else {
			  /* echo '<script>alert("Email Sent To Driver");</script>';*/
		   }	
				 
		  $update_booking="UPDATE bookings SET
					  driver_id='',job_status='1' WHERE id='$booking_id'";
					 // echo $update_booking;
					  //exit;
					  if (!mysqli_query($con,$update_booking)) {echo "Error. ".mysqli_error($con);}
		$sql_sub="Delete from driver_bookings where booking_id=".$booking_id;
 if (!mysqli_query($con,$sql_sub)) {//echo $sql_check;die('Update 3 INTO ORDER_Details -Error: ' . mysqli_error($con));
			}		
   echo "<script>alert('successfull');var url11 = 'list_bookings.php?'+new Date().getTime();window.location.replace(url11);</script>";  
				
}


?>