<?php

	include("../M0/session_start.php") ;

	//-------------------------------------------------------------------------------------
	if ( $_SESSION['level'] < 90 ) {
		header('Location: ' . $root_dir . '/M1/index_2.php');
		exit();
	}

	include("../M0/communs_en_tete_HTML_with_big_maps.html") ;
	
	$back_link = "../M3/gestion_sites.php";
	include('../M0/communs_en_tete_home_back.html');

	$return_path = "/M3/gestion_sites.php" ;
	include('../M0/display_user_prefixe_only.php'); 

	$AQCM05_sites		= [] ;

	//-------------------------------------------------------------------------
	$Flag_debug = 1;
	$Flag_debug = 0;

	//-------------------------------------------------------------------------
	$maintenant  = mktime(date("H"), date("i"), date("s"), date("m")  , date("d"), date("Y"));
	$heure_maintenant = date("H", $maintenant);
	$jour_maintenant = date("d", $maintenant);
	$mois_maintenant = date("m", $maintenant);
	$annee_maintenant = date("y", $maintenant);

	//-------------------------------------------------------------------------
	include ("../_identifiants/sql_identification_sans_base.php");

	//-------------------------------------------------------------------------
	date_default_timezone_set('Europe/Paris');
	
	$maintenant = time() ;

	//-------------------------------------------------------------------------
	include ("./list_all_sites_execute_1.php");
	
	if ( $NB_sites > 0 ) {

		$FLAG_no_sites = false ;

		for ($i=0; $i < $NB_sites; $i++) {

			$AQCM05_sites[$i][0]	= $aqcm_site_ID[$i] ;
			$AQCM05_sites[$i][1]	= $aqcm_site_NAME[$i] ;
			$AQCM05_sites[$i][2]	= $aqcm_site_LAT[$i] ;
			$AQCM05_sites[$i][3]	= $aqcm_site_LON[$i] ;
			
			$time_string[$i]		= date( "d/m/Y", $aqcm_site_T_MAX[$i] ) ;

		}
	}
	else {
		$FLAG_no_sites = true ;
	}
	
	$FLAG_no_sites_json		= json_encode($FLAG_no_sites) ;
	$NB_sites_json			= json_encode($NB_sites) ;
	$AQCM05_sites_json		= json_encode($AQCM05_sites) ;
	$maps_latitude_json		= json_encode($maps_latitude) ;
	$maps_longitude_json		= json_encode($maps_longitude) ;
	
	/*
	print_r($FLAG_no_sites_json);
	print_r($NB_sites_json);
	print_r($AQCM05_sites_json);
	*/

?>

<table align="center" width="<?php echo $tables_width ; ?>"  border="2" bordercolor="#ffffff" bgcolor="#000033">	
	<tr>
		<td align="center" >
			<div id="carte" align="center" style="margin-top:0px;margin-bottom:0px;" >
				<!-- Ici s'affichera la carte -->
			</div>
		</td>
	</tr>

</table>

    </body>
</html>

<script>

	var lat 			=  <?php echo $maps_latitude; ?>;	
	var lon 			=  <?php echo $maps_longitude; ?>;	
	var my_zoom 		=  <?php echo $maps_zoom; ?>;
	
	var filtre 			=  String(<?php echo $filtre; ?>).padStart(2, '0');
	if ( filtre == "00" ) { filtre = "AQCM05"; }
	
	var start_index		=  <?php echo $start_index; ?>;	
	var Nb_sites 		=  <?php echo $NB_sites_json; ?>;	
	var AQCM05_sites	=  <?php echo $AQCM05_sites_json; ?>;	
	//window.alert(Nb_sites + " sites recensés");
	
	let my_maxZoom	= 20 ;

	var macarte = null;
	var macarte = new L.map('carte', {center: [lat, lon],zoom: my_zoom,maxZoom: my_maxZoom,zoomControl:false });
	L.tileLayer('https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png', {
		attribution: 'OpenStreetMap',
		maxZoom: my_maxZoom
	}).addTo(macarte);
	var scale = L.control.scale(); 
	scale.addTo(macarte); 

	var marqueurs = {};
	for (let i = start_index; i < Nb_sites; i++) { 
		marqueurs[i] = L.marker([AQCM05_sites[i][2], AQCM05_sites[i][3]]).addTo(macarte);
		contenuHTML = AQCM05_sites[i][1] + "</br>(" + AQCM05_sites[i][0] + ")" ;
		marqueurs[i].bindPopup(contenuHTML);
	}
	
	var popup = L.popup();
	function onMapClick(e) {
		let koko 			= e.latlng.toString() ;
		let jojo 			= koko.substring(koko.indexOf("(") + 1, koko.lastIndexOf(")") );
		let hoho 			= jojo.split(',');
		let lati_string 	= parseFloat(hoho[0].trim()).toFixed(6)
		let longi_string 	= parseFloat(hoho[1].trim()).toFixed(6)
		popup.setLatLng(e.latlng)
		popup.setContent(lati_string + "</br>" + longi_string + "</br>(" + filtre + " : " + Nb_sites + " sites)"  )
		popup.openOn(macarte);
	}

	macarte.on('click', onMapClick);

</script>

