<?php
include 'common.php';

function AlreadyExistsMaxCCU($dbhandle)
 {
	
	 $return = false;
	 $strQuery = "select count(*) from max_ccu";
	 if($query_result = mysqli_query($dbhandle, $strQuery)){
		 $data = mysqli_fetch_row($query_result);
		 if( $data[0] > 0 )
		 {
			 $return = true;
		 }
		 mysqli_free_result($query_result); 
	 }
	
	 return $return;
}

function GetCCU($dbhandle){
	
	$ccu = 0;		
	$strQuery = "select * from zone_list";	
	//Query Result
	if ($query_result = mysqli_query($dbhandle, $strQuery)) {
		
		
		while($row=mysqli_fetch_array($query_result, MYSQLI_ASSOC))
		{			
			$ccu +=$row["init_x"];
		}		
		mysqli_free_result($query_result);
	}
	
	return $ccu;

}

function GetMaxCCU($dbhandle)
{
	$ccu = 0;
	
	
	 $strQuery = "select ccu from max_ccu where id=1";
	if($query_result = mysqli_query($dbhandle, $strQuery))
	{
		$data = mysqli_fetch_row($query_result);
		if(count($data) > 0)
		{			
			$ccu = $data[0];
			
			mysqli_free_result($query_result); 
		}
	}
	else{
		$ccu = 0;
	}	
	
	return $ccu;
}

function UpdateMaxCCU($dbhandle)
{
	$max_ccu = GetMaxCCU($dbhandle);
	$ccu = GetCCU($dbhandle);
	if($ccu > $max_ccu)
	{
		$now = new DateTime();
		$now->setTimezone(new DateTimeZone('UTC'));
		$strTime =  date_format($now, "Y-m-d H:i:s");
	
	
		$query = "update max_ccu set ccu=".$ccu .", report_time='".$strTime."' where id=1";		
		//Query Result
		$result = @mysqli_query($dbhandle,$query);		
	}
}

function InsertCCULog($dbhandle)
{
	//$max_ccu = GetMaxCCU($dbhandle);
	$ccu = GetCCU($dbhandle);
	$now = new DateTime();
	$now->setTimezone(new DateTimeZone('UTC'));
	$strTime =  date_format($now, "Y-m-d H:i:s");


	//$query = "update max_ccu set ccu=".$ccu .", report_time='".$strTime."' where id=1";
	$query = "insert into max_ccu (ccu,report_time) values(".$ccu .", '".$strTime."')";
	//Query Result
	$result = @mysqli_query($dbhandle,$query);		
	
}

function InsertDefaultMaxCCULValue($dbhandle, $ccu)
{
	
	$now = new DateTime();
	$now->setTimezone(new DateTimeZone('UTC'));
	$strTime =  date_format($now, "Y-m-d H:i:s");


	
	$query = "insert into max_ccu (id, ccu,report_time) values(1,".$ccu .", '".$strTime."')";
	//Query Result
	$result = @mysqli_query($dbhandle,$query);		
	
}

function ReportCCU($ccu, $zone_id, $channel, $server_version)
{
	$dbhandle = DB_Connect();
	//Select DB
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");
	
	
	
	if(!AlreadyExistsMaxCCU($dbhandle))
	{
		InsertDefaultMaxCCULValue($dbhandle,$ccu);
	}
	
	
	
	$response = "REPORT_CCU_FAILED";
	$error = "";
	
	
	
	
	$query = "update zone_list set init_x=".$ccu." where zone_id=".$zone_id." and channel_num=".$channel." and server_version='".$server_version."'";
	
	
	

	//Query Result
	$result = @mysqli_query($dbhandle,$query);
	
	if($result)
	{
		$response = "REPORT_CCU_OK";
		// don't use for upate/insert query, because these query result is true/false, not mysqli_result object
		//mysqli_free_result($result); 
	}
	else
	{
		$error = mysqli_error($dbhandle);
	}
	
	
	
 
    $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));
	
	
	
	
	
	UpdateMaxCCU($dbhandle);
	//InsertCCULog($dbhandle);
	
	
	 mysqli_close($dbhandle);
}

ReportCCU($_GET['ccu'], $_GET['zone_id'], $_GET['channel'], $_GET['server_version']);

?>