<?php
include 'common.php';

function AccountMailBoxQuery($account_id){
	//Connect DB
	$dbhandle = DB_Connect();
	
	
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");

	mysqli_query($dbhandle,"set session character_set_connection=utf8;");
	mysqli_query($dbhandle,"set session character_set_results=utf8;");
	mysqli_query($dbhandle,"set session character_set_client=utf8;");

	$strQuery = "select * from mail_box_account where account_id='".$account_id."' order by id asc";
	
	$count = 0;

	if($result = mysqli_query($dbhandle, $strQuery))
	{
		$list = "";
		while($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
		{
			$count++;
			$dataArray[] = $row["id"].",". $row["account_id"].",".$row["title"] .",".$row["content"].",".$row["item_id"].",".$row["item_count"].",".$row["time_limit"].",".$row["time_send"];
		}
		
	}
	else
	{
		
	}
	mysqli_free_result($result);

	
	if($count <= 0  )
	{
		$return = "NOT_EXIST_MAIL";
		
		$response = array('result'=>$return,'error'=>"",'list'=>null);
	}
	else
	{
		$return = "ACCOUNT_MAIL_BOX_LIST_OK";
		$response = array('result'=>$return,'error'=>"",'list'=>$dataArray);
	}
	
	$isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
	(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
	($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain');
 
	echo json_encode($response);
		
	mysqli_close($dbhandle);
}

$json = json_decode(file_get_contents('php://input'));
$account_id = $json->user_uid;

if(CheckValidatedServerIP())
{
	AccountMailBoxQuery($account_id);
}
else
{
	$response = "INVALIDATED_SERVER";
	$error = "CheckValidatedServerIP ERROR";
	$isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
	(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
	($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain'); 
 
	echo json_encode(array(	'result'=>$response,'error'=>$error	));
}

?>