<?php 
require_once(dirname(__DIR__, 1) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
echo '<meta charset="utf-8" />';
		
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][5]!="2")
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="../index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?><style type="text/css">
		#mda7 { background-color: #fff; }
		#mda7 td { border: 3px #086A87 solid; }
		#mda6 td { border: 0; }
		</style><?php
		echo '<h1>'.DATAC_COD1.'</h1>';
		//traitement global
		if(isset($_POST['bccreate'])||isset($_POST['bcregen'])) {
			require_once('./db/proc/database_code_values.php');
			$eleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves");
			include('./db/proc/database_code_gen.php');
		}
		if(isset($_POST['bccomplete'])) {
			require_once('./db/proc/database_code_values.php');
			$eleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE code = ''");
			include('./db/proc/database_code_gen.php');
		}
		//traitement individuel
		if(isset($_POST['modif'])) {
			//sélection de l'élève
			include('./db/proc/eleve_select.php');
			$txt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'barcodes_txt'"));
			$int = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'barcodes_int'"));
			$txtbc=$txt['valeur'];
			$intbc=$int['valeur'];
			$eleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE eleves_id = '".$eleve_info['eleves_id']."'");
			include('./db/proc/database_code_gen.php');
		}

		//Nombre d'élèves avec code
		$code_nb=mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE code != ''"));
		if($code_nb==0) {
			echo DATAC_COD2;
		}
		else {
			echo '<p>'.$code_nb.' élève';
			if($code_nb>1) { echo PLURIEL; }
			echo ' '.ON.' '.$nb_ele.' '.DATAC_COD3.'</p>';
		}

		echo DATAC_COD4;
		//formulaire
		echo DATAC_COD5;
		$txt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'barcodes_txt'"));
		$int = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'barcodes_int'"));
		echo '<form action="" method="POST" name="bcgen" class="padd2">
		'.DATAC_COD6.' <input type="text" class="inptxt" maxlength="5" name="txtbc" value="'.$txt['valeur'].'" /><br>
		'.DATAC_COD7.' <input type="number" min="5" max="9" name="intbc" value="'.$int['valeur'].'" /><br>';
		if((($nb_ele-$code_nb)>0)&&($code_nb!=0)) { 
			echo ' <input class="bouton boutdb" type="submit" name="bccomplete" value="'.COMPLEAT.'" />'; 
		} 
		if($code_nb==0) {
			echo ' <input class="bouton boutdb" type="submit" name="bccreate" value="'.DATAC_COD8.'" />';
		}
		if($code_nb>0) {
			echo ' <input class="bouton boutdb" type="submit" name="bcregen" value="'.DATAC_COD9.'" />';
		}

		echo '</form>';

		//Réinitialisation possible par élèves
		echo '<br>'.DATAC_COD10;

		echo '<form method="POST" name="eleve" action="" class="padd4">
			<table class="noborder">
				<tr>
					<td><label for="eleves">'.NAMECH.'</label></td>
					<td>
					  <input type="text" autocomplete="off" aria-autocomplete="none" class="inptxt" name="eleve" size="40" id="eleves" value="">
					</td>
				</tr>
				<tr><td colspan="2" style="text-align:right;"><input class="bouton boutdb" type="submit" value="'.DATAC_COD11.'" name="modif" /></td></tr>
			</table>
		</form>';

		if($code_nb!=0) {
			echo DATAC_COD12;
			//traitement de création d'un modèle
			if(isset($_POST['modelenew'])) {
				// Récupération des données
				$nom = mysqli_real_escape_string($bdd,$_POST['nom']);
				$textspe = mysqli_real_escape_string($bdd, $_POST['textspe']);
				//Vérification
				if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_barcodes WHERE nom = '".$nom."'")) > 0) {
					echo DATAC_COD13;
				}
				else {
					// Enregistrement
					mysqli_query($bdd,"INSERT INTO ".$prefix."_barcodes (nom, typusage, format, nbwidth, nbheight, type, margextg, margextd, margexth, margextb, margintg, margintd, marginth, margintb, textup, textspe, fontsize) VALUES ('".$nom."', 'eleve', '".$_POST['format']."', '".$_POST['nbwidth']."', '".$_POST['nbheight']."', '".$_POST['type']."', '".$_POST['margextg']."', '".$_POST['margextd']."', '".$_POST['margexth']."', '".$_POST['margextb']."', '".$_POST['margintg']."', '".$_POST['margintd']."', '".$_POST['marginth']."', '".$_POST['margintb']."', '".$_POST['textup']."', '".$textspe."', '".$_POST['fontsize']."')");
				}
			}

			//formulaire pour choix du modèle et des élèves pour les code-barres à sortir
			$modeles = mysqli_query($bdd,"SELECT * FROM ".$prefix."_barcodes WHERE typusage = 'eleve' ORDER BY bcmodele_id ASC");
			echo '<form method="POST" action="./include/pdf_loaders/barcodes.php" name="bcimp">';
			$modelechoix = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'barcodes_default_modeleve'"));
			if(mysqli_num_rows($modeles)==1) {
				echo DATAC_COD14;
				echo '<div id=\'bc\' style=\'display:inline\'>';
				include('db/proc/database_select_bcmod.php');
				echo '</div><br>';
			}
			else if(mysqli_num_rows($modeles)>1) {
				echo "<p><b>".DATAC_COD15."</b> <div class='box'><select name='barcodes' id='barcodes' onchange='gocar()'>";
					if($modelechoix['valeur']!="1") {
						$modopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_barcodes WHERE nom = '".$modelechoix['valeur']."'"));
					}
					foreach ($modeles as $modinfo) {
						echo '<option value="'.$modinfo['bcmodele_id'].'"';
						if($modelechoix['valeur']!="1") {
							if($modelechoix['valeur']==$modinfo['bcmodele_id']) {
								echo ' selected ';
							}
						}
						echo '>'.$modinfo['nom'].'</option>';
					}
					echo '</select></div></p>';
				echo '<div id=\'bc\' style=\'display:inline\'>';
				include('db/proc/database_select_bcmod.php');
				echo '</div><br>';
			}
			echo '<script type="text/javascript">
 			function opt(id){
		        for( var i = 1; i <= 6; i++){
		            if(i == id){
		                document.getElementById(\'div_\'+ i).style.display = \'block\';
		            }
		            else {
		                document.getElementById(\'div_\'+ i).style.display =\'none\';
		            }
		            if(id < 3) {
		            	document.getElementById(\'divselect\').style.display =\'none\';
		            }
		            else {
		            	document.getElementById(\'divselect\').style.display =\'block\';
		            }
		        } 
			}
			</script>';
			echo DATAC_COD16;
			//tous
			echo '<input type="radio" name="elechoix" id="bctous" value="tous" checked onclick="opt(0)" /><label for="bctous">'.DATAC_LIST1.'</label>';
			echo '<div id="div_0" style="display:none;" ></div>';
			//classes
			echo '<br><input type="radio" name="elechoix" id="bcdivs" value="divs" onclick="opt(1)" /><label for="bcdivs">'.DATAC_LIST2.'</label>';
			echo '<div id="div_1" style="display:none;" ><br><i>'.DATAC_LIST3.'</i><br>';
			$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves GROUP BY division ORDER BY division ASC");
			$liste_division_nb = mysqli_num_rows($liste_division);
			$liste_groupe = mysqli_query($bdd,"SELECT groupe_id, groupe_nom FROM ".$prefix."_groupes WHERE groupe_actif = 1 GROUP BY groupe_nom ORDER BY groupe_nom ASC");
			$liste_groupe_nb = mysqli_num_rows($liste_groupe);
			echo '<select class="inptxt" name="division[]" multiple size="'.($liste_division_nb+$liste_groupe_nb).'">';
			while($listed = mysqli_fetch_assoc($liste_division)) {
				echo '<option>'.$listed['division'].'</option>';
			}
			while($listeg = mysqli_fetch_assoc($liste_groupe)) {
				echo '<option value="'.$listeg['groupe_id'].'">'.$listeg['groupe_nom'].'</option>';
			}
			echo '</select></div>';
			//élèves d'une classe
			echo '<br><input type="radio" name="elechoix" id="bcinclas" value="inclas" onclick="opt(3)" /><label for="bcinclas">'.DATAC_LIST4.'</label>';
			echo '<div id="div_3" style="display:none;" >';
			echo '<style>input[name="checkch"] { display:none; } span[name="noprint"] { display:none; }</style>';
			include('db/proc/elediv_select.php');
			echo '</div>';
			//certains élèves
			echo '<br><input type="radio" id="bcelechoix" name="elechoix" value="elev" onclick="opt(2)" /><label for="bcelechoix">'.DATAC_LIST5.'</label>';
			echo '<div id="div_2" style="display:none;" ><br><i>'.DATAC_LIST6.'</i> <textarea name="eleid" maxlength="300" rows="10" cols="60"></textarea></div>';
			//planche incomplète
			echo '<p>'.DATAC_COD17.' <input type="number" class="number" name="debut" value="1" /></p>';
			//validation
			echo '<br><input class="bouton boutdb" type="submit" name="genbarcodes" value="'.DATAC_COD18.'" />';
			echo '</form>';

			// formulaires de création modif et suppression des modèles
			echo '<br><br><a href="javascript:;" onmousedown="toggleDiv(\'divmodeles\');"><b>'.DATAC_COD19.'</b></a>';
			echo '<div id="divmodeles" style="display:none;">';
			echo '<form method="POST" name="modelenew" action="">';
			echo '<br><table class="paramform">';
				echo '<th colspan="2">'.DATAC_COD20.'</th><tr><td>'.NAME.DOTS.' <input type="text" name="nom" class="inptxt" maxlength="30" value="" /></td><td>'.DATAEXP3.' <div class="box"><select name="format"><option value="P">'.LANDPO.'</option><option value="L">'.LANDPA.'</option></select></div> <br><br>'.DATATYP1.' <div class="box"><select name="type"><option>C128B</option><option>EAN128B</option><option>EAN13</option><option>QR</option><option value="codeseul">'.DATATYP21.'</option><option value="nocode">'.DATATYP22.'</option></select></div></td></tr>
				<tr><td>'.DATATYP4.' <input type="number" name="nbwidth" value="1" min="1" /></td><td>'.DATATYP5.' <input type="number" name="nbheight" value="1" min="1" /></td></tr>
				<tr><td colspan="2">'.DATATYP6.' &nbsp; '.LEFT.' <input type="number" class="number" name="margextg" /> - '.RIGHT.' <input type="number" class="number" name="margextd" /> - '.TOP.' <input type="number" class="number" name="margexth" /> - '.BOTTOM.' <input type="number" class="number" name="margextb" /></td></tr>
				<tr><td colspan="2">'.DATATYP7.' '.LEFT.' <input type="number" class="number" name="margintg" /> - '.RIGHT.' <input type="number" class="number" name="margintd" /> - '.TOP.' <input type="number" class="number" name="marginth" /> - '.BOTTOM.' <input type="number" class="number" name="margintb" /></td></tr>
				<tr><td>'.DATATYP81.' <div class="box"><select name="textup"><option value="0">'.DATATYP811.'</option><option value="1">'.DATATYP812.'</option><option value="4">'.DATATYP813.'</option><option value="2">'.DATATYP814.'</option><option value="3">'.DATATYP815.'</option></select></div><td>'.DATATYP82.' <input type="text" class="inptxt" name="textspe" maxlength="50" value="" /><br><i>'.DATATYP84.'</i></td></tr>
				<tr><td>'.DATATYP83.' <input type="number" name="fontsize" /></td><td><div style="text-align:right;"><input class="bouton boutdb" type="submit" value="'.ADD.'" name="modelenew" /></div></td></tr>
			</table>';
			echo '</form>';
			if(mysqli_num_rows($modeles)>1){
				foreach($modeles as $modinfo) {
					if($modinfo['nom']!='default') {
						echo '<form method="POST" name="modeleup'.$modinfo['bcmodele_id'].'" action="">';
						$_SESSION['id'.$modinfo['bcmodele_id']] = $modinfo['bcmodele_id'];
						echo '<br><table class="paramform">';
						echo '<th colspan="2">'.$modinfo['bcmodele_id'].' (id.) - '.$modinfo['nom'].' :</th><tr><td>'.NAME.DOTS.' <input type="text" class="inptxt" name="nom" maxlength="30" value="'.$modinfo['nom'].'" /></td><td>'.DATAEXP3.' <div class="box"><select name="format"><option value="P"';
						if($modinfo['format']=='P') { echo ' selected '; }
						echo '>'.LANDPO.'</option><option value="L"';
						if($modinfo['format']=='L') { echo ' selected '; }
						echo '>'.LANDPA.'</option></select></div> <br><br>'.DATATYP1.' <div class="box"><select name="type"><option';
						if($modinfo['type']=='C128B') { echo ' selected '; }
						echo '>C128B</option><option';
						if($modinfo['type']=='EAN128B') { echo ' selected '; }
						echo '>EAN128B</option><option';
						if($modinfo['type']=='EAN13') { echo ' selected '; }
						echo '>EAN13</option><option';
						if($modinfo['type']=='QR') { echo ' selected '; }
						echo '>QR</option><option value="codeseul"';
						if($modinfo['type']=='codeseul') { echo ' selected '; }
						echo '>'.DATATYP21.'</option><option value="nocode"';
						if($modinfo['type']=='nocode') { echo ' selected '; }
						echo '>'.DATATYP22.'</option></select></div></td></tr>
						<tr><td>'.DATATYP4.' <input type="number" name="nbwidth" value="'.$modinfo['nbwidth'].'" min="1" /></td><td>'.DATATYP5.' <input type="number" name="nbheight" value="'.$modinfo['nbheight'].'" min="1" /></td></tr>
						<tr><td colspan="2">'.DATATYP6.' &nbsp; '.LEFT.' <input type="number" class="number" name="margextg" value="'.$modinfo['margextg'].'" /> - '.RIGHT.' <input type="number" class="number" name="margextd" value="'.$modinfo['margextd'].'" /> - '.TOP.' <input type="number" class="number" name="margexth" value="'.$modinfo['margexth'].'" /> - '.BOTTOM.' <input type="number" class="number" name="margextb" value="'.$modinfo['margextb'].'" /></td></tr>
						<tr><td colspan="2">'.DATATYP7.' '.LEFT.' <input type="number" class="number" name="margintg" value="'.$modinfo['margintg'].'" /> - '.RIGHT.' <input type="number" class="number" name="margintd" value="'.$modinfo['margintd'].'" /> - '.TOP.' <input type="number" class="number" name="marginth" value="'.$modinfo['marginth'].'" /> - '.BOTTOM.' <input type="number" class="number" name="margintb" value="'.$modinfo['margintb'].'" /></td></tr>
						<tr><td>'.DATATYP81.' <div class="box"><select name="textup">';
						$upcode = array('0','1','4','2','3');
						$upname = array(DATATYP811,DATATYP812,DATATYP813,DATATYP814,DATATYP815);
						for($i=0;$i<count($upcode);$i++) {
							echo '<option value="'.$upcode[$i].'"';
							if($modinfo['textup']==$upcode[$i]) {
								echo ' selected ';
							}
							echo '>'.$upname[$i].'</option>';
						}
						echo '</select></div><td>'.DATATYP82.' <input type="text" class="inptxt" name="textspe" maxlength="50" value="'.$modinfo['textspe'].'" /><br><i>'.DATATYP84.'</i></td></tr>
						<tr><td>'.DATATYP83.' <input type="number" name="fontsize" value="'.$modinfo['fontsize'].'" /></td><td><div style="text-align:right;"><input class="bouton boutdb" type="submit" value="'.MODIFY.'" name="modeleup'.$modinfo['bcmodele_id'].'" /> <input class="bouton boutdb" type="submit" value="'.SUPPR.'" name="modeledel'.$modinfo['bcmodele_id'].'" /></div></td></tr>
						</table>';
						echo '</form>';
						// Gestion des modifications et suppressions
						if(isset($_POST['modeleup'.$modinfo['bcmodele_id'].''])) {
							// Récupération des données
							$modele_id = $modinfo['bcmodele_id'];
							$nom = mysqli_real_escape_string($bdd,$_POST['nom']);
							$textspe = mysqli_real_escape_string($bdd, $_POST['textspe']);
							// Enregistrement
							mysqli_query($bdd,"UPDATE ".$prefix."_barcodes SET nom = '".$nom."', format = '".$_POST['format']."', nbwidth = '".$_POST['nbwidth']."', nbheight = '".$_POST['nbheight']."', type = '".$_POST['type']."', margextg = '".$_POST['margextg']."', margextd = '".$_POST['margextd']."', margexth = '".$_POST['margexth']."', margextb = '".$_POST['margextb']."', margintg = '".$_POST['margintg']."', margintd = '".$_POST['margintd']."', marginth = '".$_POST['marginth']."', margintb = '".$_POST['margintb']."', textup = '".$_POST['textup']."', textspe = '".$textspe."', fontsize = '".$_POST['fontsize']."' WHERE bcmodele_id = '".$modinfo['bcmodele_id']."'");
							echo '<script language="Javascript">
							<!--
							document.location.replace("database.php?page=barcodes");
							// -->
							</script>';
						}
						if(isset($_POST['modeledel'.$modinfo['bcmodele_id'].''])) {
							$modele_id = $modinfo['bcmodele_id'];
							if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'barcodes_default_modeleve' AND valeur = '".$modinfo['bcmodele_id']."'"))>0) {
								mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '1' WHERE param_nom = 'barcodes_default_modeleve'");
							}
							mysqli_query($bdd,"DELETE FROM ".$prefix."_barcodes WHERE bcmodele_id = '".$modele_id."'");
							echo '<script language="Javascript">
							<!--
							document.location.replace("database.php?page=barcodes");
							// -->
							</script>';
						}
					}
				}
			}
			echo '</div>';
		}
		echo "<script language='javascript'>
			window.onmousedown = function (e) {
		    var el = e.target;
		    if (el.tagName.toLowerCase() == 'option' && el.parentNode.hasAttribute('multiple')) {
		        e.preventDefault();

		        if (el.hasAttribute('selected')) el.removeAttribute('selected');
		        else el.setAttribute('selected', '');

		        var select = el.parentNode.cloneNode(true);
		        el.parentNode.parentNode.replaceChild(select, el.parentNode);
		    }
		}
		</script>";
	}	
}
else {
	include('include/noaccess.php');
	echo '<br><a href="../index.php">'.ACCUEIL.'</a></p>';
}
?>