<?php
require_once "header.php";
require_once "config.php";
require_once $mohaPath . "/utils.php";
require_once $mohaPath . "/constants.php";
require_once "class/db.class.php";
require_once $configPath . "properties2log.php";

$indexIeeeAddress = htmlGetFriendlyNames();

//print_r($indexIeeeAddress);
print (_("This is the Statistics of Moha"));

if (empty($indexIeeeAddress))
{
	$response = _("ERROR getting list of Friendly names: json error =>") . json_last_error_msg() .EOLH;
}else
{
	$query = "SELECT DISTINCT device, property FROM logs";
	$result = $mohaDB->query($query);
	$devices = mysqli_fetch_all($result, MYSQLI_ASSOC);
	$devicesList = array();
//	print_r($devices);
	print '<form method="GET" action="display_stats.php">';
	//print '<input type="hidden" name="fn" value="">';
	print '<label for="devicesSelectList">' . _("Choose a device:") . '</label>
		<select name="device" id="devicesSelectList">' ;
	foreach ($devices as $value)
	{
		if ( array_key_exists($value["device"], $indexIeeeAddress))
		{
			$devicesList[$value["device"]]["fn"] = $indexIeeeAddress[$value["device"]];
			$devicesList[$value["device"]][] = $value["property"];
		}
	}
	//$devicesList = array_intersect_key($indexIeeeAddress, $tmp);
	//print_r($devicesList);
	foreach($devicesList as $ieeeAddress => $array)
	{
		print '<optgroup label="' . $array["fn"] . '">';
		foreach($array as $key => $property)
		{
			if ($key != "fn")
			{
				print '<option value="' . $ieeeAddress . '|' . $array["fn"] . '|' . $property . '">' . $property . '</option>' . EOL;
			}
		}
		print '</optgroup>' . EOL;
	}
	print "</select>" . EOLH;
	print '<input type="date" name="startDate">' . EOLH;
	print '<input type="date" name="endDate">' . EOLH;
	print '<input type="submit" value="Afficher les statistiques">';
	print "</form>" . EOL;
}

require_once "footer.php";
?>