<?php
ob_start();
//include "head.php";
include "dbconfig.php";
require_once('pdf_demo/fpdf.php');
//require('pdf_demo/html_table.php');
//require('pdf_demo/html2pdf.php');
include 'phpmailer/PHPMailerAutoload.php';
//use Spatie\Browsershot\Browsershot;
error_reporting(E_ALL); 
session_start();

 $booking_id=4014;//$_GET['booking_id'];
/*
$sql="SELECT bookings.days AS days,bookings.start_date AS start_date,bookings.end_date AS end_date,bookings.pickup_date,bookings.id AS booking_id,bookings.rate AS rate,bookings.supp_cost AS supp_cost,bookings.comments, bookings.parking,bookings.driver_id, bookings.drop_address,bookings.drop_post_code,bookings.pickup_address,bookings.pickup_post_code,bookings. bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time, bookings.hours,bookings.mins,bookings.other,bookings.job_status,bookings.passengers, customers.id AS customer_id, customers.name AS customer_name,customers.address1,customers.post_code,customers.city, customers.phone as customer_phone, payment_type.name AS payment_type,  company_contact.name as company_contact,company_contact.email,company_contact.mobile as passenger_phone,company_contact.id as passenger_id, 
vehicle_make.make as make,vehicle_make.id as make_id
FROM bookings LEFT JOIN customers ON bookings.customer_id = customers.id 
LEFT JOIN vehicle_make on bookings.vehicle_id=vehicle_make.id
LEFT JOIN payment_type ON bookings.payment_type = payment_type.id  LEFT JOIN company_contact ON bookings.passenger_id = company_contact.id WHERE bookings.id=".$booking_id." ORDER BY bookings.id DESC";
*/
$sql="SELECT bookings.start_date,bookings.id AS booking_id,bookings.days,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,bookings.bags,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,employees.email AS driver_email,employees.phone as driver_phone,employees.pco_number,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.id=".$booking_id." ORDER BY bookings.id DESC";

//echo $sql;
$res=$con->query($sql);

$booking_data= mysqli_fetch_assoc($res);
$_SESSION['invoice_no']=$booking_id."_jobcard";

?>	
 <form method="post" action="">
					<div class="col-md-2">
                       <input type="button" id="b1" class="btn btn-primary btn-sm" onClick="printDiv('invoice')" value="Print" /> 
                    </div>
                    <div class="col-md-2">
                       <input type="submit" name="send_email" id="send_email" class="btn btn-danger btn-sm"  value="Send Email" /> 
					    <!--input type="hidden" name="invoice_no" id="invoice_no"  value="<?php //echo $booking_id.'_jobcard'; ?>" /--> 
                    </div>
					<!--input type="button" value="Capture" id="capture" /-->
                   </form>
<style>
/*
#pd {
width:70%;
height:auto;
margin-left:15%;
float:left;

margin-top:4%;

box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
#half {
width:45%;
height:auto;

}
#halfh {
width:30%;
height:auto;
margin-left:1%;
}
#full {
width:100%;
height:auto;
padding:2%;

}
#fullh {
width:100%;
height:auto;

}
@media screen and (max-width:768px) {

#pd {
width:100%;
height:auto;
margin-left:0;



}
#half {
width:100%;
height:auto;

}
#halfh {
width:100%;
height:auto;
margin-left:0;
}
}
</style>

                          
	<style>
		
		#canvas{
	
			width:100%;
			height:100%;
			background:#FFFFFF;
			
		}
		#invoice_div{
	
			
			background:#FFFFFF;
			
			width:100%;
			left:10%;
			text-align:justify;
			padding-left:10%;
			
			
		}
		.movable_div{
		margin-top:10px;
		width:80px;
		height:80px;
		padding:10px;
		
			color: white;
			font-family: Verdana;
			cursor: move;
			position: absolute;
		}
		#design{
		
			width: 510px;
			margin: 0 auto;
			background: purple;
			border: 1px solid black;
			color : white;
			padding: 30px;
			
		}
		*/
	</style>					
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
	<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
	
	<!-- this script helps us to capture any div --> 
	<script src="html2canvas.js"></script>

                    
</head>

<body bgcolor="#FFFFFF"> 
  
  
  					
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  
					  <meta charset="utf-8">
					  <head><style>
					/*  
					  .full{
		width:100%;
		
		}
		.half{
		width:50%;
		
		}
		 .full1{
		width:60%;
		
		}
		.half1{
		width:30%;
		margin-right:10px;
	
		
		}
		.half2{
		width:45.5%;
		margin-right:10px;
	min-height:120px;
		
		}
		.half3{
		width:92%;
		margin-right:10px;
	min-height:100px;
		
		}
		
h2 {
	
	 style="padding:10px; color:#000000; background-color:#68B0EC;"
	 }
*{
	font-size:14px;
	font-family:arial;
	color:#000000;}
	 */
</style></head>
					  <body>
					  <div id="invoice">';
					 
                             $mins=str_replace('&#39;','0',$booking_data['mins']);
                         
						    $start_date = date("d/m/y", strtotime($booking_data['start_date']));
                            $start1 = date("l", strtotime($booking_data['start_date']));
						     $end_date = date("d/m/y", strtotime($booking_data['end_date']));
                            $end1 = date("l", strtotime($booking_data['end_date']));
							$driver_name=@$booking_data['driver'];
							$driver_email=@$booking_data['driver_email'];
							$driver_phone=@$booking_data['driver_phone'];
					 $invoice_html.=' 	
                     <div class="full" style="width:100%;">
					 <div class="half" style="float:left; width:70%;">
						<!--img src="wings_logo.JPG" style="margin-left:5%;"-->
						
						<h3 align="left" style=" float:left;">
						<img src="wings_logo.JPG" style="margin-left:0%;"></h3> 
							 <h3 align="left" style=" float:left;"><font face="Times New Roman, Times, serif"><b style="font-size:25px;">Wings Chauffeurs Ltd.</b></font>
							 <b style="font-size:12px;"><br>Office 2.25, 450 Bath Rd,<br>
							 West Drayton,Middlesex,London,<br>
							 UB7 0EB<br></b></h3>
						
						</div>
						
						
						
						 <div class="half" style="float:right; width:30%;">
						 <br>
						  	<h1>JOB CARD</h1>	
							</div>
					</div>
					<br><br><br><br><br>		
							';
			

					        
                  $invoice_html.=
				  
					 '
		 
		<div class="full" style="width:100%;">
		 <br><br>
		 <p style="color:#E3060A; font-size:20px;"><strong style="font-size:20px;">Booking Ref : </strong>'.$booking_id.'</p>
		 </div>
		 <div class="full1" style="float:left; width:100%;">
		 <div class="half1" style="float:left; width:18%; margin-right:10px; border:solid 1px #000000;" align="center">
		  <div class="full" style="float:left; width:100%; background:#6666FF; border:solid 1px #000000;">
		  <strong>Start Date</strong>
		  </div>
	     '.$start1.' '.$start_date.'<br>
		 <strong>Time : </strong>'.$booking_data['hours'].":".$mins.'
		 </div>
		  <div class="half1" style="float:left; width:18%; margin-right:10px; border:solid 1px #000000;" align="center">
		  <div class="full" style="float:left; width:100%; background:#6666FF; border:solid 1px #000000;">
		  <strong>End Date</strong>
		  </div>
		 '.$end1.' '.$end_date.'
		 <br><br>
		 </div>
		 <div class="half1" style="float:left; width:18%; margin-right:10px; border:solid 1px #000000;" align="center">
		  <div class="full" style="float:left; width:100%; background:#6666FF; border:solid 1px #000000;">
		  <strong>No. of Days</strong>
		  </div>
		  '.$booking_data['days'].'
		  <br><br>
		 </div>
		 </div>
		 
		 <div class="full" style="width:100%; float:left; margin-top:10px;">
		 
		 <div class="half1" style="float:left; width:30%; margin-right:10px; border:solid 1px #000000;" align="center">
		  <div class="full" style="width:100%; float:left; background:#6666FF; border:solid 1px #000000;">
		  <strong>Driver</strong>
		  </div>
	     <p>'.$driver_name.'</p>
		 <p><strong>Phone : </strong>'.$driver_phone.'</p>
		 <p><strong>Driver PCO No : </strong>'.$booking_data['pco_number'].'</p>
		 </div>
		 
		 <div class="half1" style="float:left; width:30%; margin-right:10px; border:solid 1px #000000;" align="center">
		  <div class="full" style="float:left; width:100%; background:#6666FF; border:solid 1px #000000;">
		  <strong>Client</strong>
		  </div>
	      <p><strong>Passenger :</strong> </b>'.$booking_data['company_contact'].'</p>
		 <p><strong>Phone :</strong> '.$booking_data['passenger_phone'].'</p>
		  <p><strong>No. of Passengers :</strong> '.$booking_data['passengers'].' , <strong>No. of Bags :</strong> '.$booking_data['bags'].'</p>
		 </div>
		 
		 <div class="half1" style="float:left; width:30%; margin-right:10px; border:solid 1px #000000;" align="center">
		  <div class="full" style="float:left; width:100%; background:#6666FF; border:solid 1px #000000;">
		  <strong>Vehical</strong>
		  </div>
	     <p><strong>Car : </strong>'.strtoupper($booking_data['make']).'</p>
		 <p><strong>Car Color : </strong>'.strtoupper($booking_data['color']).'</p>
		 <p><strong>Car Reg : </strong>'.strtoupper($booking_data['reg']).'</p>
		 </div>
		 </div>
		 
		  <div class="full" style="width:100%; float:left; margin-top:10px;">
		 
		 <div class="half2" style="width:45.5%; margin-right:10px; min-height:120px; float:left; border:solid 1px #000000;" align="center">
		  <div class="full" style="width:100%; float:left; background:#6666FF; border:solid 1px #000000;">
		  <strong>Pickup From</strong>
		  </div>
	     <p>';
		 $flight_no=$booking_data['flight_no'];
		 if($flight_no!="")
		 {
		  $invoice_html .= '<strong>Flight No. </strong>: '.$flight_no.'';  
		 }
		 $invoice_html .='</p>
		 <p>'.$booking_data['pickup_address'].' , '.$booking_data['pickup_post_code'].'</p>
		 
		 </div>
		 
		 <div class="half2" style="width:45.5%; margin-right:10px; min-height:120px; float:left; border:solid 1px #000000;" align="center">
		  <div class="full" style="width:100%; float:left; background:#6666FF; border:solid 1px #000000;">
		  <strong>Drop To</strong>
		  </div>
	     
		 <p>'.$booking_data['drop_address'].','.$booking_data['drop_post_code'].'s</p>
		 </div>
		 
		
		 </div>
		  <div class="half3" style="width:92%; margin-right:10px; min-height:100px; float:left; margin-top:10px; border:solid 1px #000000;" align="center">
		  <div class="full" style=" width:100%; float:left; background:#6666FF; border:solid 1px #000000;">
		  <strong>Operator Notes</strong>
		  </div>
	     <p>'.$booking_data['comments'].'</p>
		 
		 </div>
		 
		 
		 ';
        
		  
		   
		 $invoice_html .= '  
		 
		
		
		 <p>&nbsp;</p>
		 <p>&nbsp;</p>
		 <p>&nbsp;</p>
		 <p>&nbsp;</p>
		  <p>&nbsp;</p>
		<p>&nbsp;</p>
		
		
		<b><p><strong>Terms and Conditions :</strong></p></b>
		

<b> 
Driver are responsible for the safety of vehicles.</b> 
</p>
	
		
		 ';
       
       
			
			
				  echo $invoice_html;
				  //echo '<div class="col-md-11" id="invoice_div" style="margin-left:-10%;"><br>'.$invoice_html.'</div>';
				/* echo '<div id="canvas"  class="col-md-11" style="margin-left:20px;">
		'.$invoice_html.'	
	</div>';*/
	?>
				  
    <script type="text/javascript">
		 /*
function printDiv() {
     var divToPrint = document.getElementById('invoice');
    var htmlToPrint = ''+
    '<style type="text/css">'+'th{border: 1px solid #7A7575;'+
'table td{align-content:left;padding-top:1px;font-size:12px;font-family:Arial;}'+
        '</style>';
    htmlToPrint += divToPrint.outerHTML;
    newWin = window.open("");
    newWin.document.write(htmlToPrint);
    newWin.print();
    newWin.close();
}
*/
function printDiv(divName) {
         
$('#b1').hide();
$('#send_email').hide();
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     //document.body.innerHTML = printContents;

     window.print();
$('#b1').show();
$('#send_email').show();
   //  document.body.innerHTML = originalContents;
}
function captureDiv() {

div_content = document.querySelector("#invoice_div")
				//make it as html5 canvas
				html2canvas(div_content).then(function(canvas) {
					//change the canvas to jpeg image
					data = canvas.toDataURL('image/jpeg');
					
					//then call a super hero php to save the image
					save_img(data);
				});
//alert('something wrong');

					
		}			

                   </script>
				   
  <script type="text/javascript">	
		$(function(){	
			
			//to make a div draggable
			$('.movable_div').draggable(
				{containment: "#canvas", scroll: false}
			);
			
			//to capture the entered text in the textbox 
			$('#textbox').keyup(function(){
				var text = $(this).val();
				$('.movable_div').text(text);
			});	
			
			//to change the background once the user select
			$('#background').change(function(){
				var background = $(this).val();
				$('#canvas').css('background', 'url(bg_img/'+background+')');
			});
			
			//font size handler here. 
			$('#slider').change(function(){
				fontSize = $(this).val();
				$('.movable_div').css('font-size', fontSize+'px');
			});		

			//here is the hero, after the capture button is clicked
			//he will take the screen shot of the div and save it as image.
			$('#capture').click(function(){
				//get the div content
				div_content = document.querySelector("#invoice_div")
				//make it as html5 canvas
				html2canvas(div_content).then(function(canvas) {
					//change the canvas to jpeg image
					data = canvas.toDataURL('image/jpeg');
					
					//then call a super hero php to save the image
					save_img(data);
				});
			});	
			
				
		});
		
		
		//to save the canvas image
		function save_img(data){
			//ajax method.
			
		//	var invoice_no=$('#invoice_no').val();
			//alert(invoice_no);
		//	var dataString = 'invoice_no='+ invoice_no + '&data='+ data;
           
	
			$.post('save_jpg.php', {data: data }, function(res){
				//if the file saved properly, trigger a popup to the user.
				if(res != ''){
				
					//yes = confirm('File saved in output folder, click ok to see it!');
					//if(yes){
						//location.href =document.URL+'output/'+res+'.jpg';
					//}
				}
				else{
					//alert('something wrong');
				}
			}); 
		}
    </script>				    
  <?php 
    echo '<script>captureDiv();</script>';
  // $image="output/".$_SESSION['invoice_no'].".jpg";
	   if(isset($_POST['send_email'])){
	  //echo $invoice_html;
	  echo '<script>captureDiv();</script>';
  // $passenger='jas';
   $subject="Your Invoice";
   //$to=$driver_email;//'jaspinder32@gmail.com';
   $message="
   <h2>Dear ".$driver_name."</h2>
   <br/>
   <h3>Please find the attached Job Card</h3>";//$invoice_html;
   $image="output/".$_SESSION['invoice_no'].".jpg";
  // echo $message;
 /*  $pdf1=new FPDF();
  
		//
		 $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(50,100);
		// $pdf1->SetFont('Arial','B',80);
		 $pdf1->Cell(50, 10,$message, 0, 1);
		// $pdf1->setXY(50,150);
		// $pdf1->Cell(30, 20,$last_name, 0, 1);
		 $pdf->Image($image,10,10,-300);
		  $name_plate='bookings/'.$booking_id.'_invoice.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 = new FPDF(); 
$pdf->AddPage();
$pdf->Image($image,0,0);
//$pdf->Output();
 $name_plate='bookings/'.$booking_id.'6_invoice.pdf';
		 $pdf->Output($name_plate,'F');
		 */
		 
		 
		// $pdf->Output($booking_file,'F'); */
  
 /* $pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',12);
$pdf-> Image('output/'.$image,100,15,35,35);
 $name_plate='bookings/'.$booking_id.'_invoice.pdf';
		 $pdf->Output($name_plate,'F');*/
////////////////////////////////////////////////////////		 
		 
////////////////////////////////////////////////////******************************************************////////////////////////////////////		 
/*
		// require("fpdf.php");
class PDF extends FPDF {
    const DPI = 96;
    const MM_IN_INCH = 25.4;
    const A4_HEIGHT = 297;
    const A4_WIDTH = 210;
    // tweak these values (in pixels)
    const MAX_WIDTH = 1000;
    const MAX_HEIGHT = 500;
    function pixelsToMM($val) {
        return $val * self::MM_IN_INCH / self::DPI;
    }
    function resizeToFit($imgFilename) {
        list($width, $height) = getimagesize($imgFilename);
        $widthScale = self::MAX_WIDTH / $width;
        $heightScale = self::MAX_HEIGHT / $height;
        $scale = min($widthScale, $heightScale);
        return array(
            round($this->pixelsToMM($scale * $width)),
            round($this->pixelsToMM($scale * $height))
        );
    }
    function centreImage($img) {
        list($width, $height) = $this->resizeToFit($img);
        // you will probably want to swap the width/height
        // around depending on the page's orientation
        $this->Image(
            $img, (self::A4_HEIGHT - $width) / 2,
            (self::A4_WIDTH - $height) / 2,
            $width,
            $height
        );
    }
}
// usage:
$pdf = new PDF();
$pdf->AddPage("L");
$pdf->centreImage($image);
//$pdf->Output();
$name_plate='bookings/'.$booking_id.'_jobcard.pdf';
		 $pdf->Output($name_plate,'F');		 

*/		 
///////////////////////////////////////////////////////////******************************************************//////////////////////////////////		 
		 // $booking_file ='bookings/'.$rand.'_driver_booking.pdf';
		 
		 //////////////////////////  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'); */
//require __DIR__.'/vendor/autoload.php';
//D:\xampp\htdocs\wings\html2pdf-master
//require 'html2pdf-master\src\Html2Pdf';
//use Spipu\Html2Pdf\Html2Pdf;
//use Spipu\Html2Pdf\Exception\Html2PdfException;
//use Spipu\Html2Pdf\Exception\ExceptionFormatter;

//$name_plate='bookings/'.$booking_id.'3_invoice.pdf';
//$html2pdf = new Html2Pdf();
//$html2pdf->writeHTML('<h1>HelloWorld</h1>This is my first test');
//$html2pdf->output($name_plate,'F');

/*
$pdf=new PDF();
$pdf->AddPage();
    $pdf->SetFont('Arial','',12);
    
   // $pdf->WriteHTML($_POST['text']);
   // $pdf->Output();

$html='<table border="1">
<tr>
<td width="200" height="30">cell 1</td><td width="200" height="30" bgcolor="#D0D0FF">cell 2</td>
</tr>
<tr>
<td width="200" height="30">cell 3</td><td width="200" height="30">cell 4</td>
</tr>
</table>';
$name_plate='bookings/'.$booking_id.'5_invoice.pdf';
$pdf->WriteHTML(utf8_decode($message));
//$pdf->Output();
 $pdf->Output($name_plate,'F');
  */
 // imagepng(imagecreatefromstring(file_get_contents($message)), "output.png");
  //function send_passenger_email($to,$message,$passenger)
	function send_driver_email($image,$driver_email,$message,$driver_name)
		 {
		$driver_email=$driver_email;
		$message=$message;   
		$driver_name=$driver_name;
		//$file_name=$file_name;
		$image=$image;
		
		$mail = new PHPMailer;
		$mail->SMTPAuth   = true;
		$mail->IsHTML(true);  
		$mail->setFrom('info@wingschauffeurs.com', 'Wings Chauffeurs Ltd.');
		$mail->addAddress($driver_email,$driver_name);
		$mail->AddCC('info@wingschauffeurs.com', 'wings');
		$mail->Subject  = 'Your  Job Card';
		$mail->Body     =$message;
		//$mail->addAttachment($file_name);
		$mail->addAttachment($image);
		if(!$mail->send()) {
		  echo 'Message was not sent.';
		  echo 'Mailer error: ' . $mail->ErrorInfo;
		  }else{echo '<script>alert("Job Card sent in email."); //window.location.replace("list_invoice.php");
		  </script>';
		  }
		}
		send_driver_email($image,$driver_email,$message,$driver_name);
		
	   }
	   ?>
        