<?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=1;		
//========================================================================================|
//============================Define The Data=============================================|
//========================================================================================|
	for($i=0;$i<=256;$i++)
	{
		for($j=0;$j<9;$j++)
		{
			/*
			0 trunk-no 			0 attempt
			1 trunk no          1 success
		    ...                 2 call deny
			                    3 network busy
	                            4 caller clear
			           			5 sub busy
		             			6 no answer
	             			    7 signal error
	               			    8 total call duration
			*/
			$trunk_csta[$i][$j]=0;
		}
	}
	
	//===in going,out going,in-out-going data from DB===
	for($i=0;$i<9;$i++)
	{
		$in_trunk[$i]=0;
		$out_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_320";
	$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";
	
	for($day=0;$day<$slot;$day++)
	{
		
		$table_name	="mscSum";
		$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($sel_in_out==0)
			$trunk_where="and key_type=3 ";
		else if($sel_in_out==1)
			$trunk_where="and key_type=4 ";
		else
			$trunk_where="and key_type>2 and key_type<5 ";
		
		$select="select key_e1_tg,sum(Attempt) as attempt,sum(Success) as success,sum(CallDeny) as call_deny,sum(NetBusy) as network_busy,sum(CallerClear) as caller_clear,sum(SubBusy) as sub_busy,sum(NoAnswer) as no_answer,sum(SignalErr) as signal_error,sum(Duration) as duration ";
		$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<9;$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	="mscSum";
		$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($sel_in_out==0)
			$trunk_where="and key_type=3 ";
		else if($sel_in_out==1)
			$trunk_where="and key_type=4 ";
		else
			$trunk_where="and key_type>2 and key_type<5 ";
		
		$select="select key_e1_tg,sum(Attempt) as attempt,sum(Success) as success,sum(CallDeny) as call_deny,sum(NetBusy) as network_busy,sum(CallerClear) as caller_clear,sum(SubBusy) as sub_busy,sum(NoAnswer) as no_answer,sum(SignalErr) as signal_error,sum(Duration) as duration ";
		$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<9;$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 in_trunk,out_trunk,total_trunk===
	for($day=0;$day<$slot;$day++)
	{	
		$table_name	="mscSum";
		$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")));
		
		//===in-trunk data=====
		$select="select key_e1_tg,sum(Attempt) as attempt,sum(Success) as success,sum(CallDeny) as call_deny,sum(NetBusy) as network_busy,sum(CallerClear) as call_clear,sum(SubBusy) as sub_busy,sum(NoAnswer) as no_answer,sum(SignalErr) as signal_error,sum(Duration) as duration ";
		$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{	
			//if the trunk group is disable
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			
			for($tt=0;$tt<9;$tt++)
			{
				$in_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		//===out-trunk=======	
		$select="select key_e1_tg,sum(Attempt) as attempt,sum(Success) as success,sum(CallDeny) as call_deny,sum(NetBusy) as network_busy,sum(CallerClear) as caller_clear,sum(SubBusy) as sub_busy,sum(NoAnswer) as no_answer,sum(SignalErr) as signal_error,sum(Duration) as duration ";
		$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<9;$tt++)
			{
				$out_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,sum(Attempt) as attempt,sum(Success) as success,sum(CallDeny) as call_deny,sum(NetBusy) as network_busy,sum(CallerClear) as caller_clear,sum(SubBusy) as sub_busy,sum(NoAnswer) as no_answer,sum(SignalErr) as signal_error,sum(Duration) as duration ";
		$from="from $table_name ";
		$trunk_where="and key_type>2 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;
		//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<9;$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	="mscSum";
		$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")));
		
		//===in-trunk data=====
		$select="select key_e1_tg,sum(Attempt) as attempt,sum(Success) as success,sum(CallDeny) as call_deny,sum(NetBusy) as network_busy,sum(CallerClear) as call_clear,sum(SubBusy) as sub_busy,sum(NoAnswer) as no_answer,sum(SignalErr) as signal_error,sum(Duration) as duration ";
		$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{	
			//if the trunk group is disable
			$trunk_no=$rows[0];
			if($trunk_status[$trunk_no]==0)
				continue;
			
			for($tt=0;$tt<9;$tt++)
			{
				$in_trunk[$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));
		
		//===out-trunk=======	
		$select="select key_e1_tg,sum(Attempt) as attempt,sum(Success) as success,sum(CallDeny) as call_deny,sum(NetBusy) as network_busy,sum(CallerClear) as caller_clear,sum(SubBusy) as sub_busy,sum(NoAnswer) as no_answer,sum(SignalErr) as signal_error,sum(Duration) as duration ";
		$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<9;$tt++)
			{
				$out_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,sum(Attempt) as attempt,sum(Success) as success,sum(CallDeny) as call_deny,sum(NetBusy) as network_busy,sum(CallerClear) as caller_clear,sum(SubBusy) as sub_busy,sum(NoAnswer) as no_answer,sum(SignalErr) as signal_error,sum(Duration) as duration ";
		$from="from $table_name ";
		$trunk_where="and key_type>2 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;
		//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<9;$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<9;$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<9;$i++)
	{
		echo "in-type-$i:{$in_trunk[$i]}<br>";
		echo "out-type-$i:{$out_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($sel_in_out==0)
		$temp="In";
	else if($sel_in_out==1)
		$temp="Out";
	else if($sel_in_out==2)
		$temp="In&Out";
			
	echo "<tr bgcolor=\"#E6E6E6\">";
	echo "<td>&nbsp</td><td colspan=11 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>";
		echo "<td align=\"center\" width=\"8%\">Attempt</td>";
		echo "<td align=\"center\" width=\"8%\">Success</td>";
		echo "<td align=\"center\" width=\"8%\"><nobr>Call deny</td>";
		echo "<td align=\"center\" width=\"8%\"><nobr>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%\"><nobr>No answer</td>";
		echo "<td align=\"center\" width=\"8%\">Signal error</td>";
		echo "<td align=\"center\" width=\"8%\">Total call duration</td>";
		echo "<td align=\"center\" width=\"10%\">Average call<br>duration(s)</td>";
	echo "</tr>";
	
	for($i=0;$i<=256;$i++)
	{
		if($trunk_status[$i]!=1)
			continue;
		echo "<tr>";
		
		$t=$i;
		echo "<td><a target=\"_parent\" href=\"../../performance/mscCsta/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<9;$j++)
		{
			echo "<td align=\"center\">";
			if($j==0)
				echo "{$trunk_csta[$i][$j]}";
			if($j>0&&$j<8)
			{
				echo "{$trunk_csta[$i][$j]}";
				echo "<br>";
				if($trunk_csta[$i][0]==0)
					$rate=0;
				else
				{	
					$rate=$trunk_csta[$i][$j]/$trunk_csta[$i][0];
					$rate=$rate*100;
					$rate=number_format($rate,2,"."," ");
				}
				if($rate==0)
					$rate=0;
				echo "$rate%";
			}
			if($j==8)
			{
				echo "{$trunk_csta[$i][$j]}(s)";
				$hour=floor($trunk_csta[$i][$j]/3600);
				$minute=floor($trunk_csta[$i][$j]%3600/60);
				$second=$trunk_csta[$i][$j]%60;
				if($hour==0&&$minute==0&&$second==0)
					echo "<br>";
				else
					echo "<br>$hour:$minute:$second";
			}
			echo "</td>";
		}
		//==average call duration=====
		echo "<td align=\"center\">";
		if($trunk_csta[$i][1]!=0)
			$ave=round($trunk_csta[$i][8]/$trunk_csta[$i][1]);
		else
			$ave=0;	
		echo "$ave";
		echo "</td>";
		echo "</tr>";
	}
	echo "</table>";
	
	
//========================================================================================|
//=========================Show the table-2===============================================|
//========================================================================================|

	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=8 align=\"center\">2.Success/failure</td>";
	echo "</tr>";
	echo "<tr bgcolor=\"#E6E6E6\">";
	echo "<td align=\"center\">Type</td>";
	echo "<td align=\"center\">Success</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>";
	
	//====in trunk data=====
	echo "<tr>";
	echo "<td align=\"center\">In</td>";
	for($i=0;$i<7;$i++)
	{
		echo "<td align=\"center\">";
		if($in_trunk[0]!=0)
		{
			$rate=$in_trunk[$i+1]/$in_trunk[0];
			$rate=$rate*100;
			$rate=number_format($rate,2,"."," ");
		}	
		else
			$rate=0;
		if($rate==0)
			$rate=0;		
		echo "$rate%";			
		echo "</td>";
	}
	echo "</tr>";
	
	//===out trunk data=======
	echo "<tr>";
	echo "<td align=\"center\">Out</td>";
	for($i=0;$i<7;$i++)
	{
		echo "<td align=\"center\">";
		if($out_trunk[0]!=0)
		{
			$rate=$out_trunk[$i+1]/$out_trunk[0];
			$rate=$rate*100;
			$rate=number_format($rate,2,"."," ");
		}	
		else
			$rate=0;
		if($rate==0)
			$rate=0;		
		echo "$rate%";			
		echo "</td>";
	}
	echo "</tr>";
	
	//====total trunk data====
	echo "<tr>";
	echo "<td align=\"center\">In&Out</td>";
	for($i=0;$i<7;$i++)
	{
		echo "<td align=\"center\">";
		if($total_trunk[0]!=0)
		{
			$rate=$total_trunk[$i+1]/$total_trunk[0];
			$rate=$rate*100;
			$rate=number_format($rate,2,"."," ");
		}	
		else
			$rate=0;
		if($rate==0)
			$rate=0;		
		echo "$rate%";			
		echo "</td>";
	}
	echo "</table></center>";
	
	
//========================================================================================|
//=======================Draw the Graphics-1==============================================|
//========================================================================================|

	echo "<p><b>3.In/Out(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);
				
	//=====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/9);
	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 two block height=======
	if($total_trunk[0]==0)
	{
		$in_trunk_percent="0%";
		$out_trunk_percent="0%";
		$height[0]=0;
		$heighr[1]=0;
	}
	else
	{
		$in_trunk_percent=change_percent($in_trunk[0]/$total_trunk[0]);
		$out_trunk_percent=change_percent($out_trunk[0]/$total_trunk[0]);
		$height[0]=floor(180*$in_trunk[0]/$total_trunk[0]);
		$height[1]=floor(180*$out_trunk[0]/$total_trunk[0]);		
	}
	
	//======draw the data block=======
	for($i=0;$i<2;$i++)
	{		
		$x1=90+(4*$i+1)*50;
		if($height[$i]==0)
			$height[$i]=1;
		$y1=200-$height[$i];
		$x2=90+(4*$i+2)*50;
		$y2=199;
		imagefilledrectangle($im,$x1,$y1,$x2,$y2,$color[$i]);
	}
	
	//=====draw the legend string=======
	imagestring($im,2,155,205,"In",$black_color);
	imagestring($im,2,138,220,"(".$in_trunk_percent.")",$black_color);
	imagestring($im,2,355,205,"Out",$black_color);
	imagestring($im,2,340,220,"(".$out_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>4.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>

