<?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\">";


$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_378";
$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];
}


$cstaConf=GetCstaConf($sysTypeNo);

$slot=$sel_date_from-$sel_date_to+1;
$total_hour_num=$slot*24;
$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>";

$tgno=$keyOne;
$type_select=$keyTwo+1;
$cstaDataTmp=GetCstaDataByHour($cstaConf,$startTime,$endTime,"detail",$type_select,$tgno);
$cstaData=$cstaDataTmp[0][$type_select][$tgno];
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"
);

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

//Table-1
{
	$table_title="TG $tgno [$trunk_element_title]";
	$table_title.=" $typeLabel[$type_select] ";
	$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")));
	
	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\">";
	echo "<td align=\"center\" width=\"10%\">Time</td>";
	$fieldwidth=90/$showfieldnum;
	for($i=0;$i<$showfieldnum;$i++)
	{
		echo "<td align=\"center\" width=\"{$fieldwidth}%\">$showfield[$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>";
		
		for($j=0;$j<$showfieldnum;$j++)
		{
			$cstaData[$datafield[$j]][$hour]+=0;
			echo "<td align=\"center\">";
			//Attempt
			if($j == 0)
			{
				$value=$cstaData[Attempt][$hour]+0;
				echo "$value";
			}
			//Duration
			else if($j == ($showfieldnum-2))
			{
				$value=$cstaData[Duration][$hour]+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[Success][$hour]+0;
				$value=$cstaData[Duration][$hour]+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[$datafield[$j]][$hour]+0;
				$percent=GetPercent($value,$cstaData[Attempt][$hour]+0);
				echo "$value<br>";
				echo "$percent";
			}
			echo "</td>";
		}
		
		echo "</tr>";
	}
	
	echo "</table>";
}

//Graphics-1
{
	unset($tmpColor);
	unset($tmpData);
	
	if($flag0 == "on")
	{
		$tmpColor[]=array(0x66,0xff,0x33);
		$tmpData[]=$cstaData[$datafield[1]];
	}
	
	if($flag1 == "on")
	{
		$tmpColor[]=array(0x66,0xff,0xff);
		$tmpData[]=$cstaData[$datafield[2]];
	}
	if($flag2 == "on")
	{
		$tmpColor[]=array(0,0x66,0x66);
		$tmpData[]=$cstaData[$datafield[3]];
	}
	if($flag3 == "on")
	{
		$tmpColor[]=array(0x66,0,0xcc);
		$tmpData[]=$cstaData[$datafield[4]];
	}
	if($flag4 == "on")
	{
		$tmpColor[]=array(0x66,0x33,0);
		$tmpData[]=$cstaData[$datafield[5]];
	}
	if($flag5 == "on")
	{
		$tmpColor[]=array(0xcc,0,0x66);
		$tmpData[]=$cstaData[$datafield[6]];
	}
	if($flag6 == "on")
	{
		$tmpColor[]=array(0xcc,0xee,0x66);
		$tmpData[]=$cstaData[$datafield[7]];
	}
	if($type_select > 1)
	{
		if($flag7 == "on")
		{
			$tmpColor[]=array(0xff,0xee,0);
			$tmpData[]=$cstaData[$datafield[8]];
		}
	}
	
	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
	);
	
	unset($tmpColor);
	unset($remarkBlock);
	switch($type_select)
	{
	case 1:
		$remarkBlock=array(
			"Success",
			"Normal",
			"No Resource",
			"No Service",
			"Invalid Service",
			"Invalid Message",
			"Signal Error"
		);
		$tmpColor=array(
			array(0x66,0xff,0x33),
			array(0x66,0xff,0xff),
			array(0,0x66,0x66),
			array(0x66,0,0xcc),
			array(0x66,0x33,0),
			array(0xcc,0,0x66),
			array(0xcc,0xee,0x66)
		);
		$pointArr=array(
			array(60,367),
			array(160,367),
			array(260,367),
			array(360,367),
			array(460,367),
			array(560,367),
			array(660,367)
		);
		break;
	case 2:
	case 3:
		$remarkBlock=array(
			"Success",
			"Normal",
			"Call Deny",
			"Net Busy",
			"Caller Clear",
			"Sub Busy",
			"No Answer",
			"Signal Error"
		);
		$tmpColor=array(
			array(0x66,0xff,0x33),
			array(0x66,0xff,0xff),
			array(0,0x66,0x66),
			array(0x66,0,0xcc),
			array(0x66,0x33,0),
			array(0xcc,0,0x66),
			array(0xcc,0xee,0x66),
			array(0xff,0,0)
		);
		$pointArr=array(
			array(60,367),
			array(160,367),
			array(260,367),
			array(360,367),
			array(460,367),
			array(560,367),
			array(660,367),
			array(760,367)
		);
		break;
	case 4:
	case 5:
		$remarkBlock=array(
			"Success",
			"Normal",
			"Sub","Error",
			"No","Service",
			"Net","Busy",
			"Message","Error",
			"Signal","Error",
			"No","Resource"
		);
		$tmpColor=array(
			array(0x66,0xff,0x33),
			array(0x66,0xff,0xff),
			array(0,0x66,0x66),
			array(0x66,0,0xcc),
			array(0x66,0x33,0),
			array(0xcc,0,0x66),
			array(0xcc,0xee,0x66),
			array(0xff,0,0)
		);
		$pointArr=array(
			array(60,367),
			array(160,367),
			array(260,367),
			array(360,367),
			array(460,367),
			array(560,367),
			array(660,367),
			array(760,367)
		);
		break;
	}
	$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
if(!($type_select == 1 || $type_select == 4 || $type_select == 5))
{
	for($i=0;$i<$total_hour_num;$i++)
	{
		if($cstaData[Success][$i] > 0)
			$cstaData[AveDuration][$i]=ceil($cstaData[Duration][$i]/$cstaData[Success][$i]);
		else
			$cstaData[AveDuration][$i]=0;
	}

	unset($tmpColor);
	unset($tmpData);
	
	if($type_select == 1)
	{
		if($flag7 == "on")
		{
			$tmpColor[]=array(0x66,0xff,0xff);
			$tmpData[]=$cstaData[Duration];
		}
		if($flag8 == "on")
		{
			$tmpColor[]=array(0,0x66,0x66);	
			$tmpData[]=$cstaData[AveDuration];
		}
		$lineStart=7;
	}
	else
	{
		if($flag8 == "on")
		{
			$tmpColor[]=array(0x66,0xff,0xff);
			$tmpData[]=$cstaData[Duration];
		}
		if($flag9 == "on")
		{
			$tmpColor[]=array(0,0x66,0x66);	
			$tmpData[]=$cstaData[AveDuration];
		}
		$lineStart=8;
	}
	
	echo "<br><br><b>Duration Statistic Chart</b><br>";
	$image=CreateImage(820,390);
	
	$boldline=$select_checkbox_id-$lineStart;
	for($i=$lineStart;$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);
}
?>