<?php
error_reporting(E_ALL);
include "../dbconfig.php";
$sql="SELECT employees.id as driver_id,employees.first_name,employees.phone,employees.tfl_expiry,employees.pco_number,employees.pco_expiry,employees.driver_photo,employees.address,employees.post_code,employees.badge_expiry,employees.badge_number,
vehicles.mot_date,vehicles.reg,vehicles.make,vehicles.insurance_date
FROM employees
JOIN vehicles on vehicles.driver_id=employees.id
ORDER BY employees.first_name ASC
"; // this is main big SQL Query to fetch data
//echo $sql;
include "../pdf_demo/fpdf.php";

// Define Header and Footer inside the class /////////////////////////////////////
class PDF extends FPDF
{
function Header()
{
	$this->SetY(5);  
    $this->SetFont('Arial', 'B', 20); 
    $this->SetFillColor(0,0,125); 
    $this->SetTextColor(225); 
    $this->Cell(0, 10, "List of Drivers", 0, 0, 'L', true); 
	
    $this->SetTextColor(0);
    $this->SetFont('Arial', '', 12); 
	$this->Ln(10);
	$this->Cell(100, 10, "Limo Chauffers Ltd.,",0,1);
	$this->SetY(20);
	$this->Cell(100, 10, "12 Lampton Park Road,",0,1);
    $this->SetY(25);;
	$this->Cell(100, 10, "Hounslow,",0,1);
    $this->SetY(30);
	$this->Cell(100, 10, "Middlesex",0,1);
	$this->SetY(35);
	$this->Cell(100, 10, "TW3 4HS ",0,1);
	 $this->SetY(40);
	$this->Cell(100, 10, "United Kingdom ",0,1); 

	$this->SetXY(115,15);   
    $this->SetTextColor(0); 
 	$this->SetFont('Arial', '', 10); 
	$this->Cell(100, 10, "Phone: (0)2082304594",0,1);
	$this->Ln(8);
	$this->SetXY(115,20);   
	$this->Cell(100, 10, "Mobile:(0)7808939220,",0,1);
	$this->Ln(8);
	$this->SetXY(115,25);
	$this->Cell(100, 10, "Email: info@limochauffeurs.com",0,1);
	$this->Ln(3);
	$this->SetXY(115,30);
	
	$this->Cell(100, 10, "Website: www.limochauffeurs.com");
	$this->SetFont('Arial', 'B', 11); 
	$this->Ln(3);
	$this->SetXY(115,35);
	
	$this->Cell(100, 10, "VAT No: 1234569898");
	// Set up the Invoice To Data
}
 //footer
 function Footer() { 
    $this->SetFont('Arial', '', 10); 
    $this->SetTextColor(0); 
    $this->SetY(-15);
	$this->Cell(0, 20, "Limo Chauffeurs Ltd", 'T', 1, 'C');
    
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); 
	
	//$this->Image('footer.JPG',0,750,620,50); 
}
}
	  $pdf=new PDF();
	  //Add first page
	  $pdf->AddPage('L');
	  $pdf->SetFont( 'Arial', '', 20 );
	  $pdf->SetAutoPageBreak(false);
	  $y_axis_initial = 50;
 
	  $pdf->SetX(5);
  
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(5);
        $pdf->SetFillColor(153,204,255); 
		
        $pdf->Cell(30, 6, 'Driver Badge ', 1, 0, 'L', 1);
		$pdf->Cell(60, 6, 'Driver Name', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'MOT Date', 1, 0, 'L', 1);
		$pdf->Cell(40, 6, 'Insurance Date', 1, 0, 'L', 1);
		$pdf->Cell(40, 6, 'TFL Veh. Exp', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'PCO No.', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'PCO Exp.', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Badge Exp.', 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 = 20;

//Set Row Height

 $No = 0;
while($row = mysqli_fetch_array($report))
{
    $badge_number = $row['badge_number'];
	$first_name = $row['first_name'];
	$mot_date = $row['mot_date'];
	$mot_date =date("d/m/Y",strtotime($mot_date));
	$insurance_date =date("d/m/Y",strtotime($row['insurance_date']));
	$tfl_expiry = date("d/m/Y",strtotime($row['tfl_expiry']));
	$pco_expiry = date("d/m/Y",strtotime($row['pco_expiry']));
	$badge_expiry = date("d/m/Y",strtotime($row['badge_expiry']));
	
	$pco_number = $row['pco_number'];
	//If the current row is the last one, create new page and print column title
    if ($i == $max)
    {
        $pdf->AddPage('L');
        $y_axis =50;
        //print column titles for the current page
        $pdf->SetY($y_axis);
        $pdf->SetX(5);
		
    $pdf->Cell(30, 6, $badge_number, 1, 0, 'L', 1);
	$pdf->Cell(60, 6, $first_name, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $mot_date, 1, 0, 'L', 1);
	$pdf->Cell(40, 6, $insurance_date, 1, 0, 'L', 1);
	$pdf->Cell(40, 6, $tfl_expiry, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $pco_number, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $pco_expiry, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $badge_expiry, 1, 0, 'L', 1);
       
        //Go to next row
        $y_axis = $y_axis + $row_height;

        //Set $i variable to 0 (first row)
       $i = $i + 1;
    }
    $No = $No +1;
   
    

    $pdf->SetY($y_axis);
    $pdf->SetX(5);
	
    $pdf->Cell(30, 6, $badge_number, 1, 0, 'L', 1);
	$pdf->Cell(60, 6, $first_name, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $mot_date, 1, 0, 'L', 1);
	$pdf->Cell(40, 6, $insurance_date, 1, 0, 'L', 1);
	$pdf->Cell(40, 6, $tfl_expiry, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $pco_number, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $pco_expiry, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $badge_expiry, 1, 0, 'L', 1);
	
	
    //Go to next row
    $y_axis = $y_axis + $row_height;
    $i = $i + 1;
}

ob_clean();

// Now Print this pdf page 
$pdf->Output('Driver_TFL_DATA.pdf','D');


?>