<?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'][4] ?? 0) != 2) {
		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 = 'param'; 
		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 PARA2; ?> - InfodocLog</title>
	<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="css/jquery-ui.css">
	<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';
		    }
		}
	</script>
	<script language="javascript">
		function toggleModu(divid,aplus,aminus,amin2){
			if(document.getElementById(divid).style.display == "none"){
				document.getElementById(divid).style.display = "block";
				document.getElementById(aplus).style.display = "none";
				document.getElementById(aminus).style.display = "inline-block";
				document.getElementById(amin2).style.display = "none";
			}else{
				document.getElementById(divid).style.display = "none";
				document.getElementById(aplus).style.display = "inline-block";
				document.getElementById(aminus).style.display = "none";
				document.getElementById(amin2).style.display = "inline-block";
			}
		}
	</script>
	<?php
	include('include/tablesorter.php');
	?>
	<script type='text/javascript'>

		function getXhr(){
							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("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
			   xhr = false; 
			} 
			return xhr;
		}

		/**
		* Méthode qui sera appelée sur le click du bouton la division
		*/
		function zone(){
			
			var xhr = getXhr();
			xhr.onreadystatechange = function(){
				if(xhr.readyState == 4 && xhr.status == 200){
					leselect = xhr.responseText;
					document.getElementById('fuseau').innerHTML = leselect;
				}
			}

			xhr.open("POST","db/proc/fuseau_select.php",true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			sel = document.getElementById('bigzone');
			idauteur = sel.options[sel.selectedIndex].value;
			xhr.send("idAuteur="+idauteur);
		}
		</script>
		<?php include('js/idl/chat_box.php'); ?>
</head>

<body>
	<?php $page_header = 'param'; ?>
	<?php include('include/header.php'); 
	if (isset($_SESSION['user_droits']) && isset($pdo)) {
	    $user_id = (int)($_SESSION['user_id'] ?? 0);
	    if ($user_id > 0) {
	        // Vérifie si l'utilisateur existe déjà
	        $stmt = $pdo->prepare(
	            "SELECT COUNT(user_id)
	             FROM {$prefix}_userconnect
	             WHERE user_id = :user_id"
	        );
	        $stmt->execute([
	            ':user_id' => $user_id
	        ]);
	        $exists = (int)$stmt->fetchColumn();
	        if ($exists > 0) {
	            // UPDATE dernière connexion
	            $stmt = $pdo->prepare(
	                "UPDATE {$prefix}_userconnect
	                 SET connected = :connected
	                 WHERE user_id = :user_id"
	            );
	            $stmt->execute([
	                ':connected' => time(),
	                ':user_id' => $user_id
	            ]);
	        } else {
	            // INSERT première connexion
	            $stmt = $pdo->prepare(
	                "INSERT INTO {$prefix}_userconnect (user_id, connected)
	                 VALUES (:user_id, :connected)"
	            );
	            $stmt->execute([
	                ':user_id' => $user_id,
	                ':connected' => time()
	            ]);
	        }
	    }
	    include('include/chat_div.php');
	}?>
	
	<section>
		<?php include('include/nav/nav_param.php'); ?>
		<article id="art_parametres">
			<?php
			if(!isset($_GET['page'])) {
				include('./contenu/param_index.php');
			}
			else {
				switch($_GET['page']) {
					case'interface':
						include('./contenu/param_interface.php');
						break;
					case'users':
						include('./contenu/param_users.php');
						break;
					case'etab':
						include('./contenu/param_etab.php');
						break;
					case'calendrier':
						include('./contenu/param_calendrier.php');
						break;
					case'horaires':
						include('./contenu/param_horaires.php');
						break;
					case'savoirs':
						include('./contenu/param_savoirs.php');
						break;
					case'majlog':
						include('./contenu/param_majlog.php');
						break;
				}
			}
			?>
		</article>
		
	</section>
	
	<footer id="foot_param">
	<?php include('include/footer.php'); ?>
	</footer>

</body>

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