<?php
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	$droits = substr($_SESSION['user_droits'],3,1);
	
	if ($droits==1||@$_SESSION['accesjournal']=='0') {
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		
		?><style type="text/css">
		#mfr5 { background-color: #fff; }
		#mfr5 td { border: 3px #B90404 solid; }
		#mfr4 td { border:0; }
		</style><?php
		echo '<h1>'.FREQNAV51.'</h1>';
		
		// Si aucun journal n'est possible (aucune inscription) :
		$verif_freq = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq");
		$nb_freq = mysqli_num_rows($verif_freq);
		//Pour la gestion des différents espaces :
		$espopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_gestion'"));
		$esp0 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first'"));

		if($nb_freq==0) {
			echo '<h2>'.FREQC_JOUR1.'</h2>';
		}

		else {
			// Choix des créneaux à consulter : 
			echo '<div name="formulaire">';
			echo '<h2>'.FREQC_JOUR2.'</h2>';
			echo '<form method="POST" name="journal" action="" class="padd1">';
			echo '<div class="box"><select name="jtypo" value="">';
			echo '<option value="ins" ';
			if(@$_SESSION['jtypo']=='ins') { echo ' selected '; }
			echo '>Inscriptions</option><option value="res" ';
			if(@$_SESSION['jtypo']=='res') { echo ' selected '; }
			echo '>Réservations</option></select></div>';
			include('./include/datepicker/dp2_past.php');
			echo '<p><b>'.FREQC_JOUR3.'</b></p>
			<p>'.FREQC_JOUR4.' <input type="text" size="10" class="datepick1 inptxt" name="datepick1" value="';
			if(isset($_SESSION['datepick1'])) {
				echo $_SESSION['datepick1'];
			}
			else {
				echo date('d/m/Y');
			}
			echo '"></p>';
			echo '<div class="freqjour"><br>'.FREQC_JOUR5.' 
			<div class="box"><select name="crenpicker1" value="">';
			echo '<option></option>';
			if($freq_gestion_jourscren['valeur']==0) {
				$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE hor_freq LIKE '001%' ORDER BY num_creneau ASC");
			}
			else {
				$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE hor_freq LIKE '%001%' ORDER BY num_creneau ASC");
			}
			foreach($hor as $listeh) {
				$cren = $listeh['hor_name'];
				$deb_cren = substr($cren, 0, 1);
				echo '<option value="'.$listeh['hor_name'].'"';
				if(isset($_SESSION['crenpicker1'])) {
					if($_SESSION['crenpicker1']==$listeh['hor_name']) {
						echo ' selected ';
					}
				}
				echo '>'.$listeh['hor_name'].' - ';
				include('./include/hor_boucle.php');
				echo '</option>';
			}
			echo '</select></div>';
			echo '<br><br>'.FREQC_JOUR6.' <div class="box"><select name="crenpicker2" value="">';
			echo '<option></option>';
			foreach($hor as $listeh) {
				$cren = $listeh['hor_name'];
				$deb_cren = substr($cren, 0, 1);
				echo '<option value="'.$listeh['hor_name'].'"';
				if(isset($_SESSION['crenpicker2'])) {
					if($_SESSION['crenpicker2']==$listeh['hor_name']) {
						echo ' selected ';
					}
				}
				echo '>'.$listeh['hor_name'].' - ';
				include('./include/hor_boucle.php');
				echo '</option>';
			}
			echo '</select></div></div>
			<br><p>Date de fin de période : <input type="text" size="10" class="datepick2 inptxt" name="datepick2" value="';
			if(isset($_SESSION['datepick2'])) {
				echo $_SESSION['datepick2'];
			}
			echo '"><br>'.FREQC_JOUR7.'</p>
			<p><b>'.FREQC_JOUR8.'</b></p>
			'.ACTIV.DOTS.' <div class="box"><select name="activite" value="">';
			$activ = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqactiv WHERE code_activ <> 'ZERO' ORDER BY nom_activ ASC");
			$nb_activ = mysqli_num_rows($activ);
			if($nb_activ==0) {
				echo '</select></div>';
			}
			else {
				echo '<option></option>';
				foreach($activ as $check_activ) {
					echo '<option value="'.$check_activ['code_activ'].'"';
					if(isset($_SESSION['activite'])) {
						if($_SESSION['activite']==$check_activ['code_activ']) {
							echo ' selected ';
						}
					}
					echo '>'.$check_activ['nom_activ'].'</option>';
				}
				echo '</select></div>';
			}
			if($espopt['valeur']==1) {
				echo ' &nbsp; '.ZONE.DOTS.' ';
				echo '<div class="box"><select name="espace">';
				echo '<option></option>';
				$liste_activ = mysqli_query($bdd,"SELECT espace_id, espace_nom FROM ".$prefix."_espaces ORDER BY espace_nom ASC");
				echo '<option value="0"';
				if(isset($_SESSION['espace'])) { 
					if($_SESSION['espace']!="") {
						if($_SESSION['espace']==0) { 
							echo ' selected '; 
						}
					}
				}
				echo '>'.$esp0['valeur'].'</option>';
				while($liste = mysqli_fetch_assoc($liste_activ)) {
					echo '<option value="'.$liste['espace_id'].'"';
					if(isset($_SESSION['espace'])) { 
						if($_SESSION['espace']!="") {
							if($_SESSION['espace']==$liste['espace_id']) { 
								echo ' selected '; 
							}
						}
					}
					echo '>'.$liste['espace_nom'].'</option>';
				}
				echo '</select></div>';
			}
			echo '<br><br>'.LEVEL.DOTS.' <div class="box"><select name="niveau" value="">';
			if(isset($_SESSION['niveau'])) {
				if($_SESSION['niveau']=='6') { $champs = '6ème'; }
				else if($_SESSION['niveau']=='5') { $champs = '5ème'; }
				else if($_SESSION['niveau']=='4') { $champs = '4ème'; }
				else if($_SESSION['niveau']=='3') { $champs = '3ème'; }
				else if($_SESSION['niveau']=='2') { $champs = '2nde'; }
				else if($_SESSION['niveau']=='1') { $champs = '1re'; }
				else if ($_SESSION['niveau'] == 'T') { $champs = 'Tale'; }
				else { $champs = ''; }
				echo '<option value="'.$_SESSION['niveau'].'">'.$champs.'</option>';
			}
			echo '<option></option><option value="6">6ème</option><option value="5">5ème</option><option value="4">4ème</option><option value="3">3ème</option><option value="2">2nde</option><option value="1">1re</option><option value="t">Tale</option></select></div>';
			echo ' &nbsp; '.DATAEXP1.' <div class="box"><select name="division" value="">';
				$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves GROUP BY division ORDER BY division ASC");
				echo '<option></option>';
				while($liste = mysqli_fetch_assoc($liste_division)) {
					echo '<option';
					if(isset($_SESSION['division'])) {
						if($_SESSION['division']==$liste['division']) {
							echo ' selected ';
						}
					}
					echo '>'.$liste['division'].'</option>';
				}
				$liste_groupe = mysqli_query($bdd,"SELECT groupe_nom FROM ".$prefix."_groupes WHERE groupe_actif = 1 GROUP BY groupe_nom ORDER BY groupe_nom ASC");
				while($listeg = mysqli_fetch_assoc($liste_groupe)) {
					echo '<option';
					if(isset($_SESSION['division'])) {
						if($_SESSION['division']==$listeg['groupe_nom']) {
							echo ' selected ';
						}
					}
					echo '>'.$listeg['groupe_nom'].'</option>';
				}
			echo '</select></div>
			<br><br><label for="eleves">'.NAMECH.'</label>
				  <input type="text" autocomplete="off" aria-autocomplete="none" class="inptxt" name="eleve" size="20" id="eleves" value="';
			if(isset($_SESSION['eleve'])) {
				echo $_SESSION['eleve'];
			}
			echo '">
			
			<p class="paddvalid1"><input class="bouton boutfreq" type="submit" value="'.STATS_REQUEST.'" name="affjournal" /></p>';
			
			echo '</form></div>';
			
			echo '</article><article id="artleft" class="leftwidth">';
			if(isset($_POST['affjournal'])) {
				echo '<h2>'.REQUEST.DOTS.'</h2>';
				if($_POST['datepick1']!=""&&$_POST['datepick2']!="") {
					$day1=substr($_POST['datepick1'],6,4).'-'.substr($_POST['datepick1'],3,2).'-'.substr($_POST['datepick1'],0,2);
					$day2=substr($_POST['datepick2'],6,4).'-'.substr($_POST['datepick2'],3,2).'-'.substr($_POST['datepick2'],0,2);
					$dayT1=strtotime($day1);
					$dayT2=strtotime($day2);
					if($dayT1>=$dayT2) {
						echo FREQC_JOUR10;
						include('./db/proc/freq_today.php');
					}
					else {
						$_SESSION['jtypo'] = $_POST['jtypo'];
						$_SESSION['crenpicker1'] = "";
						$_SESSION['crenpicker2'] = "";
						$_SESSION['datepick1'] = $_POST['datepick1'];
						$_SESSION['datepick2'] = $_POST['datepick2'];
						$_SESSION['activite'] = $_POST['activite'];
						$_SESSION['eleve'] = $_POST['eleve'];
						$_SESSION['division'] = $_POST['division'];
						$_SESSION['niveau'] = $_POST['niveau'];
						if($espopt['valeur']==1) { $_SESSION['espace'] = $_POST['espace']; }
						include('./db/proc/freq_period.php');
						echo '<script language="Javascript">
						<!--
						document.location.replace("freq.php?page=journal");
						// -->
						</script>';
						
					}
				}
				else if($_POST['datepick1']!="") {
					$_SESSION['jtypo'] = $_POST['jtypo'];
					$_SESSION['datepick2'] = "";
					$_SESSION['datepick1'] = $_POST['datepick1'];
					$_SESSION['crenpicker1'] = $_POST['crenpicker1'];
					$_SESSION['crenpicker2'] = $_POST['crenpicker2'];
					$_SESSION['activite'] = $_POST['activite'];
					$_SESSION['eleve'] = $_POST['eleve'];
					$_SESSION['division'] = $_POST['division'];
					$_SESSION['niveau'] = $_POST['niveau'];
					if($espopt['valeur']==1) { $_SESSION['espace'] = $_POST['espace']; }
					include('./db/proc/freq_day.php');
					echo '<script language="Javascript">
					<!--
					document.location.replace("freq.php?page=journal");
					// -->
					</script>';
				}
				else {
					echo FREQC_JOUR11;
					include('./db/proc/freq_today.php');
				}
			}
			else if(isset($_SESSION['datepick1'])&&isset($_SESSION['datepick2'])) {
				if($_SESSION['datepick1']!=""&&$_SESSION['datepick2']!="") {
					$_POST['jtypo'] = $_SESSION['jtypo'];
					$_POST['datepick1'] = $_SESSION['datepick1'];
					$_POST['datepick2'] = $_SESSION['datepick2'];
					$_POST['activite'] = $_SESSION['activite'];
					$_POST['eleve'] = $_SESSION['eleve'];
					$_POST['division'] = $_SESSION['division'];
					$_POST['niveau'] = $_SESSION['niveau'];
					if($espopt['valeur']==1) { $_POST['espace'] = $_SESSION['espace']; }
					include('./db/proc/freq_period.php');
				}
				else if($_SESSION['datepick1']!="") {
					$_POST['jtypo'] = $_SESSION['jtypo'];
					$_POST['datepick1'] = $_SESSION['datepick1'];
					$_POST['crenpicker1'] = $_SESSION['crenpicker1'];
					$_POST['crenpicker2'] = $_SESSION['crenpicker2'];
					$_POST['activite'] = $_SESSION['activite'];
					$_POST['eleve'] = $_SESSION['eleve'];
					$_POST['division'] = $_SESSION['division'];
					$_POST['niveau'] = $_SESSION['niveau'];
					if($espopt['valeur']==1) { $_POST['espace'] = $_SESSION['espace']; }
					include('./db/proc/freq_day.php');
				}
			}
			else {
				include('./db/proc/freq_today.php');
			}
			//Impression :
			echo '<br><a href="javascript:;" onclick="javascript:window.print()" value="" name="impressappel"><img src="img/printer.png" title="'.PRINT3.'"/></a>'; 
		}
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php">'.ACCUEIL.'</a></p>';
}
?>