<?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']);


//Ajout ou Modification
if($_POST['myAction']=='affecterSol' && !empty($_POST['myCreneau']) && !empty($_POST['myDay'])){
	
	$myCreneau = $_POST['myCreneau'];
	$myDay = $_POST['myDay'];
	
}else{
	$_SESSION['msg']='Une erreur a été rencontrée pour affecter une sollicitation.';
	header('location:agenda-cap.php#sollicitation');
	exit();
}	

?>
<!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?123"></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']="";
			}
			?>
		});
				
			function validAffectSol(){
				if(!testerRadio('document.frmSolAffecter.chxSol','Veuillez sélectionner une sollicitation pour continuer.'))	return false;
				
				return true;
			}
		
			function affectSol(){
				if(validAffectSol()) document.frmSolAffecter.submit();
			}


	</script>
	<style type="text/css">
		.frmVoir th{ font-weight: normal; }
		.frmVoir th, .frmVoir td{ padding: 5px 10px; vertical-align: middle; }
		.contentPage form input {
			margin-bottom: 0;
		}
	</style>
	
</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">Choisir une sollicitation pour un créneau</div>
		<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs"></div>
		<div class="clearfix"></div>
		
		<div class="textIntro"><br/>
			<div class="clearfix"></div>
				<ul>
					<li>Journée : <b><?php echo MysqlToDateFr($myDay); ?></b></li>
					<li>Période : <b><?php echo getCreneauPlus($myCreneau); ?></b></li>
				</ul>
			<br /> 
			<?php
					$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>';
			
					echo '<form name="frmSolAffecter" method="post" action="sollicitations-cap-actions.php">';
						echo '<input type="hidden" name="myAction" value="affecterSol">';
						echo '<input type="hidden" name="myDay" value="'.$myDay.'">';
						echo '<input type="hidden" name="myCreneau" value="'.$myCreneau.'">';
		

					echo '<table cellspacing="0" cellpadding="0">';
						echo '<tr><th>&nbsp;</th><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 valign="middle"><input type="radio" name="chxSol" value="'.$v['idCoLy'].'"></td>';
								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 '</form>';
			?>
				<div style="height: 20px;"></div>	
				<div class="btnDeci"><a href="javascript:void(0);" onClick="affectSol()">Choisir cette sollicitation</a></div>
				<div style="height: 20px;"></div>
				<div class="btnDeci"><a href="agenda-cap.php#sollicitation">Annuler</a></div>
			</form>

		</div>
		<div class="clearfix"></div>	
		
	</div>
	
	<?php include('include/footer.php');?>

</body>
</html>