<?php
	
	//--------------------------------------------------------------------------------------------------------
	// initialisation des variables
	//--------------------------------------------------------------------------------------------------------
	$Flag_debug_1 = 1;
	$Flag_debug_1 = 0;

	$code_erreur = 0;
	$NB_sites   = 0 ;
	
	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données
	//--------------------------------------------------------------------------------------------------------
	$mysql_base ='AQCM05_main' ;
	$link = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	
	if ( ! $link) {
		
		$code_erreur = 100;
		
		if ($Flag_debug_1 == 1) {
			echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo '</br>';
			echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; echo '</br>';
		}

		echo "Connexion impossible à la base de donnée MySQL !" . PHP_EOL; echo '</br>';
		exit($code_erreur);
	}

	//--------------------------------------------------------------------------------------------------------
	// recherche des sondes du site demandé
	//--------------------------------------------------------------------------------------------------------
	$query = "
		SELECT 
			SITES_ID,SITES_NAMES,SITES_STATUS,SITES_T_MAX,SITES_NB_MAX,SITES_RX_WARNING_EN,SITES_LON,SITES_LAT 
		FROM AQCM05_LIST_SITES 
		WHERE (1=1) 
		ORDER BY SITES_ID
		";

	if ($Flag_debug_1 == 1) {
		echo $query . "</br>" ;
	}

	$code_erreur = 0;

	$result_configuration = mysqli_query($link, $query); 
	
	if (!$result_configuration) {
		
		$code_erreur = 11;
		
	} else {
		
		if (mysqli_num_rows($result_configuration) == 0) {	
			$code_erreur = 12;
			
		} else {
			
			$i=0;
			while ($row = mysqli_fetch_array($result_configuration, MYSQLI_BOTH)) {

				if ( $_SESSION['aqcm_PREFIXE'] == "00" ) {
						
					$aqcm_site_ID[$i]				= $row["SITES_ID"] ;
					$aqcm_site_NAME[$i]				= $row["SITES_NAMES"] ;
					$aqcm_site_STATUS[$i]			= $row["SITES_STATUS"] ;
					$aqcm_site_T_MAX[$i]				= $row["SITES_T_MAX"] ;
					$aqcm_site_NB_MAX[$i]			= $row["SITES_NB_MAX"] ;
					$aqcm_site_RX_WARNING_EN[$i]	= $row["SITES_RX_WARNING_EN"] ;
					$aqcm_site_LON[$i]				= $row["SITES_LON"] ;
					$aqcm_site_LAT[$i]				= $row["SITES_LAT"] ;
					$i++ ;

				}
				else {
					
					if ( substr( $row["SITES_ID"], 0, 2) == $_SESSION['aqcm_PREFIXE'] ) {
						
						$aqcm_site_ID[$i]				= $row["SITES_ID"] ;
						$aqcm_site_NAME[$i]				= $row["SITES_NAMES"] ;
						$aqcm_site_STATUS[$i]			= $row["SITES_STATUS"] ;
						$aqcm_site_T_MAX[$i]				= $row["SITES_T_MAX"] ;
						$aqcm_site_NB_MAX[$i]			= $row["SITES_NB_MAX"] ;
						$aqcm_site_RX_WARNING_EN[$i]	= $row["SITES_RX_WARNING_EN"] ;
						$aqcm_site_LON[$i]				= $row["SITES_LON"] ;
						$aqcm_site_LAT[$i]				= $row["SITES_LAT"] ;
						$i++ ;
						
					}
										
				}

			}
			
			$NB_sites = $i;
			$filtre	= $_SESSION['aqcm_PREFIXE'] ;
			
			if ( $_SESSION['aqcm_PREFIXE'] == "00" ) {
				$maps_latitude	= "46.691653" ;
				$maps_longitude	= "1.977539" ;
				$maps_zoom			= 5 ;
				$start_index		= 1 ;
			}
			else {
				$maps_latitude	= (array_sum($aqcm_site_LAT))/$NB_sites ;
				$maps_longitude	= (array_sum($aqcm_site_LON))/$NB_sites ;
				$maps_zoom			= 9 ;
				$start_index		= 0 ;
			}

			if ($Flag_debug_1 == 1) {
				echo "NB_sites = " . $NB_sites . "</br>";
				
			}
		}
	}

	if ($Flag_debug_1 == 1) {
		if ( $code_erreur != 0 ) {
			echo "code erreur = " . strval($code_erreur) . "</br>";
		}
	}

	mysqli_close($link);

?>
