<?php
include('config/config.inc.php');
include('include/agenda.inc.php');
include('config/lib.inc.php');
$maCleDeCryptage = getCleDeCryptage();	

$tabDate=array();
foreach($arrWeek as $kw=>$vw){
	foreach($vw as $vDay){
		if(!empty($vDay)){
			$tabDate[]=$vDay;
		}
	}
}

if(isset($_GET['filtreCarte']) && !empty($_GET['filtreCarte']) && in_array($_GET['filtreCarte'],$tabDate)){
	$current_date=$_GET['filtreCarte'];
}else $current_date='all';

$query="select * from eta where lat!='' and lon!=''";
$db = connect_base();
$queryRequest = $db->prepare( $query );
$queryRequest->execute();
$data=$queryRequest->fetchAll();

$info=array();


foreach($data as $cle=>$val){
	$query="select * from capitaines where num_eta=:num_eta";
	$db = connect_base();
	$queryRequest = $db->prepare( $query );
	$queryRequest->bindParam( ':num_eta', $val['id_eta'], PDO::PARAM_INT );
	$queryRequest->execute();
	if($queryRequest->rowCount()>0){;
		$info[$val['id_eta']]['nom']=decrypter($maCleDeCryptage,$val['nom_eta']);
		$info[$val['id_eta']]['adresse']=decrypter($maCleDeCryptage,$val['adresse1']);
		$info[$val['id_eta']]['cp']=decrypter($maCleDeCryptage,$val['cp']);
		$info[$val['id_eta']]['ville']=decrypter($maCleDeCryptage,$val['ville']);
		$info[$val['id_eta']]['lat']=$val['lat'];
		$info[$val['id_eta']]['lon']=$val['lon'];
		foreach($arrWeek as $kw=>$vw){
			foreach($vw as $vDay){
				if(!empty($vDay)){
					$info[$val['id_eta']][$vDay] = 0;
				}
			}
		}
	}
}
$query="select * from agenda where statut='libre'";
$db = connect_base();
$queryRequest = $db->prepare( $query );
$queryRequest->execute();
$data=$queryRequest->fetchAll();
foreach($data as $cle=>$val){
	$info[$val['numEta']][$val['jour']]++;
}


foreach($info as $cle=>$val){
	foreach($arrWeek as $kw=>$vw){
		foreach($vw as $vDay){
			if(!empty($vDay)){
				if($val[$vDay]>0) $info[$cle]['all']=1;
			}
		}
	}
	if(!isset($info[$cle]['all'])) $info[$cle]['all']=0;
}
?>
<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="css/bootstrap.min.css">
		<link rel="stylesheet" href="css/style.css">
		<script src="js/jquery-3.2.1.min.js"></script>
		<script src="js/bootstrap.min.js"></script>      
		<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAD8dr4sS_iFafcU_AowzS9p9B306iQEZQ"></script>		
		<title>FAFIH</title>
		<style>
			.lienCarte{color: #87318b;font-family: "Myriad Pro Bold";}
			.lienCarte:hover{ text-decoration: none;color: #87318b;}
			.filtreCarte{ text-align: center;font-size: 16px;color: #87318b;text-align: center;font-family: "Myriad Pro Bold";text-transform: uppercase; padding:16px 0 10px 0;}
			.filtreCarte select{ color:#000; margin-top:6px;}
		</style>
		<script>
			function contact_eta(a){
				document.frmCE.ncfa.value=a;
				document.frmCE.submit();
			}
			function pinSymbol(color) {
				return {
					path: 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z M -2,-30 a 2,2 0 1,1 4,0 2,2 0 1,1 -4,0',
					fillColor: color,
					fillOpacity: 1,
					strokeColor: '#000',
					strokeWeight: 2,
					scale: 1,
			   };
			}	
			//#FFA64D on
			//#CACAD9 off
			function initialisation(){
				var optionsCarte = {
					zoom: 5,
					center: new google.maps.LatLng(46.52863469527167,2.43896484375)
				}
				maCarte = new google.maps.Map(document.getElementById("divCarte"), optionsCarte);
				<?php
				if($current_date!='all') $param="&mydate=".$current_date;
				else $param="";
				foreach($info as $cle=>$val){
					$text="";
					$text.=$val['nom']."<br />".$val['adresse']."<br />".$val['cp']."<br />".$val['ville'];
					if($val[$current_date]>0){
						$color="#FFA64D";
						$text.="<br /><br /><a href=\"sollicitation-agenda.php?ncfa=".$cle."\" class=\"lienCarte\">Solliciter l'établissement</a>";
					}else{
						$color="#CACAD9";
						$text.="<br /><br /><a href=\"javascript:contact_eta(".$cle.");\" class=\"lienCarte\">Contacter l'établissement</a>";
					}
					echo "var optionsMarqueur = {
					position: new google.maps.LatLng(".$val['lat'].",".$val['lon']."),
					map: maCarte,
					title:'".addslashes($val['nom_eta'])."',
					icon: pinSymbol(\"".$color."\"),
					};
					var marqueur".$cle." = new google.maps.Marker(optionsMarqueur);
					  var infowindow".$cle." = new google.maps.InfoWindow({
						content: ''
					  });
					marqueur".$cle.".addListener('click', function() {
							infowindow".$cle.".setContent('".addslashes($text)."');
							infowindow".$cle.".open(maCarte, this);
					  });
				";
					
				}
				?>
				
			}
			$(document).ready(function() {
				initialisation();
			});
			function reloadCarte(a){
				document.frmCarte.filtreCarte.value=a;
				document.frmCarte.submit();
			}
		</script>
	</head>
	<body>
	<?php include('include/header.php');?>
		<div class="container-fluid" style="margin-top: -20px;">
			<div class="row" style="background-color: #5B2050;">
				<a href="#" style="cursor:auto">
					<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 noPad">
					<img class="visible-sm visible-md visible-lg img-responsive imageBandeau" src="img/slide/deciday_decouvrez_deciday_slider.jpg" alt="Métiers Hotel-Resto">
					<img class="hidden-lg hidden-md hidden-sm img-responsive imageBandeau" src="img/slide/slider_new_resp.jpg" alt="Métiers Hotel-Resto">
				</div>
				</a>
			</div>
		</div>
		
		<div class="container contenuTxt"><br>
			<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">UNE JOURNÉE NATIONALE POUR DÉCOUVRIR 
un métier et se préparer un avenir
</div>
			<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs bgTitrePage"></div>
			<div class="clearfix"></div>
    </div>
	<div class="container contenuCarte">
		<div class="filtreCarte">Filtrer sur une date : <br />
			<select name="dateCarte" id="dateCarte" onChange="reloadCarte(this.value);">
				<option value="all">toutes les dates</option>
				<?php
				foreach($arrWeek as $kw=>$vw){
					foreach($vw as $vDay){
						if(!empty($vDay)){
							echo '<option value="'.$vDay.'"';
							if($vDay==$current_date) echo ' selected="selected"';
							echo '>'.$vDay.'</option>';
						}
					}
				}
				?>
			</select>
		</div>
		<div id="divCarte" style='height:500px;'></div>
	</div><!--fin contenuCarte-->
	<form name="frmCarte" method="get">
	<input type="hidden" name="filtreCarte" id="filtreCarte">	
	</form>	
	<form name="frmCE" method="post" action="contact-eta.php">
		<input type="hidden" name="ncfa" />
		<input type="hidden" name="myaction" value="contactEta" />
	</form>	
		<?php include('include/footer.php');?>
	</body>
</html>