<?php     
global $objDbConn;

if(0)
{
	echo "sel_date_from=$sel_date_from,sel_date_to=$sel_date_to<br>";
	echo "keyOne=$keyOne,keyTwo=$keyTwo<br>";
}
if(!isset($keyTwo) || trim($keyTwo)=="")
	$keyTwo=0;

//Get the trunk title
for($i=0;$i<=256;$i++)
{
	$trunk_element_title[$i]="--";
}
$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=====
$database="OBJ_320";
$sql="SELECT T1.* FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND T2.name_2='tgTitle'";
$result=@mysqli_query($objDbConn,$sql);
$TrunkGroupCount=mysqli_num_rows($result);

if($TrunkGroupCount>0)
{
	while($rows = @mysqli_fetch_array($result))
	{
		//get the instanceNo of the tg
		$trunk_no=$rows[instanceNo]+0;
		$trunk_status[$trunk_no]=1;
		$trunk_element_title[$trunk_no]=$rows[initValue];
	}
}
if(0)
{
	echo "<pre>";
	print_r($trunk_status);
	print_r($trunk_element_title);
	echo "</pre>";
}

$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]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",3);
$cstaDataChip[4]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",4);

for($tg_no=0;$tg_no<=256;$tg_no++)
{
	$cstaDataChip[3][0][3][$tg_no][Attempt]+=0;
	$cstaDataChip[3][0][3][$tg_no][Success]+=0;
	$cstaDataChip[3][0][3][$tg_no][Duration]+=0;
	$cstaDataChip[3][0][3][$tg_no][SignalErr]+=0;
	$cstaDataChip[3][0][3][$tg_no][CallDeny]+=0;
	$cstaDataChip[3][0][3][$tg_no][NetBusy]+=0;
	$cstaDataChip[3][0][3][$tg_no][CallerClear]+=0;
	$cstaDataChip[3][0][3][$tg_no][Normal]+=0;
	$cstaDataChip[3][0][3][$tg_no][SubBusy]+=0;
	$cstaDataChip[3][0][3][$tg_no][NoAnswer]+=0;
	
	$cstaDataChip[4][0][4][$tg_no][Attempt]+=0;
	$cstaDataChip[4][0][4][$tg_no][Success]+=0;
	$cstaDataChip[4][0][4][$tg_no][Duration]+=0;
	$cstaDataChip[4][0][4][$tg_no][SignalErr]+=0;
	$cstaDataChip[4][0][4][$tg_no][CallDeny]+=0;
	$cstaDataChip[4][0][4][$tg_no][NetBusy]+0;
	$cstaDataChip[4][0][4][$tg_no][CallerClear]+=0;
	$cstaDataChip[4][0][4][$tg_no][Normal]+=0;
	$cstaDataChip[4][0][4][$tg_no][SubBusy]+=0;
	$cstaDataChip[4][0][4][$tg_no][NoAnswer]+=0;
	
	
	$cstaDataChip[2][0][2][$tg_no][Attempt]=$cstaDataChip[3][0][3][$tg_no][Attempt]+$cstaDataChip[4][0][4][$tg_no][Attempt];
	$cstaDataChip[2][0][2][$tg_no][Success]=$cstaDataChip[3][0][3][$tg_no][Success]+$cstaDataChip[4][0][4][$tg_no][Success];
	$cstaDataChip[2][0][2][$tg_no][Duration]=$cstaDataChip[3][0][3][$tg_no][Duration]+$cstaDataChip[4][0][4][$tg_no][Duration];
	$cstaDataChip[2][0][2][$tg_no][SignalErr]=$cstaDataChip[3][0][3][$tg_no][SignalErr]+$cstaDataChip[4][0][4][$tg_no][SignalErr];
	$cstaDataChip[2][0][2][$tg_no][CallDeny]=$cstaDataChip[3][0][3][$tg_no][CallDeny]+$cstaDataChip[4][0][4][$tg_no][CallDeny];
	$cstaDataChip[2][0][2][$tg_no][NetBusy]=$cstaDataChip[3][0][3][$tg_no][NetBusy]+$cstaDataChip[4][0][4][$tg_no][NetBusy];
	$cstaDataChip[2][0][2][$tg_no][CallerClear]=$cstaDataChip[3][0][3][$tg_no][CallerClear]+$cstaDataChip[4][0][4][$tg_no][CallerClear];
	$cstaDataChip[2][0][2][$tg_no][Normal]=$cstaDataChip[3][0][3][$tg_no][Normal]+$cstaDataChip[4][0][4][$tg_no][Normal];
	$cstaDataChip[2][0][2][$tg_no][SubBusy]=$cstaDataChip[3][0][3][$tg_no][SubBusy]+$cstaDataChip[4][0][4][$tg_no][SubBusy];
	$cstaDataChip[2][0][2][$tg_no][NoAnswer]=$cstaDataChip[3][0][3][$tg_no][NoAnswer]+$cstaDataChip[4][0][4][$tg_no][NoAnswer];
	
	$inData[Attempt]+=$cstaDataChip[3][0][3][$tg_no][Attempt];
	$inData[Success]+=$cstaDataChip[3][0][3][$tg_no][Success];
	$inData[Duration]+=$cstaDataChip[3][0][3][$tg_no][Duration];
	$inData[SignalErr]+=$cstaDataChip[3][0][3][$tg_no][SignalErr];
	$inData[CallDeny]+=$cstaDataChip[3][0][3][$tg_no][CallDeny];
	$inData[NetBusy]+=$cstaDataChip[3][0][3][$tg_no][NetBusy];
	$inData[CallerClear]+=$cstaDataChip[3][0][3][$tg_no][CallerClear];
	$inData[Normal]+=$cstaDataChip[3][0][3][$tg_no][Normal];
	$inData[SubBusy]+=$cstaDataChip[3][0][3][$tg_no][SubBusy];
	$inData[NoAnswer]+=$cstaDataChip[3][0][3][$tg_no][NoAnswer];
	
	$outData[Attempt]+=$cstaDataChip[4][0][4][$tg_no][Attempt];
	$outData[Success]+=$cstaDataChip[4][0][4][$tg_no][Success];
	$outData[Duration]+=$cstaDataChip[4][0][4][$tg_no][Duration];
	$outData[SignalErr]+=$cstaDataChip[4][0][4][$tg_no][SignalErr];
	$outData[CallDeny]+=$cstaDataChip[4][0][4][$tg_no][CallDeny];
	$outData[NetBusy]+=$cstaDataChip[4][0][4][$tg_no][NetBusy];
	$outData[CallerClear]+=$cstaDataChip[4][0][4][$tg_no][CallerClear];
	$outData[Normal]+=$cstaDataChip[4][0][4][$tg_no][Normal];
	$outData[SubBusy]+=$cstaDataChip[4][0][4][$tg_no][SubBusy];
	$outData[NoAnswer]+=$cstaDataChip[4][0][4][$tg_no][NoAnswer];
	
	$inoutData[Attempt]+=$cstaDataChip[2][0][2][$tg_no][Attempt];
	$inoutData[Success]+=$cstaDataChip[2][0][2][$tg_no][Success];
	$inoutData[Duration]+=$cstaDataChip[2][0][2][$tg_no][Duration];
	$inoutData[SignalErr]+=$cstaDataChip[2][0][2][$tg_no][SignalErr];
	$inoutData[CallDeny]+=$cstaDataChip[2][0][2][$tg_no][CallDeny];
	$inoutData[NetBusy]+=$cstaDataChip[2][0][2][$tg_no][NetBusy];
	$inoutData[CallerClear]+=$cstaDataChip[2][0][2][$tg_no][CallerClear];
	$inoutData[Normal]+=$cstaDataChip[2][0][2][$tg_no][Normal];
	$inoutData[SubBusy]+=$cstaDataChip[2][0][2][$tg_no][SubBusy];
	$inoutData[NoAnswer]+=$cstaDataChip[2][0][2][$tg_no][NoAnswer];
}	



if($keyTwo == 0)
{
	$cstaData=$cstaDataChip[2][0][2];
}
else if($keyTwo == 1)
{
	$cstaData=$cstaDataChip[3][0][3];
}
else if($keyTwo == 2)
{
	$cstaData=$cstaDataChip[4][0][4];
}
else
{
	$cstaData=$cstaDataChip[2][0][2];
}

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

//Table-1
{
	$table_title="Trunk Group";
	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(
		"TG No.",
		"Element Title",
		"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);
	$keyTwoRelation=array(5,6,7,8,9,-1,10,11,-1,12,13,14,15,16);
	
	echo "<br><b><center>$table_title</center></b>";
	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	
	//Table title
	echo "<tr bgcolor=\"#E6E6E6\">";
	for($i=0;$i<$columnNum;$i++)
	{
		echo "<td align=\"center\">{$columnTitle[$i]}</td>";
	}
	echo "</tr>";
	//if(sizeof($cstaData) > 0)
		ksort($cstaData);
	$total_record_num=0;

	foreach($cstaData as $tg_no => $tg_data)
	{
		if($trunk_status[$tg_no] != 1)
			continue;
		echo "<tr>";
		//Trunk Group No.
		echo "<td align=\"center\">";
		echo "<a href=# onclick=\"javascript:toHourPage('$sysTypeNo','$show_type','$page_id','$sel_date_from','$sel_date_to','{$tg_no}','{$keyTwo}');\">$tg_no</a>";
		echo "</td>";
		
		//Trunk Group Title
		echo "<td align=\"center\">{$trunk_element_title[$tg_no]}</td>";
		
		//Attempt
		echo "<td align=\"center\">";
		echo "{$tg_data[Attempt]}";
		echo "</td>";
		
		//Success
		echo "<td align=\"center\">";
		echo "{$tg_data[Success]}<br>";
		$percent=GetPercent($tg_data[Success],$tg_data[Attempt]);
		echo "$percent";
		echo "</td>";
		
		//CallDeny
		echo "<td align=\"center\">";
		echo "{$tg_data[CallDeny]}<br>";
		$percent=GetPercent($tg_data[CallDeny],$tg_data[Attempt]);
		echo "$percent";
		echo "</td>";
		
		//NetBusy
		echo "<td align=\"center\">";
		echo "{$tg_data[NetBusy]}<br>";
		$percent=GetPercent($tg_data[NetBusy],$tg_data[Attempt]);
		echo "$percent";
		echo "</td>";
		
		//CallerClear
		echo "<td align=\"center\">";
		echo "{$tg_data[CallerClear]}<br>";
		$percent=GetPercent($tg_data[CallerClear],$tg_data[Attempt]);
		echo "$percent";
		echo "</td>";
		
		//SubBusy
		echo "<td align=\"center\">";
		echo "{$tg_data[SubBusy]}<br>";
		$percent=GetPercent($tg_data[SubBusy],$tg_data[Attempt]);
		echo "$percent";
		echo "</td>";
		
		//NoAnswer
		echo "<td align=\"center\">";
		echo "{$tg_data[NoAnswer]}<br>";
		$percent=GetPercent($tg_data[NoAnswer],$tg_data[Attempt]);
		echo "$percent";
		echo "</td>";
		
		//SignalErr
		echo "<td align=\"center\">";
		echo "{$tg_data[SignalErr]}<br>";
		$percent=GetPercent($tg_data[SignalErr],$tg_data[Attempt]);
		echo "$percent";
		echo "</td>";
		
		//Total Call Duration
		echo "<td align=\"center\">";
		echo "{$tg_data[Duration]}<br>";
		$timestamp=SecondToTimeStamp($tg_data[Duration]);
		echo "{$timestamp}";
		echo "</td>";
		
		//Average Call Duration
		if($tg_data[Success] > 0)
			$value=ceil($tg_data[Duration]/$tg_data[Success]);
		else
			$value=0;
		echo "<td align=\"center\">";
		echo "{$value}";
		echo "</td>";
		
		$total_record_num++;
		echo "</tr>";
	}
	
	if($total_record_num == 0)
		echo "<tr><td>--</td><td>--</td><td>--</td><td>--</td><td>--</td><td>--</td><td>--</td><td>--</td><td>--</td><td>--</td><td>--</td><td>--</td></tr>";
	echo "</table>";
}

//Table-2
{
	$columnTitle=array(
		"Type",
		"Success",
		"Call Deny",
		"Net Busy",
		"Caller Clear",
		"Sub Busy",
		"No Answer",
		"Signal Error",
	);
	$columnNum=sizeof($columnTitle);
	
	$table_title="Trunk Group General 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")));
	echo "<br><b><center>$table_title</center></b>";
	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	//Table title
	echo "<tr bgcolor=\"#E6E6E6\">";
	for($i=0;$i<$columnNum;$i++)
	{
		echo "<td align=\"center\">{$columnTitle[$i]}</td>";
	}
	echo "</tr>";
	
	//In
	echo "<tr>";
	echo "<td>In</td>";
	if($inData[Attempt] == 0)
	{
		for($i=0;$i<7;$i++)
			echo "<td align=\"center\">0%</td>";
	}
	else
	{
		$percent=GetPercent($inData[Success],$inData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inData[CallDeny],$inData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inData[NetBusy],$inData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inData[CallerClear],$inData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inData[SubBusy],$inData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inData[NoAnswer],$inData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inData[SignalErr],$inData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
	}
	echo "</tr>";
	
	//Out
	echo "<tr>";
	echo "<td>Out</td>";
	if($outData[Attempt] == 0)
	{
		for($i=0;$i<7;$i++)
			echo "<td align=\"center\">0%</td>";
	}
	else
	{
		$percent=GetPercent($outData[Success],$outData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($outData[CallDeny],$outData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($outData[NetBusy],$outData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($outData[CallerClear],$outData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($outData[SubBusy],$outData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($outData[NoAnwser],$outData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($outData[SignalErr],$outData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
	}
	echo "</tr>";
	
	//In&Out
	echo "<tr>";
	echo "<td>In&Out</td>";
	if($inoutData[Attempt] == 0)
	{
		for($i=0;$i<7;$i++)
			echo "<td align=\"center\">0%</td>";
	}
	else
	{
		$percent=GetPercent($inoutData[Success],$inoutData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inoutData[CallDeny],$inoutData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inoutData[NetBusy],$inoutData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inoutData[CallerClear],$inoutData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inoutData[SubBusy],$inoutData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inoutData[NoAnwser],$inoutData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
		
		$percent=GetPercent($inoutData[SignalErr],$inoutData[Attempt]);
		echo "<td align=\"center\">$percent</td>";
	}
	echo "</tr>";
	echo "</table>";
}

//Graphics-1
{
	//Graphics-1, Part-1
	$tmpData[]=$inData[Attempt]+0;
	$tmpData[]=$outData[Attempt]+0;
	
	$tmpColor[]=array(0x00,0xff,0x00);
	$tmpColor[]=array(0x00,0x00,0xff);
	
	echo "<br><b>In.Out Attempt Percentage Chart</b><br>";
	$image=CreateImage(710,320);
	
	$image=DrawPercentHistogram(
		$image,
		$tmpData,
		$tmpColor,
		array(30,20),
		array(150,250),1,0
	);
	
	//Graphics-1, Part-2
	unset($tmpData);
	$tmpData[]=array($inData[Success]+0,$outData[Success]+0);
	$tmpData[]=array($inData[CallDeny]+0,$outData[CallDeny]+0);
	$tmpData[]=array($inData[NetBusy]+0,$outData[NetBusy]+0);
	$tmpData[]=array($inData[CallerClear]+0,$outData[CallerClear]+0);
	$tmpData[]=array($inData[SubBusy]+0,$outData[SubBusy]+0);
	$tmpData[]=array($inData[NoAnswer]+0,$outData[NoAnswer]+0);
	$tmpData[]=array($inData[SignalErr]+0,$outData[SignalErr]+0);
	
	$image=DrawStackPercentHistogram($image,
		$tmpData,
		$tmpColor,
		array(220,20),
		array(470,250),1,0
	);
	
	$remarkBlock=array(
		"In",
		"Out",
	);
	$pointArr=array(
		array(60,295),
		array(370,295)
	);
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,$tmpColor,0,2);
	
	unset($remarkBlock);
	unset($pointArr);
	$remarkBlock=array(
		"Success",
		"Call","Deny",
		"Net","Busy",
		"Caller","Clear",
		"Sub","Busy",
		"No","Answer",
		"Signal","Error"
	);
	$pointArr=array(
		array(245,275),
		array(320,270),array(320,280),
		array(385,270),array(380,280),
		array(440,270),array(440,280),
		array(507,270),array(505,280),
		array(570,270),array(565,280),
		array(625,270),array(627,280)
	);
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,0,0,2);
	
	DrawImage($image);
}
?>