<?php
require('fpdf.php');

	header( 'content-type: text/html; charset=utf-8' );
	include('database.php');
	include('fonctions.php');	


class PDF extends FPDF
{
// Chargement des données
function LoadData()
{
	$annee = date('Y');	
	$table = "reservation" .$annee;
	$mois = "avril";
	
	$connexion = DB(); 
	
	$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and mois ='$mois' and mode = 'actif' ";			
	//echo $sql;
	mysqli_query($connexion, "SET NAMES 'utf8'");		
	$result = mysqli_query($connexion, $sql );
	$tab_jour = array();
				
	while ($data = mysqli_fetch_assoc ($result))				
	{	
				//$jour = "coco" ; //$data['rendezvous'];
				//$semaine = "lulu"; //$data['semaine'];		
				$seance1 = $data['seance1'];
				$seance2 = $data['seance2'];
				$seance3 = $data['seance3'];
				$seance4 = $data['seance4'];
				$seance5 = $data['seance5'];
				$seance6 = $data['seance6'];
				$seance7 = $data['seance7'];
				$seance8 = $data['seance8'];
				
				if($seance1 =="")
				{
					$seance1 = "Libre";
				}
				if($seance2 =="")
				{
					$seance2 = "Libre";
				}
				if($seance3 =="")
				{
					$seance3 = "Libre";
				}
				if($seance4 =="")
				{
					$seance4 = "Libre";
				}
				if($seance5 =="")
				{
					$seance5 = "Libre";
				}
				if($seance6 =="")
				{
					$seance6 = "Libre";
				}
				if($seance7 =="")
				{
					$seance7 = "Libre";
				}
				if($seance8 =="")
				{
					$seance8 = "Libre";
				}
				
				$str = $seance1.','.$seance2.','.$seance3.','.$seance4.','.$seance5.','.$seance6.','.$seance7.','.$seance8;
				
				$tab_data[] = explode(',',trim($str));
	}	
	
	//echo '<pre>';
	//print_r($tab_data);
	return($tab_data);
}

// Tableau simple
function BasicTable($header, $data)
{
	// En-tête
	foreach($header as $col)
		$this->Cell(40,7,$col,1);
	$this->Ln();
	// Données
	foreach($data as $row)
	{
		foreach($row as $col)
			$this->Cell(40,16,$col,2);
		$this->Ln();
	}
}

// Tableau amélioré
function ImprovedTable($header, $data)
{
	// Largeurs des colonnes
	$w = array(20, 20, 20, 20, 40, 35, 45, 40, 40, 40);
	// En-tête
	for($i=0;$i<count($header);$i++)
		$this->Cell($w[$i],7,$header[$i],1,0,'C');
	$this->Ln();
	// Données
	foreach($data as $row)
	{
		$this->Cell($w[0],6,$row[0],'LN');
		$this->Cell($w[1],6,$row[1],'LN');
		$this->Cell($w[2],6,$row[2],'LN');
		$this->Cell($w[3],6,$row[3],'LN');
		$this->Cell($w[4],6,$row[4],'LN');
		$this->Cell($w[5],6,$row[5],'LN');
		$this->Cell($w[6],6,$row[6],'LN');
		
		$this->Ln();
	}
	// Trait de terminaison
	$this->Cell(array_sum($w),0,'','T');
}

// Tableau coloré
function FancyTable($header, $data)
{
	// Couleurs, épaisseur du trait et police grasse
	$this->SetFillColor(255,0,0);
	$this->SetTextColor(255);
	$this->SetDrawColor(128,0,0);
	$this->SetLineWidth(.3);
	$this->SetFont('','B');
	// En-tête
	$w = array(40, 35, 45, 40);
	for($i=0;$i<count($header);$i++)
		$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
	$this->Ln();
	// Restauration des couleurs et de la police
	$this->SetFillColor(224,235,255);
	$this->SetTextColor(0);
	$this->SetFont('');
	// Données
	$fill = false;
	foreach($data as $row)
	{
		$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
		$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
		$this->Cell($w[2],6,number_format($row[2],0,',',' '),'LR',0,'R',$fill);
		$this->Cell($w[3],6,number_format($row[3],0,',',' '),'LR',0,'R',$fill);
		$this->Ln();
		$fill = !$fill;
	}
	// Trait de terminaison
	$this->Cell(array_sum($w),0,'','T');
}
}

$pdf = new PDF('L');
// Titres des colonnes
$header = array('Seance1', 'Seance2', 'Seance3', 'Seance4', 'Seance5', 'Seance6', 'Seance7', 'Seance8');
// Chargement des données
$data = $pdf->LoadData();
$pdf->SetFont('Arial','',10);
$pdf->AddPage();
$pdf->BasicTable($header,$data);
$pdf->AddPage();
$pdf->ImprovedTable($header,$data);
//$pdf->AddPage();
//$pdf->FancyTable($header,$data);
$pdf->Output();
?>
