<?php 
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
		
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][5]<"2") {
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="../../index.php">'.ACCUEIL.'</a></p>';
	}
	else {
			
		// variables diverses
		$margextd = 15;
		$margexth = 15;
		$margextg = 15;
		$margextb = 15;
		$margintd = 2;
		$marginth = 2;
		$margintg = 2;
		$margintb = 2;
		$fontsize = 7;

		if($_POST['format']=='P') { $pagew = 210; } else { $pagew = 297; }
		if($_POST['ordrefin']=='div') { 
			$ordree = ' ORDER BY division ASC, nom ASC, prenom ASC';
			$ordreg = ' ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC';
		}
		else if($_POST['ordrefin']=='nom') { 
			$ordree = ' ORDER BY nom ASC, prenom ASC';
			$ordreg = ' ORDER BY eleves.nom ASC, eleves.prenom ASC';
		}
		else if($_POST['ordrefin']=='nai') { 
			$ordree = ' ORDER BY naissance ASC, nom ASC, prenom ASC';
			$ordreg = ' ORDER BY eleves.naissance ASC, eleves.nom ASC, eleves.prenom ASC';
		}

		$k=0;
			
		if($_POST['titre']!='') {
			$bctab .= '<h1>'.$_POST['titre'].'</h1>';
		}
		$bctab .= '<table><thead><tr>';

		for($i=1;$i<11;$i++) {
			if($_POST['col'.$i]!='0') {
				$bctab .= '<th';
				if($_POST['txtlarg'.$i]!='0'&&$_POST['txtlarg'.$i]!='') {
					$bctab .= ' style="width:'.$_POST['txtlarg'.$i].'cm;"';
				}
				$bctab .= '>';
				if($_POST['col'.$i]=="eleves_id") { $bctab .= IDENT2; }
				else if($_POST['col'.$i]=="nom") { $bctab .= NAME; }
				else if($_POST['col'.$i]=="prenom") { $bctab .= FORNAME; }
				else if($_POST['col'.$i]=="division") { $bctab .= DIV; }
				else if($_POST['col'.$i]=="naissance") { $bctab .= BDAY; }
				else if($_POST['col'.$i]=="sexe") { $bctab .= SEX; }
				else if($_POST['col'.$i]=="code") { $bctab .= CODE; }
				$bctab .= '</th>';
			}
			else if($_POST['col'.$i]=='0'&&$_POST['txtcol'.$i]!='') {
				$bctab .= '<th';
				if($_POST['txtlarg'.$i]!='0'&&$_POST['txtlarg'.$i]!='') {
					$bctab .= ' style="width:'.$_POST['txtlarg'.$i].'cm;"';
				}
				$bctab .= '>'.$_POST['txtcol'.$i].'</th>';
			}
		}

		$bctab .= '</tr></thead><tbody>';
		
		$division = mysqli_real_escape_string($bdd,$_POST['division']);
		
		$eleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE division = '".$division."' ".$ordree."");
		$eleves_nb = mysqli_num_rows($eleves);
		if($eleves_nb>0) {
			foreach($eleves as $eleve_data) {
				$bctab .= '<tr>';
				for($i=1;$i<11;$i++) {
					if($_POST['col'.$i]!='0') {
						$bctab .= '<td>'.mysqli_real_escape_string($bdd,$eleve_data[$_POST['col'.$i]]).'</td>';
					}
					else if($_POST['col'.$i]=='0'&&$_POST['txtcol'.$i]!='') {
						$bctab .= '<td></td>';
					}
				}
				$bctab .= '</tr>';
			}
		}
		else {
			$groupele = mysqli_query($bdd,"SELECT groupele.groupe_id, groupele.eleves_id, eleves.nom, eleves.prenom, eleves.division, eleves.code, eleves.naissance, eleves.sexe
			FROM ".$prefix."_groupele groupele
			INNER JOIN ".$prefix."_eleves eleves
			ON groupele.eleves_id = eleves.eleves_id
			WHERE groupele.groupe_id = '".$division."' 
			".$ordreg."");
			$eleves_nb = mysqli_num_rows($groupele);
			foreach($groupele as $eleve_data) {
				$bctab .= '<tr>';
				for($i=1;$i<11;$i++) {
					if($_POST['col'.$i]!='0') {
						$bctab .= '<td>'.mysqli_real_escape_string($bdd,$eleve_data[$_POST['col'.$i]]).'</td>';
					}
					else if($_POST['txtcol'.$i]!='') {
						$bctab .= '<td></td>';
					}
				}
				$bctab .= '</tr>';
			}
		}

		$bctab .= '</tbody></table>';

		$html = '
		<html>
		<head>
		<style>
		body {font-family: sans-serif;
			font-size: '.$fontsize.'pt;
		}
		@page :first {
			margin-left : '.$margextg.'mm;
			margin-right : '.$margextd.'mm;
			margin-top : '.$margexth.'mm; 
			margin-bottom : '.$margextb.'mm;
		}
		table {
			border-collapse:collapse;
			border :1px solid black;
		}
		th {
			border :1px solid black;
			text-align:left; 
			padding: '.$marginth.'mm '.$margintd.'mm '.$margintb.'mm '.$margintg.'mm;
		}
		td { 
			border :1px solid black;
			text-align:left; 
			padding: '.$marginth.'mm '.$margintd.'mm '.$margintb.'mm '.$margintg.'mm;
		}
		</style>
		</head>
		<body>
		'.$bctab.'
		</body>
		</html>
		';
		$path = (getenv('MPDF_ROOT')) ? getenv('MPDF_ROOT') : __DIR__;
		if(phpversion()>=8.0) {
			require_once '../mpdf8/autoload.php';
		}
		else {
			require_once '../mpdf/autoload.php';
		}
		$mpdf = new \Mpdf\Mpdf([
			'orientation' => ''.$_POST['format'].'',
			/*'margin_left' => $modele['margextg'].'mm',
			'margin_right' => $margextd.'mm',
			'margin_top' => $modele['margexth'].'mm',
			'margin_bottom' => $modele['margextb'].'mm',*/
			'showBarcodeNumbers' => FALSE
		]);
		try {
			$mpdf->WriteHTML($html);
		} catch (\Mpdf\MpdfException $e) {
			die ($e->getMessage());
		}
		ob_end_clean();
		$mpdf->Output('eleves_liste.pdf', \Mpdf\Output\Destination::DOWNLOAD);
	}
}
else {
	include('../noaccess.php');
	echo '<a href="../../index.php">'.ACCUEIL.'</a></p>';
}