<?php

	include('include/entete.php');

	if(isset($_GET['ncfa']) && !empty($_GET['ncfa'])){
		$ncfa = $_GET['ncfa'];
	}else{
		header('location:./');
		exit();
	}

	$mydate = "";

	if(isset($_GET['mydate']) && !empty($_GET['mydate'])){
		$mydate = $_GET['mydate'];
	}

	include('config/config.inc.php');
	include('config/lib.inc.php');
	include('include/agenda.inc.php');

	$maCleDeCryptage = getCleDeCryptage();	
	
	//Affiche les infos du CFA
	$query="
		SELECT * from eta 
		WHERE id_eta=:num_eta
	";
	$db = connect_base();
	$queryRequest = $db->prepare( $query );
	$queryRequest->bindParam( ':num_eta', $ncfa, PDO::PARAM_INT );
	$queryRequest->execute();
	$dataCfa=$queryRequest->fetchAll();
	$countCfa = $queryRequest->rowCount();
	if($countCfa!=1){	//CFA introuvable
		header('location:./');
		exit();
	}
	$nomEta = $dataCfa[0]['nom_eta'];

	/***************************************
	DISPONIBILITES MATIN ET AM PAR DATES
	***************************************/
	
	//Iniitialise les disponibilités par défaut (sur la premier jour et sur le second)
	$arrDispos = array();
	foreach($arrWeek as $kw=>$vw){
		foreach($vw as $vDay){
			if(!empty($vDay)){
				foreach($arrCreneauxSol as $kCr => $vCr){
					if($vDay==$theGreatDay)		$arrDispos[$vDay][$vCr] = 'libre';
					else	$arrDispos[$vDay][$vCr] = 'indisponible';
				}
			}
		}
	}
	//echo '<pre>';	print_r($arrDispos);	echo '</pre>';

	//Contrôle si au moins un capitaine est déclaré pour le CFA
	$query="
		SELECT * from capitaines
		WHERE num_eta=:num_eta
		ORDER BY id_Cap";
	$queryRequest = $db->prepare( $query );
	$queryRequest->bindParam( ':num_eta', $ncfa, PDO::PARAM_INT );
	$queryRequest->execute();
	//$dataCap=$queryRequest->fetchAll();
	$countCap = $queryRequest->rowCount();
	if($countCap==0){
		header('location:./');
		exit();
	}
	
	
	//Met à jour les disponibilités
	$query="
		SELECT * from capitaines
		INNER JOIN agenda ON id_cap = numCap
		WHERE num_eta=:num_eta
		ORDER BY jour,creneau,id_cap";
	$queryRequest = $db->prepare( $query );
	$queryRequest->bindParam( ':num_eta', $ncfa, PDO::PARAM_INT );
	$queryRequest->execute();
	$data=$queryRequest->fetchAll();
	$count = $queryRequest->rowCount();

	$arrCountCreneaux = array();

	foreach($data as $k=>$v){
		
		//echo '<pre>';	print_r($v);	echo '</pre>';
		
		$thejour = $v['jour'];
		if($v['creneau']=='am1' || $v['creneau']=='am2')	$thecreneau = 'am';
		if($v['creneau']=='pm1' || $v['creneau']=='pm2')	$thecreneau = 'pm';
		
		if($v['statut']=='libre'){
			$arrDispos[$thejour][$thecreneau] = 'libre';	//Si un capitaine n'a effectué aucune action sur un creneau de la première date
			if(isset($arrCountCreneaux[$thejour][$thecreneau]))		$arrCountCreneaux[$thejour][$thecreneau]++;	else	$arrCountCreneaux[$thejour][$thecreneau]=1;	//Compte le nombre de dispos par créneau
		}
	
	}

	//echo '<pre>';	print_r($arrCountCreneaux);	echo '</pre>';

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>FAFIH - DECIDAY</title>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/fonctions.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="css/style.css" rel="stylesheet" type="text/css">
<script>
	
function submitAgendaS(mydate,mycreneau){
	document.frmAgendaS.mydate.value = mydate;
	document.frmAgendaS.mycreneau.value = mycreneau;
	if(document.frmAgendaS.ncfa.value>0){
		if(window.confirm('Êtes-vous sûr de choisir ce créneau ?'))	document.frmAgendaS.submit();
	}
}	
	
jQuery(document).ready(function(){
	
	<?php
	if(isset($_SESSION['msg']) && !empty($_SESSION['msg'])){
		?>
	alert('<?php print addslashes($_SESSION['msg']);?>');
	<?php
		$_SESSION['msg']="";
	}
	?>
	
});
</script>
																			
</head>

<body>
	<?php include('include/header.php');?>
	
	<div class="container contentPage">

		<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs bgTitrePage"></div>
		<div class="col-lg-8 col-md-8 col-sm-10 col-xs-12 titreContenuPage">Collèges, Lycées<br />Réservez la date pour la venue d'un trinôme au sein de votre établissement  :</div>
		<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs bgTitrePage"></div>
		<div class="clearfix"></div>
		
		<div class="headerCap" style="text-align: center; margin:18px 0 12px;">
			<b>CFA sélectionné :</b> <br /><span class="ssTitreViolet"><b><?php echo decrypter($maCleDeCryptage,$dataCfa[0]['nom_eta']);?></b></span><br />
			<?php if(!empty($mydate))	echo '<b>Date Souhaitée :</b><br /> <span class="ssTitreViolet">'.MysqlToDateFr($mydate).'</span><br />'; ?>
			<br />Merci de sélectionner un créneau parmi les disponibilités ci-dessous : <br />
		</div>
		
		<?php
		
				
				foreach($arrWeek as $kw=>$vw){
					echo '<table class="arrWeek">'."\r\n";
						
						echo '<caption>Semaine '.$kw.'</caption>';
						
						$idDay = 0;
						echo '<tr>'."\r\n";
							echo '<th class="empty">&nbsp;</th>';
							foreach($vw as $vDay){
								if(!empty($vDay))	$th = '<th>'.$arrDaysWeek[$idDay].'<br /><span>'.MysqlToDateFr($vDay).'</span></th>';	else	$th = '<th>&nbsp;</th>';
								echo $th."\r\n";
								$idDay++;
							}
						echo '</tr>'."\r\n";
						
						foreach($arrCreneauxSol as $kCr => $vCr){
							echo '<tr>'."\r\n";
								echo '<th>'.$arrCreneauxSolTitle[$kCr].'</th>';
								foreach($vw as $vDay){
									if(!empty($vDay)){
										
										//Un enregistrement existe t'il pour le créneau ?
										if($arrDispos[$vDay][$arrCreneauxSol[$kCr]]=="indisponible"){
											$td = '<td class="isGrey">Indisponible</td>';
										}
										if($arrDispos[$vDay][$arrCreneauxSol[$kCr]]=="libre"){
											//if($vDay==$theGreatDay)	$color = "isPurple";
											$color = "isOrange";
											if(isset($arrCountCreneaux[$vDay][$arrCreneauxSol[$kCr]])){
												if($arrCountCreneaux[$vDay][$arrCreneauxSol[$kCr]]>2)	$color = "isGreen";
											}
											$td = '<td class="'.$color.'"><a href="javascript:submitAgendaS(\''.$vDay.'\',\''.$arrCreneauxSol[$kCr].'\')" style="text-transform:uppercase">Réserver</a></td>';
										}
																	
									}else	$td = '<td class="empty">&nbsp;</td>';	
									echo $td."\r\n";
								}
							echo '</tr>'."\r\n";
						}
					
					echo '</table>'."\r\n";
				}
		?>
		
		<form name="frmAgendaS" id="frmAgendaS" method="post" action="sollicitation-form.php">
			<input type="hidden" name="myaction" value="agendaSelect" />
			<input type="hidden" name="ncfa" value="<?php echo $ncfa; ?>" />
			<input type="hidden" name="mydate" />
			<input type="hidden" name="mycreneau" />
		</form>
		
	</div>
	
	<?php include('include/footer.php');?>
</body>
</html>