<?php
$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")));
//echo "startTime=$startTime,endTime=$endTime<br>";

//KeyOne=1
$cstaDataChip[5]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",5);
$cstaDataChip[6]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",6);
$cstaDataChip[7]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",7);
$cstaDataChip[8]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",8);
//KeyOne=2
$cstaDataChip[9]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",9);
//KeyOne=3
$cstaDataChip[10]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",10);
$cstaDataChip[11]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",11);
//KeyOne=4
$cstaDataChip[12]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",12);
$cstaDataChip[13]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",13);
//KeyOne=5
$cstaDataChip[14]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",14);
$cstaDataChip[15]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",15);
$cstaDataChip[16]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",16);
//KeyOne=6
$cstaDataChip[17]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",17);
$cstaDataChip[18]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",18);
$cstaDataChip[19]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",19);
$cstaDataChip[20]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",20);
$cstaDataChip[21]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",21);
$cstaDataChip[22]=GetCstaDataByDay($cstaConf,$startTime,$endTime,"sum",22);

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

//Table-1
{
	$table_title="Service 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")));
	
	$rowTitle=array(
		"Subs to subs",
		"Subs to trunk",
		"Trunk to subs",
		"Trunk to trunk",
		"Intraconnection",
		"--",
		"Subs to IP",
		"Trunk to IP",
		"--",
		"Mobile originated",
		"Mobile terminated",
		"GSM",
		"UMTS",
		"In trunk",
		"Out trunk",
		"IP terminated"
	);
	$rowNum=sizeof($rowTitle);
	
	$rowSubTitle=array(
		"GSM UMTS",
		"GSM UMTS",
		"GSM UMTS",
		"--",
		"--",
		"--",
		"GSM UMTS",
		"--",
		"--",
		"GSM UMTS",
		"GSM UMTS",
		"--",
		"--",
		"--",
		"--",
		"--"
	);
	$rowSubNum=sizeof($rowSubTitle);
	
	$columnTitle=array(
		"Type",
		"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);
	
	$objInderRelation=array(1,1,1,1,2,-1,3,3,-1,4,4,-1,-1,5,5,6);
	$keyOneRelation=array(5,6,7,8,9,-1,10,11,-1,12,13,-1,-1,14,15,16);
	$objInderRelation3g=array(7,7,7,-1,-1,-1,7,-1,-1,7,7,-1,-1,-1,-1,-1);
	$keyOneRelation3g=array(17,18,19,-1,-1,-1,20,-1,-1,21,22,-1,-1,-1,-1,-1);
	$dataFiled=array("Attempt","Success","CallDeny","NetBusy","CallerClear","SubBusy","NoAnswer","SignalErr","Duration");
	
	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++)
	{
		if($i==0)
			echo "<td colspan=\"2\" align=\"center\">{$columnTitle[$i]}</td>";
		else
			echo "<td align=\"center\">{$columnTitle[$i]}</td>";
	}
	echo "</tr>";
	
	for($i=0;$i<$rowNum;$i++)
	{
		echo "<tr>";
		if($rowTitle[$i] == "--")
		{
			$columnNum1=$columnNum+1;
			echo "<td colspan=\"$columnNum1\">&nbsp;</td>";
			echo "</tr>";
		}
		else
		{
			if($rowSubTitle[$i] !="--")
			{
				echo "<td rowspan=\"2\" align=\"center\">";
				echo "<a href=# onclick=\"javascript:toHourPage('$sysTypeNo','$show_type','$page_id','$sel_date_from','$sel_date_to','{$rowTitle[$i]}','-1');\">{$rowTitle[$i]}</a>";						
			}
			else
			{
				echo "<td colspan=\"2\" align=\"center\">";
				echo "<a href=# onclick=\"javascript:toHourPage('$sysTypeNo','$show_type','$page_id','$sel_date_from','$sel_date_to','{$rowTitle[$i]}','-1');\">{$rowTitle[$i]}</a>";						
			}
			echo "</td>";
			$obj=$objInderRelation[$i];
			$key=$keyOneRelation[$i];
			$rowSubTitleNum=explode(' ',$rowSubTitle[$i]);
			for($iii=0;$iii<sizeof($rowSubTitleNum);$iii++)
			{				
				if($rowSubTitleNum[$iii] !="--" && $iii==0)					
  				echo "<td>$rowSubTitleNum[$iii]</td>";					
				else if($rowSubTitleNum[$iii] !="--" && $iii==1)
				{
					echo "<tr><td>$rowSubTitleNum[$iii]</td>";
					$obj=$objInderRelation3g[$i];
					$key=$keyOneRelation3g[$i];
				}
				//debug_log("/tmp/debugperformance","i:$i,obj:$obj,key:$key");
				for($j=0;$j<$columnNum-1;$j++)
				{
					$field=$dataFiled[$j];
					$cstaDataChip[$key][$obj][$key][$field]+=0;				
					echo "<td align=\"center\">";
					if($i==9 && $rowSubTitleNum[$iii]=="GSM")
						$gsmMO[$field]=$cstaDataChip[$key][$obj][$key][$field];
					if($i==10&&$rowSubTitleNum[$iii] =="GSM")
						$gsmMT[$field]=$cstaDataChip[$key][$obj][$key][$field];
					if($i==9&&$rowSubTitleNum[$iii] =="UMTS")
						$umtsMO[$field]=$cstaDataChip[$key][$obj][$key][$field];
					if($i==10&&$rowSubTitleNum[$iii] =="UMTS")
						$umtsMT[$field]=$cstaDataChip[$key][$obj][$key][$field];
					$gsm[$field]=$gsmMO[$field]+$gsmMT[$field];
					$umts[$field]=$umtsMO[$field]+$umtsMT[$field];
					if($j == 0)
					{						
						if($i==11)				
							echo "{$gsm[$field]}";
						else if($i==12)
							echo "{$umts[$field]}";
						else
							echo "{$cstaDataChip[$key][$obj][$key][$field]}";
					}
					else if($j==$columnNum-3)
					{
						if($i==11)
						{
							echo "{$gsm[$field]}<br>";
							$value=$gsm[$field];							
						}
						else if($i==12)
						{
							echo "{$umts[$field]}<br>";
							$value=$umts[$field];
						}
						else
						{
							echo "{$cstaDataChip[$key][$obj][$key][$field]}<br>";
							$value=$cstaDataChip[$key][$obj][$key][$field];												
						}
						echo SecondToTimeStamp($value);
					}
					else if($j==$columnNum-2)
					{
						if($i==11)
						{
							if($gsm[Attempt]>0)
							{
								if($gsm[Success]>0)
									$value=ceil($gsm[Duration]/$gsm[Success]);
								else
									$value=0;
							}
							else
								$value=0;
						}
						else if($i==12)
						{
							if($umts[Attempt]>0)
							{
								if($umts[Success]>0)
									$value=ceil($umts[Duration]/$umts[Success]);
								else
									$value=0;
							}
							else
								$value=0;
						}
						else
						{
							if($cstaDataChip[$key][$obj][$key][Attempt] > 0)
							{
								if($cstaDataChip[$key][$obj][$key][Success] > 0) 
									$value=ceil($cstaDataChip[$key][$obj][$key][Duration]/$cstaDataChip[$key][$obj][$key][Success]);
								else
									$value=0;
							}
							else
								$value=0;
						}
						echo "{$value}";
					}
					else
					{			
						if($i==11)
						{
							echo "$gsm[$field]<br>";
							$percent=GetPercent($gsm[$field],$gsm[Attempt]);
						}	
						else if($i==12)
						{
							echo "$umts[$field]<br>";
							$percent=GetPercent($umts[$field],$umts[Attempt]);
						}	
						else
						{
							echo "{$cstaDataChip[$key][$obj][$key][$field]}<br>";
							$percent=GetPercent($cstaDataChip[$key][$obj][$key][$field],$cstaDataChip[$key][$obj][$key][Attempt]);
						}
						echo "{$percent}";
					}
				}	
			}
			echo "</td>";
			echo "</tr>";
		}
	}	
	echo "</table>";
}

//Graphics-1, Part-1,GSM
{
	$tmpData[]=$cstaDataChip[5][1][5][Attempt];
	$tmpData[]=$cstaDataChip[6][1][6][Attempt];
	$tmpData[]=$cstaDataChip[7][1][7][Attempt];
	$tmpData[]=$cstaDataChip[8][1][8][Attempt];
	$tmpData[]=$cstaDataChip[9][2][9][Attempt];
	$tmpData[]=$cstaDataChip[10][3][10][Attempt];
	$tmpData[]=$cstaDataChip[11][3][11][Attempt];
	if(0)
	{
		echo "<pre>";
		print_r($tmpData);
		echo "</pre>";
	}
	$tmpColor[]=array(0x99,0x00,0x33);
	$tmpColor[]=array(0x99,0x00,0xcc);
	$tmpColor[]=array(0x99,0x99,0x00);
	$tmpColor[]=array(0x99,0xcc,0x99);
	$tmpColor[]=array(0x99,0xff,0x33);
	$tmpColor[]=array(0xff,0x99,0x66);
	$tmpColor[]=array(0xff,0x99,0xff);
	
	echo "<br><b>Service(Attempt) GSM</b><br>";
	$image=CreateImage(820,320);
	$image=DrawPercentHistogram(
		$image,
		$tmpData,
		$tmpColor,
		array(30,20),
		array(410,250),1,0
	);
	$remarkBlock=array("SUB SUB","SUB TRK","TRK SUB","TRK TRK","INTRA","SUB IP","TRK IP");
	$pointArr=array(
		array(50,290),
		array(103,290),
		array(156,290),
		array(210,290),
		array(272,290),
		array(322,290),
		array(378,290)
	);
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,0,0,2);
	
	//Graphics-1, Part-2
	unset($tmpData);
	$tmpData[]=$cstaDataChip[12][4][12][Attempt];
	$tmpData[]=$cstaDataChip[13][4][13][Attempt];
	$tmpData[]=$cstaDataChip[14][5][14][Attempt];
	$tmpData[]=$cstaDataChip[15][5][15][Attempt];
	$tmpData[]=$cstaDataChip[16][6][16][Attempt];
	unset($tmpColor);
	$tmpColor[]=array(0xff,0xff,0x00);
	$tmpColor[]=array(0x33,0x03,0x33);
	$tmpColor[]=array(0x00,0xcc,0xff);
	$tmpColor[]=array(0x00,0x66,0xcc);
	$tmpColor[]=array(0x33,0xff,0x99);
	if(0)
	{
		echo "<pre>";
		print_r($tmpData);
		echo "</pre>";
	}
	$image=DrawPercentHistogram(
		$image,
		$tmpData,
		$tmpColor,
		array(450,20),
		array(330,250),0,1
	);
	unset($remarkBlock);
	unset($pointArr);
	$remarkBlock=array("MO","MT","IN TRK","OUT TRK","IP");
	$pointArr=array(
		array(490,290),
		array(550,290),
		array(598,290),
		array(656,290),
		array(730,290)
	);
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,0,0,2);
	DrawImage($image);
}


//Graphics-2,UMTS
{
	unset($tmpData);
	$tmpData[]=$cstaDataChip[17][7][17][Attempt];
	$tmpData[]=$cstaDataChip[18][7][18][Attempt];
	$tmpData[]=$cstaDataChip[19][7][19][Attempt];
	$tmpData[]=$cstaDataChip[20][7][20][Attempt];
	$tmpData[]=$cstaDataChip[21][7][21][Attempt];
	$tmpData[]=$cstaDataChip[22][7][22][Attempt];
	if(0)
	{
		echo "<pre>";
		print_r($tmpData);
		echo "</pre>";
	}
	unset($tmpColor);
	$tmpColor[]=array(0x99,0x00,0x33);
	$tmpColor[]=array(0x99,0x00,0xcc);
	$tmpColor[]=array(0x99,0x99,0x00);
	$tmpColor[]=array(0xff,0x99,0xff);
	$tmpColor[]=array(0xff,0xff,0x00);
	$tmpColor[]=array(0x33,0x03,0x33);
	echo "<br><b>Service(Attempt) UMTS</b><br>";
	$image=CreateImage(820,320);
	$image=DrawPercentHistogram(
		$image,
		$tmpData,
		$tmpColor,
		array(30,20),
		array(400,250),1,0
	);
	$remarkBlock=array("SUB SUB","SUB TRK","TRK SUB","TRK IP","MO","MT");
	$pointArr=array(
		array(52,290),
		array(120,290),
		array(180,290),
		array(250,290),
		array(315,290),
		array(375,290)
	);
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,0,0,2);
	DrawImage($image);
}

	
	//Graphics-3, Part-1,GSM
{
	echo "<br><br><b>Success/Failure GSM</b><br>";
	unset($tmpData);
	$datafieldnum=sizeof($dataFiled)-1;
	for($i=1;$i<$datafieldnum;$i++)
	{
		$tmpData[$i-1][]=$cstaDataChip[5][1][5][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[6][1][6][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[7][1][7][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[8][1][8][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[9][2][9][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[10][3][10][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[11][3][11][$dataFiled[$i]];
	}
	unset($tmpColor);
	$tmpColor[]=array(0x99,0x00,0x33);
	$tmpColor[]=array(0x99,0x00,0xcc);
	$tmpColor[]=array(0x99,0x99,0x00);
	$tmpColor[]=array(0x99,0xcc,0x99);
	$tmpColor[]=array(0x99,0xff,0x33);
	$tmpColor[]=array(0xff,0x99,0x66);
	$tmpColor[]=array(0xff,0x99,0xff);
	
	$image=CreateImage(820,350);
	$image=DrawStackPercentHistogram($image,
		$tmpData,
		$tmpColor,
		array(30,20),
		array(380,280),1,0
	);


	//Graphics-3, Part-2
	unset($tmpData);
	for($i=1;$i<$datafieldnum;$i++)
	{
		$tmpData[$i-1][]=$cstaDataChip[12][4][12][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[13][4][13][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[14][5][14][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[15][5][15][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[16][6][16][$dataFiled[$i]];
	}
	unset($tmpColor);
	$tmpColor[]=array(0xff,0xff,0x00);
	$tmpColor[]=array(0x33,0x03,0x33);
	$tmpColor[]=array(0x00,0xcc,0xff);
	$tmpColor[]=array(0x00,0x66,0xcc);
	$tmpColor[]=array(0x33,0xff,0x99);
   
  $image=DrawStackPercentHistogram($image,
		$tmpData,
		$tmpColor,
		array(450,20),
		array(330,280),1,0
	);

	unset($remarkBlock);
	unset($pointArr);
	$remarkBlock=array(
		"Success",
		"Call","Deny",
		"Net","Busy",
		"Caller","Clear",
		"Sub","Busy",
		"No","Answer",
		"Signal","Error",
		"Success",
		"Call","Deny",
		"Net","Busy",
		"Caller","Clear",
		"Sub","Busy",
		"No","Answer",
		"Signal","Error"
	);
	$pointArr=array(
		array(45,305),
		array(105,300),array(105,310),
		array(157,300),array(155,310),
		array(200,300),array(202,310),
		array(256,300),array(254,310),
		array(308,300),array(300,310),
		array(348,300),array(350,310),
		array(445,305),
		array(505,300),array(505,310),
		array(557,300),array(555,310),
		array(600,300),array(602,310),
		array(656,300),array(654,310),
		array(708,300),array(700,310),
		array(748,300),array(750,310)
	);
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,0,0,2);
	DrawImage($image);
}
	
	//Graphics-4, UMTS
{
	echo "<br><br><b>Success/Failure UMTS</b><br>";
	unset($tmpData);
	$datafieldnum=sizeof($dataFiled)-1;
	for($i=1;$i<$datafieldnum;$i++)
	{
		$tmpData[$i-1][]=$cstaDataChip[17][7][17][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[18][7][18][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[19][7][19][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[20][7][20][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[21][7][21][$dataFiled[$i]];
		$tmpData[$i-1][]=$cstaDataChip[22][7][22][$dataFiled[$i]];
	}
	unset($tmpColor);
	$tmpColor[]=array(0x99,0x00,0x33);
	$tmpColor[]=array(0x99,0x00,0xcc);
	$tmpColor[]=array(0x99,0x99,0x00);
	$tmpColor[]=array(0xff,0x99,0xff);
	$tmpColor[]=array(0xff,0xff,0x00);
	$tmpColor[]=array(0x33,0x03,0x33);
	
	$image=CreateImage(820,380);
	$image=DrawStackPercentHistogram($image,
		$tmpData,
		$tmpColor,
		array(30,20),
		array(440,280),1,0
	);
}
	unset($remarkBlock);
	unset($pointArr);
	$remarkBlock=array(
		"Success",
		"Call","Deny",
		"Net","Busy",
		"Caller","Clear",
		"Sub","Busy",
		"No","Answer",
		"Signal","Error",
	);
	$pointArr=array(
		array(45,305),
		array(120,300),array(120,310),
		array(185,300),array(185,310),
		array(240,300),array(240,310),
		array(300,300),array(300,310),
		array(350,300),array(350,310),
		array(405,300),array(405,310),

	);
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,0,0,2);

	
	
	unset($remarkBlock);
	unset($pointArr);
	$remarkBlock=array(
		"SUB SUB",
		"SUB TRK",
		"TRK SUB",
		"MO",
		"MT",
		"IN TRK",
		"TRK IP",
		"TRK TRK",
		"INTRA",
		"OUT TRK",
		"IP",
		"SUB IP"
	);
	$pointArr=array(
		array(30,330),
		array(175,330),
		array(315,330),
		array(460,330),
		array(605,330),
		array(750,330),
		array(30,345),
		array(175,345),
		array(315,345),
		array(460,345),
		array(605,345),
		array(30,360)
	);
	unset($tmpColor);
	$tmpColor[]=array(0x99,0x00,0x33);
	$tmpColor[]=array(0x99,0x00,0xcc);
	$tmpColor[]=array(0x99,0x99,0x00);
	$tmpColor[]=array(0x99,0xcc,0x99);
	$tmpColor[]=array(0x99,0xff,0x33);
	$tmpColor[]=array(0xff,0x99,0x66);
	$tmpColor[]=array(0xff,0x99,0xff);
	$tmpColor[]=array(0xff,0xff,0x00);
	$tmpColor[]=array(0x33,0x03,0x33);
	$tmpColor[]=array(0x00,0xcc,0xff);
	$tmpColor[]=array(0x00,0x66,0xcc);
	$tmpColor[]=array(0x33,0xff,0x99);

	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,$tmpColor,0,2);
	
	DrawImage($image);


	//=======show the remark label========
{
	echo "<table width=\"820\" border=0>";

	echo "<tr>";
		echo "<td width=\"34%\"><font color=blue>SUB SUB</font>-> Subscriber to subscriber</td>";
		echo "<td width=\"34%\"><font color=blue>TRK TRK</font>-> Trunk to trunk</td>";
		echo "<td width=\"33%\"><font color=blue>MO</font>-> Mobile originated</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td width=\"33%\"><font color=blue>SUB TRK</font>-> Subscriber to trunk</td>";
		echo "<td width=\"33%\"><font color=blue>INTRA</font>-> Intraconnection</td>";
		echo "<td width=\"33%\"><font color=blue>MT</font>-> Mobile terminated</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td width=\"33%\"><font color=blue>TRK SUB</font>-> Trunk to subscriber</td>";
		echo "<td width=\"33%\"><font color=blue>SUB IP</font>-> Subscriber to IP</td>";
		echo "<td width=\"34%\"><font color=blue>IN TRK</font>-> In trunk</td>";
	echo "</tr>";
	echo "<tr>";	
		echo "<td width=\"33%\"><font color=blue>TRK IP</font>-> Trunk to IP</td>";
		echo "<td width=\"34%\"><font color=blue>OUT TRK</font>-> Out trunk</td>";
		echo "<td width=\"33%\"><font color=blue>IP</font>-> IP terminated</td>";
	echo "</tr>";
	echo "<tr>";		
		echo "<td width=\"33%\"><font color=blue>GSM</font>-> Mob originated  GSM+Mob terminated  GSM</td>";
		echo "<td width=\"33%\"><font color=blue>UMTS</font>-> Mob originated  UMTS+Mob terminated  UMTS</td>";
	echo "</tr>";	
	echo "</table>";
}

?>