<?php
require("../../inc/header.inc");
?>

<?php
	//=====scroll the page=====
	if($scroll_y > 0){
		//echo "<body style=\"overflow-x:hidden\" topmargin=\"5\" leftmargin=\"12\" onLoad='self.scrollBy(0,$scroll_y)'>";
		echo "<body leftmargin=\"15\" rightmargin=\"10\" onload=\"javascript:scroll_div('$scroll_y');javascript:adjust_frame('down');\" onresize=\"javascript:adjust_frame('down');\" style=\"overflow:hidden;overflow-x:hidden;overflow-y:hidden;\">";
	}else{
		echo "<body leftmargin=\"15\" rightmargin=\"10\" onload=\"javascript:adjust_frame('down');\" onresize=\"javascript:adjust_frame('down');\" style=\"overflow:hidden;overflow-x:hidden;overflow-y:hidden;\">";
	}
?>

<?php
adjust_head_frame("down");
adjust_title_head_frame("down");
adjust_title_tail_frame("down");

adjust_content_head_frame("down");	
echo "<br>";
//========================================================================================|
//========================Initial The Data From Index Page================================|
//========================================================================================| 
	if(!isset($sel_date_from))
		$sel_date_from=0;
	if(!isset($sel_date_to))
		$sel_date_to=0;
	if(!isset($sel_in_out))
		$sel_in_out=0;	
	if(!isset($from_hour))
		$from_hour="00";
	if(!isset($from_minute))
		$from_minute="00";
	if(!isset($from_second))
		$from_second="00";
	if(!isset($to_hour))
		$to_hour="23";
	if(!isset($to_minute))
		$to_minute="59";
	if(!isset($to_second))
		$to_second="59";				
	$DEBUG=0;		
//========================================================================================|
//============================Define The Data=============================================|
//========================================================================================|
	$detailTableFields=array("Attempt",
												   "Success",
												   "Duration",
												   "lu_normal",
												   "lu_res_not_ok",
												   "lu_ser_not_ok",
												   "lu_ser_no_imp",
												   "lu_msg_error",
												   "lu_pro_error",
												   "cc_normal",
												   "cc_call_deny",
												   "cc_net_busy",
												   "cc_caller_clear",
												   "cc_sub_busy",
												   "cc_no_answer",
												   "cc_sig_error",
												   "sm_normal",
												   "sm_error_sub",
												   "sm_ser_not_ok",
												   "sm_net_busy",
												   "sm_msg_error",
												   "sm_pro_error",
												   "sm_res_not_ok"
												   );
	$detailTableFieldsSql="";								   
	for($i=0;$i<sizeof($detailTableFields);$i++)
	{
		$detailTableFieldsSql.="sum(".$detailTableFields[$i].") as ".$detailTableFields[$i];
		if($i<sizeof($detailTableFields)-1)
			$detailTableFieldsSql.=",";
	}
	
	
	
	for($i=0;$i<=256;$i++)
	{
		for($j=0;$j<sizeof($detailTableFields);$j++)
		{
			/*
			0 trunk-no 			
			1 trunk no      
		    ...           			               			  
	               			
			*/
			$trunk_csta[$i][$j]=0;
		}
	}
	
	//===in going,out going,in-out-going data from DB===
	for($i=0;$i<sizeof($detailTableFields);$i++)
	{
		$lu_trunk[$i]=0;
		$mo_trunk[$i]=0;
		$mt_trunk[$i]=0;
		$smo_trunk[$i]=0;
		$smt_trunk[$i]=0;
		$total_trunk[$i]=0;
	}
//========================================================================================|
//============================Get The Trunk Group Element Title===========================|
//========================================================================================|
	//====initial=====
	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'";
	$result=@mysqli_query($pubConn,$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];
		}
	}


//========================================================================================|
//=============================Get The Data From DB=======================================|
//========================================================================================|
	//=====data for per trunk=======
	$db=$csta_db;
	$slot=$sel_date_from-$sel_date_to+1;
	$time_column="csta_datetime";
	//echo "sel_in_out=$sel_in_out<br>";
	$service_type=$sel_in_out+1;
	for($day=0;$day<$slot;$day++)
	{
		
		$table_name	="ipalimSum";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$table_name=$table_name."_".$temp;
		$from_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		//echo "table_name:$table_name<br>";
		//echo "from_time:$from_time<br>";
		//echo "to_time:$to_time<br>";
		//echo "in_out:$sel_in_out<br>";
		
		if($service_type < 6)
			$trunk_where="and key_type=".$service_type." ";
		else
			$trunk_where="";
			
		
		
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		//echo "sql:$sql<br>";
	
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		
		do
		{
			$gro=$rows[0];
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$trunk_csta[$gro][$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));	
	}
	
	//get the 0-hour of this day
	for($day=0;$day<$slot;$day++)
	{
		
		$table_name	="ipalimSum";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$table_name=$table_name."_".$temp;
		$from_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		//echo "table_name:$table_name<br>";
		//echo "from_time:$from_time<br>";
		//echo "to_time:$to_time<br>";
		//echo "in_out:$sel_in_out<br>";
		
		if($service_type < 6)
			$trunk_where="and key_type=".$service_type." ";
		else
			$trunk_where="";
		
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		//echo "sql:$sql<br>";
	
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		
		do
		{
			$gro=$rows[0];
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$trunk_csta[$gro][$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));	
		
	}
	
	//=====data for lu_trunk,mo_trunk,mt_trunk,smo_trunk,smt_trunk===
	for($day=0;$day<$slot;$day++)
	{	
		$table_name	="ipalimSum";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$table_name=$table_name."_".$temp;
		$from_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		
		//===lu-trunk data=====
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type=1 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			//if the trunk group is disable
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$lu_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		//===mo-trunk=======	
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type=2 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$mo_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		//===mt-trunk=======	
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type=3 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$mt_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		//===smo-trunk=======	
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type=4 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$smo_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		//===smt-trunk=======	
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type=5 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$smt_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		
		//=====total_trunk data=======	
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type>0 and key_type<6 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		//echo "$sql<br>";
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$total_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));		
		
	}

	//get the 0-hour of the day
	for($day=0;$day<$slot;$day++)
	{	
		$table_name	="ipalimSum";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$table_name=$table_name."_".$temp;
		$from_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$to_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		
		//===lu-trunk data=====
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type=1 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			//if the trunk group is disable
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$lu_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		//===mo-trunk=======	
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type=2 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$mo_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		//===mt-trunk=======	
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type=3 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$mt_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		//===smo-trunk=======	
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type=4 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$smo_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		//===smt-trunk=======	
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type=5 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$smt_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		

		//=====total_trunk data=======	
		$select="select key_e1_tg,$detailTableFieldsSql ";
		$from="from $table_name ";
		$trunk_where="and key_type>0 and key_type<6 ";
		$where="where $time_column >='$from_time' and $time_column <'$to_time' ".$trunk_where;
		$group="group by key_e1_tg ";
		$order="order by key_e1_tg ";
		$sql=$select.$from.$where.$group.$order;
		//echo "$sql<br>";
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		if(!$rows)
			continue;
		do{	
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			for($tt=0;$tt<sizeof($detailTableFields);$tt++)
			{
				$total_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));		
		
	}


if(0)
{
	for($i=0;$i<sizeof($detailTableFields);$i++)
	{
		$in_trunk[$i]=rand(100,300);
		$out_trunk[$i]=rand(100,300);
		$total_trunk[$i]=$in_trunk[$i]+$out_trunk[$i];
	}
}

//========================================================================================|
//==============================Test The Data=============================================|
//========================================================================================|
//====test the csta data====
if(0)
{
	$test_type_label=array("attempt","success","call clear","no answer","subs busy","network busy","call barred","signalling timeout","total call duration");
	for($i=0;$i<=256;$i++)
	{
		for($j=0;$j<9;$j++)
		{
			echo "group-$i-{$test_type_label[$j]}-$j:{$trunk_csta[$i][$j]}<br>";
		}
	}
}	
//====test the title of trunk group===
if(0)
{
	for($i=0;$i<=256;$i++)
	{	
		echo "group:$i {$trunk_element_title[$i]}<br>";
		
	}
}	

//====test the status of trunk group===
if(0)
{
	for($i=0;$i<=256;$i++)
	{	
		echo "group:$i {$trunk_status[$i]}<br>";
	}
}

//=====test the in_trunk,out_trunk,total_trunk==
if(0)
{
	for($i=0;$i<sizeof($detailTableFields);$i++)
	{
		echo "lu-type-$i:{$lu_trunk[$i]}<br>";
		echo "mo-type-$i:{$mo_trunk[$i]}<br>";
		echo "mt-type-$i:{$mt_trunk[$i]}<br>";
		echo "smo-type-$i:{$smo_trunk[$i]}<br>";
		echo "smt-type-$i:{$smt_trunk[$i]}<br>";
		//echo "total-type-$i:{$total_trunk[$i]}<br>";
	}
}
//========================================================================================|
//===========================Show the table-1=============================================|
//========================================================================================|
  
    //===get the numbers of days that selected===
	$slot=$sel_date_from-$sel_date_to+1;
	
	//====get the table title====
	$title_time=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
	if($slot==1)
		$table_title="Trunk statistic Date:".$title_time;
	else
		$table_title="Trunk statistic Date:".$title_time." to ".date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_to,date("Y")));

	echo "<center><b>$table_title</b></center>";
	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	if($service_type==1)
	{
		$temp="LU";
		$tempi=11;
	}
	else if($service_type==2)
	{
		$temp="Call MO";
		$tempi=12;
	}
	else if($service_type==3)
	{
		$temp="Call MT";
		$tempi=12;
	}
	else if($service_type==4)
	{
		$temp="SM MO";
		$tempi=12;
	}
	else if($service_type==5)
	{
		$temp="SM MT";
		$tempi=12;
	}
			
	echo "<tr bgcolor=\"#E6E6E6\">";
	echo "<td>&nbsp</td><td colspan=$tempi align=\"center\">$temp</td>";
	echo "</tr>";	
	echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td align=\"center\" width=\"6%\"><nobr>TG No.</td>";
		echo "<td align=\"center\" width=\"12%\"><nobr>Element Title</td>";
		
		//LU
		if($service_type == 1)
		{
			echo "<td align=\"center\" width=\"8%\">Attempt</td>";
			echo "<td align=\"center\" width=\"8%\">Success</td>";
			echo "<td align=\"center\" width=\"8%\">Normal</td>";
			echo "<td align=\"center\" width=\"8%\"><nobr>No Resource</td>";
			echo "<td align=\"center\" width=\"8%\"><nobr>No Service</td>";
			echo "<td align=\"center\" width=\"8%\">Invalid Service</td>";
			echo "<td align=\"center\" width=\"8%\">Invalid Message</td>";
			echo "<td align=\"center\" width=\"8%\">Signal Error</td>";
			echo "<td align=\"center\" width=\"8%\">Duration</td>";
			echo "<td align=\"center\" width=\"10%\">Average Duration(s)</td>";
		}
		//call
		else if($service_type == 2 || $service_type == 3)
		{
			echo "<td align=\"center\" width=\"8%\">Attempt</td>";
			echo "<td align=\"center\" width=\"8%\">Success</td>";
			echo "<td align=\"center\" width=\"8%\">Normal</td>";
			echo "<td align=\"center\" width=\"8%\">Call Deny</td>";
			echo "<td align=\"center\" width=\"8%\">Net Busy</td>";
			echo "<td align=\"center\" width=\"8%\">Caller Clear</td>";
			echo "<td align=\"center\" width=\"8%\">Sub Busy</td>";
			echo "<td align=\"center\" width=\"8%\">No Answer</td>";
			echo "<td align=\"center\" width=\"8%\">Signal Error</td>";
			echo "<td align=\"center\" width=\"8%\">Duration</td>";
			echo "<td align=\"center\" width=\"10%\">Average Duration(s)</td>";
		}
		//SM
		else if($service_type == 4 || $service_type == 5)
		{
			echo "<td align=\"center\" width=\"8%\">Attempt</td>";
			echo "<td align=\"center\" width=\"8%\">Success</td>";
			echo "<td align=\"center\" width=\"8%\">Normal</td>";
			echo "<td align=\"center\" width=\"8%\">Sub Error</td>";
			echo "<td align=\"center\" width=\"8%\">No Service</td>";
			echo "<td align=\"center\" width=\"8%\">Net Busy</td>";
			echo "<td align=\"center\" width=\"8%\">Message Error</td>";
			echo "<td align=\"center\" width=\"8%\">Signal Error</td>";
			echo "<td align=\"center\" width=\"8%\">No Resource</td>";
			echo "<td align=\"center\" width=\"8%\">Duration</td>";
			echo "<td align=\"center\" width=\"10%\">Average Duration(s)</td>";
		}
		
	echo "</tr>";
	
	if($service_type == 1)
		$DataRelation=array(0,1,3,4,5,6,7,8,2);
	else if($service_type == 2 || $service_type == 3)
		$DataRelation=array(0,1,9,10,11,12,13,14,15,2);
	else if($service_type == 4 || $service_type == 5)
		$DataRelation=array(0,1,16,17,18,19,20,21,22,2);
	
	
	for($i=0;$i<=256;$i++)
	{
		if($trunk_status[$i]!=1)
			continue;
		echo "<tr>";
		
		$t=$i;
		echo "<td><a target=\"_parent\" href=\"../../performance/ipalimCsta/trunk_detail_index.php?trunk_no=$i&sel_date_from=$sel_date_from&sel_date_to=$sel_date_to&sel_in_out=$sel_in_out&element_title={$trunk_element_title[$i]}\">$t</a></td>";
		echo "<td align=\"left\">{$trunk_element_title[$i]}</td>";
		for($j=0;$j<sizeof($DataRelation);$j++)
		{
			$m_j=$DataRelation[$j];
			echo "<td align=\"center\">";
			if($m_j==0)
			{
				echo "{$trunk_csta[$i][$m_j]}";
			}
			else if($m_j==2)
			{
				echo "{$trunk_csta[$i][$m_j]}(s)";
				$hour=floor($trunk_csta[$i][$m_j]/3600);
				$minute=floor($trunk_csta[$i][$m_j]%3600/60);
				$second=$trunk_csta[$i][$m_j]%60;
				if($hour==0&&$minute==0&&$second==0)
					echo "<br>";
				else
					echo "<br>$hour:$minute:$second";
			}
			else
			{
				echo "{$trunk_csta[$i][$m_j]}";
				echo "<br>";
				if($trunk_csta[$i][0]==0)
					$rate=0;
				else
				{	
					$rate=$trunk_csta[$i][$m_j]/$trunk_csta[$i][0];
					$rate=$rate*100;
					$rate=number_format($rate,2,"."," ");
				}
				if($rate==0)
					$rate=0;
				echo "$rate%";
			}
			
			echo "</td>";
		}
		//==average call duration=====
		echo "<td align=\"center\">";
		if($trunk_csta[$i][1]!=0)
			$ave=round($trunk_csta[$i][2]/$trunk_csta[$i][1]);
		else
			$ave=0;	
		echo "$ave";
		echo "</td>";
		echo "</tr>";
	}
	echo "</table>";
	
	
//========================================================================================|
//=========================Show the table-2 LU===============================================|
//========================================================================================|
	echo "<br><center><b>$table_title</b></center>";
	echo "<center>";
	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	echo "<tr bgcolor=\"#E6E6E6\">";
	echo "<td colspan=7 align=\"center\">Success/failure</td>";
	echo "</tr>";
	echo "<tr bgcolor=\"#E6E6E6\">";
	
	//LU	
	echo "<td align=\"center\">Type</td>";
	echo "<td align=\"center\">Normal</td>";
	echo "<td align=\"center\"><nobr>No Resource</td>";
	echo "<td align=\"center\"><nobr>No Service</td>";
	echo "<td align=\"center\">Invalid Service</td>";
	echo "<td align=\"center\">Invalid Message</td>";
	echo "<td align=\"center\">Signal Error</td>";

	
	echo "</tr>";
	
	//====LU trunk data=====
	$DataRelation=array(3,4,5,6,7,8);
	echo "<tr>";
	echo "<td align=\"center\">LU</td>";
	for($i=0;$i<sizeof($DataRelation);$i++)
	{
		$m_i=$DataRelation[$i];
		echo "<td align=\"center\">";
		if($lu_trunk[0]!=0)
		{
			$rate=$lu_trunk[$m_i]/$lu_trunk[0];
			$rate=$rate*100;
			$rate=number_format($rate,2,"."," ");
		}	
		else
			$rate=0;
		if($rate==0)
			$rate=0;		
		echo "$rate%";			
		echo "</td>";
	}
	echo "</tr>";
	echo "</table></center>";

//========================================================================================|
//=========================Show the table-3 Call===============================================|
//========================================================================================|
	//echo "<br><center><b>$table_title</b></center>";
	echo "<br><center>";
	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	//echo "<tr bgcolor=\"#E6E6E6\">";
	//echo "<td colspan=8 align=\"center\">Success/failure</td>";
	//echo "</tr>";
	echo "<tr bgcolor=\"#E6E6E6\">";
	
	//Call	
	echo "<td align=\"center\">Type</td>";
	echo "<td align=\"center\">Normal</td>";
	echo "<td align=\"center\">Call Deny</td>";
	echo "<td align=\"center\">Net Busy</td>";
	echo "<td align=\"center\">Caller Clear</td>";
	echo "<td align=\"center\">Sub Busy</td>";
	echo "<td align=\"center\">No Answer</td>";
	echo "<td align=\"center\">Signal Error</td>";
	
	echo "</tr>";
	
	//====mo trunk data=====
	$DataRelation=array(9,10,11,12,13,14,15);
	echo "<tr>";
	echo "<td align=\"center\">Call MO</td>";
	for($i=0;$i<sizeof($DataRelation);$i++)
	{
		$m_i=$DataRelation[$i];
		echo "<td align=\"center\">";
		if($mo_trunk[0]!=0)
		{
			$rate=$mo_trunk[$m_i]/$mo_trunk[0];
			$rate=$rate*100;
			$rate=number_format($rate,2,"."," ");
		}	
		else
			$rate=0;
		if($rate==0)
			$rate=0;		
		echo "$rate%";			
		echo "</td>";
	}
	echo "</tr>";
	
	
	echo "<tr>";
	echo "<td align=\"center\">Call MT</td>";
	for($i=0;$i<sizeof($DataRelation);$i++)
	{
		$m_i=$DataRelation[$i];
		echo "<td align=\"center\">";
		if($mt_trunk[0]!=0)
		{
			$rate=$mt_trunk[$m_i]/$mt_trunk[0];
			$rate=$rate*100;
			$rate=number_format($rate,2,"."," ");
		}	
		else
			$rate=0;
		if($rate==0)
			$rate=0;		
		echo "$rate%";			
		echo "</td>";
	}
	echo "</tr>";
	echo "</table></center>";
	
	//========================================================================================|
//=========================Show the table-4 SM===============================================|
//========================================================================================|
	//echo "<br><center><b>$table_title</b></center>";
	echo "<br><center>";
	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	//echo "<tr bgcolor=\"#E6E6E6\">";
	//echo "<td colspan=8 align=\"center\">Success/failure</td>";
	//echo "</tr>";
	echo "<tr bgcolor=\"#E6E6E6\">";

	//Call	
	echo "<td align=\"center\">Type</td>";
	echo "<td align=\"center\">Normal</td>";
	echo "<td align=\"center\">Sub Error</td>";
	echo "<td align=\"center\">No Service</td>";
	echo "<td align=\"center\">Net Busy</td>";
	echo "<td align=\"center\">Message Error</td>";
	echo "<td align=\"center\">Signal Error</td>";
	echo "<td align=\"center\">No Resource</td>";
	
	echo "</tr>";
	
	//====mo trunk data=====
	$DataRelation=array(16,17,18,19,20,21,22);
	echo "<tr>";
	echo "<td align=\"center\">SM MO</td>";
	for($i=0;$i<sizeof($DataRelation);$i++)
	{
		$m_i=$DataRelation[$i];
		echo "<td align=\"center\">";
		if($smo_trunk[0]!=0)
		{
			$rate=$smo_trunk[$m_i]/$smo_trunk[0];
			$rate=$rate*100;
			$rate=number_format($rate,2,"."," ");
		}	
		else
			$rate=0;
		if($rate==0)
			$rate=0;		
		echo "$rate%";			
		echo "</td>";
	}
	echo "</tr>";
	
	
	echo "<tr>";
	echo "<td align=\"center\">SM MT</td>";
	for($i=0;$i<sizeof($DataRelation);$i++)
	{
		$m_i=$DataRelation[$i];
		echo "<td align=\"center\">";
		if($smt_trunk[0]!=0)
		{
			$rate=$smt_trunk[$m_i]/$smt_trunk[0];
			$rate=$rate*100;
			$rate=number_format($rate,2,"."," ");
		}	
		else
			$rate=0;
		if($rate==0)
			$rate=0;		
		echo "$rate%";			
		echo "</td>";
	}
	echo "</tr>";
	echo "</table></center>";
	
	
	
	
	
//========================================================================================|
//=======================Draw the Graphics-1==============================================|
//========================================================================================|

	echo "<p><b>LU/Call MO/Call MT/SM MO/SM MT(Attempt Call)</b><br>";
	$sizex=510;
	$sizey=240;
	$im = ImageCreate ($sizex, $sizey);
	$background_color=ImageColorAllocate($im,255,255,255);
	$black_color=ImageColorAllocate($im,0,0,0);
	$white_color=ImageColorAllocate($im,255,255,255);

	$bgcolor=F8F8F8;
	$fgcolor=000000;
	
	$background_color = ImageColorAllocate ($im, 
				hexdec(substr($bgcolor, 0, 2)), 
				hexdec(substr($bgcolor, 2, 2)),
				hexdec(substr($bgcolor, 4, 2)));
	
	$text_color = ImageColorAllocate ($im, 
				hexdec(substr($fgcolor, 0, 2)), 
				hexdec(substr($fgcolor, 2, 2)),
				hexdec(substr($fgcolor, 4, 2)));
	$color[0]=ImageColorAllocate($im,0x33,0x33,0xff);
	$color[1]=ImageColorAllocate($im,0x66,0xaa,0);
	$color[2]=ImageColorAllocate($im,0x66,0xff,0x33);
	$color[3]=ImageColorAllocate($im,0x66,0xff,0xff);
	$color[4]=ImageColorAllocate($im,0,0x66,0x66);
	
				
	//=====background====
	ImageFilledRectangle($im,0,0,$sizex,$sizey,ImageColorAllocate ($im, 198, 198, 198));
	ImageFilledRectangle($im,2,2,$sizex,$sizey,ImageColorAllocate ($im, 130, 130, 130));
	$pointArr=array(0,$sizey-1,3,$sizey-4,4,$sizey-1);
	Imagefilledpolygon($im, $pointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
	$pointArr=array($sizex-1,0,$sizex-4,3,$sizex,4);
	Imagefilledpolygon($im, $pointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
	ImageFilledRectangle($im,2,2,$sizex-3,$sizey-3,$background_color);
	
	imageLine($im,40,20,40,200,$black_color);
	imageLine($im,40,20,490,20,$black_color);
	imageLine($im,490,200,490,20,$black_color);
	imageLine($im,40,200,490,200,$black_color);
	
	//=====draw the horizonal line======
	$style = array($black_color,$black_color,$black_color,$black_color,$black_color,$white_color,$white_color,$white_color,$white_color,$white_color);
	imagesetstyle($im, $style);
	for($i=10;$i>0;$i--)
	{
		$x1=40;
		$y1=20+(10-$i)*18;
		$x2=490;
		$y2=$y1;
		if($i!=10)
			imageline($im,$x1,$y1,$x2,$y2,IMG_COLOR_STYLED);
		imagestring($im,2,$x1-22,$y2-5,($i*10)."%",$black_color);
	}			
	
	//====draw the vitical line======
	$u=floor(450/10);
	for($i=0;$i<10;$i++)
	{
		$x1=40+$i*$u;
		$y1=20;
		$x2=$x1;
		$y2=200;
		ImageDashedLine($im,$x1,$y1,$x2,$y2,$black_color);
	}
	
	//=====get the five block height=======
	if($total_trunk[0]==0)
	{
		$lu_trunk_percent="0%";
		$mo_trunk_percent="0%";
		$mt_trunk_percent="0%";
		$smo_trunk_percent="0%";
		$smt_trunk_percent="0%";
		$height[0]=0;
		$heighr[1]=0;
		$height[2]=0;
		$height[3]=0;
		$height[4]=0;
	}
	else
	{
		$lu_trunk_percent=change_percent($lu_trunk[0]/$total_trunk[0]);
		$mo_trunk_percent=change_percent($mo_trunk[0]/$total_trunk[0]);
		$mt_trunk_percent=change_percent($mt_trunk[0]/$total_trunk[0]);
		$smo_trunk_percent=change_percent($smo_trunk[0]/$total_trunk[0]);
		$smt_trunk_percent=change_percent($smt_trunk[0]/$total_trunk[0]);
		$height[0]=floor(180*$lu_trunk[0]/$total_trunk[0]);
		$height[1]=floor(180*$mo_trunk[0]/$total_trunk[0]);
		$height[2]=floor(180*$mt_trunk[0]/$total_trunk[0]);
		$height[3]=floor(180*$smo_trunk[0]/$total_trunk[0]);		
		$height[4]=floor(180*$smt_trunk[0]/$total_trunk[0]);
	}
	
	//======draw the data block=======
	for($i=0;$i<5;$i++)
	{		
		$x1=85+(2*$i)*45;
		if($height[$i]==0)
			$height[$i]=1;
		$y1=200-$height[$i];
		$x2=85+(2*$i+1)*45;
		$y2=199;
		imagefilledrectangle($im,$x1,$y1,$x2,$y2,$color[$i]);
	}
	
	//=====draw the legend string=======
	imagestring($im,2,105,205,"LU",$black_color);
	imagestring($im,2,88,220,"(".$lu_trunk_percent.")",$black_color);
	imagestring($im,2,175,205,"Call MO",$black_color);
	imagestring($im,2,180,220,"(".$mo_trunk_percent.")",$black_color);
	imagestring($im,2,265,205,"Call MT",$black_color);
	imagestring($im,2,270,220,"(".$mt_trunk_percent.")",$black_color);
	imagestring($im,2,363,205,"SM MT",$black_color);
	imagestring($im,2,365,220,"(".$smo_trunk_percent.")",$black_color);
	imagestring($im,2,450,205,"SM MO",$black_color);
	imagestring($im,2,455,220,"(".$smt_trunk_percent.")",$black_color);
	
	//====show the graphics=====
	//-----输出图形文件，请勿修改
	//delete *.png
	$handle=opendir('.');
	while($file=readdir($handle)){
		$retval="";
		$pt=strrpos($file,".");
		if($pt) $retval=substr($file,$pt+1,strlen($file)-$pt);
		if($retval=='png') unlink($file);
	}
	$filename="tg1_".time().".png";
	if(1){
		ImagePng ($im,"$filename");
		
		ImageDestroy($im);	
	}
	//-----显示图片  
	echo "<img src=\"$filename\"></img>";
/*	
//========================================================================================|
//==========================Draw the Graphics-2===========================================|
//========================================================================================|

	echo "<p><b>Success/Failure<br>";
	$sizex=600;
	$sizey=250;
	$image=imagecreate($sizex,$sizey);
	$bgcolor=imagecolorallocate($image,255,255,255);
	$black_color=ImageColorAllocate($image,0,0,0);	
	$white_color=ImageColorAllocate($image,255,255,255);
	$bgcolor=F8F8F8;
	$fgcolor=000000;
	$color[0]=ImageColorAllocate($image,0x33,0x33,0xff);
	$color[1]=ImageColorAllocate($image,0x66,0xaa,0);
	$background_color = ImageColorAllocate ($image, 
				hexdec(substr($bgcolor, 0, 2)), 
				hexdec(substr($bgcolor, 2, 2)),
				hexdec(substr($bgcolor, 4, 2)));
	
	$text_color = ImageColorAllocate ($image, 
				hexdec(substr($fgcolor, 0, 2)), 
				hexdec(substr($fgcolor, 2, 2)),
				hexdec(substr($fgcolor, 4, 2)));
				
	//====background====
	ImageFilledRectangle($image,0,0,$sizex,$sizey,ImageColorAllocate ($image, 198, 198, 198));
	ImageFilledRectangle($image,2,2,$sizex,$sizey,ImageColorAllocate ($image, 130, 130, 130));
	$pointArr=array(0,$sizey-1,3,$sizey-4,4,$sizey-1);
	Imagefilledpolygon($image, $pointArr, 3, ImageColorAllocate ($image, 130, 130, 130));
	$pointArr=array($sizex-1,0,$sizex-4,3,$sizex,4);
	Imagefilledpolygon($image, $pointArr, 3, ImageColorAllocate ($image, 130, 130, 130));
	ImageFilledRectangle($image,2,2,$sizex-3,$sizey-3,$background_color);
	
	imageLine($image,40,20,40,200,$black_color);
	imageLine($image,40,20,580,20,$black_color);
	imageLine($image,580,200,580,20,$black_color);
	imageLine($image,40,200,580,200,$black_color);	
	
	//====draw the horizonal line===
	$style = array($black_color,$black_color,$black_color,$black_color,$black_color,$white_color,$white_color,$white_color,$white_color,$white_color);
	imagesetstyle($image, $style);
	for($i=10;$i>0;$i--)
	{
		$x1=40;
		$y1=20+(10-$i)*18;
		$x2=580;
		$y2=$y1;
		if($i!=10)
			imageline($image, $x1,$y1,$x2,$y2, IMG_COLOR_STYLED);
		
		imagestring($image,2,$x1-22,$y2-5,($i*10)."%",$black_color);
	}
	
	//====draw the vitical line=====
	$u=floor(540/15);
	for($i=0;$i<16;$i++)
	{
		$x1=40+$i*$u;
		$y1=20;
		$x2=$x1;
		$y2=200;
		ImageDashedLine($image,$x1,$y1,$x2,$y2,$black_color);
	}		
	
	//=====get the block height======	
	for($j=0;$j<7;$j++)
	{
		if($total_trunk[0]==0)
			$total_height[$j][0]=1;
		else
			$total_height[$j][0]=floor(180*$in_trunk[$j+1]/$total_trunk[0]);
	}
	for($j=0;$j<7;$j++)
	{
		if($total_trunk[0]==0)
			$total_height[$j][1]=1;
		else
			$total_height[$j][1]=floor(180*$out_trunk[$j+1]/$total_trunk[0]);
	}
	
	//======draw the data block=====
	for($i=0;$i<7;$i++)
	{
		for($j=0;$j<2;$j++)
		{
			if($total_height[$i][$j]<1)
				$total_height[$i][$j]=1;
			//echo "lie-$i-hang-$j-{$total_height[$i][$j]}<br>";
			$x1=40+(2*$i+1)*36;
			$y1=199;
			for($z=0;$z<$j+1;$z++)
			{
				$y1=$y1-$total_height[$i][$z];
			}
			$x2=40+(2*$i+2)*36;
			$y2=199;
			for($z=0;$z<$j;$z++)
			{
				$y2=$y2-$total_height[$i][$z];
			}
			
			imagefilledrectangle($image,$x1,$y1,$x2,$y2,$color[$j]);
		}
	}
	
	//=====draw the legend string=====
	imagefilledrectangle($image,150,230,165,245,$color[0]);
	imagestring($image,2,170,230,"In",$black_color);
	imagefilledrectangle($image,320,230,335,245,$color[1]);
	imagestring($image,2,340,230,"Out",$black_color);
	
	imagestring($image,2,77,205,"Success",$black_color);
	imagestring($image,2,135,205,"Call deny",$black_color);
	imagestring($image,2,215,205,"Net busy",$black_color);
	imagestring($image,2,293,200,"Caller",$black_color);
	imagestring($image,2,298,212,"clear",$black_color);
	imagestring($image,2,360,205,"Sub busy",$black_color);
	imagestring($image,2,430,205,"No answer",$black_color);
	imagestring($image,2,508,200,"Signal",$black_color);
	imagestring($image,2,512,212,"error",$black_color);
	
	//=====show the success failure graphics====
	$tt=time()-1505;
	$file_name="tg2_".$tt.".png";
	imagepng($image,$file_name);
	imagedestroy($image);
	echo "<img src=$file_name></img>";
	
*/
adjust_content_tail("down");
?>

</body>

