<?php
include('include/entete.php');
include('include/entete_cap.php');
include('config/config.inc.php');
include('config/lib.inc.php');

$maCleDeCryptage = getCleDeCryptage();	

$query="select * from eta,capitaines where id_eta=num_eta and id_cap=:id_cap";
$db = connect_base();
$queryRequest = $db->prepare( $query );
$queryRequest->bindParam( ':id_cap', $_SESSION['cap_num'], PDO::PARAM_INT );
$queryRequest->execute();
$data=$queryRequest->fetchAll();
$capitaine=decrypter($maCleDeCryptage,$data[0]['prenom_cap'])." ".decrypter($maCleDeCryptage,$data[0]['nom_cap']);

?>
<!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>
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">Votre espace Capitaine</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;">
			<span class="ssTitreViolet"><?php echo decrypter($maCleDeCryptage,$data[0]['nom_eta']);?></span><br />
			<b>Capitaine : </b><?php echo $capitaine;?> <a href="modifier-cap.php">modifier</a><br />
			<a href="deco.php">Déconnexion</a>
		</div>
		
		<div class="menuPage">
			<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
				<a href="index-cap.php">Votre trinôme</a>
			</div>
			<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
				<a href="agenda-cap.php">Votre agenda</a>
			</div>
			<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
				<a href="bao-cap.php">Boite à outils</a>
			</div>
			<div class="clearfix"></div>
		</div>
		
		<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs"></div>
		<div class="col-lg-8 col-md-8 col-sm-10 col-xs-12 titreContenuPage">Votre agenda</div>
		<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs"></div>
		<div class="clearfix"></div>
		
		<div class="textIntro"><br/>Organisez l’agenda de votre trinôme :
			<div class="clearfix"></div>
				<ul>
					<li>Sélectionnez jusqu’à 4 créneaux de disponibilité par jour</li>
					<li>Suivez et répondez aux <b>sollicitations des collèges et lycées</b></li>
				</ul>
			<br /> 
			<?php
			
			//if($data[0]['num_eta']!=399 && $data[0]['num_eta']!=402){
			
			//	echo 'La page sera disponible prochainement.';
				
			//}else{
				
				//SOLLICITATIONS DU CAPITAINE EN ATTENTE
				
				echo '<br /><br />';
						
				echo '<a name="sollicitation"></a>';
				echo '<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs"></div>';
				echo '<div class="col-lg-8 col-md-8 col-sm-10 col-xs-12 titreContenuPage">Vos sollicitations en attente</div>';
				echo '<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs"></div>';
				echo '<div class="clearfix"></div>';

				$query="SELECT * FROM sollicitations WHERE idCapitaine=:id_cap AND traitement=0 ORDER BY chxJour, chxCreneau, dateCreate";
				$db = connect_base();
				$queryRequest = $db->prepare( $query );
				$queryRequest->bindParam( ':id_cap', $_SESSION['cap_num'], PDO::PARAM_INT );
				$queryRequest->execute();
				$countSol = $queryRequest->rowCount();
				$dataSol = $queryRequest->fetchAll();

				if($countSol==0){
					echo '<div align="center">Vous n\'avez aucune sollicitation en attente.</div>';
				}else{
					echo '<table>';
						echo '<tr><th>Etablissement</th><th>Contact</th><th>Localisation</th><th>Jour</th><th>Creneau</th><th colspan="2">Actions</th></tr>';
					foreach($dataSol as $k => $v){
						$myjourSol = MysqlToDateFr($v['chxJour']);
						$mycreneauSol = getCreneauSol($v['chxCreneau']);
						echo '<tr>';
							echo '<td>'.decrypter($maCleDeCryptage,$v['etaCoLy']).'</td>';
							echo '<td>'.decrypter($maCleDeCryptage,$v['nomCoLy']).' '.decrypter($maCleDeCryptage,$v['prenomCoLy']).'</td>';
							echo '<td>'.decrypter($maCleDeCryptage,$v['cpCoLy']).' '.decrypter($maCleDeCryptage,$v['villeCoLy']).'</td>';
							echo '<td>'.$myjourSol.'</td>';
							echo '<td>'.$mycreneauSol.'</td>';
							echo '<td align="center"><a href="sollicitation-cap-voir.php?idSol='.$v['idCoLy'].'">Voir</a></td>';
							echo '<td align="center"><a href="javascript:refusSol(\''.$v['chxJour'].'\',\''.$v['chxCreneau'].'\','.$v['idCoLy'].')">Refuser</a></td>';
						echo '</tr>';
					}
					echo '<table>';
				}
				
				echo '<br />';
				
				/*
				$arrWeek = array();
				$arrWeek[3] = array('','','','2019-01-17','2019-01-18');
				$arrWeek[4] = array('2019-01-21','2019-01-22','2019-01-23','2019-01-24','2019-01-25');
				$arrWeek[5] = array('2019-01-28','2019-01-29','2019-01-30','2019-01-31','2019-02-01');
				$arrWeek[6] = array('2019-02-04','2019-02-05','2019-02-06','2019-02-07','2019-02-08');
				
				$arrDaysWeek = array('lundi','mardi','mercredi','jeudi','vendredi');
				
				$arrCreneaux = array('am1','am2','pm1','pm2');
				$arrCreneauxTitle = array('Matin 1','Matin 2','Après-midi 1','Après-midi 2');
				
				$theGreatDay = '2019-01-17';
				*/
				
				include('include/agenda.inc.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><span>'.$arrDaysWeek[$idDay].'</span><br />'.MysqlToDateFr($vDay).'</th>';	else	$th = '<th>&nbsp;</th>';
								echo $th."\r\n";
								$idDay++;
							}
						echo '</tr>'."\r\n";
						
						foreach($arrCreneaux as $kCr => $vCr){
							if($arrCreneaux[$kCr]=="pm1")	$isMidDay = ' class="isMidDay"';	else	$isMidDay = '';
							echo '<tr'.$isMidDay.'>'."\r\n";
								echo '<th>'.$arrCreneauxTitle[$kCr].'</th>';
								foreach($vw as $vDay){
									if(!empty($vDay)){
										
										//Un enregistrement existe t'il pour le créneau ?
										$query="SELECT * FROM agenda LEFT JOIN collegelycee ON idCoLy=numCoLy WHERE numCap=:numCap AND jour=:jour AND creneau=:creneau 
										";
										$queryRequest = $db->prepare( $query );
										$queryRequest->bindParam( ':numCap', $_SESSION['cap_num'], PDO::PARAM_INT );
										$queryRequest->bindParam( ':jour', $vDay, PDO::PARAM_STR );
										$queryRequest->bindParam( ':creneau', $arrCreneaux[$kCr], PDO::PARAM_STR );
										$queryRequest->execute();
										$countD = $queryRequest->rowCount();
										$dataD=$queryRequest->fetchAll();
										if($countD>1){ //Erreur Pas possible
											$td = '<td class="empty">Erreur recontrée sur ce créneau</td>';	
										}elseif($countD==1){
											if($dataD[0]['statut']=="bloque"){
												$td = '<td class="isGrey"><b>Indisponible</b><br /><a href="javascript:updateAg(\'D3B\',\''.$vDay.'\',\''.$arrCreneaux[$kCr].'\',0)">Rendre disponible</a></td>';
											}
											if($dataD[0]['statut']=="libre"){
												$td = '<td class="isWhite"><b>Disponible</b><br /><a href="javascript:reservAg(\'R3S\',\''.$vDay.'\',\''.$arrCreneaux[$kCr].'\',\'\')">Identifier un rendez-vous</a><br><a href="javascript:updateAg(\'BL0\',\''.$vDay.'\',\''.$arrCreneaux[$kCr].'\',0)">Rendre indisponible</a><br><a href="javascript:affecterSol(\''.$vDay.'\',\''.$arrCreneaux[$kCr].'\')">Affecter une sollicitation</a></td>';
											}
											if($dataD[0]['statut']=="reserve"){
												$reserve = decrypter($maCleDeCryptage,$dataD[0]['etaCoLy'])."<br /><span>(".decrypter($maCleDeCryptage,$dataD[0]['cpCoLy'])." ".decrypter($maCleDeCryptage,$dataD[0]['villeCoLy']).")</span> ";
												$td = '<td class="isPurple"><b>'.$reserve.'</b><br /><a href="javascript:reservAg(\'R3S\',\''.$vDay.'\',\''.$arrCreneaux[$kCr].'\','.$dataD[0]['idCoLy'].')">Modifier</a><br /><a href="javascript:updateAg(\'BL0\',\''.$vDay.'\',\''.$arrCreneaux[$kCr].'\',1)">Rendre indisponible</a></td>';
											}
										}/*else{	//Créneau vide
										
											if($vDay==$theGreatDay)	$td = '<td class="isWhite">Libre<br /><a href="javascript:reservAg(\'R3S\',\''.$vDay.'\',\''.$arrCreneaux[$kCr].'\',\'\')">Réserver</a><br><a href="javascript:updateAg(\'BL0\',\''.$vDay.'\',\''.$arrCreneaux[$kCr].'\')">Bloquer</a></td>';	else	$td = '<td class="isGrey">Bloqué<br /><a href="javascript:updateAg(\'D3B\',\''.$vDay.'\',\''.$arrCreneaux[$kCr].'\')">Débloquer</a></td>';
											
										}*/
										
									}else	$td = '<td class="empty">&nbsp;</td>';	
									echo $td."\r\n";
								}
							echo '</tr>'."\r\n";
						}
					
					echo '</table>'."\r\n";
				}
			
			//}
					
			?>
		</div>
		<div class="clearfix"></div>
		
		<script type="text/javascript">
			function updateAg(myAction,myDay,myCreneau,confirm){
				document.frmAgenda.myAction.value = myAction;
				document.frmAgenda.myDay.value = myDay;
				document.frmAgenda.myCreneau.value = myCreneau;
				if(confirm==1){
					if(window.confirm('Attention : Vous allez bloquer un créneau déjà réservé, ce qui annulera la réservation.\nAucune notification ne sera envoyée.\nVeuillez contacter l\'établissement pour l\'en informer.')){
						document.frmAgenda.submit();
					}	
				}else	document.frmAgenda.submit();
			}
			function reservAg(myAction,myDay,myCreneau,idCoLy){
				document.frmReserv.myAction.value = myAction;
				document.frmReserv.myDay.value = myDay;
				document.frmReserv.myCreneau.value = myCreneau;
				document.frmReserv.idCoLy.value = idCoLy;
				document.frmReserv.submit();
			}
			function refusSol(myDay,myCreneau,idCoLy){
				document.frmSolRefus.myDay.value = myDay;
				document.frmSolRefus.myCreneau.value = myCreneau;
				document.frmSolRefus.idCoLy.value = idCoLy;
				if(window.confirm('Êtes-vous sur ?'))	document.frmSolRefus.submit();
			}
			function affecterSol(myDay,myCreneau){
				document.frmSolAffecter.myDay.value = myDay;
				document.frmSolAffecter.myCreneau.value = myCreneau;
				document.frmSolAffecter.submit();
			}
		</script>
		
		<form name="frmAgenda" method="post" action="agenda-cap-actions.php">
			<input type="hidden" name="myAction">
			<input type="hidden" name="myDay">
			<input type="hidden" name="myCreneau">
		</form>
		<form name="frmReserv" method="post" action="agenda-cap-form.php">
			<input type="hidden" name="myAction">
			<input type="hidden" name="myDay">
			<input type="hidden" name="myCreneau">
			<input type="hidden" name="idCoLy">
		</form>
		<form name="frmSolRefus" method="post" action="sollicitations-cap-actions.php">
			<input type="hidden" name="myAction" value="refusSol">
			<input type="hidden" name="myDay">
			<input type="hidden" name="myCreneau">
			<input type="hidden" name="idCoLy">
		</form>
		<form name="frmSolAffecter" method="post" action="sollicitations-cap-choix.php">
			<input type="hidden" name="myAction" value="affecterSol">
			<input type="hidden" name="myDay">
			<input type="hidden" name="myCreneau">
		</form>
		
	</div>
	
	<?php include('include/footer.php');?>

</body>
</html>