<form action="" method="post">
<div class="continer-fluid col-md-12 well">
        <div class="col-md-4 pull-left">
			<?php
            $rowcount=mysqli_num_rows($res);

if($rowcount==0)
{
	echo '<tr style="cursor:pointer; background-color:#F80307; color:#F3EBEB;"><td></td><td></td><td></td><td><p>You have no Jobs, Add Some Jobs.</p></td></tr>';
}
				

echo $rowcount.' jobs found.';

?>
</div>
		<div class="col-md-3">
        <input type="text" id="driver_name" name="driver_name" placeholder="Type Driver Name" class="form-control"  tabindex="1" autocomplete="off">
          <input type="hidden" name="driver_id" id="driver_id" placeholder="" />
		    <div id="suggesstion-box"></div>
	</div>
    <div class="col-md-2">
    
    <input type="submit" name="confirm_booking" class="btn btn-sm btn-success pull-right" value="Allocate Driver"/>
    
	</div>
    <div class="col-md-3">
    
    <input name="delete_booking" type="submit" class="btn btn-sm btn-danger pull-right" value="Delete"/>
    
	</div>
    </div>

<table id="bookings" class="table list_jobs table-hover table-bordered table-striped table-info" cellspacing="0" border="1">

        <thead>
			
            <tr class="btn-primary" style="padding:10px;">

                           <th>Job ID</th>
                           <th>Pickup</th>
                           
                           <th class="col-md-2">Company</th>
                           <th>Passenger</th>
                           <th class="col-md-2">Pickup Address</th>
                           
                           <th class="col-md-2">Drop To</th>
                          
                            <th>Driver</th>
                           <th>Vehicle</th>
                           <th>Comments</th>
                           <th>Edit</th>
                            <th><input type="checkbox" id="select_all" /></th>
                           
            </tr>

        </thead>

        <tfoot>

            <tr class="btn-primary">
                          <th>Job ID</th>
                           <th>Pickup</th>
                          
                           <th>Company</th>
                           <th>Passenger</th>
                           <th class="col-md-2">Pickup Address</th>
                           
                           <th>Drop To</th>
                           
                            <th>Driver</th>
                           <th>Vehicle</th>
                           <th>Comments</th>
                           <th>Edit</th>
                           <th><input type="checkbox" id="select_all" /></th>
                           
            </tr>

        </tfoot>

        <tbody>
        
        <?php

                while($job=mysqli_fetch_assoc($res))

                  {
					if ($job['job_status']=='2'){?>
                     <tr data-field="comments" class="confirmed">
                    

							 <td style="background-color:#46E804; color:#0F0F0F;">
                             
                                
						    <?php echo $job['booking_id']; 
						   }else{
								echo '<tr data-field="comments" class="pending">';
								echo '<td style="background-color:#F1070B; color:#FFF8F8;">'.$job['booking_id'];
								}
							?>
                            </td>
                          <input type="hidden" name="booking_id[]" value=<?php echo $job['booking_id'];?>>
                           <td>
						   <?php $start = date("d/m/y", strtotime($job['pickup_date'])); 
						   		   
						   echo $start." <b>@ ".$job['hours'].":".$job['mins']."<br> ".$job['passengers']." pax";?></td>
                           <input type="hidden" name="pickup_date[]" value=<?php echo $start;?>>
                           
                            <input type="hidden" name="hours[]" value=<?php echo $job['hours'];?>>
                            <input type="hidden" name="mins[]" value=<?php echo $job['mins'];?>>
                           
                           
                            <td><?php echo $job['customer_name']."<br>".$job['customer_phone'];?> 
                            <input type="hidden" name="customer_name[]" value=<?php echo $job['customer_name'];?>>
                           
                             
                            </td>
                            
                            <td><?php echo $job['company_contact']."<br>".$job['passenger_phone'];?></td>
                            
                            <td><?php echo $job['pickup_address']."<span class='badge' style='padding:10px;'>".$job['pickup_post_code']."</span>";?></td>
                            <td><?php echo $job['drop_address']."<span class='badge' style='padding:10px;'>".$job['drop_post_code']."</span>";?></td>
                           
                           <input type="hidden" name="company_contact[]" value="<?php echo $job['company_contact'];?>">
                           <input type="hidden" name="passenger_phone[]" value="<?php echo $job['passenger_phone'];?>">
                           
                           <input type="hidden" name="email[]" value=<?php echo $job['email'];?>>
                           <input type="hidden" name="pickup_address[]" value="<?php echo $job['pickup_address'];?>">
                           <input type="hidden" name="pickup_post_code[]" value="<?php echo $job['pickup_post_code'];?>">
                           <input type="hidden" name="drop_address[]" value="<?php echo $job['drop_address'];?>">
                           <input type="hidden" name="drop_post_code[]" value="<?php echo $job['drop_post_code'];?>">
                           <!-- check if driver is allocated or not -->
                           <?php 
						   if($job['driver']==''){;?>
                            <td style="background-color:#F4080C; color:#F8F5F5; font-size:18px;">Allocate Driver
                           <?php }else{?>
                           <td>
                             <?php echo $job['driver'];}?>
                          </td>
                          <input name="id[]" type="hidden" id="id" value=<?php echo $job['booking_id']; ?>>
                          
                        <?php $id=$job['booking_id'];?>
                        
                        <td>
                             <?php echo $job['make'];?>
                             <input name="make[]" type="hidden" id="make" value=<?php echo "'".$job['make']."'"; ?>>
                             
                          </td>
                            <td class="input" style="margin-top:10px;">
							
							<?php echo $job['comments'];?></td>
                            
                       <td><a href="edit_booking.php?booking_id=<?php echo $job['booking_id'];?>"><i class="fa fa-pencil-square-o"></i></a></td> 						 
       <td style="text-align:right;"><input name="selected_booking[]" type="checkbox" class="checkbox selected_booking" value=<?php echo $job['booking_id'];?>></td>
                        
                           
                 
              </tr>
   <?php } ?>
   		 </tbody>
			   </table>
</table>
</form>
<?php
	function send_driver_email($to,$message)
		 {
		   $to=$to;
		   $message-$message;
		   $email='info@limochauffeurs.com Limo Chauffeurs Ltd.';
		   $header = "From:$email \r\n";
		   $header .= "MIME-Version: 1.0\r\n";
		   $header .= "Content-type: text/html\r\n";
		   $subject="Your Booking Sheet";
		   $email_success = mail ($to,$subject,$message,$header);
		   //echo $message;
		   /*
		   
		   if( $email_success == true ) {
			  echo '<script>alert("Email Sent To Customer");</script>';
		   }else {
			  echo "Message could not be sent...";
		   }
		  */
			
		}

function send_passenger_email($to,$message)
		 {
		   $to=$to;
		   $message-$message;
		   $email='info@limochauffeurs.com Limo Chauffeurs Ltd.';
		   $header = "From:$email \r\n";
		   $header .= "MIME-Version: 1.0\r\n";
		   $header .= "Content-type: text/html\r\n";
		   $subject="Your Booking Confirmation";
		   $email_success = mail ($to,$subject,$message,$header);
		   //echo $message;
		   /*
		   
		   if( $email_success == true ) {
			  echo '<script>alert("Email Sent To Customer");</script>';
		   }else {
			  echo "Message could not be sent...";
		   }
		  */
			
		}


function send_driver_name_plate($file_name,$name_plate,$driver_email,$driver_name)
{

		$path='';
		$file_name=$file_name;
		$driver_email=$driver_email;
		$driver_name=$driver_name;
		$name_plate=$name_plate;
		//echo $driver_email;
		$path="bookings/";
		$mail = new PHPMailer;
		$mail->setFrom('info@limochauffeurs.com', 'Limo Cauffeurs Ltd.');
		$mail->addAddress($driver_email, $driver_name);
		$mail->Subject  = 'Name Plate of Passenger';
		$mail->Body     = 'Please find attached is the name plate of passenger';
		$mail->addAttachment($file_name);
		$mail->addAttachment($name_plate);
		if(!$mail->send()) {
		  echo 'Message was not sent.';
		  echo 'Mailer error: ' . $mail->ErrorInfo;
		} else {
			echo '<script>alert("A Confirmation Email has been sent to Driver and Passenger"); window.location.replace("list_bookings.php")</script>';
		}
		

}
?>
<?php /////////// if Delete is Pressed //

 if(isset($_POST['delete_booking']))
			{
		
		/*	
			echo "<pre>";
  				 print_r($_POST);
			echo "</pre>";
		exit;
		*/	
		$bookingIds = $_POST;
		$count=count($_POST['selected_booking']);
		for ($i=0;$i<$count;$i++){
			$counter=1+$i;
			$index = array_search($_POST['selected_booking'][$i] ,$bookingIds['booking_id']);
			//echo "index=".$index;
		$booking_id=$con->real_escape_string($_POST['selected_booking'][$i]);
		
		
		$update_booking="DELETE FROM bookings WHERE
					id=$booking_id";
					 // echo $update_booking;
					  if (!mysqli_query($con,$update_booking)) {
							echo $update_booking;
							die('update 3 INTO Bookings -Error: ' . mysqli_error($con));
							exit;
						 }
					  
		}
		echo '<script>alert("Record Deleted"); window.location.replace("list_bookings.php")</script>';
		}
?>
<?php
 if(!empty($_POST['confirm_booking'])&&!empty($_POST['driver_id']))
			{
		
		/*		
			echo "<pre>";
  				 print_r($_POST);
			echo "</pre>";
		//exit;
		*/
		$bookingIds = $_POST;
		$count=count($_POST['selected_booking']);
		$driver_message='';
		$passenger_message='';
		$driver_message .= "<h1>LIMO CHAUFEURS LTD. - Driver Booking Sheet</h3><br><hr>";
		$passenger_message .= "<h1>LIMO CHAUFEURS LTD. - Booking Confirmation</h3>";
		$driver_message .='<table>';
        $counter=0;
		
	include "pdf_demo/pdf_header_footer.php";
	  
	  $pdf=new PDF();
	  
	  //Add first page
	  $pdf->AddPage('L');
	  $pdf->SetFont( 'Arial', '', 20 );
	 $y_axis_initial = 50;

$pdf->SetFillColor(232, 232, 232);
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(10);

		$pdf->Cell(15, 6, 'Sr.No.', 1, 0, 'L', 1);
		$pdf->Cell(15, 6, 'Ref', 1, 0, 'L', 1);
        $pdf->Cell(40, 6, 'Pickup Date', 1, 0, 'L', 1);
		$pdf->Cell(60, 6, 'Passenger.', 1, 0, 'L', 1);
		$pdf->Cell(70, 6, 'Pickup From', 1, 0, 'L', 1);
        
        $pdf->Cell(70, 6, 'Drop To', 1, 0, 'L', 1);
        

$row_height = 6;
$y_axis = 50;
$y_axis = $y_axis + $row_height;
//echo $sql;
//exit;
$report=mysqli_query($con,$sql);

//initialize counter
$i = 0;

//Set maximum rows per page
$max = 25;

//Set Row Height

 $No = 0;
 $color='';
 $additional_details='';
 $footer='';
 $terms='';;
		 $pdf1=new FPDF();
		for ($i=0;$i<$count;$i++){
			$counter=1+$i;
			$index = array_search($_POST['selected_booking'][$i] ,$bookingIds['booking_id']);
			//echo "index=".$index;
		$driver=$con->real_escape_string($_POST['driver_id']);
		if($driver==''){
			echo '<script>alert("You did not selected any Driver");return false;</script>';
		}
		$booking_id=$con->real_escape_string($_POST['selected_booking'][$i]);
		
		
		$update_booking="UPDATE bookings SET
					  driver_id=$driver,
					  job_status=2 WHERE id=$booking_id";
					  //echo $update_booking;
					  if (!mysqli_query($con,$update_booking)) {
							echo $update_booking;
							die('update 3 INTO Bookings -Error: ' . mysqli_error($con));
							exit;
						 }
		//echo $update_booking;
		$driver_booking="INSERT INTO driver_bookings 
		(booking_id,driver_id)
		VALUES ($booking_id,$driver)";
		if (!mysqli_query($con,$driver_booking)) {
							echo $driver_booking;
							die('INSERT 33 INTO Bookings -Error: ' . mysqli_error($con));
							exit;
						 }
						 
	    // reterive email of this driver
		$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
FROM vehicles
LEFT JOIN employees on vehicles.driver_id=employees.id
where employees.id=$driver";
		//echo $sql;
		if (!mysqli_query($con,$driver_booking)) {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'];
		$pco_number=$driver['pco_number'];
		$pco_expiry=$driver['pco_expiry'];
		$driver_address=$driver['address'];
		$driver_post_code=$driver['post_code'];
		$driver_photo=$driver['driver_photo'];
		$color=$driver['color'];
		$reg=$driver['reg'];
		//echo $driver_email;
		//exit;
		//Customer details for sending email
		$passenger=$con->real_escape_string($_POST['company_contact'][$index]);
		$passenger_email=$con->real_escape_string($_POST['email'][$index]);
		$passenger_phone=$con->real_escape_string($_POST['passenger_phone'][$index]);
		$pickup_address=$con->real_escape_string($_POST['pickup_address'][$index]);
		$pickup_post_code=$con->real_escape_string($_POST['pickup_post_code'][$index]);
		$pickup_date=$con->real_escape_string($_POST['pickup_date'][$index]);
		$hours=$con->real_escape_string($_POST['hours'][$index]);
		$mins=$con->real_escape_string($_POST['mins'][$index]);
		$make=$con->real_escape_string($_POST['make'][$index]);
		
		//////// Now Select the Registration and Car Color of this Selectd Driver 
		$pickup_time=$hours.":".$mins;
		//exit;
		$drop_address=$con->real_escape_string($_POST['drop_address'][$index]);
		$drop_post_code=$con->real_escape_string($_POST['drop_post_code'][$index]);
		//echo "Customer".$passenger;
		$name=$passenger;
		// Now send email to Passenger///////////////////////////////////////////////////////////////
		 $passenger_message ='';
         $to = $passenger_email;
		 $No=$No+1; 
		 $passenger_message .= '    
		 <h3>
		 <hr>
		 <p style="color:#E3060A"><strong>Booking Ref: </strong>'.$booking_id.'</p>
		 <hr>
		 
	     <p><strong>Date: </strong>'.$pickup_date.'</p>
		 <p>Time: '.$pickup_time.'</p>
		 
         <p>Driver: </b>'.$driver_name.'</p>
		 <p style="color:#E3060A">Driver Phone: '.$driver_phone.'</p>
		 <p background-color: rgb(255, 0, 0);><strong>Pickup From: </strong>'.$pickup_address.','.$pickup_post_code.'</p>
         <p><strong>Drop To: </strong>'.$drop_address.','.$drop_post_code.'</p>
		 
		 
		 </h3>
        <hr>
		 <p><b>Additional Details of Driver:</b></p>
		 <img class="img-circle" width="80" height="80" src=http://webdynamicslondon.co.uk/cabezy/'.$driver_photo.'>
		  <p>Name: '.$driver_name.'</p>
		 <p><strong>Driver PCO Number: </strong>'.$pco_number.'</p>
		  <p><strong>PCO Expiry Date: </strong>'.date("d/m/Y",strtotime($pco_expiry)).'</p>
		 <p><strong>Car: </strong>'.$make.'</p>
		 <p><strong>Car Color: </strong>'.$color.'</p>
		 <p><strong>Car Reg: </strong>'.$reg.'</p>
		 
		 <hr>
		 <p>Thanks for booking at LIMO CHAUFEURS LTD. Contact Number - 0208 230 4594 M:- 079808939220</p>
		<b><p>Terms and Conditions:</p></b>
		<p style="font-size:8px;">Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:</p>
		
		 ';
		send_passenger_email($to,$passenger_message);
		//echo $passenger_message;
		 /////////////////////// Email Customer Ends///////////////////////////////////////
		 
		 ////////////////////////// Now Send to Driver ///////////////////////////////////////
		 $driver_message .= '    
		 <h3>
		 <hr>
		 <p style="color:#E3060A"><strong>Booking Ref: </strong>'.$booking_id.'</p>
		 <hr>
		 
	     <p><strong>Date: </strong>'.$pickup_date.'</p>
		 <p>Time: '.$pickup_time.'</p>
		 
         <p>Passenger: </b>'.$passenger.'</p>
		 <p style="color:#E3060A">Passenger Phone: '.$passenger_phone.'</p>
		 <p><strong>Pickup From: </strong>'.$pickup_address.','.$pickup_post_code.'</p>
         <p><strong>Drop To: </strong>'.$drop_address.','.$drop_post_code.'</p>
		 </h3>
		 ';
		 ///////////////////////////// Driver Email Ends /////////////////////////////////////////////
		 $pdf->setY($y_axis);
		 $pdf->Cell(15, 6, $No, 1, 0, 'L', 1);
		 $pdf->Cell(15, 6, $booking_id, 1, 0, 'L', 1);
	     $pdf->Cell(40, 6, $pickup_date." @ ".$pickup_time, 1, 0, 'L', 1);
		 $pdf->Cell(40, 6, $pickup_time, 1, 0, 'L', 1);
		 $pdf->Cell(60, 6, $passenger." M: ".$passenger_phone, 1, 0, 'L', 1);
		 $pdf->Cell(70, 6, $pickup_address.", ".$pickup_post_code , 1, 0, 'L', 1);
		 $pdf->Cell(70, 6, $drop_address.", ".$drop_post_code , 1, 0, 'L', 1);
		 $y_axis = $y_axis + 6;
		 //////////////////////////  Email Each Passenger///////////////////////////////////////
		// send_passenger_email($to,$passenger_message);
		
		 ////////////////////////////  Make Name Plates ///////////////////////////////////////
		 
		 $pdf1->AddPage('L','A4');
		 $pdf1->SetFont('Arial','B',15);
         $pdf1->setXY(50,50);
		 //$pdf1->Cell(50, 6, "Passenger Name Plate for Booking Ref:".$booking_id, 0, 1);
		 $pdf1->setXY(100,100);
		 $pdf1->SetFont('Arial','B',60);
		 $pdf1->Cell(50, 20,$passenger, 0, 1);
		 //////////////////////////  Name Plate Done ///////////////////////////////////////
		
		}
		 $additional_details .= '<hr>
		 <p><b>Additional Details of Driver:</b></p>
		 <img class="img-circle" width="80" height="80" src="wwww.webdynamicslondon.co.uk/cabezy/drivers/'.$driver_photo.'>
		  <p>Name: '.$driver_name.'</p>
		 <p><strong>Driver PCO Number: </strong>'.$pco_number.'</p>
		  <p><strong>PCO Expiry Date: </strong>'.date("d/m/Y",strtotime($pco_expiry)).'</p>
		 <p><strong>Car: </strong>'.$make.'</p>
		 <p><strong>Car Color: </strong>'.$color.'</p>
		 <p><strong>Car Reg: </strong>'.$reg.'</p>
		 <p>Address: '.$driver_address.'</p>
		 <p>Post Code: '.$driver_post_code.'</p>
		 
		 ';
		 ////////Footer//////////////
		 
		$footer .= '<hr><p>Thanks for booking at LIMO CHAUFEURS LTD. Contact Number - 0208 230 4594 M:- 079808939220</p>';
		$terms .= '<b><p>Terms and Conditions:</p></b>
		<p style="font-size:8px;">Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:Terms and Conditions:</p>
		
		 ';
		 
		 $driver_message=$driver_message.$additional_details.$footer.$terms;
		 //////////////////////////  Now Create PDF Of Name Plates Selected ///////////////////////////////////////
		 $name_plate='bookings/'.$booking_id.'_name_plate.pdf';
		//
		 
		 $pdf1->Output($name_plate,'F');
		 
		 //////////////////////////  Now Create PDF FIle for Bookings to Send to Driver ///////////////////////////////////////
	     $rand=mt_rand(99, 99999);
		 $booking_file ='bookings/'.$rand.'_driver_booking.pdf';
		 //$pdf->Output($booking_file,'F');
		 
	     //////////////////////////  All Ready Now Send Mail to driver ///////////////////////////////////////
		 echo $driver_message;
		 //exit;
		 send_driver_email($driver_email,$driver_message);
		 send_driver_name_plate($booking_file,$name_plate,$driver_email,$driver_name);
		
		 exit;
		 
		 //////////////////////////  Disco ///////////////////////////////////////
		
	}
?>	
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Success</h4>
      </div>
      <div class="modal-body">
        <p>Records Updated Succesfully.</p>
      </div>
      <div class="modal-footer">
        <button id="close_me" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

	 
	<script type="text/javascript">
$(document).ready(function(){
    $('#select_all').on('click',function(){
        if(this.checked){
            $('.checkbox').each(function(){
                this.checked = true;
            });
        }else{
             $('.checkbox').each(function(){
                this.checked = false;
            });
        }
    });
    
    $('.checkbox').on('click',function(){
        if($('.checkbox:checked').length == $('.checkbox').length){
            $('#select_all').prop('checked',true);
        }else{
            $('#select_all').prop('checked',false);
        }
    });
});
/////////////////////// Selecting a Driver on key up in driver box/////////////////
$("#driver_name").keyup(function(){
		$('#contact_id').val('');
		//$("#delivery_address").hide();
		//if any other div are open close them or hide them
		
		$.ajax({
		type: "POST",
		url: "ajax/find_driver.php",
		data:'keyword='+$(this).val(),
		beforeSend: function(){
			$("#contact_name").css("background","#F5C211");
		},
		success: function(data){
			$("#suggesstion-box").show();
			$("#suggesstion-box").html(data);
			$("#contact_name").css("background","#green");
		}
		});
});
	
	function selectDriver(val) {
	var data=val;
	 var data = data.split('-');
	var id=data[0];
	var name=data[1];
	
	$("#driver_id").val(id);
	$("#driver_name").val(name);
	
	$("#suggesstion-box").hide();
	}
	
///////////////////////////////////////////////////////////////////////////////////
</script>