<?php
if($_POST['ordredeb']!=$_POST['ordrefin']) {
	$resprint_query = $_SESSION['resprint_query'];
	if($_POST['ordrefin']=="div") {
		$ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="nom") {
		$ordre = "ORDER BY eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="hor") {
		$ordre = "ORDER BY freq.freqres_id ASC";
	}
	else if($_POST['ordrefin']=="esp") {
		$ordre = "ORDER BY espaces.espace_nom ASC, eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="act") {
		$ordre = "ORDER BY activites.nom_activ ASC, eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	else { $ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC"; }
	$list_reservs = $_SESSION['listdeb'];

	if($resprint_query==0||$resprint_query==1) {
		$filtre1 = $_SESSION['filtre1'];
		$inscrits_select = "SELECT freq.freqres_id, freq.eleves_id, freq.code_freqres, freq.statut, espaces.espace_id, espaces.espace_nom, eleves.eleves_id, eleves.nom, eleves.prenom, eleves.division, activites.activ_id, activites.code_activ, activites.nom_activ  
		FROM ".$prefix."_freqres freq 
		INNER JOIN ".$prefix."_eleves eleves
		ON freq.eleves_id = eleves.eleves_id
		LEFT JOIN ".$prefix."_espaces espaces
		ON espaces.espace_id = freq.espace_id 
		LEFT JOIN ".$prefix."_freqactiv activites
		ON activites.code_activ = freq.code_activ 
		".$filtre1." 
		AND freq.statut = '0' 
		".$ordre."";
		$inscrits = mysqli_query($bdd,$inscrits_select);
		$inscrits_info = mysqli_fetch_assoc($inscrits);
		$nb_inscrits = mysqli_num_rows($inscrits);
	}	

	else if($resprint_query==2) { //si aucun créneau, alors on affiche la journée
		$filtre1 = $_SESSION['filtre1'];
		$inscrits_select = "SELECT freq.freqres_id, freq.eleves_id, freq.hor_name, freq.code_freqres, freq.statut, espaces.espace_id, espaces.espace_nom, eleves.eleves_id, eleves.nom, eleves.prenom, eleves.division, activites.activ_id, activites.code_activ, activites.nom_activ, hor.num_creneau  
		FROM ".$prefix."_freqres freq 
		INNER JOIN ".$prefix."_eleves eleves
		ON freq.eleves_id = eleves.eleves_id
		LEFT JOIN ".$prefix."_espaces espaces
		ON espaces.espace_id = freq.espace_id 
		LEFT JOIN ".$prefix."_freqactiv activites
		ON activites.code_activ = freq.code_activ 
		LEFT JOIN ".$prefix."_horaires hor
		ON hor.hor_name = freq.hor_name 
		".$filtre1." 
		AND freq.statut = '0' 
		ORDER BY hor.num_creneau ASC, 
		".substr($ordre,8)."";
		$inscrits = mysqli_query($bdd,$inscrits_select);
		$inscrits_info = mysqli_fetch_assoc($inscrits);
		$nb_inscrits = mysqli_num_rows($inscrits);
		$list_reservs .= '<h3>'.RESA6.DOTS.' '.date('d/m/Y', strtotime(str_replace("/","-",$_POST['dateprint']))).'</h3>';
	}
	$list_reservs .= '<h3>'.FREQ_INF3.' (<span id="basnb">'.$nb_inscrits.'</span>) :</h3>';

	if($nb_inscrits == 0) {
		$list_reservs .= '<p>'.NONE.'.</p>'; $list_reservs .= '<p>'.NONE.'.</p>';
	}

	else {
		//inscrits					
		$list_reservs .= '<table class="100p" id="tablesorter" class="tablesorter"><thead><tr><th>' . DIV . '</th><th>' . NAMEUP . '</th><th>' . FORNAME . '</th>';
		if($resprint_query==2) {
			$list_reservs .= '<th>' . CREN1 . '</th>';
		}
		if($_SESSION['espopt']==1) {
			$list_reservs .= '<th>' . ZONE . '</th>';
		}
		if($_SESSION['activ']==1) {
			$list_reservs .= '<th>' . ACTIV . '</th>';
		}
		$list_reservs .= '</tr></thead><tbody>';
		foreach($inscrits as $liste) {
			$list_reservs .= '<tr id="bas'.$liste['freqres_id'].'"><td>'.$liste['division'].'</td><td>'.$liste['nom'].'</td><td>'.$liste['prenom'].'</td>';
			if($resprint_query==2) {
				// créneau
				$creneau = $liste['hor_name'];
				$hor = mysqli_query($bdd,"SELECT num_creneau, debut, fin FROM ".$prefix."_horaires WHERE hor_name = '".$creneau."'");
				$hor_info = mysqli_fetch_assoc($hor);
				$deb_cren = substr($creneau, 0, 1);
				$list_reservs .= '<td>';
				if($hor_info['num_creneau']<10) {
					$list_reservs .= '0'.$hor_info['num_creneau'].' - ';
				}
				else {
					$list_reservs .= ''.$hor_info['num_creneau'].' - ';
				}
				$list_reservs .= MOMENT[$deb_cren];
				$fin_cren = substr($creneau, 1, 1);
				$list_reservs .= ' ('.$deb_cren.$fin_cren.') '.FROM.' ';
				$deb = $hor_info['debut'];
				$dh = substr($deb, 0, 2);
				$dm = substr($deb, 3, 2);
				$list_reservs .= ''.$dh.'h'.$dm.' '.TO.' ';
				$fin = $hor_info['fin'];
				$fh = substr($fin, 0, 2);
				$fm = substr($fin, 3, 2);
				$list_reservs .= ''.$fh.'h'.$fm.'</td>';
			}
			if($_SESSION['espopt']==1) {
				if($liste['espace_id']==0) { $list_reservs .= '<td>'.$_SESSION['esp0'].'</td>'; }
				else { $list_reservs .= '<td>'.$liste['espace_nom'].'</td>'; }
			}
			if($_SESSION['activ']==1) {
				$list_reservs .= '<td>';
				if($liste['activ_id']!="") { $list_reservs .= $liste['nom_activ']; }
				$list_reservs .= '</td>';
			}
			$list_reservs .= '</tr>';
		}
		$list_reservs .= '</tbody></table>';
		$mail_reservs = '<style> table { border-collapse:collapse; } td { border:1px solid grey; padding: 5px; } </style>';
		$mail_reservs .= $list_reservs;
		$_SESSION['freq_liste'] = $mail_reservs;
	}
}
?>