<?php
echo '<meta charset="utf-8" />
<META HTTP-EQUIV="Refresh" CONTENT="180">'; // rafraichit la page toutes les 3 minutes
if(isset($_SESSION['user_droits'])) {
	$droits = substr($_SESSION['user_droits'],3,1);
	
	if ($droits!=2&&$droits!=5&&$droits!=6&&$droits!=3)
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		$individu=0;
		
		?><style type="text/css">
		#mfr2 { background-color: #fff; }
		#mfr2 td { border: 3px #B90404 solid; }
		#mfr1 td { border:0; }
		</style><?php

		echo '<h1 name="noprint">'.RESA2.'</h1>';

		//Vérification des suspensions
		include('db/proc/freq_finsuspension.php');
		
		$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires");
		$hor_nb = mysqli_num_rows($hor);
		
		// 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) {
			if($droits!=3) {
				echo '<p><a href="database.php">'.FREQC_IND1.'</a>.</p>';
			}
		}

		$res_activ = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_resactiv'"));
		$verresp = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_verrespace'"));
		$espopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_gestion'"));
		$reseleve_could=mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_reseleve'"));
		$reseleve_activ = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freq_reseleve_activ'"));

		$classe_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_classe'"));
		$classe_info_valeur = substr($classe_info['valeur'],0,1);
		if($classe_info_valeur!=substr($_SESSION['classinscript'],0,1)&&$_SESSION['classinscript']!='def') {
			$classe_info_valeur=substr($_SESSION['classinscript'],0,1);
		}

		// S'il n'y a pas d'horaires configurés :		
		if($hor_nb==0) {
			if($droits!=3) {
				echo '<p><a href="param.php?page=horaires">'.FREQ_HORNEED.'</a>.</p>';
			}
		}

		else {
			
			echo '<h2 name="noprint">'.date("d/m/Y").' - '.date("H:i").'';
			$heureh = date("H:i:s");
			$soluce = 0;
			$dday = date("Y-m-d");
			$date = new DateTime($dday);
			$num_cren = 0;
			$_SESSION['num_cren'] = $num_cren;
		
			// Boucle pour définir le créneau actuel :
			while($creneau_info = mysqli_fetch_assoc($hor)) {
				if($heureh >= $creneau_info['debut']&&$heureh < $creneau_info['fin']) {
					$soluce = 1;
					$num_cren = $creneau_info['num_creneau'];
					$_SESSION['num_cren'] = $num_cren;
					break;
				}
			}
			echo '</h2>';
			
			if($droits!=3) {	
				// Suppression de résidus de réservations (affinés au créneau) :
				$dday = date("Y-m-d");
				$freqres = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqres WHERE statut='0'");
				while($freqres_info = mysqli_fetch_assoc($freqres)) {
					$creneau = $freqres_info['hor_name'];
					$hor = mysqli_query($bdd,"SELECT num_creneau FROM ".$prefix."_horaires WHERE hor_name = '".$creneau."'");
					$hor_info = mysqli_fetch_assoc($hor);
					if(isset($_SESSION['num_cren'])) {
						if($freqres_info['date_freqres'] == $dday && $hor_info['num_creneau'] < $_SESSION['num_cren']) {
							mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '4' WHERE date_freqres = DATE(CONVERT_TZ(UTC_TIMESTAMP(), 'UTC', '".$fuseau."')) AND hor_name = '".$creneau."' AND statut = '0'");
						}
					}
				}
			}
			include('./db/proc/freq_res_options.php');
				
			// Gestion de l'impression des listes de réservation
			// Seulement si on est administrateur :
			if($droits==2||$droits==3) {
				// Seulement s'il y a des réservations :
				$reserves = mysqli_query($bdd,"SELECT freqres_id FROM ".$prefix."_freqres WHERE statut='0'");
				$nb_res = mysqli_num_rows($reserves);
				if($nb_res!=0) {
					echo '<br> <br><h2 name="noprint" class="top50">'.PRINT4.'</a></h2>';
					echo '<form method="POST" name="resprint" action="" class="padd1">
					<p name="noprint">'.DATE.DOTS.' <input type="date" class="dateprint inptxt" name="dateprint" value="';
					if(isset($_SESSION['dateprint'])) {
						echo $_SESSION['dateprint'];
					}
					echo '">
					<br><br>'.CREN1.' '.FACULT.DOTS.' <div class="box" name="noprint"><select name="crenprint" value="">';
					echo '<option></option>';
					if($freq_gestion_jourscren['valeur']==0) {
						$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE hor_resa LIKE '001%' ORDER BY num_creneau ASC");
					}
					else {
						$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE hor_resa LIKE '%001%' ORDER BY num_creneau ASC");
					}
					foreach($hor as $liste) {
						$cren = $liste['hor_name'];
						$deb_cren = substr("$cren", 0, 1);
						echo '<option value="'.$liste['hor_name'].'"';
						if(isset($_SESSION['crenprint'])) {
							if($_SESSION['crenprint']==$liste['hor_name']) {
								echo ' selected ';
							}
						}
						echo '>'.$liste['hor_name'].' - ';
						echo MOMENT[$deb_cren];
						$deb = $liste['debut'];
						$dh = substr("$deb", 0, 2);
						$dm = substr("$deb", 3, 2);
						echo ' de '.$dh.'h'.$dm.' à ';
						$fin = $liste['fin'];
						$fh = substr("$fin", 0, 2);
						$fm = substr("$fin", 3, 2);
						echo ''.$fh.'h'.$fm.'';
						echo '</option>';
					}
					echo '</select></div></p><p name="noprint"><input class="bouton boutfreq" type="submit" value="'.FREQC_RES1.'" name="resprint" /></p></form>';
					if(isset($_POST['resprint'])) {
						if($_POST['dateprint']!=""&&$_POST['crenprint']!="") { // si date et créneau
							$_SESSION['dateprint'] = $_POST['dateprint'];
							$_SESSION['crenprint'] = $_POST['crenprint'];
							$resprint_query = 1;
							include('./db/proc/freq_res_print.php');
						}
						else if($_POST['dateprint']!="") { // si date seulement
							$_SESSION['crenprint'] = "";
							$_SESSION['dateprint'] = $_POST['dateprint'];
							$resprint_query = 2;
							include('./db/proc/freq_res_print.php');
						}
						else { // sinon erreur et affichage par défaut
							echo '<p>'.FREQC_RES2.'</p>';
							$resprint_query = 0;
							include('./db/proc/freq_res_print.php');
						}
						?><script language="Javascript">
						<!--
						document.location.replace("freq.php?page=reserve");
						// -->
						</script><?php
					}
					else if(isset($_SESSION['dateprint'])&&isset($_SESSION['crenprint'])) {
						if($_SESSION['dateprint']!=""&&$_SESSION['crenprint']!="") {
							$resprint_query = 1;
							$_POST['dateprint'] = $_SESSION['dateprint'];
							$_POST['crenprint'] = $_SESSION['crenprint'];
							include('./db/proc/freq_res_print.php');
						}
						else if($_SESSION['dateprint']!="") {
							$resprint_query = 2;
							$_POST['dateprint'] = $_SESSION['dateprint'];
							$_POST['crenprint'] = "";
							include('./db/proc/freq_res_print.php');
						}
					}
					else { // affichage par défaut
						$resprint_query = 0;
						include('./db/proc/freq_res_print.php');
					}
				}
			}
			echo '</div>';
			$pagelisteresa = 1;
			include('./db/proc/freq_res_all.php');
			?><script type="text/javascript">
			var timer = setTimeout(function() {
			  window.location = window.location;
			}, 60000);
			</script><?php
		}	
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="../index.php">'.ACCUEIL.'</a></p>';
}
?>