<?php
global $objDbConn;
if(0)
{
	echo "$PHP_SELF<br>";
	echo "sysTypeNo=$sysTypeNo,show_type=$show_type,page_id=$page_id,keyOne=$keyOne,keyTwo=$keyTwo<br>";
	echo "sel_date_from=$sel_date_from,sel_date_to=$sel_date_to<br>";
	echo "select_checkbox_id=$select_checkbox_id<br>";
}

echo "<input type=\"hidden\" name=\"select_checkbox_id\" value=\"$select_checkbox_id\">";
echo "<input type=\"hidden\" name=\"scroll_y\" value=\"$scroll_y\">";

$cstaConf=GetCstaConf($sysTypeNo);

$slot=$sel_date_from-$sel_date_to+1;
$startTime=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
$endTime=date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-$sel_date_to,date("Y")));
//echo "startTime=$startTime,endTime=$endTime<br>";

$cstaDataChip[3]=GetCstaDataByHour($cstaConf,$startTime,$endTime,"sum",3,$keyOne);
$cstaDataChip[4]=GetCstaDataByHour($cstaConf,$startTime,$endTime,"sum",4,$keyOne);

$total_hour_num=$slot*24;
for($i=0;$i<$total_hour_num;$i++)
{
	$inData[Attempt][$i]=$cstaDataChip[3][0][3][$keyOne][Attempt][$i]+0;
	$inData[Success][$i]=$cstaDataChip[3][0][3][$keyOne][Success][$i]+0;
	$inData[Duration][$i]=$cstaDataChip[3][0][3][$keyOne][Duration][$i]+0;
	$inData[SignalErr][$i]=$cstaDataChip[3][0][3][$keyOne][SignalErr][$i]+0;
	$inData[CallDeny][$i]=$cstaDataChip[3][0][3][$keyOne][CallDeny][$i]+0;
	$inData[NetBusy][$i]=$cstaDataChip[3][0][3][$keyOne][NetBusy][$i]+0;
	$inData[CallerClear][$i]=$cstaDataChip[3][0][3][$keyOne][CallerClear][$i]+0;
	$inData[Normal][$i]=$cstaDataChip[3][0][3][$keyOne][Normal][$i]+0;
	$inData[SubBusy][$i]=$cstaDataChip[3][0][3][$keyOne][SubBusy][$i]+0;
	$inData[NoAnswer][$i]=$cstaDataChip[3][0][3][$keyOne][NoAnswer][$i]+0;
	
	$outData[Attempt][$i]=$cstaDataChip[4][0][4][$keyOne][Attempt][$i]+0;
	$outData[Success][$i]=$cstaDataChip[4][0][4][$keyOne][Success][$i]+0;
	$outData[Duration][$i]=$cstaDataChip[4][0][4][$keyOne][Duration][$i]+0;
	$outData[SignalErr][$i]=$cstaDataChip[4][0][4][$keyOne][SignalErr][$i]+0;
	$outData[CallDeny][$i]=$cstaDataChip[4][0][4][$keyOne][CallDeny][$i]+0;
	$outData[NetBusy][$i]=$cstaDataChip[4][0][4][$keyOne][NetBusy][$i]+0;
	$outData[CallerClear][$i]=$cstaDataChip[4][0][4][$keyOne][CallerClear][$i]+0;
	$outData[Normal][$i]=$cstaDataChip[4][0][4][$keyOne][Normal][$i]+0;
	$outData[SubBusy][$i]=$cstaDataChip[4][0][4][$keyOne][SubBusy][$i]+0;
	$outData[NoAnswer][$i]=$cstaDataChip[4][0][4][$keyOne][NoAnswer][$i]+0;
	
	$inoutData[Attempt][$i]=$inData[Attempt][$i]+$outData[Attempt][$i];
	$inoutData[Success][$i]=$inData[Success][$i]+$outData[Success][$i];
	$inoutData[Duration][$i]=$inData[Duration][$i]+$outData[Duration][$i];
	$inoutData[SignalErr][$i]=$inData[SignalErr][$i]+$outData[SignalErr][$i];
	$inoutData[CallDeny][$i]=$inData[CallDeny][$i]+$outData[CallDeny][$i];
	$inoutData[NetBusy][$i]=$inData[NetBusy][$i]+$outData[NetBusy][$i];
	$inoutData[CallerClear][$i]=$inData[CallerClear][$i]+$outData[CallerClear][$i];
	$inoutData[Normal][$i]=$inData[Normal][$i]+$outData[Normal][$i];
	$inoutData[SubBusy][$i]=$inData[SubBusy][$i]+$outData[SubBusy][$i];
	$inoutData[NoAnswer][$i]=$inData[NoAnswer][$i]+$outData[NoAnswer][$i];
}


//In&Out
if($keyTwo == 0)
{
	$cstaData=$inoutData;
}
//In
else if($keyTwo == 1)
{
	$cstaData=$inData;
}
else if($keyTwo == 2)
{
	$cstaData=$outData;
}

if(0)
{
	echo "<pre>";
	print_r($cstaData);
	echo "</pre>";
}

//Get the trunk title
$trunk_element_title="--";

$dualTableLevelWhere="T1.level_1=T2.level_1 AND
					  T1.level_2=T2.level_2 AND
					  T1.level_3=T2.level_3 AND
					  T1.level_4=T2.level_4 AND
					  T1.level_5=T2.level_5 AND
					  T1.level_6=T2.level_6 AND
					  T1.level_7=T2.level_7 AND
					  T1.level_8=T2.level_8 AND
					  T1.level_9=T2.level_9 AND
					  T1.level_10=T2.level_10";
					  
//===get the title of tg=====
$instanceNo=omc_keep_length($keyOne,'0',4);
$database="OBJ_320";
$sql="SELECT T1.* FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND T2.name_2='tgTitle' and T1.instanceNo='$instanceNo' ";
$result=@mysqli_query($objDbConn,$sql);
$TrunkGroupCount=mysqli_num_rows($result);

if($TrunkGroupCount>0)
{
	$rows = @mysqli_fetch_array($result);
	$trunk_element_title=$rows[initValue];
}
//Table-1
{
	$table_title="TG $keyOne [$trunk_element_title]";
	if($keyTwo == 0)
		$table_title.=" Incoming&Outgoing ";
	else if($keyTwo == 1)
		$table_title.=" Incoming ";
	else if($keyTwo == 2)
		$table_title.=" Outgoing ";
		
	$table_title.="Statistic&nbsp;&nbsp;Date:";
	if($slot>1)
		$table_title.=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")))." to ".date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_to,date("Y")));
	else
		$table_title.=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
	
	$columnTitle=array(
		"Time",
		"Attempt",
		"Success",
		"Call Deny",
		"Net Busy",
		"Caller<br>Clear",
		"Sub Busy",
		"No<br>Answer",
		"Signal<br>Error",
		"Total Call<br>Duration",
		"Average Call<br>Duration(s)"
	);
	$columnNum=sizeof($columnTitle);
	
	echo "<br><b><center>$table_title</center></b>";
	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	echo "<tr bgcolor=\"#E6E6E6\">";
	for($i=0;$i<$columnNum;$i++)
	{
		echo "<td align=\"center\">{$columnTitle[$i]}</td>";
	}
	echo "</tr>";
	
	for($hour=0;$hour<$total_hour_num;$hour++)
	{
		$hour_title=date("m-d H",mktime($hour,0,0,date("m"),date("d")-$sel_date_from,date("Y")))."H";
		echo "<tr>";
		
		echo "<td align=\"center\">";
		echo "$hour_title";
		echo "</td>";
		
		//Attempt
		echo "<td align=\"center\">";
		echo "{$cstaData[Attempt][$hour]}<br>";
		echo "</td>";
		
		//Success
		echo "<td align=\"center\">";
		echo "{$cstaData[Success][$hour]}<br>";
		$percent=GetPercent($cstaData[Success][$hour],$cstaData[Attempt][$hour]);
		echo "{$percent}";
		echo "</td>";
		
		//CallDeny
		echo "<td align=\"center\">";
		echo "{$cstaData[CallDeny][$hour]}<br>";
		$percent=GetPercent($cstaData[CallDeny][$hour],$cstaData[Attempt][$hour]);
		echo "{$percent}";
		echo "</td>";
		
		//NetBusy
		echo "<td align=\"center\">";
		echo "{$cstaData[NetBusy][$hour]}<br>";
		$percent=GetPercent($cstaData[NetBusy][$hour],$cstaData[Attempt][$hour]);
		echo "{$percent}";
		echo "</td>";
		
		//CallerClear
		echo "<td align=\"center\">";
		echo "{$cstaData[CallerClear][$hour]}<br>";
		$percent=GetPercent($cstaData[CallerClear][$hour],$cstaData[Attempt][$hour]);
		echo "{$percent}";
		echo "</td>";

		//SubBusy
		echo "<td align=\"center\">";
		echo "{$cstaData[SubBusy][$hour]}<br>";
		$percent=GetPercent($cstaData[SubBusy][$hour],$cstaData[Attempt][$hour]);
		echo "{$percent}";
		echo "</td>";
		
		//NoAnswer
		echo "<td align=\"center\">";
		echo "{$cstaData[NoAnswer][$hour]}<br>";
		$percent=GetPercent($cstaData[NoAnswer][$hour],$cstaData[Attempt][$hour]);
		echo "{$percent}";
		echo "</td>";
		
		//SignalErr
		echo "<td align=\"center\">";
		echo "{$cstaData[SignalErr][$hour]}<br>";
		$percent=GetPercent($cstaData[SignalErr][$hour],$cstaData[Attempt][$hour]);
		echo "{$percent}";
		echo "</td>";
	
		//TotalCallDuration
		echo "<td align=\"center\">";
		echo "{$cstaData[Duration][$hour]}<br>";
		$timestamp=SecondToTimeStamp($cstaData[Duration][$hour]);
		echo "{$timestamp}";
		echo "</td>";
		
		//AverageCallDuration
		if($cstaData[Success][$hour] > 0)
			$value=ceil($cstaData[Duration][$hour]/$cstaData[Success][$hour]);
		else
			$value=0;
		echo "<td align=\"center\">";
		echo "{$value}";
		echo "</td>";
		
		echo "</tr>";
	}
	echo "</table>";
}

//Graphics-1
{
	if($flag0 == "on")
	{
		$tmpColor[]=array(0x66,0xff,0x33);
		$tmpData[]=$cstaData[Success];
	}
	if($flag1 == "on")
	{
		$tmpColor[]=array(0x66,0xff,0xff);
		$tmpData[]=$cstaData[CallDeny];
	}
	if($flag2 == "on")
	{
		$tmpColor[]=array(0,0x66,0x66);
		$tmpData[]=$cstaData[NetBusy];
	}
	if($flag3 == "on")
	{
		$tmpColor[]=array(0x66,0,0xcc);
		$tmpData[]=$cstaData[CallerClear];
	}
	if($flag4 == "on")
	{
		$tmpColor[]=array(0x66,0x33,0);
		$tmpData[]=$cstaData[SubBusy];
	}
	if($flag5 == "on")
	{
		$tmpColor[]=array(0xcc,0,0x66);
		$tmpData[]=$cstaData[NoAnswer];
	}
	if($flag6 == "on")
	{
		$tmpColor[]=array(0xff,0,0);
		$tmpData[]=$cstaData[SignalErr];
	}
	
	echo "<br><b>Hourly Statistic Chart</b><br>";
	if(0)
	{
		echo "flag0=$flag0,flag1=$flag1,flag2=$flag2,flag3=$flag3,flag4=$flag4,flag5=$flag5,flag6=$flag6,flag7=$flag7,flag8=$flag8,flag9=$flag9,flag10=$flag10,flag11=$flag11,flag12=$flag12,flag13=$flag13,flag14=$flag14,flag15=$flag15,flag16=$flag16<br>";
	}
	$boldline=$select_checkbox_id;
	for($i=0;$i<$select_checkbox_id;$i++)
	{
		$param="flag$i";
		if($$param != "on")
			$boldline--;
	}
	
	$image=CreateImage(820,390);
	$image=DrawLineHistogram(
		$image,
		$tmpData,
		$tmpColor,
		750/(24*$slot),
		24,
		array(50,20),
		array(750,280),
		$boldline,1,0,1
	);
	
	$remarkBlock=array(
		"Success",
		"Call Deny",
		"Network Busy",
		"Caller Clear",
		"Sub Busy",
		"No Answer",
		"Signal Error"
	);
	$pointArr=array(
		array(60,367),
		array(160,367),
		array(260,367),
		array(360,367),
		array(460,367),
		array(560,367),
		array(660,367)
	);
	
	unset($tmpColor);
	$tmpColor[]=array(0x66,0xff,0x33);
	$tmpColor[]=array(0x66,0xff,0xff);
	$tmpColor[]=array(0,0x66,0x66);
	$tmpColor[]=array(0x66,0,0xcc);
	$tmpColor[]=array(0x66,0x33,0);
	$tmpColor[]=array(0xcc,0,0x66);
	$tmpColor[]=array(0xff,0,0);
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,$tmpColor,0,2);
	DrawTimeStamp($image,$sel_date_from,$sel_date_to,array(50,355),750/24);
	
	echo "<table border=0 width=\"820\">";
	echo "<tr>";
	for($i=0;$i<sizeof($remarkBlock);$i++)
	{
		echo "<td>";
		AddCheckbox($remarkBlock[$i]);
		echo "</td>";
	}
	echo "</tr>";
	echo "</table>";
	
	DrawImage($image);
}

//Graphics-2
{
	for($i=0;$i<$total_hour_num;$i++)
	{
		if($cstaData[Success][$i] > 0)
			$aveduration_value[$i]=ceil($cstaData[Duration][$i]/$cstaData[Success][$i]);
		else
			$aveduration_value[$i]=0;
	}
	
	unset($tmpColor);
	unset($tmpData);
	
	if($flag7 == "on")
	{
		$tmpColor[]=array(0x66,0xff,0xff);
		$tmpData[]=$cstaData[Duration];
	}
	if($flag8 == "on")
	{
		$tmpColor[]=array(0,0x66,0x66);	
		$tmpData[]=$aveduration_value;
	}
	
	echo "<br><br><b>Duration Statistic Chart</b><br>";
	$image=CreateImage(820,390);
	
	$boldline=$select_checkbox_id-7;
	for($i=7;$i<$select_checkbox_id;$i++)
	{
		$param="flag$i";
		if($$param != "on")
			$boldline--;
	}
	
	$image=DrawLineHistogram(
		$image,
		$tmpData,
		$tmpColor,
		750/(24*$slot),
		24,
		array(50,20),
		array(750,280),
		$boldline,1,0,1
	);
	
	unset($tmpColor);
	$tmpColor[]=array(0x66,0xff,0xff);
	$tmpColor[]=array(0,0x66,0x66);	
	
	$remarkBlock=array(
		"Total Call Duration",
		"Average Call Duration"
	);
	$pointArr=array(
		array(60,367),
		array(260,367)
	);
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,$tmpColor,0,2);
	DrawTimeStamp($image,$sel_date_from,$sel_date_to,array(50,355),750/24);
	
	echo "<table border=0 width=\"820\">";
	echo "<tr>";
	for($i=0;$i<sizeof($remarkBlock);$i++)
	{
		echo "<td>";
		AddCheckbox($remarkBlock[$i]);
		echo "</td>";
	}
	echo "</tr>";
	echo "</table>";
	
	DrawImage($image);
}
?>
