<?php

	//-----------------------------------------------------------------------------------------------------------------
	// requêtes pour WIREGUARD
	//-----------------------------------------------------------------------------------------------------------------
	echo("</br><u>MESURES</u></br>");
	
	$Nb_peers = 0 ;

	if ( $code_erreur == 0 ) {

		for( $i = 0; $i < $NB_sites; $i++ ) {
			
			$query_peers[$i] = "
				SELECT aqcm_MAC,aqcm_NAME,aqcm_SITE_ID 
				FROM AQCM05_SITE_".$aqcm_SITES_ID[$i].".AQCM05_SONDES_MESURES 
				WHERE ( aqcm_SITE_ID != '00000_000' )
			";
			//echo( $query_peers[$i] . "</br>" ) ;
		}
		
		$query_all_peers = $query_peers[0] ;
		
		for( $i = 1; $i < $NB_sites; $i++ ) {
			$query_all_peers = $query_all_peers . " UNION ALL " . $query_peers[$i] ;
		}
		$query_all_peers = $query_all_peers . " ORDER BY aqcm_MAC ASC,aqcm_SITE_ID ASC,aqcm_NAME ASC" ;
		
		//echo( $query_all_peers . "</br>" ) ;
		
	}

	if ( $code_erreur == 0 ) {

		$result = mysqli_query($link, $query_all_peers); 
		
		if ( ! $result ) {
			
			echo "MySQL -> erreur lors de la requête !<br>"; flush() ;
			echo "</br>";  flush() ;
			
		}
		else {
			
			if ( mysqli_num_rows($result) == 0 ) {
				
				echo "MySQL -> résultat de la requête vide !<br>"; flush() ;
				echo "</br>";  flush() ;
				
			}
			else {

				$aqcm_SITE_ID 	= [];
				$aqcm_MAC 		= [];
				$aqcm_NAME 		= [];
				while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
					
					$aqcm_SITE_ID[$Nb_peers]	= $row["aqcm_SITE_ID"] ;
					$aqcm_MAC[$Nb_peers]		= $row["aqcm_MAC"] ;
					$aqcm_NAME[$Nb_peers]		= $row["aqcm_NAME"] ;

					//echo($aqcm_MAC[$Nb_peers]	 . "</br>");
					
					$Nb_peers++ ;
					
					
				}
			}
		}
	}

	if ( ( $code_erreur == 0 ) && ( $Nb_peers > 0 ) ) {

		$IP_occurences_mesures = array_count_values($aqcm_MAC) ;
		
		$k = 0;
		for( $i = 0; $i < $Nb_peers; $i++) {
			
			$Nb_occurences = $IP_occurences_mesures[$aqcm_MAC[$i]] ;
			if ( $Nb_occurences > 1 ) {
				
				echo("<font color='".$duplicate_colors[$k]."'>");
				for( $j = 0; $j < $Nb_occurences; $j++ ) {
					
					echo( "&nbsp;&nbsp;" . $aqcm_MAC[$i] . " ; " . $aqcm_SITE_ID[$i] . " ; " . $aqcm_NAME[$i] );echo("</br>");
					$i++;
					
				}
				$i--;
				echo("</font>");
				$k++;
				if ($k>1) $k=0;
				
			}
			else {
				
				//echo( "&nbsp;&nbsp;&nbsp;single : " . $aqcm_MAC[$i] . " ; " . $aqcm_SITE_ID[$i] . " ; " . $aqcm_NAME[$i] );echo("</br>");
				
			}
			
		}
		
	}
	else {
		echo("</br>");
	}



?>