<?php
// Gestion pour les suspensions
if(isset($_POST['suspension'])) {
	$seleve = $_POST['eleve'];
	// récup du nom et du prénom
	$snom = substr($seleve, 0, strpos($seleve,","));
	$sprenom_beg = strstr($seleve, ', ');
	$sdiv_find1 = '('; // on recherche la parenthèse avant la division
	$sdiv_pos1 = strpos($seleve, $sdiv_find1); // on définit la position de la 1re parenthèse
	$sdiv_find2 = ')'; // on recherche la parenthèse après la division
	$sdiv_pos2 = strpos($seleve, $sdiv_find2); // on définit la position de la 2e parenthèse
	$sprenom_end = $sdiv_pos2 - $sdiv_pos1 + 2;
	$sprenom = substr($sprenom_beg, 2, -$sprenom_end);
	// recherche de la division
	$sdiv_deb = $sdiv_pos1 + 1; // on définit le premier caractère pour la division
	$sdiv = substr($seleve, $sdiv_deb, -1); // on trouve la division
	// sélection de l'élève dans la bdd
	$eleve = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE nom = '".mysqli_real_escape_string($bdd,$snom)."' AND prenom = '".mysqli_real_escape_string($bdd,$sprenom)."' AND division = '$sdiv'");
	$eleve_info = mysqli_fetch_assoc($eleve);
	$eleve_id = $eleve_info['eleves_id'];
	// vérif dans freqresref
	$verifele = mysqli_query($bdd,"SELECT eleves_id FROM ".$prefix."_freqresref WHERE eleves_id = '".$eleve_id."'");
	$verifele_nb = mysqli_num_rows($verifele);
	if($verifele_nb==0) {
		if($_POST['nbjours_ele']==0) { $date='0000-00-00'; }
		else {
			$calendar = mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date > '".date('Y-m-d')."' AND statut = 1");
			$i=0;
			foreach($calendar as $datecheck) {
				if($i==$_POST['nbjours_ele']+1) { break; }
				$date = $datecheck['date'];
				$i++;
			}
		}
		mysqli_query($bdd,"INSERT ".$prefix."_freqresref (eleves_id, todate) VALUES ('".$eleve_id."', '".$date."')");
		mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '1' WHERE eleves_id = '".$eleve_id."' AND user_id = '0' AND statut = '0'");
	}
}
echo '<p>'.FREQC_PARAM51.' <input type="number" min="0" class="number" name="nbjours" id="nbjours" value="'.$periodsus['valeur'].'" /> '.FREQC_PARAM52.' &nbsp;  <input class="bouton boutfreq" type="submit" onclick="suspduree(this);colorval(\'suspduree\');" id="suspduree" name="valjours" value="'.CHANGE.'" /></p>';
echo '<script type="text/javascript">
	function suspduree(param) {
	var param = "suspduree";
	var val = document.getElementById("nbjours").value;          
   	$.ajax({
	    type: "POST",
	    data: { param: param, val: val },
	    url: "js/idl/freq_param_syn.php",
	   });
	}
</script>';
echo '<form method="POST" name="eleve" action="#suspension">
<label for="eleves">'.NAMECH.'</label> <input type="text" autocomplete="off" aria-autocomplete="none" class="inptxt" name="eleve" size="20" id="elevester" value=""> <input type="number" min="0" class="number" name="nbjours_ele" value="'.$periodsus['valeur'].'" /> jours &nbsp; <input class="bouton boutfreq" type="submit" value="'.SUSPEND.'" name="suspension" />
</form><br>';
// Gestion de l'affichage des suspendus
// 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 { $ordre = ""; }
// affichage proprement dit
$inscrits_select = "SELECT freq.eleves_id, freq.todate, eleves.nom, eleves.prenom, eleves.division 
FROM ".$prefix."_freqresref freq 
INNER JOIN ".$prefix."_eleves eleves
ON freq.eleves_id = eleves.eleves_id
".$ordre."";
$inscrits = mysqli_query($bdd,$inscrits_select);
$nb_inscrits = mysqli_num_rows($inscrits);

if($nb_inscrits != 0) {
	echo '<table style="width:500px;">';
	foreach($inscrits as $liste) {
		echo '<tr id="ann'.$liste['eleves_id'].'"><td>'.$liste['division'].' - '.$liste['nom'].' '.$liste['prenom'].'</td><td>';
		if($liste['todate']!='0000-00-00') { echo 'jusqu\'au '.substr($liste['todate'],8,4).'/'.substr($liste['todate'],5,2).'/'.substr($liste['todate'],0,4); }
		echo '</td>';
		echo '<td><input class="bouton boutfreq" type="submit" value="'.ABORD.'" onclick="annul'.$liste['eleves_id'].'(this,\'ann'.$liste['eleves_id'].'\');" name="annuler'.$liste['eleves_id'].'" /></td></tr>';
		echo '<script type="text/javascript">
			function annul'.$liste['eleves_id'].'(param, divs) {
			var param = "suspannul";
			document.getElementById(divs).style.display = "none";   
			var ele_id = '.$liste['eleves_id'].';       
            $.ajax({
			    type: "POST",
			    data: { param: param, ele_id: ele_id },
			    url: "js/idl/freq_param_syn.php",
			   });					
			}
		</script>';
	}
	echo '</table>';
}	
?>