<?php
require_once(__DIR__ . '/../../../vendor/autoload.php');
use Fpdf\Fpdf;

ini_set("log_errors", 1);
ini_set('error_reporting', E_ALL);

/*
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="courrier.pdf"');
header('Content-type: text/html; charset=UTF-8');
*/

if(isset($_REQUEST['actlist'])) $actlist=filter_var($_REQUEST['actlist'], FILTER_SANITIZE_STRING); else $actlist='';
if(isset($_REQUEST['NoClient'])) $NoClient=filter_var($_REQUEST['NoClient'], FILTER_SANITIZE_STRING); else $NoClient='';
if(isset($_REQUEST['Nom'])) $Nom=filter_var($_REQUEST['Nom'], FILTER_SANITIZE_STRING); else $Nom='';
if(isset($_REQUEST['Prenom'])) $Prenom=filter_var($_REQUEST['Prenom'], FILTER_SANITIZE_STRING); else $Prenom='';
if(isset($_REQUEST['email'])) $email=filter_var($_REQUEST['email'], FILTER_SANITIZE_STRING); else $email='';
if(isset($_REQUEST['NoChasis'])) $NoChasis=filter_var($_REQUEST['NoChasis'], FILTER_SANITIZE_STRING); else $NoChasis='';
if(isset($_REQUEST['Model'])) $Model=filter_var($_REQUEST['Model'], FILTER_SANITIZE_STRING); else $Model='';
if(isset($_REQUEST['Date'])) $Date=filter_var($_REQUEST['Date'], FILTER_SANITIZE_STRING); else $Date='';
if(isset($_REQUEST['Km'])) $Km=filter_var($_REQUEST['Km'], FILTER_SANITIZE_STRING); else $Km='';
if(isset($_REQUEST['LastSRvc'])) $LastSRvc=filter_var($_REQUEST['LastSRvc'], FILTER_SANITIZE_STRING); else $LastSRvc='';
if(isset($_REQUEST['NxtSrv'])) $NxtSrv=filter_var($_REQUEST['NxtSrv'], FILTER_SANITIZE_STRING); else $NxtSrv='';
if(isset($_REQUEST['Description'])) $Description=filter_var($_REQUEST['Description'], FILTER_SANITIZE_STRING); else $Description='';

class PDF extends FPDF{
	function Header(){
		$this->Image('../../../logowbnew.png',3, 0.5,15);
		$this->SetFillColor(96,96,96);
		$this->SetTextColor(0,0,0);
		$this->SetFont('Arial','B',9);
		//$this->from='UTF-8';
		$this->SetXY(0.7,1);
		$this->SetFont('Arial','BU',14);
		$this->Text(1, 4.5, '');
		$this->Ln(4);
		}
	function Footer(){ //Position at 1.2 cm from bottom
		$this->SetXY(0.7,-1.2);
		$this->SetFont('Arial','I',8);
		//$this->from='UTF-8';
		$this->SetXY(0.7,-1.0);
		$this->Cell(0,0.5,utf8_decode('Centre Honda Genève rue des Charmilles 3  CH-1203   Tél. +41 22 339 7000  Fax. +41 22 339 7009  www.hondageneve.com '),1,0,'C');
		}
	var $widths;
	var $aligns;

	function SetWidths($w){	//Set the array of column widths
		$this->widths=$w;
	}

	function SetAligns($a){	//Set the array of column alignments
		$this->aligns=$a;
	}

	function Row($data){ //Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($data);$i++)
			$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
		$h=0.7*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h);
		//Draw the cells of the row
		for($i=0;$i<count($data);$i++){
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'J';
			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();
			//Draw the border
			$this->Rect($x,$y,$w,$h);
			//Print the text
			$this->MultiCell($w,0.7,$data[$i],0,$a);
			//Put the position to the right of the cell
			$this->SetXY($x+$w,$y);
		}
		//Go to the next line
		$this->Ln($h);
	}

	function CheckPageBreak($h){ //If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger)
			$this->AddPage($this->CurOrientation);
	}

	function NbLines($w,$txt){ //Computes the number of lines a MultiCell of width w will take
		$cw=&$this->CurrentFont['cw'];
		if($w==0)
			$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r",'',$txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb--;
		$sep=-1;
		$i=0;
		$j=0;
		$l=0;
		$nl=1;
		while($i<$nb){
			$c=$s[$i];
			if($c=="\n"){
				$i++;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
				continue;
			}
			if($c==' ')
				$sep=$i;
			$l+=$cw[$c];
			if($l>$wmax){
				if($sep==-1){
					if($i==$j)
						$i++;
				}
				else
					$i=$sep+1;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
			}
			else
				$i++;
		}
		return $nl;
	}
}

$CLInFo = GetClientInfos($NoClient);

$today=strftime("%d %B %Y");

$pdf=new PDF('P','cm','A4');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFillColor(96,96,96);
$pdf->SetTextColor(0,0,0);
$pdf->SetAligns('J');

$pdf->setFont('Arial', '', 10);
$pdf->Text(14, 9, utf8_decode("Genève le : ".$today));

$pdf->setFont('Arial', '', 10);
$Nom = ucfirst($Nom);

$pdf->Text(14,5, ucwords($CLInFo['Allocution']));
$pdf->Text(14,5.5, utf8_decode($Nom)." ".utf8_decode($Prenom));
$pdf->Text(14,6,utf8_decode($CLInFo['Adresse']));
$pdf->Text(14,6.5,$CLInFo['NPA']." ".utf8_decode($CLInFo['Localite']));

$pdf->setFont('Arial', 'B', 12);
$pdf->Text(1.5, 11, 'Concerne: suivi de service');

$pdf->setFont('Arial', '', 10);
$pdf->Text(1.5,12, ucwords($CLInFo['Allocution'])." ".utf8_decode($Nom).",");

$pdf->setFont('Arial', '', 10);
$pdf->Text(1.5,13, utf8_decode("Selon notre base de données, le dernier service de votre ".$Model." date du ".$Date." avec ".number_format($Km, 0, '', '\'')." km"));

$pdf->setFont('Arial', '', 10);
$pdf->Text(1.5,13.5, utf8_decode("Nous nous permettons de vous contacter pour vous rappeler que le prochain service de votre véhicule est à ".number_format($NxtSrv, 0, '', '\'')." km "));
$pdf->Text(1.5,14, utf8_decode("ou 2 ans. Vous avez la possibilité de prendre rendez-vous sur notre site web à la page:"));

$pdf->setFont('Arial', 'i', 10);
$pdf->Text(1.5,15.5,utf8_decode("Une remise de Frs 25.- vous sera automatiquement accordée lors de la facturation de l'intervention en prenant"));
$pdf->Text(1.5,16,utf8_decode("rendez-vous en ligne"));

$pdf->setFont('Arial', 'B', 10);
$pdf->Text(7.5,16.5,"https://services.hondageneve.com/");

$pdf->setFont('Arial', '', 10);
$pdf->Text(1.5,17.5,utf8_decode("Vous pouvez également nous contacter au:"));
$pdf->setFont('Arial', 'B', 10);
$pdf->Text(6.5,19,utf8_decode("022 339 70 02 du 8h à 18h le samedi de 9h à 12h"));
$pdf->setFont('Arial', '', 10);
$pdf->Text(1.5,20,utf8_decode("Nous vous remercions par avance de votre attention et vous prions, ".ucwords($CLInFo['Allocution']).", d'agréer nos salutations les meilleures."));
$pdf->setFont('Arial', 'B', 11);
$pdf->Text(7.5,22.5,utf8_decode("Votre Team Centre Honda Genève"));

$pdf->SetFillColor(0xdd,0xdd,0xdd);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('Arial','',8);
$pdf->SetXY(1,$pdf->GetY()+0.7);
$fond=0;
$pdf->SetWidths(array(3,2,11,2,1));
$printer="colors";
$datenow = date("Ymd");
//$pdf->output();
$pdf->Output('/opt/webbike/files/suivis/'.$NoClient.'_'.$datenow.'.pdf','F');
$cmd='lp -d '.$printer.' /opt/webbike/files/suivis/'.$NoClient.'_'.$datenow.'.pdf';
exec($cmd);
$cmd2 = 'rm -rf /opt/webbike/files/suivis/'.$NoClient.'_'.$datenow.'.pdf';
//exec($cmd2);
?>
