<?php session_start(); ?>
<!doctype html>
<?php require_once('config.php');
require_once(BASE_PATH . '/db/idl_connect.php');

	if(isset($_SESSION['user_droits_arr'])) {
	$droits = (string)($_SESSION['user_droits_arr'] ?? '');
	$_SESSION['user_droits'] = trim($droits);
	if (($_SESSION['user_droits'][3] ?? 1) == 1) {
			echo '<link rel="stylesheet" type="text/css" href="css/idl_solo.css" />
			<link rel="stylesheet" type="text/css" href="css/idl_print.css" media="print" />';
			$page_header = 'freq'; 
			include('include/header.php');
			echo '<p class="noaccess">'.NOMODULE.'<br><a href="index.php">'.ACCUEIL.'</a></p>';
		}
		else { 
			?>
		<html>
		<head>

		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title><?php echo FREQT; ?> - InfodocLog</title>
		<link rel="stylesheet" href="css/jquery-ui.css">
		<link rel="stylesheet" type="text/css" href="css/idl_solo.css" />
		<link rel="stylesheet" type="text/css" href="css/idl_solo_resp.css" />
		<link rel="stylesheet" type="text/css" href="css/idl_print.css" media="print" />
		<link rel="stylesheet" href="include/fontawesome/css/all.css">
		<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
		<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
		<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
		<script type="text/javascript" src="js/jquery-3.7.1.js"></script>
		<script type="text/javascript" src="js/jquery-ui.js"></script>
		<script type="text/javascript" src="js/jquery.tablesorter.min.js"></script>
		<script type="text/javascript" src="js/jquery.tablesorter.widgets.min.js"></script>
		<script type="text/javascript" src="js/jquery.tablesorter.pager.min.js"></script>
		<script language="javascript">
			function toggleDiv(divid){
				var element = document.getElementById(divid);
			    if (element.style.display === 'none') {
			        element.style.display = 'block';
			    } else {
			        element.style.display = 'none';
			    }
			}
			function toggleValid(invalidDivId, validDivId) {
		        document.getElementById(invalidDivId).style.display = 'none';
		        document.getElementById(validDivId).style.display = 'block';
				document.getElementById('validerBoutonValidb').disabled = false;
				document.getElementById('validerBoutonInvalida').disabled = true;
		    }
		</script>
		<?php
		include('include/tablesorter.php'); 	
		mysqli_query($bdd,"DELETE FROM ".$prefix."_tchat WHERE created_at < NOW() - INTERVAL 30 DAY");
		?>
		
		<script type='text/javascript'>

			function getXhr(){
				const ALERT2 = "<?php echo ALERT2; ?>";
				var xhr = null; 
				if(window.XMLHttpRequest) // Firefox et autres
				   xhr = new XMLHttpRequest(); 
				else if(window.ActiveXObject){ // Internet Explorer 
				   try {
							xhr = new ActiveXObject("Msxml2.XMLHTTP");
						} catch (e) {
							xhr = new ActiveXObject("Microsoft.XMLHTTP");
						}
				}
				else { // XMLHttpRequest non supporté par le navigateur 
				   alert(ALERT2); 
				   xhr = false; 
				} 
								return xhr;
			}

			/**
			* Méthode qui sera appelée sur le click du bouton pour les div
			*/
			function go(){				
				// POUR DIV
				var xhr = getXhr();
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						leselect = xhr.responseText;
						document.getElementById('divselect').innerHTML = leselect;
					}
				}

				xhr.open("POST","db/proc/freq_select_div.php",true);
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				sel = document.getElementById('classe');
				idauteur = sel.options[sel.selectedIndex].value;
				xhr.send("idAuteur="+idauteur);
				document.getElementById("checkopt").style.display = 'block';
			}
		</script>
		<script>
	    $(function() {
		    $("#eleves").autocomplete({
		        source: function(request, response) {
		            $.ajax({
		                url: "include/suggestions.php",
		                dataType: "json",
		                data: {
		                    term: request.term,
			            	mode: "eleves"
		                },
		                success: function(data) {
		                    // on passe les données à l'autocomplete
		                    response(data);		                    
		                }
		            });
		        }
		    });
		});
	    $(function() {
	        $("#elevesbis").autocomplete({
	            source: function(request, response) {
	                $.ajax({
	                    url: "include/suggestions.php",
	                    dataType: "json",
	                    data: {
	                        term: request.term,
			            	mode: "eleves"
	                    },
	                    success: function(data) {
	                        response(data);
	                    }
	                });
	            }
	        });
	    });
	    $(function() {
	        $("#elevester").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 include('js/idl/chat_box.php'); ?>

	</head>

	<body>
		
		<?php $page_header = 'freq'; ?>
		<?php include('include/header.php'); 
		$stmt = $pdo->prepare("
		    SELECT valeur 
		    FROM ".$prefix."_parametres 
		    WHERE param_nom = :param
		");
		$stmt->execute([
		    'param' => 'freq_codeonly'
		]);
		$onlycode_check_info = $stmt->fetch(PDO::FETCH_ASSOC);
		if(isset($_SESSION['freqcodeonly'])&&($onlycode_check_info['valeur']!=@$_SESSION['freqcodeonly'])&&@$_SESSION['freqcodeonly']!='def') {
			$onlycode_check_info['valeur']=@$_SESSION['freqcodeonly'];
		}
		if(isset($_SESSION['user_droits'])&&isset($bdd)) {
			$dnns = @mysqli_fetch_array(mysqli_query($bdd,"SELECT COUNT(user_id) AS nb FROM ".$prefix."_userconnect WHERE user_id='".$_SESSION['user_id']."'"));
	        if(@$dnns['nb']>0) {
	            @mysqli_query($bdd,"UPDATE ".$prefix."_userconnect SET connected='".time()."' WHERE user_id='".$_SESSION['user_id']."'");
	        }
	        else {
	            @mysqli_query($bdd,"INSERT INTO ".$prefix."_userconnect (user_id, connected) values ('".$_SESSION['user_id']."', '".time()."')");
	        }											
			include('include/chat_div.php');
		}?>
		<section>
			<?php include('include/nav/nav_freq.php'); ?>
			<article id="art_freq">
			<?php
			
			$api_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_api_nb'"));
			if($api_info['valeur']=='1') {
				function writeInscriptionCount($count, $prefix) {
				    $filePath = BASE_PATH . '/img/edt/'.$prefix.'/nbinscrits.txt';
				    file_put_contents($filePath, $count);
				} 
			}
			$freq_gestion_jourscren = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_gestion_jourscren'"));
			$freqres_raison_opt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freqres_raison_opt'"));
			$nivs_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv'"));
			$periodsus = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_resref_date'"));
			$freqres_max = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freqres_max'"));
			$freq_res_minutesmax = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freq_res_minutesmax'"));
			$freq_res_nbmaxday = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freq_res_nbmaxday'"));
			$freqres_max_origin = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freqres_max_origin'"));
			if($nivs_info['valeur']=="PER") { 
				$carac_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv_persoc'"));
				$caracs = explode(",", $carac_info['valeur']);
				$caracs_assoc = array_combine($caracs, $caracs);
				// $caracs_assoc est un tableau associatif
				// Copier $caracs_assoc dans un nouveau tableau pour ne pas affecter l'original
				$sorted_caracs = $caracs_assoc;
				// Trier par longueur des clés en ordre décroissant
				uksort($sorted_caracs, function($a, $b) {
				    return strlen($b) - strlen($a);  // Trier par longueur des clés
				});
			}
			if(!isset($_GET['page'])) {
				if($droits==0||$droits==3||$droits==2||$droits==4||$droits==6) {
					include('./contenu/freq_index.php');
				}
				else { include('./contenu/freq_reserve.php'); }
			}
			else {
				switch($_GET['page']) {
					case'inscript':
						include('./contenu/freq_index.php');
						break;
					case'reserve':
						include('./contenu/freq_reserve.php');
						break;
					case'retards':
						include('./contenu/freq_retards.php');
						break;
					case'exclusions':
						include('./contenu/freq_exclusions.php');
						break;
					case'journal':
						include('./contenu/freq_journal.php');
						break;
					case'stats':
						include('./contenu/freq_stats.php');
						break;
					case'param':
						include('./contenu/freq_param.php');
						break;
				}
			}
			?>
			</article>
			
		</section>

	<footer id="foot_freq">
	<?php include('include/footer.php'); ?>
	</footer>
	</body>
	
	</html>
	<?php
	}	
}
else {
	echo '<link rel="stylesheet" type="text/css" href="css/idl_solo.css" />';
	$page_header = 'freq'; 
	include('include/header.php');
	include('include/noaccess.php');
	echo '<br><a href="index.php">'.ACCUEIL.'</a></p>';
}
mysqli_close($bdd);
?>
