<?php require_once("../../db.php"); ?>
<?php try {
		
		$core = Core::getInstance();
		$stmt = $core->dbh->prepare("SELECT mess_id, mess_message, client_number FROM messages LEFT JOIN myclients ON mess_client_id = client_id WHERE NOT mess_status = '0' AND mess_whosent ='1' AND mess_date < now() - interval 5 MINUTE AND ag = :ag");
		$stmt -> bindParam("ag", $_GET["ag"], PDO::PARAM_INT);
		
		$stmt -> execute();
		$results = $stmt -> fetchAll(PDO::FETCH_ASSOC);
		
		foreach ($results as $key => $value) {
			$results[$key]["shorten_message"] = mb_substr($value["mess_message"], 0, $cf_howlongstrings_error);
			//$results[$key]["mess_date"] = date('d.m.Y. H:i:s', strtotime($value["mess_date"]));
			//$results[$key]["js_mess_date"] = strtotime($value["mess_date"]);
		}
		
		echo json_encode($results);
		
	} catch(PDOException $pe) {
		
		trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
		
	}
	
	// SELECT * FROM messages WHERE mess_client_id in (SELECT m.mess_client_id FROM messages m INNER JOIN (SELECT mess_client_id, MAX(mess_date) as mess_date FROM messages WHERE mess_whosent = '0' GROUP BY mess_client_id) as max USING(mess_client_id, mess_date)) and mess_whosent = '1'
?>