<?php
// Affichage des briques :
echo '<div id="edt_briques" name="noprint">';
echo '<table id="affbriks"><tr>';
if($num_week==1||$num_week=='01') {
	$an_prev=($annee-1);
	$sem_prev = date("oW", mktime(0,0,0,12,28,$an_prev));
	echo '<td><a href="edt.php?page=create&week='.$sem_prev.'"><i class="fas fa-arrow-alt-circle-left arrows_edt"></i></a></td>';
}
else {
	echo '<td><a href="edt.php?page=create&week='.($week-1).'"><i class="fas fa-arrow-alt-circle-left arrows_edt"></i></a></td>';
}
echo '<td> <b>'.$num_week.'</b> </td>';
if($fin_m==1||$fin_m==12) {
	if(($sem_test_y==$fin_y&&$fin_j>28&&$fin_m!=1)||($fin_y>$sem_test_y)) {
		$an_next=($annee+1);
		$sem_next = date("oW", mktime(0,0,0,01,04,$an_next));
		echo '<td><a href="edt.php?page=create&week='.$sem_next.'"><i class="fas fa-arrow-alt-circle-right arrows_edt"></i></a></td>';
	}
	else {
		echo '<td><a href="edt.php?page=create&week='.($week+1).'"><i class="fas fa-arrow-alt-circle-right arrows_edt"></i></a></td>';
	}
}													
else {
	echo '<td><a href="edt.php?page=create&week='.($week+1).'"><i class="fas fa-arrow-alt-circle-right arrows_edt"></i></a></td>';
}
echo '</tr></table>';

$allbriks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik");
$nb_allbriks = mysqli_num_rows($allbriks);
// script pour la mise en valeur des briques sélectionnées
echo '<script>
var highlight_color = "red";

function toggle_highlight(id) {
	var cadres = document.getElementsByTagName(\'div\');

	for (var i = 0; i < cadres.length; i++) {
		var cadre = cadres[i];

		cadre.style.borderColor = cadre.id == id ? highlight_color : \'grey\';
	}
}
</script>';
// pour vider un créneau
echo '<div class="cartclic" onclick="brik_contenu=this.innerHTML;"><div id="0" class="cartouche" style="background-color:#eee" name="0" onClick="toggle_highlight(this.id);"><span class="txtlibre_1">'.EDT_BRIK1.'</span></div></div>';
echo '<br />&nbsp; <br />';
// avec du texte libre
$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre != '' AND activ_brik = 1 ORDER BY textelibre ASC");
$nb_brikstl = mysqli_num_rows($briks);
foreach($briks as $briksup) {
	if($briksup['close'] == '1') {
		$bgcolor = substr($cren_colors['valeur'],7,7);
	}
	else {
		if($colorsys_info['valeur']=='niv'&&$colortex_info['valeur']=='1') {
			$colorniv = substr($briksup['textelibre'],0,1);
			if($colorniv!='6'&&$colorniv!='5'&&$colorniv!='4'&&$colorniv!='3'&&$colorniv!='2'&&$colorniv!='1'&&$colorniv!='t'&&$colorniv!='T'&&$colorniv!='a') { $nocolniv = '1'; }
			else {
				$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
				$color_info = mysqli_fetch_assoc($color);
				$bgcolor = $color_info['bgcolor'];
				$nocolniv = '0';
			}
		}
		else { $nocolniv = '1'; }
		if($nocolniv=='1') {
			$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = 'r'");
			$color_info = mysqli_fetch_assoc($color);
			$bgcolor = $color_info['bgcolor'];
		}
	}
	if($_SESSION['user_droits'][2]==2) {
		echo '<div class="cartclic" onclick="brik_contenu=this.innerHTML;"><div id="brik'.$briksup['brik_id'].'" class="cartouche" style="background-color:'.$bgcolor.';" name="'.$briksup['brik_id'].'" onClick="toggle_highlight(this.id);">';
		if(strlen($briksup['textelibre'])>16) { echo '<span class="txtlibre_2">'; }
		else { echo '<span class="txtlibre_1">'; }
		echo $briksup['textelibre'].'</span></div></div>';
		echo '<br>&nbsp; <br>';
	}
}
// avec des classes
$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre = '' AND activ_brik = 1 ORDER BY division ASC");
foreach($briks as $briksup) {
	$seq = mysqli_query($bdd,"SELECT seq_id, seq_nom, SUBSTR(seq_nom,1,13) FROM ".$prefix."_sequences WHERE seq_id ='".$briksup['seq_id']."'");
	$seq_info = mysqli_fetch_assoc($seq);
	$seq_nomaff = @mb_ereg_replace("[?]","",mb_convert_encoding($seq_info['SUBSTR(seq_nom,1,13)'],"utf-8"));
	
	if($colorsys_info['valeur']=='niv') {
		$colorniv = null;
		if($nivs_info['valeur']=="PER") { 
			foreach ($sorted_caracs as $prefixe => $niveau) {
			    // Si la division commence par ce préfixe, on a trouvé le niveau
			    if (strpos($briksup['division'], $prefixe) === 0) {
			        $colorniv = $niveau;
			        break; // On arrête la boucle une fois le niveau trouvé
			    }
			}
			if($colorniv!==null&&@$colorniv!='r') {
				$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
				$color_info = mysqli_fetch_assoc($color);
				$bgcolor = $color_info['bgcolor'];
			}
		}
		else {
			$colorniv = substr($briksup['division'],0,1);
			if($colorniv!='6'&&$colorniv!='5'&&$colorniv!='4'&&$colorniv!='3'&&$colorniv!='2'&&$colorniv!='1'&&$colorniv!='t'&&$colorniv!='T') { $colorniv = 'a'; }
			$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
			$color_info = mysqli_fetch_assoc($color);
			$bgcolor = $color_info['bgcolor'];
		}
	}
	else if($colorsys_info['valeur']=='cod') {
		if(isset($briksup['code'])) {
			$color = mysqli_query($bdd,"SELECT bgcolor_code FROM ".$prefix."_edtcode WHERE code = '".$briksup['code']."'");
			$color_info = mysqli_fetch_assoc($color);
			$bgcolor = $color_info['bgcolor_code'];
		}
		else {
			$bgcolor = '#fff';
		}
	}
	if($bgcolor=='') { $bgcolor = "#ffffff"; }
	if($_SESSION['user_droits'][2]==2) {
		echo '<div class="cartclic" onclick="brik_contenu=this.innerHTML;">';
		include('./db/proc/edt_cartouche.php');
		echo '</div>';
		echo '<br>&nbsp; <br>';
	}
}
echo '</div>';
?>