<?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">
		#mpa1 { background-color: #fff; }
		#mpa1 td { border: 3px #7D7777 solid; }
		</style><?php
		// Titre de la page
		echo PARAM_IF1;
		
		$langdef = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'language'"));
		$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];
		        }
		    }
		}
		if (count($languages) > 1):
			echo PARAM_IF2;
			echo '<div class="box"><select name="langue" id="languagedef" value="">';
			foreach ($languages as $code => $name): 
	            echo '<option value="'.htmlspecialchars($code).'"';
	            if($code == $langdef['valeur']) {
	            	echo ' selected ';
	            }
	            echo '>'.htmlspecialchars($name).'</option>';
	        endforeach;
	        echo '</select></div><input class="bouton boutparam paddvalid1" onclick="langv(this);colorval(\'langv\');" id="langv" type="submit" value="'.VALID.'" name="langmenu" /></p>';
			echo '<script type="text/javascript">
				function langv(param) {
				var param = "language";
				var vall = document.getElementById("languagedef").value;
				$.ajax({
				    type: "POST",
				    data: { param: param, vall: vall },
				    url: "js/idl/param_syn.php",
				   });
				}
			</script>';
		endif;
		//Colors_array
		echo PARAM_IF3;
		$color = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'colors_stats_array'"));
		$j=0;
		echo '<table class="paramform">';
		for($i=0;$i<20;$i++) {
			echo '<tr><td>'.COLOR1.' n°'.$i.'</td>';
			echo '<td><input type="color" id="couleur'.$i.'" name="couleur'.$i.'" maxlength="30" value="'.substr($color['valeur'],$j,7).'" />';
			echo '</td></tr>';
			$j+=7;
		}
		echo '<tr><td colspan="2"><input class="bouton boutparam" type="submit" onclick="colorstatsdef(this);colorval(\'coldefarray\');" id="coldefarray" value="'.COLOR2.'" name="coldefarray" />&nbsp; <input class="bouton boutparam" type="submit" class="preins" onclick="colorstatsmod(this);colorval(\'colorsarray\');" id="colorsarray" value="'.VALID.'" name="colorsarray" /></td></tr>
		</table>';
		$tabdef = '#FF9900#99FF00#CCFFFF#FF66FF#CC6600#FFFF33#99CC66#003399#9900FF#FF0033#CCCC00#66CC66#CCFFFF#333366#CC99CC#330033#FF6666#993300#33CC33#3399FF';
		echo '<script type="text/javascript">
			function colorstatsdef(param) {
			var param = "colorstatsdef";';
			$init = 0;
			for($i=0;$i<20;$i++) {
				echo 'document.getElementById("couleur'.$i.'").value = "'.substr($tabdef,$init,7).'";';
				$init = $init+7;
			}
			echo '$.ajax({
			    type: "POST",
			    data: { param: param },
			    url: "js/idl/param_syn.php",
			   });
			}
		</script>';
		echo '<script type="text/javascript">
			function colorstatsmod(param) {
			var param = "colorstatsmod";';
			for($i=0;$i<20;$i++) {
				echo 'c'.$i.' = document.getElementById("couleur'.$i.'").value;';
			}
			echo '$.ajax({
			    type: "POST",
			    data: { param: param';
			    for($i=0;$i<20;$i++) {
			    	echo ', c'.$i.': c'.$i;
			    }
			    echo ' },
			    url: "js/idl/param_syn.php",
			   });
			}
		</script>';

		//Info_menu
		echo '<br>'.PARAM_IF4;
		$imenuopt_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'info_menu'"));
		echo '<p>'.PARAM_IF5;
		echo '<br><input type="radio" name="imenu" id="imenua" value="1" ';
		if($imenuopt_info['valeur']=="1") { echo 'checked '; }
		echo '"/><label for="imenua">'.ACTIVED.'</label>
		<br><input type="radio" name="imenu" id="imenud" value="0" ';
		if($imenuopt_info['valeur']=="0") { echo 'checked '; }
		echo '"/><label for="imenud">'.DESACTIVED.'</label>
		<br><input class="bouton boutparam paddvalid1" onclick="infodb(this);colorval(\'infodb\');" id="infodb" type="submit" value="'.VALID.'" name="infomenu" /></p>';
		echo '<script type="text/javascript">
			function infodb(param) {
			var param = "infobulles";
			var typ = document.getElementsByName("imenu");              
            for(i = 0; i < typ.length; i++) {
                if(typ[i].checked)
                var typc = typ[i].value;
            }
			$.ajax({
			    type: "POST",
			    data: { param: param, typc: typc },
			    url: "js/idl/param_syn.php",
			   });
			}
		</script>';

		//Méthode mailing
		echo '<br>'.PARAM_IF6;
		$mail_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'mail_method'"));
		echo '<input type="text" class="inptxt" id="method" name="method" size="50" value="'.$mail_info['valeur'].'" />
		<p> '.PARAM_IF7.'<br><input class="bouton boutparam paddvalid1" onclick="melmeth(this);colorval(\'melmeth\');" id="melmeth" type="submit" value="'.VALID.'" name="mailmethod" /></p>';
		echo '<script type="text/javascript">
			function melmeth(param) {
			var param = "mailing";
			var mailmethod = document.getElementById("method").value;
			$.ajax({
			    type: "POST",
			    data: { param: param, mailmethod: mailmethod },
			    url: "js/idl/param_syn.php",
			   });
			}
		</script>';

		echo '<script type="text/javascript">
		function colorval(divs) {
			var couleur = "black 2px solid";
			var couleurw = "white 2px solid";
			var color = "black";
			var colorw = "white";			    
            document.getElementById(divs).style.border = couleur;
			document.getElementById(divs).style.color = color;
			setTimeout(() => {  
				document.getElementById(divs).style.color = colorw; 
				document.getElementById(divs).style.border = couleurw;
				}, 2000);
			}
		</script>';
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php">'.ACCUEIL.'</a></p>';
}
?>