<?php
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;
	
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_378";
$sql="SELECT T1.* FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND T2.name_2='tgTitle'";
$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);
if(0)
{
	echo "sel_date_from=$sel_date_from,sel_date_to=$sel_date_to<br>";
	echo "<pre>";
	print_r($cstaConf);
	echo "</pre>";
}

$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")));

$type_select=$keyTwo+1;
$cstaDataTmp=GetCstaDataByDay($cstaConf,$startTime,$endTime,"detail",$type_select);
$cstaData=$cstaDataTmp[0];
if(0)
{
	echo "<pre>";
	print_r($cstaData);
	echo "</pre>";
}

$typeLabel=array(
	1=>"LU",
	2=>"Call MO",
	3=>"Call MT",
	4=>"SM MO",
	5=>"SM MT"
);

//Table-1
{
	$table_title="Trunk Group {$typeLabel[$type_select]} 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>";
	
	switch($type_select)
	{
	//LU
	case 1:
		$showfield=array(
			"Attempt",
			"Success",
			"Normal",
			"No Resource",
			"No Service",
			"Invalid Service",
			"Invalid Message",
			"Signal Error",
			"Duration",
			"Average Duration"
		);
		$datafield=array(
			"Attempt",
			"Success",
			"lu_normal",
			"lu_res_not_ok",
			"lu_ser_not_ok",
			"lu_ser_no_imp",
			"lu_msg_error",
			"lu_pro_error",
			"Duration"
		);
		break;
	case 2:
	case 3:
		$showfield=array(
			"Attempt",
			"Success",
			"Normal",
			"Call Deny",
			"Net Busy",
			"Caller Clear",
			"Sub Busy",
			"No Answer",
			"Signal Error",
			"Duration",
			"Average Duration"
		);
		$datafield=array(
			"Attempt",
			"Success",
			"cc_normal",
			"cc_call_deny",
			"cc_net_busy",
			"cc_caller_clear",
			"cc_sub_busy",
			"cc_no_answer",
			"cc_sig_error",
			"Duration"
		);
		break;
	case 4:
	case 5:
		$showfield=array(
			"Attempt",
			"Success",
			"Normal",
			"Sub Error",
			"No Service",
			"Net Busy",
			"Message Error",
			"Signal Error",
			"No Resource",
			"Duration",
			"Average Duration"
		);
		$datafield=array(
			"Attempt",
			"Success",
			"sm_normal",
			"sm_error_sub",
			"sm_ser_not_ok",
			"sm_net_busy",
			"sm_msg_error",
			"sm_pro_error",
			"sm_res_not_ok",
			"Duration"
		);
		break;
	default:
		echo "unknown type";
		break;
	}
	$showfieldnum=sizeof($showfield);
	$datafieldnum=sizeof($datafield);
	
	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	echo "<tr bgcolor=\"#E6E6E6\">";
	echo "<td align=\"center\" width=\"6%\">TG NO.</td>";
	echo "<td align=\"center\" width=\"10%\">Element Title</td>";
	$fieldwidth=(100-16)/$showfieldnum;
	for($i=0;$i<$showfieldnum;$i++)
	{
		echo "<td align=\"center\" width=\"{$fieldwidth}%\">{$showfield[$i]}</td>";
	}
	echo "</tr>";
	
	for($i=0;$i<=256;$i++)
	{
		if($trunk_status[$i] != 1)
			continue;
		echo "<tr>";
		echo "<td align=\"center\">";
		echo "<a href=# onclick=\"javascript:toHourPage('$sysTypeNo','$show_type','$page_id','$sel_date_from','$sel_date_to','{$i}','{$keyTwo}');\">";
		echo "$i";
		echo "</a>";
		echo "</td>";
		
		echo "<td align=\"center\">";
		echo "{$trunk_element_title[$i]}";
		echo "</td>";
		
		for($j=0;$j<$showfieldnum;$j++)
		{
			echo "<td align=\"center\">";
			//Attempt
			if($j == 0)
			{
				$value=$cstaData[$type_select][$i][Attempt]+0;
				echo "$value";
			}
			//Duration
			else if($j == ($showfieldnum-2))
			{
				$value=$cstaData[$type_select][$i][Duration]+0;
				$timestamp=SecondToTimeStamp($value);
				if($type_select == 1 || $type_select == 4 || $type_select == 5)
					echo "--";
				else
				{
					echo "$value<br>";
					echo "$timestamp";
				}
			}
			else if($j == ($showfieldnum-1))
			{
				$tmp=$cstaData[$type_select][$i][Success]+0;
				$value=$cstaData[$type_select][$i][Duration]+0;
				if($tmp > 0)
					$value=ceil($value/$tmp);
				else
					$value=0;
				if($type_select == 1 || $type_select == 4 || $type_select == 5)
					echo "--";
				else
					echo "$value";
			}
			else
			{
				$value=$cstaData[$type_select][$i][$datafield[$j]]+0;
				$percent=GetPercent($value,$cstaData[$type_select][$i][Attempt]+0);
				echo "$value<br>";
				echo "$percent";
			}
			echo "</td>";
		}
		
		echo "</tr>";
	}
	echo "</table>";
}

//Graphics-1
{
	$colorSet=array(
		2=>array(0x66,0xff,0x33),
		3=>array(0x66,0xff,0xff),
		4=>array(0,0x66,0x66),
		5=>array(0x66,0,0xcc),
		6=>array(0x66,0x33,0),
		7=>array(0xcc,0,0x66),
		8=>array(0xff,0,0)
	);

	for($i=0;$i<=256;$i++)
	{
		for($j=2;$j<$datafieldnum-1;$j++)
		{
			$totalData[$datafield[$j]]+=$cstaData[$type_select][$i][$datafield[$j]]+0;
		}
	}
	for($j=2;$j<$datafieldnum-1;$j++)
	{
		$tmpData[]=$totalData[$datafield[$j]];
		$tmpColor[]=$colorSet[$j];
	}
	if(0)
	{
		echo "<pre>";
		print_r($totalData);
		print_r($tmpData);
		print_r($tmpColor);
		echo "</pre>";
	}
	echo "<br><b>{$typeLabel[$type_select]} Cause Percentage Chart</b><br>";
	$image=CreateImage(550,320);
	
	$image=DrawPercentHistogram(
		$image,
		$tmpData,
		$tmpColor,
		array(30,20),
		array(500,250),1,0
	);
	
	switch($type_select)
	{
	case 1:
		$remarkBlock=array(
			"Normal",
			"No","Resource",
			"No","Service",
			"Invalid","Service",
			"Invalid","Message",
			"Signal","Error"
		);
		$pointArr=array(
			array(70,290),
			array(160,285),array(140,295),
			array(235,285),array(220,295),
			array(297,285),array(297,295),
			array(374,285),array(374,295),
			array(454,285),array(456,295)
		);
		break;
	case 2:
	case 3:
		$remarkBlock=array(
			"Normal",
			"Call","Deny",
			"Net","Busy",
			"Caller","Clear",
			"Sub","Busy",
			"No","Answer",
			"Signal","Error"
		);
		$pointArr=array(
			array(62,290),
			array(136,285),array(136,295),
			array(205,285),array(205,295),
			array(265,285),array(265,295),
			array(338,285),array(338,295),
			array(408,285),array(398,295),
			array(462,285),array(464,295)
		);
		break;
	case 4:
	case 5:
		$remarkBlock=array(
			"Normal",
			"Sub","Error",
			"No","Service",
			"Net","Busy",
			"Message","Error",
			"Signal","Error",
			"No","Resource"
		);
		$pointArr=array(
			array(62,290),
			array(136,285),array(136,295),
			array(205,285),array(193,295),
			array(265,285),array(265,295),
			array(330,285),array(330,295),
			array(398,285),array(398,295),
			array(471,285),array(458,295)
		);
		break;
	}

	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,0,0,2);
	
	DrawImage($image);
}
?>
