<?php require_once("../../db.php"); ?>

<?php try {
	
		$core = Core::getInstance();
		/*
		$stmt = $core->dbh->prepare("SELECT z.ag FROM (SELECT * 
 FROM messages LEFT JOIN myclients on mess_client_id = client_id
 WHERE mess_id IN (SELECT MAX(mess_id) FROM messages GROUP BY mess_client_id)) as z WHERE z.mess_whosent = '0' group by z.ag order by z.ag ASC");*/
		$stmt = $core->dbh->prepare("SELECT z.ag, z.mess_whosent FROM (SELECT * FROM messages LEFT JOIN myclients on mess_client_id = client_id WHERE mess_id IN (SELECT MAX(mess_id) FROM messages GROUP BY mess_client_id)) as z group by z.ag order by z.ag ASC");
		$stmt -> execute();
		$results = $stmt -> fetchAll(PDO::FETCH_ASSOC);
		
		$output_res = array();
		$i = 0;
		foreach ($results as $key => $value) {
			if ($results[$key]["mess_whosent"] != "0") {
				unset($results[$key]);
			} else {
				$output_res[$i]["ag"] = $results[$key]["ag"];
				$i++;
			}
		}
		
		echo json_encode($output_res);	
		
	} catch(PDOException $pe) {
		trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
	}
?>