<?php
// Gestion des inscrits du créneau précédent
if(substr($crpre_info['valeur'],0,1)>0&&$_SESSION['num']!=1&&($droits==2||$droits==6||$droits==4)) {
	// vérification que le créneau actuel est ouvert aux inscriptions :
	$cren = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = '".$_SESSION['num']."'");
	$cren_info = mysqli_fetch_assoc($cren);
	
	// 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.h_inscription DESC";
	}
	else if($ordreaff_info['valeur']=="act") {
		$ordre = "ORDER BY freq.code_activ ASC";
	}
	else if($ordreaff_info['valeur']=="esp") {
		$ordre = "ORDER BY espaces.espace_nom ASC, eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	// affichage des inscrits du créneau précédent
	$prenum = $_SESSION['num']-1;
	$precren = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = '".$prenum."'");
	$precren_info = mysqli_fetch_assoc($precren);
	if($freq_gestion_jourscren['valeur']==0) {
		while(substr($precren_info['hor_freq'],0,3)=='000'){
			$prenum--;
			$precren = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = '".$prenum."'");
			$precren_info = mysqli_fetch_assoc($precren);
		}
	}
	else {
		while(@substr($precren_info['hor_freq'],strpos($precren_info['hor_freq'], date('D')) + strlen(date('D')), 3)=='000'){
			$prenum--;
			$precren = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = '".$prenum."'");
			$precren_info = mysqli_fetch_assoc($precren);
		}
	}
	if($prenum<$_SESSION['num']) {
		$dday = date("Y-m-d");
		$ccren = $dday.$_SESSION['ccren'];
		
		$preins_select = "SELECT freq.eleves_id, freq.date_freq, freq.code_activ, freq.hor_name, freq.h_inscription, espaces.espace_nom, espaces.espace_id, eleves.nom, SUBSTR(eleves.nom,1,10) nomsub, eleves.prenom, eleves.division, eleves.naissance, activ.nom_activ, activ.activ_id 
		FROM (SELECT * 
			FROM ".$prefix."_freq 
			WHERE date_freq = '".$dday."' AND hor_name =  '".@$precren_info['hor_name']."'
		) freq 
		INNER JOIN ".$prefix."_eleves eleves
		ON freq.eleves_id = eleves.eleves_id
		LEFT JOIN ".$prefix."_freqactiv activ
		ON activ.code_activ = freq.code_activ
		LEFT JOIN ".$prefix."_espaces espaces
		ON espaces.espace_id = freq.espace_id
		".$ordre."";
		$preins = mysqli_query($bdd,$preins_select);
		$nb_preins = mysqli_num_rows($preins);
					
		if($nb_preins != 0) {
			echo '<div name="preins" class="preinsgrappes"><h3 style="color:black;text-decoration:none;" onmouseover="this.style.textDecoration=\'underline\';" onmouseout="this.style.textDecoration=\'none\';"onmousedown="toggleDiv(\'divcrenp\');"><i class="fas fa-angle-double-right"></i> '.FREQ_INF1.' (';
			echo $precren_info['hor_name'].' - ';
			$listeh = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE hor_name = '".$precren_info['hor_name']."'"));
			$deb_cren = substr($precren_info['hor_name'], 0, 1);
			include('include/hor_boucle.php');
			echo ')</h3>';
			echo '<div id="divcrenp" style="display:';
			if(substr($crpre_info['valeur'],0,1)==1) { echo 'block'; }
			else if(substr($crpre_info['valeur'],0,1)==2) { echo 'none'; }
			echo ';"><table name="form">';
			foreach($preins as $validpreins) {
				// effacement s'ils sont inscrits dans le nouveau créneau
				$preins_verif_select = "SELECT freq.eleves_id, freq.date_freq, freq.hor_name, freq.code_activ, freq.cas, freq.h_inscription, espaces.espace_nom, espaces.espace_id, eleves.nom, eleves.prenom, eleves.division, eleves.naissance  
				FROM ".$prefix."_freq 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
				WHERE freq.date_freq = '".$dday."' AND freq.hor_name = '".$_SESSION['ccren']."' AND eleves.division = '".$validpreins['division']."' AND eleves.nom = '".addslashes($validpreins['nom'])."' AND eleves.prenom = '".addslashes($validpreins['prenom'])."'";
				$preins_verif = mysqli_query($bdd,$preins_verif_select);
				$preins_verif_nb = mysqli_num_rows($preins_verif);
				
				$preins_nb = 1;
				// formulaire
				echo '<tr id="prec'.$validpreins['eleves_id'].'"><td>'.$validpreins['division'].' - ';
				if(strlen($validpreins['nom'])>10) {
					echo mb_ereg_replace("[?]","",mb_convert_encoding($validpreins['nomsub'],"utf-8")).'... ';
				} else { echo $validpreins['nom'].' '; }
				echo mb_substr($validpreins['prenom'],0,1).'.</td>
				<td>'.$validpreins['nom_activ'].'</td>
				<td><div id="noins'.$validpreins['eleves_id'].'"';
				if($freq_gestion_jourscren['valeur']==0) {
					$cren_info['hor_freq'] = substr($cren_info['hor_freq'],0,3);
				}
				else {
					$cren_info['hor_freq'] = substr($cren_info['hor_freq'], strpos($cren_info['hor_freq'], date('D')) + strlen(date('D')), 3);
				}
				if($preins_verif_nb>0||$cren_info['hor_freq']=='000') { 
					echo ' style="display:none;" ';
				}
				echo '>';
				$activd = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqactiv WHERE default_activ = 1");
				$nom_activd = mysqli_fetch_assoc($activd);
				echo '<div class="box"><select name="selectactiv" id="selectactiv'.$validpreins['eleves_id'].'">';
				echo '<option value="ZERO"></option>';
				$liste_activ = mysqli_query($bdd,"SELECT code_activ, nom_activ FROM ".$prefix."_freqactiv WHERE activ_activ = 1 AND code_activ <> 'ZERO' ORDER BY nom_activ ASC");
				foreach($liste_activ as $liste) {
					echo '<option value="'.$liste['code_activ'].'"';
					if(isset($_SESSION['code_activ'])&&$verrtest['valeur']==0) {
						if($_SESSION['code_activ']==$liste['code_activ']) {
							echo ' selected ';
						}
					}
					else {
						if(@$nom_activd['code_activ']==$liste['code_activ']) {
							echo ' selected ';
						}
					}
					echo '>'.$liste['nom_activ'].'</option>';
				}
				echo '</select></div>';
				if($espopt['valeur']==1) {
					echo '<div class="box"><select id="espace'.$validpreins['eleves_id'].'" name="espace" value="">';
					$liste_activ = mysqli_query($bdd,"SELECT espace_id, espace_nom FROM ".$prefix."_espaces ORDER BY espace_nom ASC");
					echo '<option value="0">'.$esp0['valeur'].'</option>';
					foreach($liste_activ as $liste) {
						echo '<option value="'.$liste['espace_id'].'"';
						if($validpreins['espace_id']==0) { 
							if($liste['espace_id']==0) { echo ' selected '; }
						}
						else { 
							if($validpreins['espace_id']==$liste['espace_id']) { 
								echo ' selected '; }
						}
						echo '>'.$liste['espace_nom'].'</option>';
					}
					echo '</select></div>';
				}
				//gestion du seuil
				$dday = date("Y-m-d");
				$ccren = $dday.$_SESSION['ccren'];
				if($espopt['valeur']==0) {
					$inscrits_select = "SELECT freq.eleves_id, freq.date_freq, freq.hor_name, freq.code_activ, freq.cas, freq.h_inscription, eleves.nom, eleves.prenom, eleves.division, eleves.naissance 
					FROM (SELECT *
						FROM ".$prefix."_freq 
						WHERE date_freq = '".$dday."' AND hor_name = '".$_SESSION['ccren']."' AND cas NOT IN ('sor', 'exc') 
					) freq 
					INNER JOIN ".$prefix."_eleves eleves
					ON freq.eleves_id = eleves.eleves_id
					ORDER BY eleves.division ASC, eleves.nom DESC, eleves.prenom ASC";
					$inscrits = mysqli_query($bdd,$inscrits_select);
					$nb_inscrits = mysqli_num_rows($inscrits);
					$sin = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilnbi'");
					$sin_info = mysqli_fetch_assoc($sin);
					$sir = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilreaci'");
					$sir_info = mysqli_fetch_assoc($sir);
				}
				if($espopt['valeur']==1||$sin_info['valeur']==0||$nb_inscrits<$sin_info['valeur']) {
					echo '&nbsp; &nbsp; <input class="boutontab" type="submit" value="'.FREQ_BUTTON1.'" onclick="okpre'.$validpreins['eleves_id'].'(this);" name="okpre'.$validpreins['eleves_id'].'" /> ';
				}
				else if($sir_info['valeur']=='stopp') {
					echo '&nbsp; &nbsp; '.FREQ_RESA2.' ';
				}				
				echo '</div></td><td class="noborder">';
				$retards = mysqli_query($bdd,"SELECT * FROM ".$prefix."_retards WHERE eleves_id = '".$validpreins['eleves_id']."' AND retour_date < '".date('Y-m-d')."' ORDER BY retour_date ASC");
				$retards_nb = mysqli_num_rows($retards);
				$ret='';
				if($retards_nb!=0) {
					$ret = '<span style="font-size: 1.5em; color: #D90606;"><i class="fa-solid fa-stopwatch" title="';
					if($retards_nb>1) { $i=1; }
					while($retards_info = mysqli_fetch_assoc($retards)) {
						if($retards_nb>1) { $ret .= ''.$i.'. '; }
						if($retards_info['titre']!="") {
							$ret .= $retards_info['titre'].'. ';
						}
						if($retards_info['retour_date']!="0000-00-00") {
							$date = date('d/m/Y', strtotime(str_replace("/","-",$retards_info['retour_date'])));
							$ret .= FREQ_GEST1.' '.$date.'. ';
						}
						if($retards_info['niv_alerte']!=0) {
							$ret .= FREQ_GEST2.' ';
							if($retards_info['niv_alerte']=="1") { $ret .= FREQ_GEST3; }
							else if($retards_info['niv_alerte']=="2") { $ret .= FREQ_GEST4; }
							else if($retards_info['niv_alerte']=="3") { $ret .= FREQ_GEST5; }
							$ret .= '. ';
						}
						if($retards_nb>1) { $ret .= ''; $i++; }
					}
					$ret .= '"></i></span>';
					echo $ret;
				}
				echo '</td><td class="noborder" name="noprint">';
				$exclusion = mysqli_query($bdd,"SELECT * FROM ".$prefix."_exclusions exclusions LEFT JOIN ".$prefix."_exclucauses causes ON exclusions.exclucause_id = causes.exclucause_id WHERE eleves_id = '".$validpreins['eleves_id']."' AND (date_info >= '".date('Y-m-d')."' OR date_fin >= '".date('Y-m-d')."')");
				$exc_nb = mysqli_num_rows($exclusion);
				$exctxt='';
				if($exc_nb!=0) {
					$exc_info = mysqli_fetch_assoc($exclusion);
					$exctxt = '<span style="font-size: 1.5em; color: #D90606;"><i class="fa-solid fa-ban" title="';
					$exctxt .= FREQT2.' '.LE2.' '.substr($exc_info['date_exclusion'],8,2).'/'.substr($exc_info['date_exclusion'],5,2).'/'.substr($exc_info['date_exclusion'],0,4).' au '.substr($exc_info['date_fin'],8,2).'/'.substr($exc_info['date_fin'],5,2).'/'.substr($exc_info['date_fin'],0,4).'.';
					$exctxt .= '"></i></span>';
					echo $exctxt;
				}
				echo '</td><td class="noborder" name="noprint">';
				$limits = mysqli_query($bdd,"SELECT * FROM ".$prefix."_limits limits LEFT JOIN ".$prefix."_limitstxt txt ON limits.limittxt_id = txt.limittxt_id WHERE eleves_id = '".$validpreins['eleves_id']."' AND date_fin >= '".date('Y-m-d')."'");
				$limits_nb = mysqli_num_rows($limits);
				$limtxt='';
				if($limits_nb!=0) {
					$limtxt = '<td class="noborder" name="noprint"><span style="font-size: 1.5em; color: #D90606;"><i class="fa-solid fa-hand" title="';
					if($limits_nb>1) { $i=1; }
					foreach($limits as $limit_info) {
						if($limits_nb>1) { if($i>1) { $limtxt .= ' '; } $limtxt .= $i.'. '; }
						$limtxt .= $limit_info['limits_txt'].' '.FROM3.' '.substr($limit_info['date_debut'],8,2).'/'.substr($limit_info['date_debut'],5,2).'/'.substr($limit_info['date_debut'],0,4).'';
						if($limit_info['date_fin']>=date('Y-m-d')) {
							$limtxt .= ' '.TO3.' '.substr($limit_info['date_fin'],8,2).'/'.substr($limit_info['date_fin'],5,2).'/'.substr($limit_info['date_fin'],0,4).'.';
						}
						if($limits_nb>1) { $limtxt .= ''; $i++; }
					}
					$limtxt .= '"></i></span>';
					echo $limtxt;
				}
				echo '</td><td class="noborder" name="noprint">';
				$birtxt='';
				if($birthday['valeur']==1&&(substr($validpreins['naissance'],5,5)==date('m-d'))) {
					$naissance = $validpreins['naissance'];
					$annee = substr($naissance, 0, -6);
					$mois = substr($naissance, 5, -3);
					$jour = substr($naissance, -2);
					$date = $jour.'/'.$mois.'/'.$annee;							
					$age = Age($date);
					$birtxt = '<span style="font-size: 1.5em; color: #D90606;"><i class="fa-solid fa-cake-candles" title="'.$age.' '.YEARS2.'"></i></span>';
					echo $birtxt;
				}
				$to1 = $ret.' '.$exctxt.' '.$limtxt.' '.$birtxt;
				$to2 = str_replace("<", "&lt;", $to1);
				$to3 = str_replace("\"", "'", $to2);
				$toins = str_replace(">", "&gt;", $to3);
				echo '<input type="hidden" name="infos'.$validpreins['eleves_id'].'" id="infos'.$validpreins['eleves_id'].'" value="'.$toins.'" /></td></tr>';
				echo '<script type="text/javascript">
				function okpre'.$validpreins['eleves_id'].'(param) {
				var param = "inspre";
				document.getElementById("noins'.$validpreins['eleves_id'].'").style.display = "none";
				var element = document.getElementById("aucun");
				if(element) {
					element.style.display = "none";
				}
				var eleve_id = "'.$validpreins['eleves_id'].'"; 
				var sel = document.getElementById("selectactiv'.$validpreins['eleves_id'].'");
				var selectactiv = sel.value;
				var selectedind = sel.selectedIndex;
				var selectactivtxt = sel.options[sel.selectedIndex].text; 
				var typa = document.getElementsByName("selectact");              
	            for(i = 0; i < typa.length; i++) {
	                typa[i].selectedIndex = selectedind;
	            } ';
				if($espopt['valeur']==1) {
					echo 'var esp = document.getElementById("espace'.$validpreins['eleves_id'].'");
					var espace = esp.value; 
					var espaceind = esp.selectedIndex; 
					var espacetext = esp.options[esp.selectedIndex].text;
					var espid = "nb_tresp" + espace; 
					var typ = document.getElementsByName("espace");              
		            for(i = 0; i < typ.length; i++) {
		                typ[i].selectedIndex = espaceind;
		            }	';
				}
				else {
					echo 'var espace = ""; ';
				}
				echo 'var rnv = document.getElementById("inscrits_nb").textContent;
				var new_rnv = parseInt(rnv)+1;
				document.getElementById("inscrits_nb").textContent = new_rnv; ';
				if($espopt['valeur']==1) {
					echo 'var rne_id = document.getElementById(espid);
					if(rne_id!=null) {
						var rne = rne_id.textContent;
						var new_rne = parseInt(rne)+1;
						document.getElementById(espid).textContent = new_rne; 
					} ';						
				}
				echo 'var table = document.getElementById("listeinscrits");
				var trnew = document.createElement("tr");
				var td = document.createElement("td");
				td.innerHTML = "'.$validpreins['division'].' - '.$validpreins['nom'].' '.$validpreins['prenom'].'";
				trnew.appendChild(td); ';
				if($droits==3&&$check_consultactiv_opt['valeur']==0) { }
				else {
					echo 'var td = document.createElement("td");
					td.innerHTML = selectactivtxt;
					trnew.appendChild(td); ';
				}
				if($espopt['valeur']==1) {
					echo 'var td = document.createElement("td");
					td.innerHTML = espacetext;
					trnew.appendChild(td); ';
				}
				if($droits==2||($droits==4&&$buttonchoix['valeur']==1)||$droits==6&&$buttonchoix['valeur']==1) {
					echo 'var td = document.createElement("td");
					td.innerHTML = "<a href=\'freq.php\'>'.FREQ_RESA2.'</a>";
					trnew.appendChild(td);
					var td = document.createElement("td");
					var val = document.getElementById("infos'.$validpreins['eleves_id'].'").value;
					var val1 = val.replace("&lt;","<");
					var val2 = val.replace("&gt;",">");
					td.innerHTML = val2;
					trnew.appendChild(td); ';
				}
				echo 'table.appendChild(trnew);					
				$.ajax({
				    type: "POST",
				    data: { param: param, eleve_id: eleve_id, selectactiv: selectactiv, espace: espace },
				    url: "js/idl/freq_ind_syn.php",
				   });
				}
				</script>';	
			}
			echo '</table></div>';
			if (!isset($preins_nb)) {
				echo FREQ_INF2;
			}
			else if (isset($preins_nb)&&$preins_nb!=1) {
				echo FREQ_INF2;
			}
			echo '</div>';
		}
	}	
}
?>