<?php
include('include/entete.php');
include('include/entete_adm.php');

if(!isset($_POST['myIdRef']) || empty($_POST['myIdRef']) || !isset($_POST['myIdCap']) || empty($_POST['myIdCap'])){
	header('location:index-adm.php');
	exit();
}

$myIdRef = $_POST['myIdRef'];
$myIdCap = $_POST['myIdCap'];

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

$maCleDeCryptage = getCleDeCryptage();	

$query="select * from eta 
	INNER JOIN capitaines ON id_eta=num_eta
	WHERE id_eta=:num_eta AND id_cap=:myIdCap";
$db = connect_base();
$queryRequest = $db->prepare( $query );
$queryRequest->bindParam( ':num_eta', $myIdRef, PDO::PARAM_INT );
$queryRequest->bindParam( ':myIdCap', $myIdCap, PDO::PARAM_INT );
$queryRequest->execute();
$data=$queryRequest->fetchAll();
$count = $queryRequest->rowCount();

if($count!=1){
	header('location:index-adm.php');
	exit();
}

$nomCap = decrypter($maCleDeCryptage,$data[0]['nom_cap']).' '.decrypter($maCleDeCryptage,$data[0]['prenom_cap']);
$nomEta=decrypter($maCleDeCryptage,$data[0]['nom_eta']);

?>
<!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">
		
		<?php include('include/admin-header.php');?>
		
		<div class="btnDeci" align="center"><a href="detail-cfa-adm.php?id=<?php echo $myIdRef; ?>">Retour sur la liste des capitaines</a><br /><br /></div>
		
		<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"><?php echo $nomEta;?></div>
		<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs bgTitrePage"></div>
		<div class="clearfix"></div>
		<div class="adresseCfa">
		<?php 
			echo decrypter($maCleDeCryptage,$data[0]['adresse1']); 
			if(!empty($data[0]['adresse2']))	echo ' - '.decrypter($maCleDeCryptage,$data[0]['adresse2']); 
			echo ' - '.decrypter($maCleDeCryptage,$data[0]['cp']).' '.decrypter($maCleDeCryptage,$data[0]['ville']); 
		?>
		</div>
		
		<div class="blocReferent">Référent : <span class="ssTitreViolet"><?php echo decrypter($maCleDeCryptage,$data[0]['nom_ref']);?></span></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">Capitaine : <?php echo $nomCap; ?></div>
		<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs"></div>
		<div class="clearfix"></div>
		
		<br /><br />
		
		<?php
				//SOLLICITATIONS REFERENT
				echo '<div class="blocReferent"><span class="ssTitreViolet">Les sollicitations refusées</span></div>';
		
				$query="SELECT * FROM sollicitations WHERE idCapitaine=0 AND chxNumEta=:chxNumEta AND traitement=0 ORDER BY chxJour, chxCreneau, dateCreate";
				$db = connect_base();
				$queryRequest = $db->prepare( $query );
				$queryRequest->bindParam( ':chxNumEta', $myIdRef, PDO::PARAM_INT );
				$queryRequest->execute();
				$countSol = $queryRequest->rowCount();
				$dataSol = $queryRequest->fetchAll();

				if($countSol==0){
					echo '<div align="center">Aucune sollicitation refusée.</div>';
				}else{

					echo '<table>';
						echo '<tr><th>Etablissement</th><th>Contact</th><th>Localisation</th><th>Jour</th><th>Creneau</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 '</tr>';
					}
					echo '<table>';

				}
				
				echo '<br /><br />';
				
				//SOLLICITATIONS CAPITAINE
				echo '<div class="blocReferent"><span class="ssTitreViolet">Les sollicitations en attente</span></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', $myIdCap, PDO::PARAM_INT );
				$queryRequest->execute();
				$countSol = $queryRequest->rowCount();
				$dataSol = $queryRequest->fetchAll();

				if($countSol==0){
					echo '<div align="center">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></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 '</tr>';
					}
					echo '<table>';

				}
				
				echo '<br /><br />';
		
			/***************************
			AGENDA
			***************************/
				
				echo '<div class="blocReferent"><span class="ssTitreViolet">Agenda</span><br ><b> S </b>: Inscription suite à une sollicitation<br /><b> D </b>: Inscription directe</div>';
				
				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', $myIdCap, 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]['provenance']>0)	$provenance = "<b>S</b>";	else	$provenance = "<b>D</b>";
												
											if($dataD[0]['statut']=="bloque"){
												$td = '<td class="isGrey"><b>Indisponible</b></td>';
											}
											if($dataD[0]['statut']=="libre"){
												$td = '<td class="isWhite"><b>Disponible</b></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 />'.$provenance.'</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 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">Les trinômes</div>
		<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs"></div>
		<div class="clearfix"></div>-->
		
	
	
		
		<br /><div class="btnDeci" align="center"><a href="detail-cfa-adm.php?id=<?php echo $myIdRef; ?>">Retour sur la liste des capitaines</a></div>
	</div>
	
	<?php include('include/footer.php');?>

</body>
</html>