<?php 
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
if(isset($_SESSION['user_droits'])) {
	if($_REQUEST['param']=='clear') {
		unset($_SESSION['datepickchoix']);
		unset($_SESSION['crenpickerchoix']);
	}
	else if($_REQUEST['param']=='appelgo') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['val']."' WHERE param_nom = 'freq_appel'");
	}
	else if ($_REQUEST['param']=='sortie') {
		$hcheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT h_inscription2, h_inscription3 FROM ".$prefix."_freq WHERE freq_id = '".$_REQUEST['freq_id']."'"));
		$h_sortie = date('Ymd').'T'.date('His');
		if(@strlen($hcheck['h_inscription3'])==15) { $hs = 'h_sortie_3'; }
		else if(@strlen($hcheck['h_inscription2'])==15) { $hs = 'h_sortie_2'; }
		else { $hs = 'h_sortie'; }
		mysqli_query($bdd,"UPDATE ".$prefix."_freq SET cas = 'sor', ".$hs." = '".$h_sortie."' WHERE eleves_id = '".$_REQUEST['eleve_id']."' AND freq_id = '".$_REQUEST['freq_id']."'");		
	}
	else if ($_REQUEST['param']=='exclu') {
		$hcheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT h_inscription2, h_inscription3 FROM ".$prefix."_freq WHERE freq_id = '".$_REQUEST['freq_id']."'"));
		$h_sortie = date('Ymd').'T'.date('His');
		if(strlen($hcheck['h_inscription3'])==15) { $hs = 'h_sortie_3'; }
		else if(strlen($hcheck['h_inscription2'])==15) { $hs = 'h_sortie_2'; }
		else { $hs = 'h_sortie'; }
		mysqli_query($bdd,"UPDATE ".$prefix."_freq SET cas = 'exc', ".$hs." = '".$h_sortie."' WHERE eleves_id = '".$_REQUEST['eleve_id']."' AND freq_id = '".$_REQUEST['freq_id']."'");
	}
	else if ($_REQUEST['param']=='detexclu') {
		$_SESSION['timestop']=0;
		$yetexcluded = 1;
		include('../../db/proc/freq_exclu_newconfirm.php');
		$hcheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT h_inscription2, h_inscription3 FROM ".$prefix."_freq WHERE freq_id = '".$_REQUEST['freq_id']."'"));
		$h_sortie = date('Ymd').'T'.date('His');
		if(@strlen($hcheck['h_inscription3'])==15) { $hs = 'h_sortie_3'; }
		else if(@strlen($hcheck['h_inscription2'])==15) { $hs = 'h_sortie_2'; }
		else { $hs = 'h_sortie'; }
		mysqli_query($bdd,"UPDATE ".$prefix."_freq SET cas = 'exc', ".$hs." = '".$h_sortie."' WHERE eleves_id = '".$_REQUEST['eleve_id']."' AND freq_id = '".$_REQUEST['freq_id']."'");
	}
	else if ($_REQUEST['param']=='enlever') {
		mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '1' WHERE eleves_id = '".$_REQUEST['eleve_id']."' AND freqres_id = '".$_REQUEST['freqres_id']."'");
	}
	else if ($_REQUEST['param']=='inscrire') {
		$dday = date("Y-m-d");
		if(isset($_SESSION['datepickchoix'])) {
			unset($_SESSION['datepickchoix']);
		}
		if(isset($_SESSION['crenpickerchoix'])) {
			unset($_SESSION['crenpickerchoix']);
		}
		$eleve_id = $_REQUEST['eleve_id'];
		$code_freqres = $_REQUEST['code_freqres'];
		$code_activ = $_REQUEST['selectactiv'];
		$_SESSION['code_activ'] = $code_activ;
		// Vérification qu'il ne soit pas déjà inscrit :
		$verif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq WHERE eleves_id = '".$eleve_id."' AND date_freq = '".$_REQUEST['date_freqres']."' AND hor_name = '".substr($code_freqres,10,2)."'");
		$nb_verif = mysqli_num_rows($verif);
		// Seuils si différents espaces :
		$espopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_gestion'"));
		if($espopt['valeur']==1) {
			if($_REQUEST['espace']==0) {
				$esp_var = "AND (freq.espace_id = '0' OR freq.espace_id IS NULL) ";
			}
			else { $esp_var = "AND freq.espace_id = '".$_REQUEST['espace']."' "; }
			$inscrits_select = "SELECT freq.eleves_id, freq.date_freq, freq.hor_name, freq.cas, freq.h_inscription, espaces.espace_id, espaces.espace_nom, eleves.nom, eleves.prenom, eleves.division 
			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 freq.cas != 'sor' AND freq.cas != 'exc' ".$esp_var." 
			ORDER BY eleves.division ASC, eleves.nom DESC, eleves.prenom ASC";
			$inscrits = mysqli_query($bdd,$inscrits_select);
			$nb_inscrits = mysqli_num_rows($inscrits);
			if($_POST['espace']!=0) {
				$sin_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT seuilnbi FROM ".$prefix."_espaces WHERE espace_id = '".$_POST['espace']."'"));
				$sir_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT seuilreaci FROM ".$prefix."_espaces WHERE espace_id = '".$_POST['espace']."'"));
				$sin_valeur = $sin_info['seuilnbi'];
				$sir_valeur = $sir_info['seuilreaci'];
			}
			else {
				$sin_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilnbi'"));
				$sin_valeur = $sin_info['valeur'];
				$sir_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilreaci'"));
				$sir_valeur = $sir_info['valeur'];
			}
		}
		if(($nb_verif==0&&$espopt['valeur']==0)||($nb_verif==0&&$espopt['valeur']==1&&($nb_inscrits<$sin_valeur||$sir_valeur=="alert"))) {
			// Récupération de la division :
			$diveleve = mysqli_query($bdd,"SELECT division, sexe FROM ".$prefix."_eleves WHERE eleves_id = '".$eleve_id."'");
			$diveleve_nb = mysqli_num_rows($diveleve);
			if($diveleve_nb!=0) {
				$diveleve_info = mysqli_fetch_assoc($diveleve);
			}
			$eleve_div = $diveleve_info['division'];
			$eleve_sex = $diveleve_info['sexe'];
			// Inscription :
			$h_exact = date('Ymd').'T'.date('His');
			if($espopt['valeur']==1) {
				mysqli_query($bdd,"INSERT INTO ".$prefix."_freq (eleves_id, date_freq, hor_name, code_activ, division, sexe, h_inscription, user_id, espace_id) VALUES ('".$eleve_id."', '".$dday."', '".$_SESSION['ccren']."', '".$code_activ."', '".$eleve_div."', '".$eleve_sex."', '".$h_exact."', '".$_SESSION['user_id']."', '".$_POST['espace']."')"); }
			else { mysqli_query($bdd,"INSERT INTO ".$prefix."_freq (eleves_id, date_freq, hor_name, code_activ, division, sexe, h_inscription, user_id, espace_id) VALUES ('".$eleve_id."', '".$dday."', '".$_SESSION['ccren']."', '".$code_activ."', '".$eleve_div."', '".$eleve_sex."', '".$h_exact."', '".$_SESSION['user_id']."', '0')"); }
			mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '2' WHERE eleves_id = '".$_REQUEST['eleve_id']."' AND freqres_id = '".$_REQUEST['freqres_id']."'");
		}
		else if($nb_verif>0) { // si déjà inscrit on supprime de la réservation
			mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '2' WHERE eleves_id = '".$_REQUEST['eleve_id']."' AND freqres_id = '".$_REQUEST['freqres_id']."'");
		}
	}
	else if ($_REQUEST['param']=='inspre') {
		$dday = date("Y-m-d");
		if(isset($_SESSION['datepickchoix'])) {
			unset($_SESSION['datepickchoix']);
		}
		if(isset($_SESSION['crenpickerchoix'])) {
			unset($_SESSION['crenpickerchoix']);
		}
		$eleve_id = $_REQUEST['eleve_id'];
		$code_activ = $_REQUEST['selectactiv'];
		// Vérification qu'il ne soit pas déjà inscrit :
		$verif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq WHERE eleves_id = '".$eleve_id."' AND date_freq = '".$dday."' AND hor_name = '".$_SESSION['ccren']."' AND code_activ = '".$code_activ."'");
		$nb_verif = mysqli_num_rows($verif);
		$espopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_gestion'"));
		// Seuils si différents espaces :
		if($espopt['valeur']==1) {
			if($_REQUEST['espace']==0) {
				$esp_var = "AND (freq.espace_id = '0' OR freq.espace_id IS NULL) ";
			}
			else { $esp_var = "AND freq.espace_id = '".$_REQUEST['espace']."'"; }
			$inscrits_select = "SELECT freq.eleves_id, freq.date_freq, freq.hor_name, freq.code_activ, freq.cas, espaces.espace_id, espaces.espace_nom, eleves.nom, eleves.prenom, eleves.division 
			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 freq.code_activ = '".$code_activ."' AND freq.cas != 'sor' AND freq.cas != 'exc' ".$esp_var." 
			ORDER BY eleves.division ASC, eleves.nom DESC, eleves.prenom ASC";
			$inscrits = mysqli_query($bdd,$inscrits_select);
			$nb_inscrits = mysqli_num_rows($inscrits);
			if($_REQUEST['espace']!=0) {
				$sin = mysqli_query($bdd,"SELECT seuilnbi FROM ".$prefix."_espaces WHERE espace_id = '".$_REQUEST['espace']."'");
				$sin_info = mysqli_fetch_assoc($sin);
				$sir = mysqli_query($bdd,"SELECT seuilreaci FROM ".$prefix."_espaces WHERE espace_id = '".$_REQUEST['espace']."'");
				$sir_info = mysqli_fetch_assoc($sir);
				$sin_info['valeur'] = $sin_info['seuilnbi'];
				$sir_info['valeur'] = $sir_info['seuilreaci'];
			}
			else {
				$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(($nb_verif==0&&$espopt['valeur']==0)||($nb_verif==0&&$espopt['valeur']==1&&($nb_inscrits<$sin_info['valeur']||$sir_info['valeur']=="alert"))) {
			// Récupération de la division et du sexe :
			$diveleve = mysqli_query($bdd,"SELECT division, sexe FROM ".$prefix."_eleves WHERE eleves_id = '".$eleve_id."'");
			$diveleve_nb = mysqli_num_rows($diveleve);
			if($diveleve_nb!=0) {
				$diveleve_info = mysqli_fetch_assoc($diveleve);
			}
			$eleve_div = $diveleve_info['division'];
			$eleve_sex = $diveleve_info['sexe'];
			// Inscription :
			$h_exact = date('Ymd').'T'.date('His');
			if($espopt['valeur']==1) {
				mysqli_query($bdd,"INSERT INTO ".$prefix."_freq (eleves_id, date_freq, hor_name, code_activ, division, sexe, h_inscription, user_id, espace_id) VALUES ('".$eleve_id."', '".$dday."', '".$_SESSION['ccren']."', '".$code_activ."', '".$eleve_div."', '".$eleve_sex."', '".$h_exact."', '".$_SESSION['user_id']."', '".$_REQUEST['espace']."')");
			}
			else {
				mysqli_query($bdd,"INSERT INTO ".$prefix."_freq (eleves_id, date_freq, hor_name, code_activ, division, sexe, h_inscription, user_id, espace_id) VALUES ('".$eleve_id."', '".$dday."', '".$_SESSION['ccren']."', '".$code_activ."', '".$eleve_div."', '".$eleve_sex."', '".$h_exact."', '".$_SESSION['user_id']."', '0')");
			}
		}
	}
}
mysqli_close($bdd);
?>