<?php
$creneauele_nb = array();
$weekdown = array();
if(isset($an[5])) { $i=5; }
else if(isset($an[4])) { $i=4; }
else if(isset($an[3])) { $i=3; }
else { $i=2; }
if(substr($_SESSION['nb_year'],1,1)=='a') { $nb_sans=0; } else { $nb_sans=1; }
for($i;$i>$nb_sans;$i--) {
	if($j!=1) {
		$jourP1suite = strtotime(date("Y-m-d",strtotime($jourP1_evol[$i])) . " +".($j-1)." day");
		$jourP1suit = date('Y-m-d',$jourP1suite);
		$jourP1sui = new DateTime($jourP1suit);
		$jourP1date_evol[$i] = $jourP1sui;
	}
	else {
		$jourP1date_evol[$i] = new DateTime($jourP1_evol[$i]);
	}
	$creneauele_nb[$i]=0;
	$weekdown[$i]=0;
	foreach(new DatePeriod($jourP1date_evol[$i], new DateInterval('P7D') , $jourP2date_evol[$i]) as $dt) {
		$date = $dt->format('Y-m-d');
		//on soustrait des semaines celles où le jour est fermé
		$fermeture = mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$date."' AND statut = 0");
		$fermeture_nb = mysqli_num_rows($fermeture);
		if($fermeture_nb==1) {
			$weekdown[$i]++;
		}
		$creneauele = mysqli_query($bdd,"SELECT freq_id FROM ".$prefix."_freq freq WHERE (freq.date_freq = '".$date."' AND freq.hor_name = '".$plage_nom[$h]."') ".$filtre_activs."");
		$nb = mysqli_num_rows($creneauele);
		if($grappes_check==1) {
			$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes freq WHERE (freq.date_freq = '".$date."' AND freq.hor_name = '".$plage_nom[$h]."') AND freq.passage = '0' ".$filtre_activs."");
			while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
				$nb += $elegrap['nombre'];
			}
		}
		$creneauele_nb[$i]+=$nb;		
	}
}
$jouraff = date("Ymd", mktime(0,0,0,$mois,$jour-1+$j,$annee));
$horstart = substr($plageH[$h],0,2).substr($plageH[$h],3,2).'00';
$horend = substr($plageHend[$h],0,2).substr($plageHend[$h],3,2).'00';
$dtstart = $jouraff.'T'.$horstart;
$dtend = $jouraff.'T'.$horend;

// Affichage de la cellule :
echo '<div id="inbrik'.$h.$j.$usercode.'" class="td recept_open" style="background: linear-gradient(to right, ';
$nbele_cren = array();
if(isset($an[5])) { $i=5; }
else if(isset($an[4])) { $i=4; }
else if(isset($an[3])) { $i=3; }
else { $i=2; }
if(substr($_SESSION['nb_year'],1,1)=='a') { $nb_sans=0; } else { $nb_sans=1; }
for($i;$i>$nb_sans;$i--){
	if(($nb_week_evol[$i]-($weekdown[$i]-$nb_close_week[$i]))>0) {
		$nbele_cren[$i] = round(($creneauele_nb[$i]/($nb_week_evol[$i]-($weekdown[$i]-$nb_close_week[$i]))),2);
	}
	else { $nbele_cren[$i]=0; }
	if($nbele_cren[$i]==0) {
		echo '#fff';
	}
	else if($nbele_cren[$i]<=($moyenele_cren_evol[$i]/1.5)) {
		echo '#F3F781';
	}
	else if($nbele_cren[$i]<=($moyenele_cren_evol[$i]/1.2)) {
		echo '#F7D358';
	}
	else if($nbele_cren[$i]<=($moyenele_cren_evol[$i]*1.2)&&$nbele_cren[$i]>=($moyenele_cren_evol[$i]/1.2)) {
		echo '#FE9A2E';
	}
	else if($nbele_cren[$i]>=($moyenele_cren_evol[$i]*1.5)) {
		echo '#DF0101';
	}
	else if($nbele_cren[$i]>=($moyenele_cren_evol[$i]*1.2)) {
		echo '#FF4000';
	}
	if($i>($nb_sans+1)) { echo ', '; }
}
echo ');height:'.$heighthor[$h].'px"><span>';
if(isset($an[5])) { $i=5; }
else if(isset($an[4])) { $i=4; }
else if(isset($an[3])) { $i=3; }
else { $i=2; }
if(substr($_SESSION['nb_year'],1,1)=='a') { $nb_sans=0; } else { $nb_sans=1; }
for($i;$i>$nb_sans;$i--) { 
	echo '<!--'.$j.$plage_nom[$h].''.$creneauele_nb[$i].' / ('.$nb_week_evol[$i].'-('.$weekdown[$i].'-'.$nb_close_week[$i].')) = -->'.$nbele_cren[$i].'';
	if($i>($nb_sans+1)) { echo ' - '; }
	$total_jour[$i] += $nbele_cren[$i];
}
if(isset($an[5])) { $i=5; }
else if(isset($an[4])) { $i=4; }
else if(isset($an[3])) { $i=3; }
else { $i=2; }

echo '</span></div>';
?>
