<?php
	
	// http://www.aqcm.fr/M0/get_AQCM05_sites.php
	// http://192.168.1.230/v2/M0/get_AQCM05_sites.php
	
	//--------------------------------------------------------------------------------------------------------
	$Flag_debug_GAS = 1;
	$Flag_debug_GAS = 0;

	$code_erreur_GAS = 0;
	$NB_sites    = 0 ;

	$aqcm_SITE_ID					= [];
	$aqcm_SITE_NAMES				= [];
	$aqcm_SITE_STATUS			= [];
	$aqcm_SITE_T_MAX				= [];
	$aqcm_SITE_NB_MAX			= [];
	$aqcm_SITE_RX_WARNING_EN	= [];
	$aqcm_SITE_LON				= [];
	$aqcm_SITE_LAT				= [];
	$aqcm_SITE_IBM				= [];
	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données
	//--------------------------------------------------------------------------------------------------------
	if ( isset($Flag_login) ) {
		if ( $Flag_login ) {
			include ("./_identifiants/sql_identification_main.php");
		}
		else {
			include ("../_identifiants/sql_identification_main.php");
		}
	}
	else {
		include ("../_identifiants/sql_identification_main.php");
	}

	$link_AQCM05 = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	
	if (!$link_AQCM05) {
		$code_erreur_GAS = 1 ;
		
		if ($Flag_debug_GAS == 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_GAS);
	}

	//--------------------------------------------------------------------------------------------------------
	// recherche des sites
	//--------------------------------------------------------------------------------------------------------
	$query_sites_list = "
		SELECT SITES_ID, SITES_NAMES, SITES_STATUS, SITES_T_MAX, SITES_NB_MAX, SITES_RX_WARNING_EN, SITES_LON, SITES_LAT, SITES_IBM 
		FROM AQCM05_LIST_SITES 
		ORDER BY SITES_ID
	";

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

	$result_sites_list = mysqli_query($link_AQCM05, $query_sites_list); 
	
	if (!$result_sites_list) {
		$code_erreur_GAS = 11;
	} 
	else {
		
		if (mysqli_num_rows($result_sites_list) < 1) {	

			$code_erreur_GAS = 12;
			
		} 
		else {
			
			$i=0;
			
			while ($row = mysqli_fetch_array($result_sites_list, MYSQLI_BOTH)) {

				$aqcm_SITE_ID[$i]				= $row["SITES_ID"];
				$aqcm_SITE_NAMES[$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"];
				$aqcm_SITE_IBM[$i]				= $row["SITES_IBM"];
				
				$i++ ;
			}
			
			$NB_sites = $i;
			
			if ($Flag_debug_GAS == 1) {
				
				echo( "NB_sites = " . $NB_sites . "</br>" ) ;
				
			}
			
			for ($j = 0; $j < $NB_sites; $j++) {
				
				$sites_from_AQCM05_SITES_LIST[$j] 			= $aqcm_SITE_ID[$j] ;
				$sites_from_AQCM05_SITES_NAMES[$j]			= $aqcm_SITE_NAMES[$j] ;
				$sites_from_AQCM05_SITES_STATUS[$j] 			= $aqcm_SITE_STATUS[$j] ;
				$sites_from_AQCM05_SITES_T_MAX[$j] 			= $aqcm_SITE_T_MAX[$j] ;
				$sites_from_AQCM05_SITES_NB_MAX[$j] 			= $aqcm_SITE_NB_MAX[$j] ;
				$sites_from_AQCM05_SITES_RX_WARNING_EN[$j] 	= $aqcm_SITE_RX_WARNING_EN[$j] ;
				$sites_from_AQCM05_SITES_LON[$j]				= $aqcm_SITE_LON[$j] ;
				$sites_from_AQCM05_SITES_LAT[$j]				= $aqcm_SITE_LAT[$j] ;
				$sites_from_AQCM05_SITES_IBM[$j]				= $aqcm_SITE_IBM[$j] ;
				
				if ($Flag_debug_GAS == 1) {
					echo( $sites_from_AQCM05_SITES_LIST[$j] . "</br>" ) ;
					echo( $sites_from_AQCM05_SITES_NAMES[$j] . "</br>" ) ;
					echo( " " . "</br>" ) ;
				}
			}

		}
	}

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

	mysqli_close($link_AQCM05);

?>
