<?php
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	$droits = substr($_SESSION['user_droits'],3,1);
	
	if ($droits!=2)
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?><style type="text/css">
		#mfr3 { background-color: #fff; }
		#mfr3 td { border: 3px #B90404 solid; }
		#mfr2 td { border:0; }
		</style><?php
		echo '<h1 name="noprint">'.FREQNAV3.'</h1>';
		
		// S'il n'y a pas d'élève(s) dans la base :
		$verif_tabeleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves");
		$nb_ele = mysqli_num_rows($verif_tabeleves);
		if($nb_ele==0) {
			echo '<p><a href="database.php">'.FREQC_IND1.'</a>.</p>';
		}
	
		else {
			echo FREQC_RETAR1;
			
			// Gestion de l'enregistrement de retards et réservations :
			echo '<h2>'.FREQC_RETAR2.'</h2>';		
			//formulaire de suggestion
			echo '<div class="fleft"><form method="POST" name="inscription" action="" class="padd3">';
			echo '<table name="form" class="noborder">
				<tr>
					<td><label for="eleves">'.NAMECH.'</label></td>
					<td>
					  <input type="text" autocomplete="off" aria-autocomplete="none" class="inptxt" name="eleve" size="20" id="eleves" value="">
					</td>
				</tr>
				</table>
				<table class="top10 noborder">
				<tr><td>'.FREQC_RETAR3.DOTS.' <input type="text" class="inptxt" name="titredoc" size="40"></td></tr>
				<tr><td>'.FREQC_RETAR4.' <input type="date" class="datepick inptxt" name="datepick"></td></tr>
				<tr><td>'.FREQC_RETAR5.' <div class="box"><select name="niv_alerte"><option></option><option value="1">'.FREQ_GEST3.'</option><option value="2">'.FREQ_GEST4.'</option><option value="3">'.FREQ_GEST5.'</option></select></div></td></tr>
				<tr><td class="right"><input class="bouton boutfreq" type="submit" value="'.FREQC_IND16.'" name="newretard" /> <input class="bouton boutfreq" type="submit" value="'.RESA3.'" name="newresa" /></td></tr>
				</table>
			</form>';
			
			// Gestion de l'enregistrement des retards dans la BDD
			if(isset($_POST['newretard'])) {
				//sélection de l'élève
				include('./db/proc/eleve_select.php');
				if($_POST['datepick']) {
					$day = $_POST['datepick'];
				}
				else { $day = '0000-00-00'; }
				$titre = mysqli_real_escape_string($bdd,$_POST['titredoc']);
				$niv_alerte = $_POST['niv_alerte'];
				//vérification
				$retardverif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_retards WHERE eleves_id = '".$eleve_id."' AND titre = '".$titre."'");
				$retardverif_nb = mysqli_num_rows($retardverif);
				//inscription
				if($retardverif_nb==0) {
					mysqli_query($bdd,"INSERT INTO ".$prefix."_retards (retards_id, eleves_id, titre, retour_date, niv_alerte) VALUES ('', '".$eleve_id."', '".$titre."', '".$day."', '".$niv_alerte."')");
				}
			}
			if(isset($_POST['newresa'])) {
				//sélection de l'élève
				include('./db/proc/eleve_select.php');
				if($_POST['datepick']) {
					$day = $_POST['datepick'];
				}
				else { $day = '0000-00-00'; }
				$titre = mysqli_real_escape_string($bdd,$_POST['titredoc']);
				//vérification
				$retardverif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_sigbresas WHERE eleves_id = '".$eleve_id."' AND titre = '".$titre."'");
				$retardverif_nb = mysqli_num_rows($retardverif);
				//inscription
				if($retardverif_nb==0) {
					mysqli_query($bdd,"INSERT INTO ".$prefix."_sigbresas (resas_id, eleves_id, titre, statut, date_statut) VALUES ('', '".$eleve_id."', '".$titre."', 'Ex. mis de côté', '".$day."')");
				}
			}

			//Traitement retards mal traités BCDI ou PMB
			if(isset($_POST['correct_int'])) {
				for($i=1;$i<=$_SESSION['nblines'];$i++) {
					$nocode=1;
					unset($eleve_id);
					include('./db/proc/eleve_select.php');
					if(isset($eleve_id)) {
						$date = substr($_SESSION['date'.$i],6,4).'-'.substr($_SESSION['date'.$i],3,2).'-'.substr($_SESSION['date'.$i],0,2);
						mysqli_query($bdd,"INSERT INTO ".$prefix."_retards (retards_id, eleves_id, titre, retour_date, niv_alerte) VALUES ('', '".$eleve_id."', '".$_POST['titre'.$i]."', '".$date."', '".$_POST['alerte'.$i]."')");
					}
				}
				unset($_SESSION['nblines']);
				unset($_SESSION['allname1']);
			}
			
			// Ajout des retards depuis PMB ou BCDI (en option)
			$sigb = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_sigb'");
			$sigb_info = mysqli_fetch_assoc($sigb);
			if($sigb_info['valeur']!="non") {
				echo '<h2>Ajout d\'un fichier de retards (';
				if($sigb_info['valeur']=="pmb") { echo 'depuis PMB'; }
				else if($sigb_info['valeur']=="bcdi") { echo 'depuis BCDI'; }
				echo ')</h2>';
				// si PMB
				if($sigb_info['valeur']=="pmb") {
					echo FREQC_RETARPMB1;
					// intégration du fichier :
					echo '<form enctype="multipart/form-data" action="db/proc/freq_retards_int_pmb.php" method="POST" class="padd3">
						<table name="form" class="noborder"><tr><td><input class="boutontab" name="file" type="file" accept=".csv,.ods"/></td></tr><tr><td class="right"><input class="bouton boutfreq" type="submit" value="'.FREQC_RETARPMB2.'" name="listepmb" /></td></tr></table>
					</form>';
				}
				// si BCDI
				else if($sigb_info['valeur']=="bcdi") {
					echo FREQC_RETARBCD1;
					// intégration du fichier :
					echo '<form enctype="multipart/form-data" action="db/proc/freq_retards_int_bcdi.php" method="POST" class="padd3">
						<table name="form" class="retform"><tr><td><input name="file" type="file" accept=".csv" /></td></tr><tr><td class="right"><input class="bouton boutfreq" type="submit" value="'.FREQC_RETARBCD2.'" name="listepmb" /></td></tr></table>
					</form>';
				}
			}
			echo '</article><article id="artleft" class="freqsep journaltable">';
			if(isset($_SESSION['allname1'])) {
				echo '<h2>'.FREQC_RETAR7.'</h2>';
				echo FREQC_RETAR8;
				echo '<table class="100p">';
				echo '<form method="POST" action="" name="wrong">';
				for($i=1;$i<=$_SESSION['nblines'];$i++) {
					$line=$_SESSION['nblines'];
					echo '<tr><td>'.ELEVS4.DOTS.' <input type="text" autocomplete="off" aria-autocomplete="none" class="inptxt" name="eleve'.$i.'" size="20" id="eleves'.$i.'" value="'.$_SESSION['allname'.$i].'"></td><td>'.TITLE.DOTS.' <input type="text" class="inptxt" name="titre'.$i.'" value="'.$_SESSION['titre'.$i].'"></td><td>Date : <input type="date" class="inptxt" name="date'.$i.'" value="'.substr($_SESSION['date'.$i],6,4).'-'.substr($_SESSION['date'.$i],3,2).'-'.substr($_SESSION['date'.$i],0,2).'"></td><td>Alerte : <div class="box"><select name="alerte'.$i.'">';
					$alertcode = array('','1','2','3');
					$alertname = array('',FREQ_GEST3,FREQ_GEST4,FREQ_GEST5);
					for($ano=0;$ano<count($alertcode);$ano++) {
						echo '<option value="'.$alertcode[$ano].'"';
						if($_SESSION['alerte'.$i]==$alertcode[$ano]) {
							echo ' selected ';
						}
						echo '>'.$alertname[$ano].'</option>';
					}
					echo '</select></td></tr>';?>
					<script type="text/javascript">
					$(function(){
					    $('#eleves<?php echo $i ?>').autocomplete({
					        source: function(request, response) {
					            $.ajax({
					                url: "include/suggestions.php",
					                dataType: "json",
					                data: {
					                    term: request.term,
			            				mode: "eleves"
					                },
					                success: function(data) {
					                    response(data);
					                }
					            });
					        }
					    });
					});
				     </script><?php
				}
				echo '<tr><td colspan="4" class="right"><input class="bouton boutfreq" type="submit" name="correct_int" value="'.VALID.'" /></td></tr>';
				echo '</form></table>';
			}

			//Traitement réservations mal traités BCDI ou PMB
			if(isset($_POST['correct_intr'])) {
				for($i=1;$i<=$_SESSION['nblinesr'];$i++) {
					$nocoder=1;
					unset($eleve_id);
					include('./db/proc/eleve_select.php');
					if(isset($eleve_id)) {
						$dater = substr($_SESSION['dater'.$i],6,4).'-'.substr($_SESSION['dater'.$i],3,2).'-'.substr($_SESSION['dater'.$i],0,2);
						mysqli_query($bdd,"INSERT INTO ".$prefix."_sigbresas (resas_id, eleves_id, titre, statut, date_statut) VALUES ('', '".$eleve_id."', '".$_POST['titrer'.$i]."', '".$_POST['statutr'.$i]."', '".$dater."')");
					}
				}
				unset($_SESSION['nblinesr']);
				unset($_SESSION['allnamer1']);
			}
			
			// Ajout des réservations depuis PMB ou BCDI (en option)
			$sigb = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_sigb_resa'");
			$sigb_info = mysqli_fetch_assoc($sigb);
			if($sigb_info['valeur']!="non") {
				echo '</article><article id="art_freq"><h2>'.FREQC_RETAR9.' (';
				if($sigb_info['valeur']=="pmb") { echo FREQC_RESAPMB3; }
				else if($sigb_info['valeur']=="bcdi") { echo FREQC_RESABCD3; }
				echo ')</h2>';
				// si PMB
				if($sigb_info['valeur']=="pmb") {
					echo FREQC_RESAPMB1;
					// intégration du fichier :
					echo '<form enctype="multipart/form-data" action="db/proc/freq_resas_int_pmb.php" method="POST" class="padd3">
						<table name="form" class="noborder"><tr><td><input class="boutontab" name="filers" type="file" accept=".csv,.ods"/></td></tr><tr><td class="right"><input class="bouton boutfreq" type="submit" value="'.FREQC_RESAPMB2.'" name="listepmbresa" /></td></tr></table>
					</form>';
				}
				// si BCDI
				else if($sigb_info['valeur']=="bcdi") {
					echo FREQC_RESABCD1;
					// intégration du fichier :
					echo '<form enctype="multipart/form-data" action="db/proc/freq_resas_int_bcdi.php" method="POST" class="padd3">
						<table name="form" class="resform"><tr><td><input name="filers" type="file" accept=".csv" /></td></tr><tr><td class="right"><input class="bouton boutfreq" type="submit" value="'.FREQC_RESABCD2.'" name="listebcdiresa" /></td></tr></table>
					</form>';
				}
			}
			echo '</article><article id="artleftr" class="freqsep journaltable">';
			if(isset($_SESSION['allnamer1'])) {
				echo '<h2>'.FREQC_RETAR10.'</h2>';
				echo FREQC_RETAR8;
				echo '<table class="100p">';
				echo '<form method="POST" action="" name="wrongr">';
				for($i=1;$i<=$_SESSION['nblinesr'];$i++) {
					$line=$_SESSION['nblinesr'];
					echo '<tr><td>'.ELEVS4.DOTS.' <input type="text" autocomplete="off" aria-autocomplete="none" class="inptxt" name="eleve'.$i.'" size="20" id="elevesr'.$i.'" value="'.$_SESSION['allnamer'.$i].'"></td><td>'.TITLE.DOTS.' <input type="text" class="inptxt" name="titrer'.$i.'" value="'.$_SESSION['titrer'.$i].'"></td><td>'.DATE.DOTS.' <input type="date" class="inptxt" name="dater'.$i.'" value="'.substr($_SESSION['dater'.$i],6,4).'-'.substr($_SESSION['dater'.$i],3,2).'-'.substr($_SESSION['dater'.$i],0,2).'"></td><td>'.FREQC_RETAR11.DOTS.' <input type="text" class="inptxt" name="statutr'.$i.'" value="'.$_SESSION['statutr'.$i].'"></td></tr>';?>
					<script type="text/javascript">
					$(function(){
					    $('#elevesr<?php echo $i ?>').autocomplete({
					        source: function(request, response) {
					            $.ajax({
					                url: "include/suggestions.php",
					                dataType: "json",
					                data: {
					                    term: request.term,
			            				mode: "eleves"
					                },
					                success: function(data) {
					                    response(data);
					                }
					            });
					        }
					    });
					});
				     </script><?php
				}
				echo '<tr><td colspan="4" class="right"><input class="bouton boutfreq" type="submit" name="correct_intr" value="'.VALID.'" /></td></tr>';
				echo '</form></table>';
			}
				
			// Gestion de l'affichage des retards
			$oaf_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_retards_ordre'"));
			if($oaf_info['valeur']=='div') { $ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC"; }
			else if($oaf_info['valeur']=='nom') { $ordre = "ORDER BY eleves.nom ASC, eleves.prenom ASC"; }
			else if($oaf_info['valeur']=='tit') { $ordre = "ORDER BY retards.titre ASC"; }
			else if($oaf_info['valeur']=='dat') { $ordre = "ORDER BY retards.retour_date ASC"; }
			else if($oaf_info['valeur']=='ale') { $ordre = "ORDER BY retards.niv_alerte DESC, retards.retour_date ASC"; }
			// récupération des retards
			$retards = mysqli_query($bdd,"SELECT * FROM ".$prefix."_retards retards INNER JOIN ".$prefix."_eleves eleves ON retards.eleves_id = eleves.eleves_id ".$ordre."");
			$retards_nb = mysqli_num_rows($retards);
			if($retards_nb!=0) {
				echo '<h2>'.FREQC_RETAR14.' (';
				echo '<span id="retards_nb">'.$retards_nb.'</span>';
				echo ')</h2>';
				echo '<table id="tablesorter" class="tablesorter 100p">
				<thead><tr><th>'.DIV.'</th><th>'.ELEVS4.'</th><th>'.FREQC_RETAR3.'</th><th>'.FREQC_RETAR12.'</th><th>'.FREQC_RETAR13.'</th><th colspan="2"></th></tr></thead><tbody>';
				foreach($retards as $retards_info) {
					if($retards_info['retour_date']=="0000/00/00") {
						$retour_date = ""; }
					else { $retour_date = $retards_info['retour_date']; }
					echo '<tr id="ret'.$retards_info['retards_id'].'"><td>'.$retards_info['division'].'</td><td>'.$retards_info['nom'].' '.$retards_info['prenom'].'</td><td><span style="display:none;">'.$retards_info['titre'].'</span><input class="inptxt" type="text" name="modiftitre" id="modiftitre'.$retards_info['retards_id'].'" value="'.$retards_info['titre'].'" size="20" /></td><td><span style="display:none;">'.$retour_date.'</span><input type="date" class="inptxt" id="modifdate'.$retards_info['retards_id'].'" name="modifdate" size="10" value="'.$retour_date.'" /></td><td><span style="display:none;">'.$retards_info['niv_alerte'].'</span>
					<div class="box"><select name="modifniv" id="modifniv'.$retards_info['retards_id'].'">';
					$alertcode = array('','1','2','3');
					$alertname = array('',FREQ_GEST3,FREQ_GEST4,FREQ_GEST5);
					for($ano=0;$ano<count($alertcode);$ano++) {
						echo '<option value="'.$alertcode[$ano].'"';
						if($retards_info['niv_alerte']==$alertcode[$ano]) {
							echo ' selected ';
						}
						echo '>'.$alertname[$ano].'</option>';
					}
					echo '</select></div></td><td>
					<input class="boutontab" type="submit" value="'.MODIF_TINY.'" name="retardmodif'.$retards_info['retards_id'].'" id="retardmodif'.$retards_info['retards_id'].'" onclick="modif'.$retards_info['retards_id'].'(this);validcolor(this,\'retardmodif'.$retards_info['retards_id'].'\');" /></td><td><input class="boutontab" type="submit" value="'.SUPPR_TINY.'" name="retarddel'.$retards_info['retards_id'].'" onclick="suppr'.$retards_info['retards_id'].'(this,\'ret'.$retards_info['retards_id'].'\',retards_nb);" /></td></tr>';
					echo '<script type="text/javascript">
						function modif'.$retards_info['retards_id'].'(retards_id, modiftitre, modifdate, modifniv) {
						var retards_id = '.$retards_info['retards_id'].';
						var modiftitre = document.getElementById("modiftitre'.$retards_info['retards_id'].'").value;
						var modifdate = document.getElementById("modifdate'.$retards_info['retards_id'].'").value;
						var modifniv = document.getElementById("modifniv'.$retards_info['retards_id'].'").value;
						$.ajax({
						    type: "POST",
						    data: { retards_id: retards_id, modiftitre: modiftitre, modifdate: modifdate, modifniv:modifniv },
						    url: "js/idl/freq_retardsup.php",
						   });
						}
					</script>';		
					echo '<script type="text/javascript">
						function suppr'.$retards_info['retards_id'].'(retards_id, divs) {
						var rnb = document.getElementById("retards_nb").textContent;
						var new_rnb = rnb-1;
						document.getElementById(divs).style.display = "none";
						document.getElementById("retards_nb").textContent = new_rnb;
						var retards_id = '.$retards_info['retards_id'].';
						$.ajax({
						    type: "POST",
						    data: { retards_id: retards_id },
						    url: "js/idl/freq_retardssuppr.php",
						   });
						}						
					</script>';						
				}
				echo '</tbody></table>';
				echo '<script type="text/javascript">
					function validcolor(pChange,pDiv){
						var couleur = "green 2px solid";
						document.getElementById(pDiv).style.border = couleur;
					}
				</script>';				
			}
			echo '</div>';			

			// Gestion de l'affichage des réservations
			$oaf_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_sigb_resaord'"));
			if($oaf_info['valeur']=='div') { $ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC"; }
			else if($oaf_info['valeur']=='nom') { $ordre = "ORDER BY eleves.nom ASC, eleves.prenom ASC"; }
			else if($oaf_info['valeur']=='tit') { $ordre = "ORDER BY resas.titre ASC"; }
			else if($oaf_info['valeur']=='dat') { $ordre = "ORDER BY resas.date_statut ASC"; }
			// récupération des retards
			$resas = mysqli_query($bdd,"SELECT * FROM ".$prefix."_sigbresas resas INNER JOIN ".$prefix."_eleves eleves ON resas.eleves_id = eleves.eleves_id ".$ordre."");
			$resas_nb = mysqli_num_rows($resas);
			if($resas_nb!=0) {
				echo '<h2>'.FREQC_RETAR15.' (';
				echo '<span id="resas_nb">'.$resas_nb.'</span>';
				echo ')</h2>';
				echo '<table id="tablesorter" class="tablesorter 100p">
				<thead><tr><th>'.DIV.'</th><th>'.ELEVS4.'</th><th>'.FREQC_RETAR3.'</th><th>'.FREQC_RETAR16.'</th><th>'.FREQC_RETAR11.'</th><th colspan="2"></th></tr></thead><tbody>';
				foreach($resas as $resas_info) {
					if($resas_info['date_statut']=="0000/00/00") {
						$resas_date = ""; }
					else { $resas_date = $resas_info['date_statut']; }
					echo '<tr id="retr'.$resas_info['resas_id'].'"><td>'.$resas_info['division'].'</td><td>'.$resas_info['nom'].' '.$resas_info['prenom'].'</td><td><span style="display:none;">'.$resas_info['titre'].'</span><input class="inptxt" type="text" name="modiftitrer" id="modiftitrer'.$resas_info['resas_id'].'" value="'.$resas_info['titre'].'" size="20" /></td><td><span style="display:none;">'.$resas_date.'</span><input type="date" class="inptxt" id="modifdater'.$resas_info['resas_id'].'" name="modifdater" size="10" value="'.$resas_date.'" /></td><td><span style="display:none;">'.$resas_info['statut'].'</span><input class="inptxt" type="text" name="modifstatutr" id="modifstatutr'.$resas_info['resas_id'].'" value="'.$resas_info['statut'].'" size="20" /></td><td>
					<input class="boutontab" type="submit" value="'.MODIF_TINY.'" name="resamodifr'.$resas_info['resas_id'].'" id="resamodifr'.$resas_info['resas_id'].'" onclick="modifr'.$resas_info['resas_id'].'(this);validcolor(this,\'resamodifr'.$resas_info['resas_id'].'\');" /></td><td><input class="boutontab" type="submit" value="'.SUPPR_TINY.'" name="resadelr'.$resas_info['resas_id'].'" onclick="supprr'.$resas_info['resas_id'].'(this,\'retr'.$resas_info['resas_id'].'\',resas_nb);" /></td></tr>';
					echo '<script type="text/javascript">
						function modifr'.$resas_info['resas_id'].'(resas_id, modiftitrer, modifdater, modifstatutr) {
						var resas_id = '.$resas_info['resas_id'].';
						var modiftitrer = document.getElementById("modiftitrer'.$resas_info['resas_id'].'").value;
						var modifdater = document.getElementById("modifdater'.$resas_info['resas_id'].'").value;
						var modifstatutr = document.getElementById("modifstatutr'.$resas_info['resas_id'].'").value;
						$.ajax({
						    type: "POST",
						    data: { resas_id: resas_id, modiftitrer: modiftitrer, modifdater: modifdater, modifstatutr:modifstatutr },
						    url: "js/idl/freq_resasup.php",
						   });
						}
					</script>';		
					echo '<script type="text/javascript">
						function supprr'.$resas_info['resas_id'].'(resas_id, divs) {
						var rnbr = document.getElementById("resas_nb").textContent;
						var new_rnbr = rnbr-1;
						document.getElementById(divs).style.display = "none";
						document.getElementById("resas_nb").textContent = new_rnbr;
						var resas_id = '.$resas_info['resas_id'].';
						$.ajax({
						    type: "POST",
						    data: { resas_id: resas_id },
						    url: "js/idl/freq_resassuppr.php",
						   });
						}						
					</script>';						
				}
				echo '</tbody></table>';
				echo '<script type="text/javascript">
					function validcolor(pChange,pDiv){
						var couleur = "green 2px solid";
						document.getElementById(pDiv).style.border = couleur;
					}
				</script>';				
			}
			echo '</div>';
			echo "<script type=\"text/javascript\">
				document.getElementById('eleves').focus();
			</script>";
		}		
	}	
}
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php">'.ACCUEIL.'</a></p>';
}
?>
