<?php
$_SESSION['resprint_query'] = $resprint_query;
if($resprint_query==0) {
	$list_reservs = '<h3>';
	$hor_res =  RESA6.DOTS.' ';
	$hor_res .= date_format($date, 'd/m/Y');
	$hor_res .= ' - ';
	$crenres = $hor_info['hor_name'];
	$_SESSION['crenres'] = $crenres;
	$deb_cren = substr("$crenres", 0, 1);
	$hor_res .= MOMENT[$deb_cren];
	$fin_cren = substr($crenres, 1, 1);
	$hor_res .= ' ('.$deb_cren.$fin_cren.') '.FROM.' ';
	$deb = $hor_info['debut'];
	$dh = substr($deb, 0, 2);
	$dm = substr($deb, 3, 2);
	$hor_res .= ''.$dh.'h'.$dm.' '.TO.' ';
	$fin = $hor_info['fin'];
	$fh = substr($fin, 0, 2);
	$fm = substr($fin, 3, 2);
	$hor_res .= ''.$fh.'h'.$fm.'';
	$list_reservs .= $hor_res;
	$list_reservs .= '</h3>';
	$day = date_format($date, 'Y-m-d');
	$code_freq = $day.$_SESSION['crenres'];
}
if($resprint_query!=0) {
	//date
	$day = date('Y-m-d', strtotime(str_replace("/","-",$_POST['dateprint'])));
	//créneaux
	if($_POST['crenprint']!="") {
		$cren1 = $_POST['crenprint'];
	}
	$list_reservs = '<h3>';
	$hor_res = RESA6.DOTS.' '.date('d/m/Y', strtotime(str_replace("/","-",$_POST['dateprint']))).' ';
	if(isset($cren1)) {
		$hor = mysqli_query($bdd,"SELECT debut, fin FROM ".$prefix."_horaires WHERE hor_name = '".$cren1."'");
		$hor_info = mysqli_fetch_assoc($hor);
		$deb_cren = substr("$cren1", 0, 1);
		$hor_res .= MOMENT[$deb_cren];
		$fin_cren = substr("$cren1", 1, 1);
		$hor_res .= ' ('.$deb_cren.$fin_cren.') '.FROM.' ';
		$deb = $hor_info['debut'];
		$dh = substr("$deb", 0, 2);
		$dm = substr("$deb", 3, 2);
		$hor_res .= ''.$dh.'h'.$dm.' '.TO.' ';
		$fin = $hor_info['fin'];
		$fh = substr("$fin", 0, 2);
		$fm = substr("$fin", 3, 2);
		$hor_res .= ''.$fh.'h'.$fm.'';
		$list_reservs .= $hor_res;
		$list_reservs .= '</h3>';
	}
	else {
		$list_reservs .= '</h3>';
	}
}
$_SESSION['listdeb'] = $list_reservs;

// récupération de l'ordre d'affichage souhaité
$ordreaff = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_ordreaff'");
$ordreaff_info = mysqli_fetch_assoc($ordreaff);
if($ordreaff_info['valeur']=="div") {
	$ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
}
else if($ordreaff_info['valeur']=="nom") {
	$ordre = "ORDER BY eleves.nom ASC, eleves.prenom ASC";
}
else if($ordreaff_info['valeur']=="hor") {
	$ordre = "ORDER BY freq.freqres_id ASC";
}
else if($ordreaff_info['valeur']=="esp") {
	$ordre = "ORDER BY espaces.espace_nom ASC, eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
}
else if($ordreaff_info['valeur']=="act"&&($res_activ['valeur']==1||($reseleve_could['valeur']==1&&$reseleve_activ['valeur']==1))) {
	$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"; }

if($resprint_query==0) {
	$filtre1 = "WHERE freq.code_freqres = '".$code_freq."'";
	$inscrits_select = "SELECT freq.freqres_id, freq.eleves_id, freq.code_freqres, 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 statut = '0' 
	".$ordre."";
	$inscrits = mysqli_query($bdd,$inscrits_select);
	$inscrits_info = mysqli_fetch_assoc($inscrits);
	$nb_inscrits = mysqli_num_rows($inscrits);
}
if($resprint_query==1) { // si créneau indiqué
	$filtre1 = "WHERE freq.code_freqres LIKE '".$day.$cren1."'";
	$inscrits_select = "SELECT freq.freqres_id, freq.eleves_id, freq.code_freqres, 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 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 = "WHERE freq.code_freqres LIKE '".$day."%'";
	$inscrits_select = "SELECT freq.freqres_id, freq.eleves_id, freq.hor_name, freq.code_freqres, 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 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>';
}

$_SESSION['filtre1'] = $filtre1;
$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>';
	}
	$_SESSION['espopt'] = $espopt['valeur'];
	if($espopt['valeur']==1) {
		$list_reservs .= '<th>' . ZONE . '</th>';
	}
	$_SESSION['activ']=0;
	if($res_activ['valeur']==1||($reseleve_could['valeur']==1&&$reseleve_activ['valeur']==1)) {
		$list_reservs .= '<th>' . ACTIV . '</th>';
		$_SESSION['activ']=1;
	}
	$_SESSION['esp0'] = $esp0['valeur'];
	$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($espopt['valeur']==1) {
			if($liste['espace_id']==0) { $list_reservs .= '<td>'.$esp0['valeur'].'</td>'; }
			else { $list_reservs .= '<td>'.$liste['espace_nom'].'</td>'; }
		}
		if($res_activ['valeur']==1||($reseleve_could['valeur']==1&&$reseleve_activ['valeur']==1)) {
			$list_reservs .= '<td>';
			if($liste['code_activ']!="") { $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;
	echo $list_reservs;
	$_SESSION['freq_liste'] = $list_reservs;
	//Impression :
	echo '<br> <br><form method="POST" action="" name="mailing">'.PDF_EXTRS.' 
	<div class="box"><select id="ordrfin" name="ordrefin"><option value="div"';
	if($ordreaff_info['valeur']=="div") { echo ' selected '; }
	echo '>'.ORDER4.'</option><option value="act"';
	if($ordreaff_info['valeur']=="act") { echo ' selected '; }
	echo '>'.ORDER5.'</option><option value="nom"';
	if($ordreaff_info['valeur']=="nom") { echo ' selected '; }
	echo '>'.ORDER6.'</option><option value="hor"';
	if($ordreaff_info['valeur']=="hor") { echo ' selected '; }
	echo '>'.ORDER7.'</option>';
	if($espopt['valeur']==1) { 
		echo '<option value="esp"';
		if($ordreaff_info['valeur']=="esp") { echo ' selected '; }
		echo '>'.ORDER8.'</option>';
	}
	echo '</select></div><br>';
	echo '&nbsp; &nbsp;  <a onclick="submitpdf();" id="preview-pdf"><img src="img/pdf-icon.png" title="'.PDF_EXTRACT.'" name="noprint" /></a>';
	// Envoi par mail
	if(@$_SESSION['auth_mail']==1) {
		if(isset($_POST['mail_send'])) {
			include('./db/proc/freq_extract_resorder.php');
			include('./db/proc/mailing/mailing_reservs.php');
		}
		echo '&nbsp; &nbsp; <a href="javascript:;clearTimeout(timer);" id="mail" onmousedown="toggleDiv(\'divmail\');"><img src="img/div/mail.png" title="'.FREQ_SEND1.'" name="noprint" class="printer" /></a>';
		echo '<div name="noprint" id="divmail" style="display:none;">
		<p>'.FREQ_SEND2.'</p>';
		$mails = mysqli_query($bdd,"SELECT * FROM ".$prefix."_user WHERE mail <> ''");
		if(mysqli_num_rows($mails)>0) {
			while($mail=mysqli_fetch_array($mails)) {
				echo '<input type="checkbox" name="mail[]" id="'.$mail['mail'].'" value="'.$mail['mail'].'" checked /><label for="'.$mail['mail'].'"> '.$mail['login'].' : '.$mail['mail'].'</label><br>';
			}
		}
		echo MAILOTH.' (1)'.DOTS.' <input type="email" name="mail1" /><br>
		'.MAILOTH.' (2)'.DOTS.' <input type="email" name="mail2" /><br>
		<input type="hidden" name="ordredeb" value="'.$ordreaff_info['valeur'].'" />
		<input class="bouton boutfreq" type="submit" name="mail_send" value="'.SEND.'" />';
		echo '</div>';
	}
	echo '</form>';
	if(isset($_POST['havepdf'])) {
		include('./include/pdf_loaders/freq_liste.php');
	}
    echo '<form id="loadpdf" action="./include/pdf_loaders/freq_liste.php" method="post"><input type="hidden" name="havepdf" value="2" /><input type="hidden" name="ordredeb" value="'.$ordreaff_info['valeur'].'" /><input type="hidden" id="ordrefin" name="ordrefin" value="1" /></form>';

    echo '<script type="text/javascript">
    function submitpdf(){
    	document.getElementById("ordrefin").value = document.getElementById("ordrfin").value;
    	$("#loadpdf").submit()
    }
    </script></p>';
}
?>
