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

adjust_head_frame("down");
adjust_title_head_frame("down");
adjust_title_tail_frame("down");

adjust_content_head_frame("down");	
echo "<br>";	
//=====================================================================================|
//============================Function Flag============================================|
//=====================================================================================|
$Hide_Show_Function_On=0;
$DEBUG=0;
//=====================================================================================|
//============================Data From Index Page=====================================|
//=====================================================================================|
if(0)
{
	
	$url_temp=$_SERVER["REQUEST_URI"];
	echo "This page URL:$url_temp<br>";
	
	echo "sel_date_from:$sel_date_from<br>";
	echo "sel_date_to:$sel_date_to<br>";
}
//=====================================================================================|
//============================Define The Data==========================================|
//=====================================================================================|
	//data define
	for($i=0;$i<3;$i++)
	{
		for($j=0;$j<8;$j++)
		{
			/*
			1  conference        0 attempt
			2  transcoding       1 success
			                     2 duration
			                     3 no resource
			                     4 negotiation fail
							             5 media error
													 6 no response
													 7 general error
			*/
			$service_csta[$i][$j]=0;
		}
	}
	
//===================================================================================|
//========================Get The Data From DB=======================================|
//===================================================================================|
	$db="CSTA_DB";
	$bgn_time=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
	$end_time=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_to,date("Y")));
	//get the numbers of days that selected
	$slot=$sel_date_from-$sel_date_to+1;
	if(0)
	{
		echo "bgn_time:$bgn_time<br>";
		echo "end_time:$end_time<br>";
		echo "slot:$slot<br>";
		echo "db:$csta_db<br>";
	}
	
	//$key_type_relation=array(5=>0,6=>1,7=>2,8=>3,9=>4,10=>5,11=>6,12=>7,13=>8,14=>9,15=>10,16=>11);
	for($day=0;$day<$slot;$day++)
	{
		$table_name="mrfcDetail";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day,date("Y")));
		$table_name=$table_name."_".$temp;
		
		//echo "table_name:$table_name<br>";	
		$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 "from:$from_time--to:$to_time<br>";
		
		$select="select key_type,sum(Attempt) as attempt,sum(Success) as success,sum(Cau1) as cau1,sum(Cau2) as cau2,sum(Cau3) as cau3,sum(Cau4) as cau4,sum(Cau5) as cau5,sum(Duration) as Duration ";
		$from="from $table_name ";
		$service_where="and key_type>0 and key_type<3 ";
		$where="where csta_datetime >='$from_time' and csta_datetime <'$to_time' ".$service_where;
		$group="group by key_type ";
		$order="order by key_type ";
		$sql=$select.$from.$where.$group.$order;
		//echo "$sql<br>";
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		
		if(!$rows)
			continue;

		do{
			$key_type=$rows[key_type]+0;
			
			for($tt=0;$tt<8;$tt++)
			{
				$service_csta[$key_type][$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="mrfcDetail";
		$temp=date("d",mktime(0,0,0,date("m"),date("d")-$sel_date_from+$day-1,date("Y")));
		$table_name=$table_name."_".$temp;
		
		//echo "table_name:$table_name<br>";	
		$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 "from:$from_time--to:$to_time<br>";
		
		$select="select key_type,sum(Attempt) as attempt,sum(Success) as success,sum(Cau1) as cau1,sum(Cau2) as cau2,sum(Cau3) as cau3,sum(Cau4) as cau4,sum(Cau5) as cau5,sum(Duration) as duration ";
		$from="from $table_name ";
		$service_where="and key_type>0 and key_type<3 ";
		$where="where csta_datetime >='$from_time' and csta_datetime <'$to_time' ".$service_where;
		$group="group by key_type ";
		$order="order by key_type ";
		$sql=$select.$from.$where.$group.$order;
		//echo "$sql<br>";
		
		$result=@mysqli_query($pubConn,$sql);
		$rows=@mysqli_fetch_array($result);
		
		if(!$rows)
			continue;

		do{
			$key_type=$rows[key_type]+0;
			
			for($tt=0;$tt<8;$tt++)
			{
				$service_csta[$key_type][$tt]+=$rows[$tt+1];
				//echo "type-$service_type-$tt:{$service_csta[$service_type][$tt]}<br>";
			}
		}while($rows=mysqli_fetch_array($result));	
	}
	

//===================================================================================|
//=========================Test The Data=============================================|
//===================================================================================|
if(0)
{
	for($i=1;$i<3;$i++)
	{
		for($j=0;$j<8;$j++)
		{
			echo "type-$i-$j:{$service_csta[$i][$j]}<br>";
		}
	}
}
	
//===================================================================================|
//=======================Show The Table-1============================================|
//===================================================================================|
	//===table title
	if($slot==1)
		$table_title="Service statistic Date:".$bgn_time;
	else
		$table_title="Service statistic Date:".$bgn_time." to ".$end_time;	
	echo "<center><b>$table_title</b></center>";
	echo "<table border=\"1\" width=\"100%\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td align=\"center\" width=\"13%\">Type</td>";
		echo "<td align=\"center\" width=\"8%\">Attempt</td>";
		echo "<td align=\"center\" width=\"8%\">Success</td>";
		echo "<td align=\"center\" width=\"8%\"><nobr>No Resource</td>";
		echo "<td align=\"center\" width=\"8%\"><nobr>Negotiation Fail</td>";
		echo "<td align=\"center\" width=\"8%\">Media Error</td>";
		echo "<td align=\"center\" width=\"8%\">No Response</td>";
		echo "<td align=\"center\" width=\"8%\">General Error</td>";
		echo "<td align=\"center\" width=\"10%\">Total call duration</td>";
		echo "<td align=\"center\" width=\"13%\">Average call duration(s)</td>";
	echo "</tr>";	
	$service_type_label=array(1=>"Conference",2=>"Transcoding");
	
	//========show subs to subs,subs to local,subs to toll,trunk to subs,trunk to trunk==========
	for($i=1;$i<3;$i++)
	{
		
		echo "<tr id=\"t$i\">";
		echo "<td align=\"center\">$service_type_label[$i]</a></td>";
		for($j=0;$j<9;$j++)
		{
			echo "<td align=\"center\">";
			switch($j)
			{
			//Attempt
			case 0:
				echo "{$service_csta[$i][0]}";
				break;
			//Success
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
				echo "{$service_csta[$i][$j]}";
				if($service_csta[$i][0]==0)
					$rate=0;
				else
				{
					$rate=$service_csta[$i][$j]/$service_csta[$i][0];
					$rate=$rate*100;
					if($rate!=0&&$rate!=100)
						$rate=number_format($rate, 2, '.', '');	
				}
				echo "<br>$rate%";
				break;
			case 7:
				echo "{$service_csta[$i][$j]}(s)";
				$hour=floor($service_csta[$i][$j]/3600);
				$minute=floor($service_csta[$i][$j]%3600/60);
				$second=$service_csta[$i][$j]%60;
				if($hour==0&&$minute==0&&$second==0)
					echo "<br>";
				else
					echo "<br>$hour:$minute:$second";
				break;
			case 8:
				if($service_csta[$i][1] == 0)
					$ave=0;
				else
					$ave=$service_csta[$i][7]/$service_csta[$i][1];
					$ave=number_format($ave, 0, '.', '');	
					echo "$ave";
				break;
			}

			echo "</td>";
	  }
		echo "</tr>";
	
	}	
	echo "</table>";
	
//=====================================================================================|
//=======================Draw The Graphics-1===========================================|
//=====================================================================================|
echo "<p><b>2.Service(Attempt) & Success/Failure</b><br>";
  //=====graphics size=======
	$sizex=650;
	$sizey=250;

	$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)));
				
				
	//====graphics 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);
	
	
	
	imagedashedline($im,30,30,100,100,$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=610;
		$y2=$y1;
		
		if($i!=10)
		{
			imageline($im, $x1,$y1,190,$y2, IMG_COLOR_STYLED);
			imageline($im, 220,$y1,$x2,$y2, IMG_COLOR_STYLED);
		}
		
		imagestring($im,2,$x1-22,$y2-5,($i*10)."%",$black_color);
		imagestring($im,2,$x2+5,$y2-5,($i*10)."%",$black_color);
		
	}

	//=======draw the vitical line=======
	$u=floor(750/25);
	for($i=0;$i<25;$i++)
	{
		$x1=40+$i*$u;
		$y1=20;
		$x2=$x1;
		$y2=200;
		if($i < 20)
		ImageDashedLine($im,$x1,$y1,$x2,$y2,$black_color);
	}
	
	//=======get colors============
	$color[0]=ImageColorAllocate($im,0x99,0x00,0x33);
	$color[1]=ImageColorAllocate($im,0x99,0x00,0xcc);
	$color[2]=ImageColorAllocate($im,0x99,0x99,0x00);
	$color[3]=ImageColorAllocate($im,0x99,0xcc,0x99);
	$color[4]=ImageColorAllocate($im,0x99,0xff,0x33);
	$color[5]=ImageColorAllocate($im,0xff,0x99,0x66);
	$color[6]=ImageColorAllocate($im,0xff,0x99,0xff);
	$color[7]=ImageColorAllocate($im,0xff,0xff,0x00);
	$color[8]=ImageColorAllocate($im,0x33,0x3,0x33);
	$color[9]=ImageColorAllocate($im,0x00,0xcc,0xff);
	$color[10]=ImageColorAllocate($im,0x00,0x66,0xcc);
	$color[11]=ImageColorAllocate($im,0x33,0xff,0x99);

	
	//=====get the precent and block height(Part-1)======
	$attempt_sum_one=0;
	for($i=1;$i<3;$i++)
	{
		$attempt_sum_one+=$service_csta[$i][0];
	}
	if($attempt_sum_one==0)
	{
		//conference precent
		$conference_attempt_precent="0%";
		//transcoding precent
		$transcoding_attempt_precent="0%";
		
		for($t=0;$t<2;$t++)
			$height[$t]=0;
	}
	else
	{
		//sub to sub precent
		$conference_attempt_precent=number_format($service_csta[1][0]*100/$attempt_sum_one,2,'.','')."%";
		//sub to trunk precent
		$transcoding_attempt_precent=number_format($service_csta[2][0]*100/$attempt_sum_one,2,'.','')."%";
		
		for($t=0;$t<2;$t++)
			$height[$t]=floor(180*$service_csta[$t+1][0]/$attempt_sum_one);
		
	}
	
	//======draw the data block======
	for($i=0;$i<2;$i++)
	{		
		$x1=40+(2*$i+1)*$u;
		if($height[$i]==0)
			$height[$i]=1;
		$y1=200-$height[$i];
		$x2=40+(2*$i+2)*$u;
		$y2=199;
		imagefilledrectangle($im,$x1,$y1,$x2,$y2,$color[$i]);
	}
	
	//=====get the data height (part-2)
	//imagefilledrectangle($im,220,20,250,200,$color[$i]);
	
	$field_index=array(1,2,3,4,5,6);
	
	for($j=0;$j<6;$j++)
	{
		$x1_1=220+($j*2+1)*30;
		$y1_1=200;
		$x1_2=$x1_1+30;
		if($attempt_sum_one==0)
			$y1_2=200;
		else
			$y1_2=200-floor(180*$service_csta[1][$field_index[$j]]/$attempt_sum_one);
			
		$x2_1=$x1_1;
		$y2_1=$y1_2;
		$x2_2=$x1_2;
		if($attempt_sum_one==0)
			$y2_2=$y1_2-0;
		else
			$y2_2=$y1_2-floor(180*$service_csta[2][$field_index[$j]]/$attempt_sum_one);
		//echo "{$x1_1},{$y1_1},{$x1_2},{$y1_2}<br>";
		imagefilledrectangle($im,$x1_1,$y1_1,$x1_2,$y1_2,$color[0]);
		imagefilledrectangle($im,$x2_1,$y2_1,$x2_2,$y2_2,$color[1]);
	}
	
	
	
	
	//======draw the legend string=======
	imagestring($im,2,72,205,"CONF",$black_color);
	imagestring($im,2,60,220,"(".$conference_attempt_precent.")",$black_color);
	imagestring($im,2,131,205,"TRAN",$black_color);
	imagestring($im,2,123,220,"(".$transcoding_attempt_precent.")",$black_color);
	imagestring($im,2,245,205,"Success",$black_color);
	//imagestring($im,2,243,221,"(".$trunk_to_trunk_attempt_precent.")",$black_color);
	imagestring($im,2,320,205,"No",$black_color);
	imagestring($im,2,305,221,"Resource",$black_color);
	imagestring($im,2,355,205,"Negotiation",$black_color);
	imagestring($im,2,375,221,"Fail",$black_color);
	imagestring($im,2,431,205,"Media",$black_color);
	imagestring($im,2,430,221,"Error",$black_color);
	imagestring($im,2,500,205,"No",$black_color);
	imagestring($im,2,485,221,"Response",$black_color);
	imagestring($im,2,548,205,"General",$black_color);
	imagestring($im,2,552,221,"Error",$black_color);

	//draw the outline
	imageLine($im,40,20,40,200,$black_color);
	imageLine($im,40,20,190,20,$black_color);
	imageLine($im,190,200,190,20,$black_color);
	imageLine($im,40,200,190,200,$black_color);
	
	imageLine($im,220,200,220,20,$black_color);
	imageLine($im,220,20,610,20,$black_color);
	imageLine($im,610,200,610,20,$black_color);
	imageLine($im,220,200,610,200,$black_color);
	
	
	//======show the service attempt 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="ser3_".time().".png";
	if(1){
		ImagePng ($im,"$filename");
		
		ImageDestroy($im);	
	}
	//-----显示图片  
	echo "<img src=\"$filename\"></img>";
	
	//=======show the remark label========
	echo "<table width=\"100%\" border=0>";
	
	echo "<tr>";
		echo "<td width=\"34%\"><font color=blue>CONF</font>-> Conference</td>";
		echo "<td width=\"34%\"><font color=blue>TRAN</font>-> Transcoding</td>";
		echo "<td width=\"33%\"></td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td width=\"33%\"></td>";
		echo "<td width=\"33%\"></td>";
		echo "<td width=\"33%\"></td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td width=\"33%\"></td>";
		echo "<td width=\"33%\"></td>";
		echo "<td width=\"34%\"></td>";
	echo "</tr>";
	echo "<tr>";	
		echo "<td width=\"33%\"></td>";
		echo "<td width=\"33%\"></td>";
		echo "<td width=\"34%\"></td>";
	echo "</tr>";
	echo "<tr>";		
		echo "<td width=\"33%\"></td>";
		echo "<td width=\"33%\"></td>";
		echo "<td width=\"33%\"></td>";
	echo "</tr>";	
	echo "</table>";
	
adjust_content_tail("down");
?>


</body>
