<?php
$cren_colors = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_crencolors'"));

if(isset($_POST['weekpick'])) {
	$_GET['week'] = date("oW", strtotime($_POST['weekpick']));
	$_SESSION['weekpick'] = $_POST['weekpick'];
}
if(isset($_POST['backweek'])) {
	$_GET['week'] = date("oW");
	$_SESSION['weekpick'] = date('Y-m-d');
}

// Gestion des dates :
include('./db/proc/edt_week.php');

// Gestion impression et Gestion masquage des briques :
echo '<style type="text/css" media="print">
	body {
	webkit-transform: rotate(-90deg);
	-moz-transform: rotate(-90deg);
	filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=3);';
	if($nbJour==5) {
		// anciennes valeurs : 960 et 860
		echo 'height:960px;
		width:750px;';
	}
	else if($nbJour==6) {
		// anciennes valeurs : 1110 et 910
		echo 'height:1110px;
		width:888px;';
	}
	else if($nbJour==7) {
		// anciennes valeurs : 1160 et 1110
		echo 'height:1160px;
		width:1026px;';
	}	
	echo '}
</style>
<script language="javascript">
	function toggleDiv(divid){
		if(document.getElementById(divid).style.display == "none"){
			document.getElementById(divid).style.display = "block";
		}else{
			document.getElementById(divid).style.display = "none";
		}
	}
</script>';

// Récupération du texte enregistré pour ouverture hors séance :
$openmsg = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_openmsg'");
$openmsg_info = mysqli_fetch_assoc($openmsg);
$openmsg_txt = $openmsg_info['valeur'];

// Affichage de la semaine choisie :
echo '<div id="titreMois" name="noprint">
	<br><table class="edt_grille">
		<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="reserver.php?week='.$sem_prev.'"><i class="fas fa-arrow-alt-circle-left arrows_edt"></i></a></td>';
		}
		else {
			echo '<td><a href="reserver.php?week='.($week-1).'"><i class="fas fa-arrow-alt-circle-left arrows_edt"></i></a></td>';
		}
		echo '<td>'.WEEK.' '.$num_week.'</td>';
		$_SESSION['pdfweek'] = date('Y').$num_week;
		$an_next=($annee+1);
		$sem_test = date('d/m/Y', mktime(0,0,0,12,28,$annee));
		$sem_test_j = substr($sem_test,0,2);;
		$sem_test_m = substr($sem_test,3,2);;
		$sem_test_y = substr($sem_test,6,4);;
		$fin_j = substr($dateFinSemaineFr,0,2);
		$fin_m = substr($dateFinSemaineFr,3,2);
		$fin_y = substr($dateFinSemaineFr,6,4);
		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="reserver.php?week='.$sem_next.'"><i class="fas fa-arrow-alt-circle-right arrows_edt"></i></a></td>';
			}
			else {
				echo '<td><a href="reserver.php?week='.($week+1).'"><i class="fas fa-arrow-alt-circle-right arrows_edt"></i></a></td>';
			}
		}													
		else {
			echo '<td><a href="reserver.php?week='.($week+1).'"><i class="fas fa-arrow-alt-circle-right arrows_edt"></i></a></td>';
		}
		echo '<td><form method="POST" action="">'.EDT_BUTTON1.' <input type="date" class="inptxt" id="weekpick" name="weekpick" value="';
		if(isset($_SESSION['weekpick'])) {
			if($_SESSION['weekpick']!="") {
				echo $_SESSION['weekpick'];
			}
			else { echo date('Y-m-d'); }
		}
		else { echo date('Y-m-d'); }
		echo '" />&nbsp; <input class="bouton boutedt" type="submit" value="'.VALID.'" /></form></td>';
		echo '</tr>';
		if($week!=date('oW')) {
			echo '<tr><td colspan="3"></td><td><form method="POST" action=""><input class="bouton boutedt" type="submit" name="backweek" value="'.EDT_BUTTON2.'" /></form></td></tr>';
		}
		echo '<tr><td colspan="3">'.FROM2.' '.$dateDebSemaineFr.' '.TO2.' '.$dateFinSemaineFr.'</td><td class="w400 right">';
		echo '</td></tr>
	</table>
</div>';

// Gestion des créneaux :
$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires ORDER BY num_creneau");
$hor_nb = mysqli_num_rows($hor);
$plageH = array();
$plageDebut = array();
$plageFin = array();
$heighthor = array();
$tabheight = 0;
foreach($hor as $hor_info) {	
	$plageH[] = substr($hor_info['debut'],0,5);
	$plageHend[] = substr($hor_info['fin'],0,5);
	$plageDebut[] = $hor_info['debut'];
	$plageFin[] = $hor_info['fin'];
	$hFin[] = substr($hor_info['fin'],0,2).substr($hor_info['fin'],3,2).substr($hor_info['fin'],6,2);
	$hDebut[] = substr($hor_info['debut'],0,2).substr($hor_info['debut'],3,2).substr($hor_info['debut'],6,2);
	$plage_horid[] = $hor_info['hor_id'];
	$plage_horname[] = $hor_info['hor_name'];
	$plage_demijour[] = substr($hor_info['hor_name'],0,1);
	$hinter=strtotime($hor_info['fin'])-strtotime($hor_info['debut']);
	$num_creneau[] = $hor_info['num_creneau'];
	$heighthor[] = ($hinter/60)*0.85;
}
foreach($daysOfWeek as $key => $val) {
	$key = strtoupper(substr($key,0,1)).substr($key,1,2);
	foreach($hor as $hor_info) {
		if($freq_gestion_jourscren['valeur']==0) {
			$plage_open[$key][] = substr($hor_info['hor_freq'],2,1);
		}
		else {
			$plage_open[$key][] = @intval(substr($hor_info['hor_freq'], strpos($hor_info['hor_freq'], $key) + strlen($key), 3));
		}
	}
}
 
function translateMonth($month, $months_lang) {
    return isset($months_lang[$month]) ? $months_lang[$month] : $month;
}

$nom_mois = translateMonth($nom_mois, $months_lang);
$dateDebSemaine_m = translateMonth($dateDebSemaine_m, $months_lang);
$dateFinSemaine_m = translateMonth($dateFinSemaine_m, $months_lang);
 
echo '<br>';

// EDT CDI

echo '<div id="grilleSem">
	<h2>'.$_SESSION['lieu_nom'].' - '.FROM2.' '.$dateDebSemaine_j.'';
	if($dateDebSemaine_j==1) {
		echo FIRST;
	}
	if($dateDebSemaine_m!=$dateFinSemaine_m){
		echo ' '.$dateDebSemaine_m.'';
	}
	echo ' '.TO2.' '.$dateFinSemaine_j.'';
	if($dateFinSemaine_j==1) {
		echo FIRST;
	}
	echo ' '.$dateFinSemaine_m.'</h2>
</div>';

// Voir s'il y a verrouillage des briques des jours précédents
$brik_verr = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_brikverr'"));
$verrouillage = $brik_verr['valeur'];
$horform_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_formhor'"));

include('./db/proc/edt_table.php');
echo '<br>';
if ($nb_espedt>0) {
	echo '<div name="noprint"><br><br>';
	// BOUCLE AVEC EDT POUR CHAQUE USER AVEC DROITS DE MODIF
	$espaces = mysqli_query($bdd,"SELECT espace_id, espace_nom, edtopt, edtoptresa FROM ".$prefix."_espaces");
	foreach($espaces as $espaces_info) {
		if($espaces_info['edtoptresa']==1) {
			include('./db/proc/edt_espaces.php');		
		}
	}
	echo '</div>';				
}

?>