<?php

	//-----------------------------------------------------------------------------------------------------------------
	// requêtes pour WIREGUARD
	//-----------------------------------------------------------------------------------------------------------------
	echo("</br><u>WIREGUARD</u></br>");
	
	if ( $code_erreur == 0 ) {

		for( $i = 0; $i < $NB_sites; $i++ ) {
			
			$query_peers[$i] = "
				SELECT aqcm_WG_MAC,aqcm_WG_NAME,aqcm_WG_SITE_ID 
				FROM AQCM05_SITE_".$aqcm_SITES_ID[$i].".AQCM05_SONDES_WG 
				WHERE ( aqcm_WG_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_WG_MAC ASC,aqcm_WG_SITE_ID ASC,aqcm_WG_NAME ASC" ;
		
		//echo( $query_all_peers . "</br>" ) ;
		
	}

	if ( $code_erreur == 0 ) {

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

				$Nb_peers = 0 ;

				$aqcm_WG_SITE_ID 	= [];
				$aqcm_WG_MAC 		= [];
				$aqcm_WG_NAME 		= [];
				while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
					
					$aqcm_WG_SITE_ID[$Nb_peers]	= $row["aqcm_WG_SITE_ID"] ;
					$aqcm_WG_MAC[$Nb_peers]			= $row["aqcm_WG_MAC"] ;
					$aqcm_WG_NAME[$Nb_peers]		= $row["aqcm_WG_NAME"] ;

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

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

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



?>