<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
?>
<html>
	<head>
		<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
		<title>DevEnv</title>
		<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
	</head>
	<body>
	<table width="60%" class="table table-sm">
		<thead align="left">
			<th>Geofence</th>
			<th>Nr. of users</th>
		</thead>
		<tbody>
		<?php

			function callAPI($apiurl) {
				$curl = curl_init($apiurl);
				curl_setopt($curl, CURLOPT_URL, $apiurl);
				curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

				$headers = array(
				   "Accept: application/json",
				   "Authorization: Bo39A%2b7vKCyaeQEwTTSPJmVtBA6Ki2nKXcW4DvI5HUCE1kR72LpVBH8A3FJp7l7e",
				);
				curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
				//for debug only!
				curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
				curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

				$resp = curl_exec($curl);
				curl_close($curl);

				$result2 = json_decode($resp);
				return $result2;
			}
			$url = "https://patlar.server93.com/comGpsGate/api/v.1/applications/4/geofences";
			$api_result = callAPI($url);
			$assocDataArray = array();
			for ($i = 0; $i < count($api_result); $i++)  {
				$assocDataArray[$api_result[$i]->id] = $api_result[$i]->name;
			}

			foreach($assocDataArray as $key => $value) {
				$gfusers_url = "https://patlar.server93.com/comGpsGate/api/v.1/applications/4/geofences/".$key."/users";
				$apiUser_result = callAPI($gfusers_url);
				$assocUserArray = array();
				for ($i = 0; $i < count($apiUser_result); $i++)  {
					$assocUserArray[$apiUser_result[$i]->id] = $apiUser_result[$i]->name;
				}
				/*foreach($assocUserArray as $userKey => $userValue) {
					//echo $userKey." - ".$userValue."<br/>";
				}*/
				echo "<tr>";
					echo "<td>".$value."</td>";
					echo "<td>".count($apiUser_result)."</td>";
				echo "</tr>";
				$totalUsers += count($apiUser_result);
			}
			echo "<tr>";
				echo "<td><b>Total nr of users</b></td>";
				echo "<td>".$totalUsers."</td>";
			echo "</tr>";

		?>
	</tbody>
	</table>
	</body>
</html>