<?php
// Gestion de l'affichage des réservations
echo '<div name="noprint" class="preins paddr1"><h3>'.FREQC_RES3.'</h3>';
if($droits==2&&$pagelisteresa=='0') {
	echo RESAINS1;
}
// 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 = "eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
}
else if($ordreaff_info['valeur']=="nom") {
	$ordre = "eleves.nom ASC, eleves.prenom ASC";
}
else if($ordreaff_info['valeur']=="hor") {
	$ordre = "freq.freqres_id DESC";
}
else if($ordreaff_info['valeur']=="esp") {
	$ordre = "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 = "activites.nom_activ ASC, eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
}
else { $ordre = "eleves.division ASC, eleves.nom ASC, eleves.prenom ASC"; }
$crenchoixr = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_crenchoix'");
$crenchoix_infor = mysqli_fetch_assoc($crenchoixr);
if($_SESSION['affresa_norights']=='1'&&isset($_SESSION['ccren'])) {
	$day = date('Y-m-d');
	$reservar_select = "SELECT freq.freqres_id, freq.eleves_id, freq.date_freqres, freq.code_freqres, freq.hor_name, freq.user_id, freq.raison, eleves.nom, eleves.prenom, SUBSTR(eleves.nom,1,10) nomsub, SUBSTR(eleves.prenom,1,10) prenomsub, eleves.division, espaces.espace_id, espaces.espace_nom, hor.num_creneau, activites.activ_id, activites.code_activ, activites.nom_activ  
	FROM (
	    SELECT * 
	    FROM ".$prefix."_freqres 
	    WHERE code_freqres = '".$day.$_SESSION['ccren']."' AND statut = '0' 
	) freq 
	INNER JOIN ".$prefix."_eleves eleves
	ON freq.eleves_id = eleves.eleves_id
	INNER JOIN ".$prefix."_horaires hor
	ON freq.hor_name = hor.hor_name 
	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."_user user
	ON user.id = freq.user_id 
	ORDER BY freq.date_freqres, hor.num_creneau, ".$ordre."";
	$reservar = mysqli_query($bdd,$reservar_select);
	$reservar_info = mysqli_fetch_assoc($reservar);
	$nb_reservar = mysqli_num_rows($reservar);
}
else if($crenchoix_infor['valeur']==1) { // si choix des créneaux (on affiche l'ensemble des réservations)
	$day = date('Y-m-d');
	$reservar_select = "SELECT freq.freqres_id, freq.eleves_id, freq.date_freqres, freq.code_freqres, freq.hor_name, freq.user_id, freq.raison, eleves.nom, eleves.prenom, SUBSTR(eleves.nom,1,10) nomsub, SUBSTR(eleves.prenom,1,10) prenomsub, eleves.division, espaces.espace_id, espaces.espace_nom, hor.num_creneau, activites.activ_id, activites.code_activ, activites.nom_activ  
	FROM (
	    SELECT * 
	    FROM ".$prefix."_freqres 
	    WHERE date_freqres >= '".$day."%' AND statut = '0' 
	) freq 
	INNER JOIN ".$prefix."_eleves eleves
	ON freq.eleves_id = eleves.eleves_id
	INNER JOIN ".$prefix."_horaires hor
	ON freq.hor_name = hor.hor_name 
	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."_user user
	ON user.id = freq.user_id 
	ORDER BY freq.date_freqres, hor.num_creneau, ".$ordre."";
	$reservar = mysqli_query($bdd,$reservar_select);
	$reservar_info = mysqli_fetch_assoc($reservar);
	$nb_reservar = mysqli_num_rows($reservar);
}
else {
	$dayr = date_format($date, 'Y-m-d');
	$crenresr = $dayr.$_SESSION['crenres'];
	$reservar_select = "SELECT freq.freqres_id, freq.eleves_id, freq.date_freqres, freq.code_freqres, freq.hor_name, freq.user_id, freq.raison, espaces.espace_id, espaces.espace_nom, eleves.nom, eleves.prenom, SUBSTR(eleves.nom,1,10) nomsub, SUBSTR(eleves.prenom,1,10) prenomsub, eleves.division, activites.activ_id, activites.code_activ, activites.nom_activ   
	FROM (
	    SELECT * 
	    FROM ".$prefix."_freqres 
	    WHERE code_freqres LIKE '".$crenres."%' AND statut = '0' 
	) 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."_user user
	ON user.id = freq.user_id 				
	ORDER BY ".$ordre."";
	$reservar = mysqli_query($bdd,$reservar_select);
	$reservar_info = mysqli_fetch_assoc($reservar);
	$nb_reservar = mysqli_num_rows($reservar);
}
if($nb_reservar == 0) {
	echo '<p>'.NONE2.'.</p>';
}			
else {
	// Pour le premier item
	$crenres = $reservar_info['code_freqres'];
	echo '<p><u>'.LE.' ';
	echo date('d/m/Y', strtotime(str_replace("/","-",$reservar_info['date_freqres']))).' - ';
	$creneaur = $reservar_info['hor_name'];
	$horr = mysqli_query($bdd,"SELECT debut, fin FROM ".$prefix."_horaires WHERE hor_name = '".$creneaur."'");
	$horr_info = mysqli_fetch_assoc($horr);
	$deb_cren = substr($creneaur, 0, 1);
	$fin_cren = substr($creneaur, 1, 1);
	echo ' '.$deb_cren.$fin_cren.' : '.FROM.' ';
	$deb = $horr_info['debut'];
	$dh = substr($deb, 0, 2);
	$dm = substr($deb, 3, 2);
	echo ''.$dh.'h'.$dm.' '.TO.' ';
	$fin = $horr_info['fin'];
	$fh = substr($fin, 0, 2);
	$fm = substr($fin, 3, 2);
	$nb_eleresr = mysqli_num_rows(mysqli_query($bdd,"SELECT freqres_id FROM ".$prefix."_freqres WHERE code_freqres = '".$reservar_info['code_freqres']."' AND statut = '0'"));
	echo ''.$fh.'h'.$fm.' <b>(<span id="res'.$reservar_info['code_freqres'].'">'.$nb_eleresr.'</span>)</b></u></p>';
	$_SESSION['code_freqres'] = $reservar_info['code_freqres'];
	// Pour la suite
	foreach($reservar as $liste) {
		if(!isset($_SESSION['code_freqres'])) {
			$_SESSION['code_freqres'] = "";
		}
		if($liste['code_freqres']!=$_SESSION['code_freqres']) {
			$crenres = $liste['date_freqres'];
			echo '<p><u>'.LE.' ';
			echo date('d/m/Y', strtotime(str_replace("/","-",$crenres))).' - ';
			$creneaur = $liste['hor_name'];
			$horr = mysqli_query($bdd,"SELECT debut, fin FROM ".$prefix."_horaires WHERE hor_name = '".$creneaur."'");
			$horr_info = mysqli_fetch_assoc($horr);
			$deb_cren = substr($creneaur, 0, 1);
			$fin_cren = substr($creneaur, 1, 1);
			echo ' '.$deb_cren.$fin_cren.' : '.FROM.' ';
			$deb = $horr_info['debut'];
			$dh = substr($deb, 0, 2);
			$dm = substr($deb, 3, 2);
			echo ''.$dh.'h'.$dm.' '.TO.' ';
			$fin = $horr_info['fin'];
			$fh = substr($fin, 0, 2);
			$fm = substr($fin, 3, 2);
			$nb_eleresr = mysqli_num_rows(mysqli_query($bdd,"SELECT freqres_id FROM ".$prefix."_freqres WHERE code_freqres = '".$liste['code_freqres']."' AND statut = '0'"));
			echo ''.$fh.'h'.$fm.' <b>(<span id="res'.$liste['code_freqres'].'">'.$nb_eleresr.'</span>)</b></u></p>';
			$_SESSION['code_freqres'] = $liste['code_freqres'];
		}
		if ($droits==2||$droits==3||$_SESSION['affresa_norights']=='1') {
			echo '<p id="res'.$liste['freqres_id'].'">';
			$reserv_could=mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_reseleve'"));
			if ($reserv_could['valeur']!=0) {
				if ($liste['user_id']==0) {
					echo '<i class="fa-regular fa-star" title="'.RESA5.'"></i> ';
				}
				else {
					$users_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT nom, prenom, login FROM ".$prefix."_user WHERE id = '".$liste['user_id']."'"));
					echo '<i class="fa-solid fa-star" title="'.RESA4.' ';
					if($users_info['prenom']!='') {
						echo $users_info['prenom'].' ';
					}
					if($users_info['nom']!='') {
						echo $users_info['nom'];
					}
					if($users_info['nom']==''&&$users_info['prenom']=='') {
						echo $users_info['login'];
					}
					echo '"></i> ';
				}
			}
			if($espopt['valeur']==1) { 
				if($liste['espace_id']==0) { echo $esp0['valeur'].'. '; }
				else { echo $liste['espace_nom'].'. '; }
			}
			if($res_activ['valeur']==1||($reseleve_could['valeur']==1&&$reseleve_activ['valeur']==1)) {
				if($liste['code_activ']!=""&&$liste['code_activ']!=0) {
					echo $liste['nom_activ'].'. ';
				}
			}
			if(strlen($liste['nom'])>10) {
				echo mb_ereg_replace("[?]","",mb_convert_encoding($liste['nomsub'],"utf-8")).'... ';
			} else { echo $liste['nom'].' '; }
			echo mb_substr($liste['prenom'],0,1).'. ';
			echo $liste['division'].'';
			if($droits==2&&$pagelisteresa=='1') {
				echo ' - <input class="boutontab" type="submit" value="'.SUPPR_TINY.'" name="suppr'.$liste['freqres_id'].'" onclick="suppr'.$liste['freqres_id'].'(this,\'res'.$liste['freqres_id'].'\');" />';
			}
			else if($droits==2&&$pagelisteresa=='0') {
				echo ' - <input class="boutontab" type="submit" value="'.FREQ_BUTTON1.'" name="ins'.$liste['freqres_id'].'" onclick="ins'.$liste['freqres_id'].'(this,\'res'.$liste['freqres_id'].'\');" />';
			}
			if ($liste['raison']!="") {
				echo '<br><i>'.RESAREASON2.' '.$liste['raison'].'</i>';
			}
			if($droits==2) {
				echo '</p>'; ?>
				<script type="text/javascript">
					function suppr<?php echo $liste['freqres_id']; ?>(retards_id, divs) {
					var rnb = document.getElementById("res<?php echo $liste['code_freqres']; ?>").textContent;
					var new_rnb = rnb-1;
					document.getElementById(divs).style.display = "none";
					document.getElementById("res<?php echo $liste['code_freqres']; ?>").textContent = new_rnb;
					var divtdbas = document.getElementById("bas<?php echo $liste['freqres_id']; ?>");
					if(divtdbas) {
						document.getElementById("bas<?php echo $liste['freqres_id']; ?>").style.display = "none";
						var bnb = document.getElementById("basnb").textContent;
						var new_bnb = bnb-1;
						document.getElementById("basnb").textContent = new_bnb;
					}
					var freqres_id = <?php echo $liste['freqres_id']; ?>;
					$.ajax({
					    type: "POST",
					    data: { freqres_id: freqres_id },
					    url: "js/idl/freq_ressup.php",
					   });
					}						
				</script>
				<script type="text/javascript">
					function ins<?php echo $liste['freqres_id']; ?>(retards_id, divs) {
					var rnb = document.getElementById("res<?php echo $liste['code_freqres']; ?>").textContent;
					var new_rnb = rnb-1;
					document.getElementById(divs).style.display = "none";
					document.getElementById("res<?php echo $liste['code_freqres']; ?>").textContent = new_rnb;
					var divtdbas = document.getElementById("bas<?php echo $liste['freqres_id']; ?>");
					if(divtdbas) {
						document.getElementById("bas<?php echo $liste['freqres_id']; ?>").style.display = "none";
						var bnb = document.getElementById("basnb").textContent;
						var new_bnb = bnb-1;
						document.getElementById("basnb").textContent = new_bnb;
					}
					var freqres_id = <?php echo $liste['freqres_id']; ?>;
					$.ajax({
					    type: "POST",
					    data: { freqres_id: freqres_id },
					    url: "js/idl/freq_resins.php",
					   });
					}						
				</script><?php
			}						
		}
		else if ($droits==5||$droits==6) {
			echo '<p>'.$liste['nom'].' '.$liste['prenom'].' '.$liste['division'].'</p>';
		}
	}
}
echo '</div>';
?>