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

if(!isset($_GET['id']) || empty($_GET['id'])){
	header('location:index-adm.php');
	exit();
}
$id=$_GET['id'];
include('config/config.inc.php');
include('config/lib.inc.php');

$query="select * from eta where id_eta=:id_eta";
$db = connect_base();
$queryRequest = $db->prepare( $query );
$queryRequest->bindParam( ':id_eta', $id, PDO::PARAM_INT );
$queryRequest->execute();
if($queryRequest->rowCount()==0){
	header('location:index-adm.php');
	exit();
}
$data=$queryRequest->fetchAll();
$maCleDeCryptage = getCleDeCryptage();	
$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="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> <br/>
			Téléphone : <?php echo decrypter($maCleDeCryptage,$data[0]['tel_ref']);?> E-mail : <a href="mailto:<?echo decrypter($maCleDeCryptage,$data[0]['email_ref']); ?>"><?= decrypter($maCleDeCryptage,$data[0]['email_ref']);?> </a></div>
		
		<div class="textIntro"><br/>
			<?php 
//	Sollicitations refusées
	$querySolRef="select * FROM sollicitations WHERE idCapitaine=0 AND traitement=0 AND chxNumEta=:chxNumEta";
	$queryRequestSolRef = $db->prepare( $querySolRef );
	$queryRequestSolRef->bindParam( ':chxNumEta', $id, PDO::PARAM_INT );
	$queryRequestSolRef->execute();
	$countSolRef=$queryRequestSolRef->rowcount();
	$dataSolRef = $queryRequestSolRef->fetchAll();
				if($countSolRef==0)	echo $nomEta .' n\'a aucune sollicitation refusée.<br /><br/>';
				elseif($countSolRef==1)	echo '<b>'.$nomEta .' a '.$countSolRef.' sollicitation refusée.</b><br /><br/>';
				elseif($countSolRef>1)	echo '<b>'.$nomEta .' a '.$countSolRef.' sollicitations refusées.</b><br /><br/>';
			
//			SOLLICITATIONS REFERENT
			if($countSolRef >= 1){
				if($countSolRef == 1) echo '<div class="blocReferent"><span class="ssTitreViolet">La sollicitation refusée</span></div>';
				else echo '<div class="blocReferent"><span class="ssTitreViolet">Les sollicitations refusées</span></div>';
	
					echo '<table class="table">';
						echo '<tr><th>Etablissement</th><th>Contact</th><th>Localisation</th><th>Jour</th><th>Creneau</th></tr>';
					foreach($dataSolRef 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 />';
			?>
			
		</div>
		
		<div class="clearfix"></div>
		<br/><br/>
		
		
	
	<?php
	$query="select * from capitaines where num_eta=:num_eta";
	$db = connect_base();
	$queryRequest = $db->prepare( $query );
	$queryRequest->bindParam( ':num_eta', $id, PDO::PARAM_INT );
	$queryRequest->execute();
	include_once('include/agenda.inc.php');
		
	if($queryRequest->rowCount()>0){
		$data=$queryRequest->fetchAll();
		$index = 1;
		$capitaine = "";
		$apprenant = "";
		$professionnel = "";
		
			foreach($data as $val){				
				//Affichage noms apprenant et pro
				$query2="select * from equipes where num_capitaine=:num_cap";
				$db2 = connect_base();
				$queryRequest2 = $db2->prepare( $query2 );
				$queryRequest2->bindParam( ':num_cap', $val['id_cap'], PDO::PARAM_INT );
				$queryRequest2->execute();
				$data2=$queryRequest2->fetchAll();
				$capitaine = decrypter($maCleDeCryptage,$val['prenom_cap']).' '.decrypter($maCleDeCryptage,$val['nom_cap']);
				if(!empty($data2[0]['nom_eleve']) && !empty($data2[0]['prenom_eleve'])) $apprenant = decrypter($maCleDeCryptage,$data2[0]['nom_eleve']).' '.decrypter($maCleDeCryptage,$data2[0]['prenom_eleve']);
				if(!empty($data2[0]['nom_pro']) && !empty($data2[0]['prenom_pro'])) $professionnel = decrypter($maCleDeCryptage,$data2[0]['nom_pro']).' '.decrypter($maCleDeCryptage,$data2[0]['prenom_pro']);
				
				//Affichage rendez-vous capitaines
				$query3="select count(*) as rdv, sum(nbClasses) as classes, sum(nbEleves) as eleves FROM agenda INNER JOIN collegelycee ON idCoLy=numCoLy WHERE numCap=:num_cap AND statut= 'reserve' AND numEta=:numEta";
				$db3 = connect_base();
				$queryRequest3 = $db3->prepare( $query3 );
				$queryRequest3->bindParam( ':numEta', $id, PDO::PARAM_INT );
				$queryRequest3->bindParam( ':num_cap', $val['id_cap'], PDO::PARAM_INT );
				$queryRequest3->execute();
				//$countRdv=$queryRequest3->rowcount();
				$data3=$queryRequest3->fetchAll();
				if(isset($data3[0]['rdv']))	$rdv = $data3[0]['rdv'];	else	$rdv = 0;
				if(isset($data3[0]['classes']))	$classes = $data3[0]['classes'];	else	$classes = 0;
				if(isset($data3[0]['eleves']))	$eleves = $data3[0]['eleves'];	else	$eleves = 0;
				//Affichage sollicitations capitaines
				$query4="select * FROM sollicitations WHERE idCapitaine=:num_cap AND traitement=0 AND chxNumEta=:chxNumEta";
				$db4 = connect_base();
				$queryRequest4 = $db4->prepare( $query4 );
				$queryRequest4->bindParam( ':chxNumEta', $id, PDO::PARAM_INT );
				$queryRequest4->bindParam( ':num_cap', $val['id_cap'], PDO::PARAM_INT );
				$queryRequest4->execute();
				$countSol=$queryRequest4->rowcount();
				
				?>
		<div class="col-lg-offset-2 col-lg-8  col-md-offset-2 col-md-8  col-sm-offset-1 col-sm-10 col-sm-offset-1 col-xs-12 titreContenuPage">Détail de l'équipe n°<?=$index?> </div>
		<div class="clearfix"></div>
		<table cellpadding="0" cellspacing="0" class="table tabAdm">
			<tr>
				<th>Capitaine<br /><span>Prénom Nom</span></th>
				<th>Apprenant<br /><span>Prénom Nom</span></th>
				<th>Professionnel<br /><span>Prénom Nom</span></th>
				<th>Rendez-vous<br /><span>(Nb de classes/d'élèves)</span></th>
				<th>Sollicitations<span><br />En attente</span></th>
			</tr>
			<tr>
				<td><a href="javascript:goAgenda('<?=$val['id_cap']?>')"><?=$capitaine?></a></td>
				<td><?=$apprenant?></td>
				<td><?=$professionnel?></td>
				<td align="center"><?=$rdv?> (<?=$classes?>/<?=$eleves?>)</td>
				<td align="center"><?=$countSol?></td>
			</tr>
		</table>
		<br/><br/>
		<div class="blocReferent"><span class="ssTitreViolet">Agenda de l'équipe n°<?=$index?> </span></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><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', $val['id_cap'], 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 = "Origine : <b>Site</b>";	else	$provenance = "Origine : <b>Directe</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	$td = '<td class="empty">&nbsp;</td>';	
									echo $td."\r\n";
								}
							echo '</tr>'."\r\n";
						}
					
					echo '</table>'."\r\n";
				}
		?>
		<br/><br/>
		<?php
				$index++;
		}
			
			}
	else echo '<div align="center">Aucun trinôme pour ce CFA/LP pour l\'instant</div>';
	?>
		<script type="text/javascript">
			function goAgenda(myIdCap){
				document.frmGo.myIdCap.value = myIdCap;
				document.frmGo.submit();
			}
		</script>
		<form name="frmGo" method="post" action="admin-cap-agenda.php">
			<input type="hidden" name="myAction" value="goAgenda">
			<input type="hidden" name="myIdRef" value="<?php echo $id; ?>">
			<input type="hidden" name="myIdCap">
		</form>
		
		<br /><div class="btnDeci" align="center"><a href="suivi-cfa-adm.php">Retour sur la liste des CFA/LP</a></div>
	</div>
	
	<?php include('include/footer.php');?>

</body>
</html>