<?php
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][4]!="2") {
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		
		?><style type="text/css">
		#mpa2 { background-color: #fff; }
		#mpa2 td { border: 3px #7D7777 solid; }
		#mpa1 td { border: 0; }
		</style><?php	
		echo '<h1>'.PARANAV2.'</h1>';			
		echo '<div style="overflow: auto;  white-space: nowrap;">';
		if(isset($_SESSION['signal'])) {
			echo $_SESSION['signal'];
			unset($_SESSION['signal']);
		}

		?><script type='text/javascript'>
			function surligne(champ, erreur)
			{
				if(erreur)
					champ.style.backgroundColor = "#fba";
				else
					champ.style.backgroundColor = "";
			}
			function verifPseudo(champ)
			{
				if(champ.value.length < 2 || champ.value.length > 40)
				{
					surligne(champ, true);
					return false;	
				}
				else
				{
					surligne(champ, false);
					return true;
				}
			}
			function verifMdp(champ)
			{
				if(champ.value.length < 8 || champ.value.length > 30)
				{
					if(champ.value.length!=0) {
						surligne(champ, true);
						return false;	
					}
					else {
						surligne(champ, false);
						return true;
					}
				}
				else
				{
					surligne(champ, false);
					return true;
				}
			}
			function verifForm(f)
			{
				var pseudoOk = verifPseudo(f.login);
				var mdpOk = verifMdp(f.mdp);
				if(pseudoOk && mdpOk)
					return true;
				else
				{
					alert("Veuillez remplir correctement tous les champs");
					return false;	
				}
			}
		</script>
		<?php
		
		$espopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_gestion'")); // pour voir s'il faut préciser un espace par défaut
		$esp0 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first'"));
		$langdef = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'language'")); // langue par défaut
		
		if(isset($_SESSION['blockmodif'])) {
			echo $_SESSION['blockmodif'];
			unset($_SESSION['blockmodif']);
		}

		echo '<h2>'.PARAC_USER1.'</h2>';
		
		if(isset($_POST['usernew'])) {
			if($_POST['login']==""||$_POST['mdp']=="") {
				echo PARAC_USER2;
			}
			else {
				$login = mysqli_real_escape_string($bdd,$_POST['login']);
				// Vérification de code élève pour éviter le doublon
				$reserv_exist = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE code = '$login' OR id_constr = '$login' OR id_perso = '$login'"));
				if($reserv_exist==0&&$login!='reserver') {
					// Récupération des données
					$nom = mysqli_real_escape_string($bdd,$_POST['nom']);
					$prenom = mysqli_real_escape_string($bdd,$_POST['prenom']);
					$mail = mysqli_real_escape_string($bdd,$_POST['mail']);
					if(isset($_POST['evald'])) { $evald = 2; } else { $evald = 1; }
					if(isset($_POST['editd'])) { $editd = 2; } else { $editd = 1; }
					if(isset($_POST['edtd2'])) { $edtd = 2; }
					else if(isset($_POST['edtd1'])&&!isset($_POST['edtd2'])) { $edtd = 3; }
					else { $edtd = 1; }
					if(isset($_POST['freqd3'])) { $freqd = 2; } // administration fréq
					else if(isset($_POST['freqd1'])&&!isset($_POST['freqd2'])) { $freqd = 4; } //inscription fréq
					else if(!isset($_POST['freqd1'])&&isset($_POST['freqd2'])) { $freqd = 5; } //réservation fréq
					else if(isset($_POST['freqd1'])&&isset($_POST['freqd2'])) { $freqd = 6; } //inscription et réservation fréq
					else if(isset($_POST['freqd0'])&&!isset($_POST['freqd1'])&&!isset($_POST['freqd2'])) { $freqd = 3; } // consult. fréq
					else { $freqd = 1; }
					if(isset($_POST['freqd4'])) { $freqd = 0; } // freq nue
					if(isset($_POST['paramd'])) { $paramd = 2; } else { $paramd = 1; }
					if(isset($_POST['dbd2'])) { $dbd = 2; }
					else if(isset($_POST['dbd1'])&&!isset($_POST['dbd2'])) { $dbd = 3; }
					else { $dbd = 1; }
					if($evald.$editd.$edtd.$freqd.$paramd.$dbd=='222222') { // gestion provisoire (9) du droit complémentaire
						$sid = '2';
					}
					else { $sid = '0'; }
					if($freqd == 0) {
						if($edtd == 3) {
							$droits = '1130110';
						}
						else {
							$droits = '1110110';
						}
					}
					else {
						$droits = $evald.$editd.$edtd.$freqd.$paramd.$dbd.$sid;
					}
					$accueil = $_POST['accueil'];
					// Vérification que l'accueil soit possible en accès (sinon accueil vide -> gestion dans db/droits)
					if($accueil==1&&$evald==1) { $accueil = ""; }
					if($accueil==2&&$editd==1) { $accueil = ""; }
					if($accueil==3&&$edtd==1) { $accueil = ""; }
					if($accueil==4&&$freqd==1) { $accueil = ""; }
					if($accueil==5&&$paramd==1) { $accueil = ""; }
					if($accueil==6&&$dbd==1) { $accueil = ""; }
					// Enregistrement
					$mdp = mysqli_real_escape_string($bdd,$_POST['mdp']);
					$hashed_password = password_hash($mdp, PASSWORD_DEFAULT);
					$sel = '0';
					mysqli_query($bdd,"INSERT INTO ".$prefix."_user (nom, prenom, login, mdp, sel, mail, droits, accueil, droits_spe, edt, connected_info, espace_default, align_activ, align_espace, auth_mail, auth_chat, freq_res_dureemax, lang) VALUES ('".$nom."', '".$prenom."', '".$login."',  '".$hashed_password."', '".$sel."', '".$mail."', '".$droits."', '".$accueil."', 'def;def;def;def;def;def;def;def;def;def;def;def;def;def;def;def;def;def;def;def', '0', '0', '', 'h', 'h', '0', '0', '0', '".$langdef['valeur']."')");
				}
				else {
					if($login=='reserver') {
						$_SESSION['signal'] = '<p class="signal">'.PARAC_USER3.'</p>';
					}
					else {
						$_SESSION['signal'] = '<p class="signal">'.PARAC_USER4.'</p>';
					}
				}
			}
		}
		echo '<form method="POST" name="usernew" action="" onsubmit="return verifForm(this)">';
			echo '<table class="paramform">';
				echo '<tr><td>'.LOGIN.DOTS.'</td><td style="border-left-style: hidden;">';
				if(@$multi=="ON"&&@$multipref==1) {
					echo $_SESSION['prefident'];
				}
				echo '<input type="text" class="inptxt" name="login" maxlength="40" ';
				if(@$multi=="ON"&&@$multipref==1) {
					echo 'size="16"';
				}
				echo ' value="" onblur="verifPseudo(this)" required /></td>
				<td>'.NAME.DOTS.'</td><td style="border-left-style: hidden;"><input type="text" class="inptxt" name="nom" maxlength="30" value="" /></td>
				<td>'.FORNAME.DOTS.'</td><td style="border-left-style: hidden;"><input type="text" class="inptxt" name="prenom" maxlength="30" value="" /></td></tr>
				<tr><td>'.PW2.'<br>'.PWNBCARAC.'</td><td style="border-left-style: hidden;"><input type="password" class="inptxt" name="mdp" maxlength="30" value="" onblur="verifMdp(this)" required value="" /><i class="fa-solid fa-eye" onclick="showPass(this)" style="margin-left: -26px; cursor: pointer;"></i></td>
				<td>'.MAIL.DOTS.'</td><td style="border-left-style: hidden;"><input type="text" class="inptxt" name="mail" maxlength="100" value="" /></td>
				<td>'.PARAC_USER5.'</td><td style="border-left-style: hidden;"><div class="box"><select name="accueil" value=""><option value="1">'.EVALT.'</option><option value="2">'.SEQUT.'</option><option value="3">'.EDT1.'</option><option value="4" selected>'.FREQT.'</option><option value="5">'.PARAT.'</option><option value="6">'.DATANAV10.'</option></select></div></td></tr>';
				echo '<tr><td colspan="6"><p>'.PARAC_USER6.'</p>
				<input type="checkbox" name="freqd4" id="freqd4" /> <label for="freqd4">'.PARAC_USER7.'</label><hr style="border:1px dashed grey">
				<input type="checkbox" name="evald" id="evald" /> <label for="evald">'.EVALT.'</label><hr style="border:1px dashed grey">
				<input type="checkbox" name="editd" id="editd" /> <label for="editd">'.SEQUT.'</label><hr style="border:1px dashed grey">
				<input type="checkbox" name="edtd1" id="edtd1" /> <label for="edtd1">'.EDT1.' ('.PARAC_USER8.')</label><br>
				<input type="checkbox" name="edtd2" id="edtd2" /> <label for="edtd2">'.EDT1.' ('.PARAC_USER9.')</label><hr style="border:1px dashed grey">
				<input type="checkbox" name="freqd0" id="freqd0" /> <label for="freqd0">'.FREQT.' ('.PARAC_USER8.')</label><br>
				<input type="checkbox" name="freqd1" id="freqd1" /> <label for="freqd1">'.FREQT.' ('.PARAC_USER10.')</label><br>
				<input type="checkbox" name="freqd2" id="freqd2" /> <label for="freqd2">'.FREQT.' ('.PARAC_USER11.')</label><br>
				<input type="checkbox" name="freqd3" id="freqd3" /> <label for="freqd3">'.FREQT.' ('.PARAC_USER9.')</label><hr style="border:1px dashed grey">
				<input type="checkbox" name="dbd1" id="dbd1" /> <label for="dbd1">'.DATANAV10.' ('.PARAC_USER8.')</label><br>
				<input type="checkbox" name="dbd2" id="dbd2" /> <label for="dbd2">'.DATANAV10.' ('.PARAC_USER9.')</label><hr style="border:1px dashed grey">
				<input type="checkbox" name="paramd" id="paramd" /> <label for="paramd">'.PARAT.'</label><br>
				</td></tr>';			
			echo '</table>';
		echo '<p><span class="paddvalid1"><input class="bouton boutparam" type="submit" value="'.ADD.'" name="usernew" /></span></p>';
		echo '</form>';
		
		$langDir = BASE_PATH . '/include/lang/';
		$langFiles = scandir($langDir);
		$languages = [];
		foreach ($langFiles as $file) {
		    if (preg_match('/^([a-z]{2})\.php$/', $file, $matches)) {
		        $code = $matches[1];
		        $filePath = $langDir . $file;
		        $content = file_get_contents($filePath);

		        // Extraire le nom de la langue avec une expression régulière
		        if (preg_match('/define\(\s*\'LANG_VALID\'\s*,\s*true\s*\)/', $content) && preg_match('/\$lang_name\s*=\s*"([^"]+)"/', $content, $nameMatches)) {
		            $languages[$code] = $nameMatches[1];
		        }
		    }
		}
		// Modifications et suppressions
		echo '<h2>Modifications</h2>';
		$nb_anticip = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_creneausuiv'"));
		$crprechoix_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_crprechoix'"));
		$users = mysqli_query($bdd,"SELECT * FROM ".$prefix."_user");
		foreach($users as $userup) {
			echo '<form method="POST" name="userup'.$userup['id'].'" action="" onsubmit="return verifForm(this)">';
			echo '<table class="paramform">';
				$_SESSION['id'.$userup['id'].''] = $userup['id'];
				echo '<tr><td colspan="7" style="font-size:1.3em;"><b>'.$userup['login'].'</b> (id. '.$userup['id'].')';
				if($userup['sel']!='0') {
					echo ' <span style="float:right;"><i class="fa-solid fa-triangle-exclamation" title="'.PW_REINIT2.'"></i> '.PW_REINIT2.' </span>';
				}
				echo '</td></tr>
				<tr><td>'.LOGIN.DOTS.'</td><td style="border-left-style: hidden;">';
				if(@$multi=="ON"&&@$multipref==1) {
					echo $_SESSION['prefident'];
				}
				echo '<input type="text" class="inptxt" name="login" maxlength="40" autocomplete="off" ';
				if(@$multi=="ON"&&@$multipref==1) {
					echo 'size="16"';
				}
				echo ' value="'.$userup['login'].'"  onblur="verifPseudo(this)" /></td>
				<td>'.NAME.DOTS.'</td><td style="border-left-style: hidden;"><input type="text" class="inptxt" name="nom" maxlength="30" value="'.$userup['nom'].'" /></td>
				<td>'.FORNAME.DOTS.'</td><td style="border-left-style: hidden;"><input type="text" class="inptxt" name="prenom" maxlength="30" value="'.$userup['prenom'].'" /></td>
				</tr>
				<tr><td>'.PW2.'<br><i>'.PWVID.'</i><br>'.PWNBCARAC.'</td><td style="border-left-style: hidden;"><input type="text" class="inptxt" name="mdp" maxlength="30" value="" onblur="verifMdp(this)" /><i class="fa-solid fa-eye" onclick="showPass(this)" style="margin-left: -26px; cursor: pointer;"></i></td>
				<td>'.MAIL.DOTS.'</td><td style="border-left-style: hidden;"><input type="text" class="inptxt" name="mail" maxlength="100" value="'.$userup['mail'].'" /></td>
				<td>'.PARAC_USER5.'</td><td style="border-left-style: hidden;">';
				echo '<div class="box"><select name="accueil" value="">';
				$acccode = array('1','2','3','4','5','6');
				$accname = array(EVALT,SEQUT,EDT1,FREQT,PARAT,DATANAV10);
				for($an=0;$an<count($acccode);$an++) {
					echo '<option value="'.$acccode[$an].'"';
					if ($userup['accueil']==$acccode[$an]) {
						echo ' selected ';
					}
					echo '>'.$accname[$an].'</option>';
				}
				echo '</select></div>';
				echo '</td></tr>';
								
				echo '<tr><td colspan="2" style="vertical-align:top;"><p><center><b><i>'.PARAC_USER12.'</i></b></center></p><hr style="border:2px dashed grey">';
				$check_droits = $userup['droits'];
				$check_eval = substr("$check_droits", 0, 1);
				$check_edit = substr("$check_droits", 1, 1);
				$check_edt = substr("$check_droits", 2, 1);
				$check_freq = substr("$check_droits", 3, 1);
				$check_param = substr("$check_droits", 4, 1);
				$check_db = substr("$check_droits", 5, 1);
				$check_si = substr("$check_droits", 6, 1);
				
				$disabled1 = $check_freq == 1 ? 'disabled' : '';
				$disabledClass1 = $check_freq == 1 ? 'disabled-class' : '';
				$disabled3 = ($check_freq == 3 || $check_freq == 0) ? 'disabled' : '';
				$disabledClass3 = ($check_freq == 3 || $check_freq == 0) ? 'disabled-class' : '';
				
				if($check_eval == "2") { $check_evald = "checked"; } else { $check_evald = "0"; }
				if($check_edit == "2") { $check_editd = "checked"; } else { $check_editd = "0"; }
				if($check_edt == "2") { $check_edtd1 = "checked"; $check_edtd2 = "checked"; } else { $check_edtd1 = "0"; $check_edtd2 = "0"; }
				if($check_edt == "3") { $check_edtd1 = "checked"; }
				if($check_freq == "6") { $check_freqd0 = "checked"; $check_freqd1 = "checked"; $check_freqd2 = "checked"; } else { $check_freqd1 = "0";$check_freqd2 = "0"; }
				if($check_freq == "3") { $check_freqd0 = "checked"; }
				if($check_freq == "4") { $check_freqd0 = "checked"; $check_freqd1 = "checked"; }
				if($check_freq == "5") { $check_freqd2 = "checked"; }
				if($check_freq != 6&&$check_freq != 4) { $check_freqd1 = ""; }
				if($check_freq != 6&&$check_freq != 5) { $check_freqd2 = ""; }
				if($check_freq == "2") { $check_freqd0 = "checked"; $check_freqd1 = "checked"; $check_freqd2 = "checked"; $check_freqd3 = "checked"; } else { $check_freqd3 = "0"; }
				if($check_freq == "1") { $check_freqd0 = ""; }
				if($check_freq == "0") { $check_freqd4 = "checked"; $check_freqd0 = ""; $check_freqd1 = ""; $check_freqd2 = ""; $check_freqd3 = ""; }
				else { $check_freqd4 = ""; }
				if($check_param == "2") { $check_paramd = "checked"; } else { $check_paramd = "0"; }
				if($check_db == "2") { $check_dbd1 = "checked"; $check_dbd2 = "checked"; } else { $check_dbd1 = "0"; $check_dbd2 = "0"; }
				if($check_db == "3") { $check_dbd1 = "checked"; }
				echo '<br><input type="checkbox" name="freqd4" id="freqd4'.$userup['id'].'" '.$check_freqd4.'/> <label for="freqd4'.$userup['id'].'">'.PARAC_USER7.'</label>';
				if($check_freq=='0') {
					echo '<p class="signal_tiny">'.PARAC_USER71.'</p>';
				}
				echo '<hr style="border:1px dashed grey">
				<input type="checkbox" name="evald" id="evald'.$userup['id'].'" '.$check_evald.' ';
				if($check_freq == '0') {
					echo 'disabled';
				}
				echo '/> <label for="evald'.$userup['id'].'">'.EVALT.'</label><hr style="border:1px dashed grey">
				<input type="checkbox" name="editd" id="editd'.$userup['id'].'" '.$check_editd.' ';
				if($check_freq == '0') {
					echo 'disabled';
				}
				echo '/> <label for="editd'.$userup['id'].'">'.SEQUT.'</label><hr style="border:1px dashed grey">
				<input type="checkbox" name="edtd1" id="edtd1'.$userup['id'].'" '.$check_edtd1.'/> <label for="edtd1'.$userup['id'].'">'.EDT1.' ('.PARAC_USER8.')</label><br>
				<input type="checkbox" name="edtd2" id="edtd2'.$userup['id'].'" '.$check_edtd2.' ';
				if($check_freq == '0') {
					echo 'disabled';
				}
				echo '/> <label for="edtd2'.$userup['id'].'">'.EDT1.' ('.PARAC_USER9.')</label><hr style="border:1px dashed grey">
				<input type="checkbox" name="freqd0" id="freqd0'.$userup['id'].'" '.$check_freqd0.'/> <label for="freqd0'.$userup['id'].'">'.FREQT.' ('.PARAC_USER8.')</label><br>
				<input type="checkbox" name="freqd1" id="freqd1'.$userup['id'].'" '.$check_freqd1.' ';
				if($check_freq == '0') {
					echo 'disabled';
				}
				echo '/> <label for="freqd1'.$userup['id'].'">'.FREQT.' ('.PARAC_USER10.')</label><br>
				<input type="checkbox" name="freqd2" id="freqd2'.$userup['id'].'" '.$check_freqd2.' ';
				if($check_freq == '0') {
					echo 'disabled';
				}
				echo '/> <label for="freqd2'.$userup['id'].'">'.FREQT.' ('.PARAC_USER11.')</label><br>
				<input type="checkbox" name="freqd3" id="freqd3'.$userup['id'].'" '.$check_freqd3.' ';
				if($check_freq == '0') {
					echo 'disabled';
				}
				echo '/> <label for="freqd3'.$userup['id'].'">'.FREQT.' ('.PARAC_USER9.')</label>
				<hr style="border:1px dashed grey">
				<input type="checkbox" name="dbd1" id="dbd1'.$userup['id'].'" '.$check_dbd1.' ';
				if($check_freq == '0') {
					echo 'disabled';
				}
				echo '/> <label for="dbd1'.$userup['id'].'">'.DATANAV10.' ('.PARAC_USER8.')</label><br>
				<input type="checkbox" name="dbd2" id="dbd2'.$userup['id'].'" '.$check_dbd2.' ';
				if($check_freq == '0') {
					echo 'disabled';
				}
				echo '/> <label for="dbd2'.$userup['id'].'">'.DATANAV10.' ('.PARAC_USER9.')</label><hr style="border:1px dashed grey">
				<input type="checkbox" name="paramd" id="paramd'.$userup['id'].'" '.$check_paramd.' ';
				if($check_freq == '0') {
					echo 'disabled';
				}
				echo '/> <label for="paramd'.$userup['id'].'">'.PARAT.'</label><br>
				</td>
				<td colspan="2" style="vertical-align:top;"><p><center><b><i>'.PARAC_USER13.'</i></b></center></p><hr style="border:2px dashed grey">';
				if (count($languages) > 1):
					echo '<br>'.PARAC_USER14.' ';
					echo '<div class="box"><select name="langue" id="languagedef'.$userup['id'].'" value="">';
					foreach ($languages as $code => $name): 
			            echo '<option value="'.htmlspecialchars($code).'"';
			            if($code == $userup['lang']) {
			            	echo ' selected ';
			            }
			            echo '>'.htmlspecialchars($name).'</option>';
			        endforeach;
			        echo '</select></div><br>';
				endif;
				echo '<br>&nbsp; <input type="checkbox" name="connected" id="connected'.$userup['id'].'" ';
				if($userup['connected_info']==1) { echo 'checked'; }
				echo '/> <label for="connected'.$userup['id'].'">'.PARAC_USER16.'</label>';
				echo '<br>&nbsp; <input type="checkbox" name="tchat" id="tchat'.$userup['id'].'" ';
				if($userup['auth_chat']==1) { echo 'checked'; }
				echo '/> <label for="tchat'.$userup['id'].'">'.PARAC_USER47.'</label>';
				echo '<div class="'.$disabledClass1.' '.$disabledClass3.'">&nbsp; <input type="checkbox" name="edtbut" id="edtbut'.$userup['id'].'" ';
				if($userup['edt']==1) { echo 'checked'; }
				echo '/> <label for="edtbut'.$userup['id'].'">'.PARAC_USER15.'</label>
				
				<br>&nbsp; <input type="checkbox" name="auth_mail" id="auth_mail'.$userup['id'].'" ';
				if($userup['auth_mail']==1) { echo 'checked'; }
				echo '/> <label for="auth_mail'.$userup['id'].'">'.PARAC_USER17.'</label>';
								
				echo '</div><hr style="border:1px dashed grey">';
				if($espopt['valeur']==1) { 
					echo '<br>&nbsp; '.PARAC_USER18;
					$esp = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces ORDER BY espace_nom ASC");
					$nb_esp = mysqli_num_rows($esp);
					if($nb_esp==0) {
						echo PARAC_ETAB34;
					}
					else {
						echo '<div class="box"><select name="defaultespace" value="">';
						$espuser = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE espace_id = '".$userup['espace_default']."'"));
						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($userup['espace_default']==0&&!empty($userup['espace_default'])) {
							echo ' selected ';
						}
						echo '>'.$esp0['valeur'].'</option>';
						while($liste = mysqli_fetch_assoc($liste_activ)) {
							echo '<option value="'.$liste['espace_id'].'"';
							if($userup['espace_default']>0) {
								if(@$espuser['espace_id']==@$liste['espace_id']) {
									echo ' selected ';
								}
							}
							echo '>'.$liste['espace_nom'].'</option>';
						}
						echo '</select></div>'; 
					}
					echo '<br><br>&nbsp; '.PARAC_USER19.' <div class="box"><select name="align_espace" value="">';
					if($userup['align_espace']=='h'){ echo '<option value="h">'.PARAC_USER20.'</option><option value="v">'.PARAC_USER21.'</option>'; }
					else if($userup['align_espace']=='v'){ echo '<option value="v">'.PARAC_USER21.'</option><option value="h">'.PARAC_USER20.'</option>'; }
					echo '</select></div><hr style="border:1px dashed grey">';
				}
				echo '<br>&nbsp; '.PARAC_USER22.' <div class="box"><select name="align_activ" value="">';
				if($userup['align_activ']=='h'){ echo '<option value="h">'.PARAC_USER20.'</option><option value="v">'.PARAC_USER21.'</option>'; }
				else if($userup['align_activ']=='v'){ echo '<option value="v">'.PARAC_USER21.'</option><option value="h">'.PARAC_USER20.'</option>'; }
				echo '</select></div><hr style="border:1px dashed grey">';
				echo '<div class="'.$disabledClass1.' '.$disabledClass3.'"><p>'.PARAC_USER23.'<br><input type="number" class="number" name="daymax1" id="daymaxi'.$userup['id'].'" value="'.$userup['freq_res_dureemax'].'" /> '.PARAC_USER24.' <div class="box"><select name="dayone" id="dayone'.$userup['id'].'" value=""><option></option>';
				foreach (JOUR as $stkey => $stvalue){ 
					echo '<option value="'. $stkey .'"';
					if($userup['freq_res_dureemax']!='') {
						if(substr($userup['freq_res_dureemax'],0,3)==$stkey) {
							echo ' selected';
						}
					}
					echo '>'. $stvalue.'</option>'; 
				}
				echo '</select></div> &nbsp; &nbsp; '.TO2.' <div class="box"><select name="daytwo" id="daytwo'.$userup['id'].'" value=""><option></option>';
				foreach (JOUR as $stkey => $stvalue){ 
					echo '<option value="'. $stkey .'"';
					if($userup['freq_res_dureemax']!='') {
						if(substr($userup['freq_res_dureemax'],3,3)==$stkey) {
							echo ' selected';
						}
					}
					echo '>'. $stvalue.'</option>'; 
				}
				echo '</select></div></p></div>
				 
				</td>';
				$specdroits = explode(';',$userup['droits_spe']);
				
				echo '<td colspan="3" style="vertical-align:top;"><div class="'.$disabledClass1.' '.$disabledClass3.'"><p><center><b><i>'.PARAC_USER25.'</i></b></center></p><hr style="border:2px dashed grey">';
				echo '<br>'.PARAC_USER26.'<br> <div class="box"><select value="" id="minutes'.$userup['id'].'" name="minutes">
					<option></option>';
				$optant = array('def','0','5','10','15');
				for($i=0;$i<count($optant);$i++) {
					echo '<option value="'.$optant[$i].'"';
					if($specdroits[0]==$optant[$i]) {
						echo ' selected ';
					}
					if($optant[$i]=='def') {						
						echo '>'.PARAC_USER27.'('.$nb_anticip['valeur'].' '.PARAC_USER28.')</option>';
					}
					else {
						echo '>'.$optant[$i].' '.PARAC_USER29.'</option>';
					}
				}
				echo '</select></div>
				<br><br>'.PARAC_USER30.' <input type="number" min="0" max="20" value="'.$specdroits[1].'" name="nbcren"> '.PARAC_USER31.'
				<br><br>'.PARAC_USER32.'<br> <div class="box"><select value="" id="inscprec'.$userup['id'].'" name="inscprec">
				<option></option>';
				$optcode = array('def','0h','1h','1b','2h','2b');
				$optnom = $parac_user_opt;
				$selectedIndex = array_search($crprechoix_info['valeur'], $optcode);
				for($i=0;$i<count($optcode);$i++) {
					echo '<option value="'.$optant[$i].'"';
					if($specdroits[2]==$optcode[$i]) {
						echo ' selected ';
					}
					if($optcode[$i]=='def') {						
						$defaultText = isset($selectedIndex) ? $optnom[$selectedIndex] : $crprechoix_info['valeur'];
        				echo '>' . ''.PARAC_USER27.' (' . $defaultText . ')</option>';
					}
					else {
						echo '>'.$optnom[$i].'</option>';
					}
				}
				echo '</select></div><hr style="border:1px dashed grey">';
				echo '<input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb3" data-variable="'.$specdroits[3].'" ';
				if($check_freq == 3 || $check_freq == 0 || $check_freq == 1) {
					echo 'disabled';
				}
				if($specdroits[4]=='def') { $spec4=$specdroits[4]; }
				else if(substr($specdroits[4],0,3)=='def') { $spec4='def'; }
				else { $spec4= substr($specdroits[4],0,1); }
				echo '>
				<label for="trois'.$userup['id'].'choixcb3">'.PARAC_USER33.'</label>

				<input type="hidden" name="crenchoix" id="hidden_trois'.$userup['id'].'choixcb3" value="">
				<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb4" data-variable="'.$spec4.'" ';
				if($check_freq == 3 || $check_freq == 0 || $check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb4">'.PARAC_USER34.'</label>

				<br><input type="radio" name="ordgroup" id="ordrgroup1'.$userup['id'].'" value="-perdiv"';
				if (substr($specdroits[4],1)=='-perdiv'||substr($specdroits[4],1,1)=='') { echo ' checked '; }
				echo ' /> <label for="ordrgroup1'.$userup['id'].'">'.PARAC_USER51.'</label><input type="radio" name="ordgroup" id="ordrgroup2'.$userup['id'].'" value="-pernom"';
				if (substr($specdroits[4],1)=='-pernom') { echo ' checked '; }
				echo ' /> <label for="ordrgroup2'.$userup['id'].'">'.PARAC_USER52.'</label>

				<input type="hidden" name="classinscript" id="hidden_trois'.$userup['id'].'choixcb4" value="">
			    <br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb5" data-variable="'.$specdroits[5].'" ';
				if($check_freq == 3 || $check_freq == 0 || $check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb5">'.PARAC_USER35.'</label>
				<input type="hidden" name="affgroupe" id="hidden_trois'.$userup['id'].'choixcb5" value="">';
				if(!isset($specdroits[12])) {
					$specdroits[12]='def';
				}
				echo '<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb12" data-variable="'.$specdroits[12].'" ';
				if($check_freq == 3 || $check_freq == 0 || $check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb12">'.PARAC_USER45.'</label>
				<input type="hidden" name="freqinscritsafter" id="hidden_trois'.$userup['id'].'choixcb12" value="">';
				if(!isset($specdroits[13])) {
					$specdroits[13]='def';
				}
				echo '<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb13" data-variable="'.$specdroits[13].'" ';
				if($check_freq == 3 || $check_freq == 0 || $check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb13">'.PARAC_USER46.'</label>
				<input type="hidden" name="inscriptallday" id="hidden_trois'.$userup['id'].'choixcb13" value="">';

				if(!isset($specdroits[11])) {
					$specdroits[11]='def';
				}
				echo '<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb11" data-variable="'.$specdroits[11].'" ';
				if($check_freq == 3 || $check_freq == 0 || $check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb11">'.PARAC_USER44.'</label>
				<input type="hidden" name="accesjournal" id="hidden_trois'.$userup['id'].'choixcb11" value="">
				<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb6" data-variable="'.$specdroits[6].'" ';
				if($check_freq == 3 || $check_freq == 0 || $check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb6">'.PARAC_USER36.'</label>
				<input type="hidden" name="journalmodif" id="hidden_trois'.$userup['id'].'choixcb6" value="">
				<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb7" data-variable="'.$specdroits[7].'" ';
				if($check_freq == 3 || $check_freq == 0 || $check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb7">'.PARAC_USER37.'</label>
				<input type="hidden" name="buttonsor" id="hidden_trois'.$userup['id'].'choixcb7" value="">
				<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb8" data-variable="'.$specdroits[8].'" ';
				if($check_freq == 3 || $check_freq == 0 || $check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb8">'.PARAC_USER38.'</label>
				<input type="hidden" name="buttonexc" id="hidden_trois'.$userup['id'].'choixcb8" value="">
				</div>
				<div class="'.$disabledClass1.'">';
				if(!isset($specdroits[16])) {
					$specdroits[16]='def';
				}
				echo '<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb16" data-variable="'.$specdroits[16].'" ';
				if($check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb16">'.FREQC_PARAM130.'</label>
				<input type="hidden" name="freqcodeonly" id="hidden_trois'.$userup['id'].'choixcb16" value="">';
				if(!isset($specdroits[15])) {
					$specdroits[15]='def';
				}
				echo '<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb15" data-variable="'.$specdroits[15].'" ';
				if($check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb15">'.FREQC_PARAM129.'</label>
				<input type="hidden" name="freqvalidmes" id="hidden_trois'.$userup['id'].'choixcb15" value="">';
				if(!isset($specdroits[14])) {
					$specdroits[14]='def';
				}
				echo '<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb14" data-variable="'.$specdroits[14].'" ';
				if($check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb14">'.PARAC_USER48.'</label>
				<input type="hidden" name="freqonlyresas" id="hidden_trois'.$userup['id'].'choixcb14" value="">';

				if(!isset($specdroits[18])) {
					$specdroits[18]='def';
				}
				echo '<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb18" data-variable="'.$specdroits[18].'" ';
				if($check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb18">'.PARAC_USER53.'</label>
				<input type="hidden" name="nobuttonenlever" id="hidden_trois'.$userup['id'].'choixcb18" value="">';

				if(!isset($specdroits[17])) {
					$specdroits[17]='def';
				}
				if($check_freq=='0') {
					echo '<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb17" data-variable="'.$specdroits[17].'" ';
					echo '>
					<label for="trois'.$userup['id'].'choixcb17">'.PARAC_USER50.'</label>
					<input type="hidden" name="affresa_norights" id="hidden_trois'.$userup['id'].'choixcb17" value="">';
				}
				echo '<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb9" data-variable="'.$specdroits[9].'" ';
				if($check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb9">'.PARAC_USER39.'</label>
				<input type="hidden" name="retardsaff1" id="hidden_trois'.$userup['id'].'choixcb9" value="">
				<br><input type="checkbox" class="troischoixcb" id="trois'.$userup['id'].'choixcb10" data-variable="'.@$specdroits[10].'" ';
				if($check_freq == 1) {
					echo 'disabled';
				}
				echo '>
				<label for="trois'.$userup['id'].'choixcb10">'.PARAC_USER40.'</label>
				<input type="hidden" name="retardsaff2" id="hidden_trois'.$userup['id'].'choixcb10" value=""><hr style="border:1px dashed grey"></div>
				</div>

				<div class="checkbox-disable"><span class="info-tooltip">'.PARAC_USER41.'</span></div>
		        </label>
				</td></tr>';
			echo '</table>';
			echo '<p class="paddvalid1"><input class="bouton boutparam" type="submit" value="'.MODIFY.'" name="userup'.$userup['id'].'" />&nbsp; <input class="bouton boutparam" type="submit" value="'.SUPPR.'" name="userdel'.$userup['id'].'" /></p>';
			
			echo '</form>';

			// Gestion des modifications et suppressions
			if(isset($_POST['userup'.$userup['id'].''])) {
				// Récupération des données
				$user_id = $userup['id'];
				$nom = mysqli_real_escape_string($bdd,$_POST['nom']);
				$prenom = mysqli_real_escape_string($bdd,$_POST['prenom']);
				$login = mysqli_real_escape_string($bdd,$_POST['login']);
				$mail = mysqli_real_escape_string($bdd,$_POST['mail']);
				if(isset($_POST['evald'])) { $evald = 2; } else { $evald = 1; }
				if(isset($_POST['editd'])) { $editd = 2; } else { $editd = 1; }
				if(isset($_POST['edtd2'])) { $edtd = 2; }
				else if(isset($_POST['edtd1'])&&!isset($_POST['edtd2'])) { $edtd = 3; }
				else { $edtd = 1; }
				if(isset($_POST['freqd3'])) { $freqd = 2; } // administration fréq
				else if(isset($_POST['freqd1'])&&!isset($_POST['freqd2'])) { $freqd = 4; } //inscription fréq
				else if(!isset($_POST['freqd1'])&&isset($_POST['freqd2'])) { $freqd = 5; } //réservation fréq
				else if(isset($_POST['freqd1'])&&isset($_POST['freqd2'])) { $freqd = 6; } //inscription et réservation fréq
				else if(isset($_POST['freqd0'])&&!isset($_POST['freqd1'])&&!isset($_POST['freqd2'])) { $freqd = 3; } // consult. fréq
				else { $freqd = 1; }
				if(isset($_POST['freqd4'])) { $freqd = 0; }
				if(isset($_POST['paramd'])) { $paramd = 2; } else { $paramd = 1; }
				if(isset($_POST['dbd2'])) { $dbd = 2; }
				else if(isset($_POST['dbd1'])&&!isset($_POST['dbd2'])) { $dbd = 3; }
				else { $dbd = 1; }
				if($evald.$editd.$edtd.$freqd.$paramd.$dbd=='222222') { // gestion provisoire (9) du droit complémentaire
					$sid = '2';
				}
				else { $sid = '0'; }
				if($freqd == 0) {
					if($edtd == 3) {
						$droits = '1130110';
					}
					else {
						$droits = '1110110';
					}
				}
				else {
					$droits = $evald.$editd.$edtd.$freqd.$paramd.$dbd.$sid;
				}
				$check_admin1 = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_user WHERE droits LIKE '%20' OR  droits LIKE '%21' OR  droits LIKE '%22' OR  droits LIKE '%23' OR  droits LIKE '%24' OR  droits LIKE '%25'"));
				$check_admin2 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_user WHERE id = '".$user_id."'"));
				if($check_admin1==1&&substr($check_admin2['droits'],4,1)==2&&substr($droits,4,1)!=2) {
					$modif = 0; $_SESSION['blockmodif']=PARAC_USER42; 
					$droits = $check_admin2['droits'];
				} 
				else { $modif = 1; }
				if(!isset($_POST['langue'])) {
					$langue = $userup['lang'];
				}
				else {
					$langue = $_POST['langue'];
				}
				$accueil = $_POST['accueil'];
				if($espopt['valeur']==0) { $defaultespace=""; $align_espace="h"; } else { $defaultespace=$_POST['defaultespace']; $align_espace=$_POST['align_espace'];}
				$align_activ=$_POST['align_activ'];
				if(isset($_POST['edtbut'])) { $edtbut = 1; } else { $edtbut = 0; }
				if(isset($_POST['connected'])) { $connected = 1; } else { $connected = 0; }
				if(isset($_POST['tchat'])) { $tchat = 1; } else { $tchat = 0; }
				if(isset($_POST['auth_mail'])) { $auth_mail = 1; } else { $auth_mail = 0; }
				// Vérification que l'accueil soit possible en accès (sinon accueil vide -> gestion dans db/droits)
				if($accueil==1&&$evald==1) { $accueil = ""; }
				if($accueil==2&&$editd==1) { $accueil = ""; }
				if($accueil==3&&$edtd==1) { $accueil = ""; }
				if($accueil==4&&$freqd==1) { $accueil = ""; }
				if($accueil==5&&$paramd==1) { $accueil = ""; }
				if($accueil==6&&$dbd==1) { $accueil = ""; }
				// Vérification de code élève pour éviter le doublon
				$reserv_exist = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE code = '$login' OR id_constr = '$login' OR id_perso = '$login'"));
				if($_POST['dayone']!=""&&$_POST['daytwo']!="") {
					$daymax = $_POST['dayone'].$_POST['daytwo'];
				}
				else {
					$daymax = $_POST['daymax1'];
				}

				// Droits spécifiques en fréquentation :
				$minutes = $_POST['minutes'] ?? 'def'; // POUR [0]
				$nbcren = $_POST['nbcren'] ?? 'def'; // POUR [1]
				$inscprec = $_POST['inscprec'] ?? 'def'; // POUR [2]
				$crenchoix = $_POST['crenchoix'] ?? 'def'; // POUR [3]
				$classinscript = $_POST['classinscript'].@$_POST['ordgroup'] ?? 'def'; // POUR [4]
				$affgroupe = $_POST['affgroupe'] ?? 'def'; // POUR [5]
				$journalmodif = $_POST['journalmodif'] ?? 'def'; // POUR [6]
				$buttonsor = $_POST['buttonsor'] ?? 'def'; // POUR [7]
				$buttonexc = $_POST['buttonexc'] ?? 'def'; // POUR [8]
				$retardsaff1 = $_POST['retardsaff1'] ?? 'def'; // POUR [9]
				if($retardsaff1!=1) {
					$retardsaff2 = $_POST['retardsaff2'] ?? 'def'; // POUR [10]
				}
				else {
					$retardsaff2  = '0';
				}
				$accesjournal = $_POST['accesjournal'] ?? 'def'; // POUR [11]
				$freqinscritsafter = $_POST['freqinscritsafter'] ?? 'def'; // POUR [12]
				$inscriptallday = $_POST['inscriptallday'] ?? 'def'; // POUR [13]
				$freqonlyresas = $_POST['freqonlyresas'] ?? 'def'; // POUR [14]
				$freqvalidmes = $_POST['freqvalidmes'] ?? 'def'; // POUR [15]
				$freqcodeonly = $_POST['freqcodeonly'] ?? 'def'; // POUR [16]
				$affresa_norights = $_POST['affresa_norights'] ?? 'def'; // POUR [17]
				$nobuttonenlever = $_POST['nobuttonenlever'] ?? 'def'; // POUR [18]
				$droitsspechaine = $minutes.';'.$nbcren.';'.$inscprec.';'.$crenchoix.';'.$classinscript.';'.$affgroupe.';'.$journalmodif.';'.$buttonsor.';'.$buttonexc.';'.$retardsaff1.';'.$retardsaff2.';'.$accesjournal.';'.$freqinscritsafter.';'.$inscriptallday.';'.$freqonlyresas.';'.$freqvalidmes.';'.$freqcodeonly.';'.$affresa_norights.';'.$nobuttonenlever;
				// Mise à jour
				if($_POST['mdp']!="") {
					$mdp = mysqli_real_escape_string($bdd,$_POST['mdp']);
					$hashed_password = password_hash($mdp, PASSWORD_DEFAULT);
					$sel = '0';					
					if($reserv_exist==0&&$login!='reserver') {
						mysqli_query($bdd,"UPDATE ".$prefix."_user SET nom='".$nom."', prenom='".$prenom."', login='".$login."', mdp='".$hashed_password."', sel = '".$sel."', mail='".$mail."', droits='".$droits."', accueil='".$accueil."', droits_spe = '".$droitsspechaine."', edt='".$edtbut."', connected_info='".$connected."', espace_default='".$defaultespace."', align_activ='".$align_activ."', align_espace='".$align_espace."', auth_mail='".$auth_mail."', auth_chat='".$tchat."', freq_res_dureemax='".$daymax."', lang='".$langue."' WHERE id = '".$user_id."'");
					}
					else {
						if($login=='reserver') {
							$_SESSION['signal'] = '<p class="signal">'.PARAC_USER3.'</p>';
						}
						else {
							$_SESSION['signal'] = '<p class="signal">'.PARAC_USER4.'</p>';
						}
						mysqli_query($bdd,"UPDATE ".$prefix."_user SET nom='".$nom."', prenom='".$prenom."', mdp='".$hashed_password."', sel = '".$sel."', mail='".$mail."', droits='".$droits."', accueil='".$accueil."', droits_spe = '".$droitsspechaine."', edt='".$edtbut."', connected_info='".$connected."', espace_default='".$defaultespace."', align_activ='".$align_activ."', align_espace='".$align_espace."', auth_mail='".$auth_mail."', auth_chat='".$tchat."', freq_res_dureemax='".$daymax."', lang='".$langue."' WHERE id = '".$user_id."'");
					}
				}
				else {
					if($reserv_exist==0&&$login!='reserver') {
						mysqli_query($bdd,"UPDATE ".$prefix."_user SET nom='".$nom."', prenom='".$prenom."', login='".$login."', mail='".$mail."', droits='".$droits."', accueil='".$accueil."', droits_spe = '".$droitsspechaine."', edt='".$edtbut."', connected_info='".$connected."', espace_default='".$defaultespace."', align_activ='".$align_activ."', align_espace='".$align_espace."', auth_mail='".$auth_mail."', auth_chat='".$tchat."', freq_res_dureemax='".$daymax."', lang='".$langue."' WHERE id = '".$user_id."'");
					}
					else {
						if($login=='reserver') {
							$_SESSION['signal'] = '<p class="signal">'.PARAC_USER3.'</p>';
						}
						else {
							$_SESSION['signal'] = '<p class="signal">'.PARAC_USER4.'</p>';
						}
						mysqli_query($bdd,"UPDATE ".$prefix."_user SET nom='".$nom."', prenom='".$prenom."', mail='".$mail."', droits='".$droits."', accueil='".$accueil."', droits_spe = '".$droitsspechaine."', edt='".$edtbut."', connected_info='".$connected."', espace_default='".$defaultespace."', align_activ='".$align_activ."', align_espace='".$align_espace."', auth_mail='".$auth_mail."', auth_chat='".$tchat."', freq_res_dureemax='".$daymax."', lang='".$langue."' WHERE id = '".$user_id."'");
					}
				}
				if($_SESSION['user_id']==$user_id) {
					$_SESSION['user_droits'] = $droits;
					$_SESSION['align_espace']=$align_espace;
					$_SESSION['align_activ']=$align_activ;
					$_SESSION['connected_info']=$_POST['connected'];
					$_SESSION['auth_mail']=$_POST['auth_mail'];
					$_SESSION['auth_chat']=$_POST['tchat'];
					$_SESSION['freq_res_dureemax']=$daymax;
					$_SESSION['minutes'] = $minutes;
					$_SESSION['nbcren'] = $nbcren;
					$_SESSION['inscprec'] = $inscprec;
					$_SESSION['crenchoix'] = $crenchoix;
					$_SESSION['classinscript'] = $classinscript;
					$_SESSION['affgroupe'] = $affgroupe;
					$_SESSION['journalmodif'] = $journalmodif;
					$_SESSION['buttonsor'] = $buttonsor;
					$_SESSION['buttonexc'] = $buttonexc;
					$_SESSION['retardsaff1'] = $retardsaff1;
					$_SESSION['retardsaff2'] = $retardsaff2;
					$_SESSION['accesjournal'] = $accesjournal;
					$_SESSION['freqinscritsafter'] = $freqinscritsafter;
					$_SESSION['inscriptallday'] = $inscriptallday;
					$_SESSION['freqonlyresas'] = $freqonlyresas;
					$_SESSION['freqvalidmes'] = $freqvalidmes;
					$_SESSION['freqcodeonly'] = $freqcodeonly;
					$_SESSION['affresa_norights'] = $affresa_norights;
					$_SESSION['lang'] = $langue;
				}
				?><script language="Javascript">
					<!--
					document.location.replace("param.php?page=users");
					// -->
				</script>
				<?php			
			}
			if(isset($_POST['userdel'.$userup['id'].''])) {
				$user_id = $userup['id'];
				$verifedt = mysqli_query($bdd,"SELECT * FROM ".$prefix."_userevent WHERE user_id = '".$user_id."'");
				$verifedt_nb = mysqli_num_rows($verifedt);
				if($verifedt_nb>0) {
					echo '<form method="POST" name="confirmsup" action="">'.PARAC_USER43.'
					<input class="bouton boutparam" type="submit" value="'.$parac_user_opt[2].'" name="confdel'.$userup['id'].'" /> <input class="bouton boutparam" type="submit" value="'.$parac_user_opt[1].'" name="back'.$userup['id'].'" /></form>';
				}
				else if($verifedt_nb==0) {
					mysqli_query($bdd,"DELETE FROM ".$prefix."_user WHERE id = '".$user_id."'");
					?><script language="Javascript">
					<!--
					document.location.replace("param.php?page=users");
					// -->
					</script><?php
				}
			}
			if(isset($_POST['confdel'.$userup['id'].''])) {
				$user_id = $userup['id'];
				mysqli_query($bdd,"DELETE FROM ".$prefix."_user WHERE id = '".$user_id."'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE user_id = '".$user_id."'");
				?><script language="Javascript">
					<!--
					document.location.replace("param.php?page=users");
					// -->
				</script><?php
			}
			if(isset($_POST['back'.$userup['id'].''])) {
				?><script language="Javascript">
					<!--
					document.location.replace("param.php?page=users");
					// -->
				</script><?php
			}
		}
		echo '</div>';
		?><script language="Javascript">
			function showPass(element) {
			    var passwordField = element.previousElementSibling;
			    var eyeIcon = element;
			    if (passwordField.type === "password") {
			      passwordField.type = "text";
			      eyeIcon.classList.remove("fa-eye");
			      eyeIcon.classList.add("fa-eye-slash");
			    } else {
			      passwordField.type = "password";
			      eyeIcon.classList.remove("fa-eye-slash");
			      eyeIcon.classList.add("fa-eye");
			    }
			  }
		</script>
		<script>
        document.addEventListener('DOMContentLoaded', () => {
            const checkboxes = document.querySelectorAll('.troischoixcb');

            checkboxes.forEach(checkbox => {
                const value = checkbox.getAttribute('data-variable');                

                // Initialise l'état de chaque checkbox en fonction de la variable associée
                switch (value) {
                    case 'def':
                        checkbox.dataset.state = "indeterminate";
                        checkbox.indeterminate = true;
                        checkbox.checked = false;
                        break;
                    case '1':
                        checkbox.dataset.state = "checked";
                        checkbox.indeterminate = false;
                        checkbox.checked = true;
                        break;
                    case '0':
                        checkbox.dataset.state = "unchecked";
                        checkbox.indeterminate = false;
                        checkbox.checked = false;
                        break;
                    default:
                        break;
                }

                checkbox.addEventListener('change', () => {
                    let currentState = checkbox.dataset.state;

                    switch (currentState) {
                        case "indeterminate":
                            checkbox.dataset.state = "checked";
                            checkbox.indeterminate = false;
                            checkbox.checked = true;
                            break;
                        case "checked":
                            checkbox.dataset.state = "unchecked";
                            checkbox.indeterminate = false;
                            checkbox.checked = false;
                            break;
                        case "unchecked":
                            checkbox.dataset.state = "indeterminate";
                            checkbox.indeterminate = true;
                            checkbox.checked = false;
                            break;
                        default:
                            break;
                    }

                    // Met à jour le champ caché associé
                    const hiddenField = document.getElementById(`hidden_${checkbox.id}`);
                    switch (checkbox.dataset.state) {
                        case "indeterminate":
                            hiddenField.value = 'def';
                            break;
                        case "checked":
                            hiddenField.value = '1';
                            break;
                        case "unchecked":
                            hiddenField.value = '0';
                            break;
                    }
                });

                // Met à jour la valeur du champ caché lors du chargement
                const hiddenField = document.getElementById(`hidden_${checkbox.id}`);
                switch (checkbox.dataset.state) {
                    case "indeterminate":
                        hiddenField.value = 'def';
                        break;
                    case "checked":
                        hiddenField.value = '1';
                        break;
                    case "unchecked":
                        hiddenField.value = '0';
                        break;
                }
            });
        });
    </script>
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const checkboxes = document.querySelectorAll('.checkboxexplain');

            checkboxes.forEach(checkbox => {
                // Définir la checkbox comme indéterminée
                checkbox.indeterminate = true;
                checkbox.checked = false;

                // Empêcher la modification du checkbox
                checkbox.addEventListener('click', (e) => {
                    e.preventDefault();
                    e.stopPropagation();
                });

                // Pour assurer que l'état ne change pas visuellement
                checkbox.addEventListener('mousedown', (e) => {
                    e.preventDefault();
                });
            });
        });
    </script>
    <?php
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php">'.ACCUEIL.'</a></p>';
}
?>
