<?php
if(0)
{
	echo "$PHP_SELF<br>";
	echo "sysTypeNo=$sysTypeNo,show_type=$show_type,page_id=$page_id,keyOne=$keyOne<br>";
	echo "sel_date_from=$sel_date_from,sel_date_to=$sel_date_to<br>";
	echo "select_checkbox_id=$select_checkbox_id<br>";
}

echo "<input type=\"hidden\" name=\"select_checkbox_id\" value=\"$select_checkbox_id\">";
echo "<input type=\"hidden\" name=\"scroll_y\" value=\"$scroll_y\">";

$cstaConf=GetCstaConf($sysTypeNo);

$slot=$sel_date_from-$sel_date_to+1;
$total_hour_num=$slot*24;
$startTime=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
$endTime=date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-$sel_date_to,date("Y")));
//echo "startTime=$startTime,endTime=$endTime<br>";

$cstaData=GetCstaDataByHour($cstaConf,$startTime,$endTime,"detail",$keyOne);
if(0)
{
	echo "<pre>";
	print_r($cstaData);
	echo "</pre>";
}

//Table-1
{
	if($keyOne == 1)
		$table_title="Conference";
	else
		$table_title="Transcoding";
	if($slot > 1)
		$table_title.=" Statistic&nbsp;&nbsp;Date:".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.=" Statistic&nbsp;&nbsp;Date:".date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
	echo "<br><b><center>$table_title</center></b>";
	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	echo "<tr bgcolor=\"#E6E6E6\">";
	echo "<td align=\"center\" width=\"13%\">Time</td>";
	echo "<td align=\"center\" width=\"8%\">Attempt</td>";
	echo "<td align=\"center\" width=\"8%\">Success</td>";
	echo "<td align=\"center\" width=\"8%\">No Resource</td>";
	echo "<td align=\"center\" width=\"8%\">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>";
	
	for($hour=0;$hour<$total_hour_num;$hour++)
	{
		echo "<tr>";
		$hour_title=date("m-d H",mktime($hour,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
		echo "<td align=\"center\">{$hour_title}H</td>";
		
		echo "<td align=\"center\">";
		$value=$cstaData[0][$keyOne][Attempt][$hour]+0;
		echo "$value";
		echo "</td>";
		
		echo "<td align=\"center\">";
		$value=$cstaData[0][$keyOne][Success][$hour]+0;
		$percent=GetPercent($cstaData[0][$keyOne][Success][$hour],$cstaData[0][$keyOne][Attempt][$hour]);
		echo "$value<br>";
		echo "$percent";
		echo "</td>";
		
		echo "<td align=\"center\">";
		$value=$cstaData[0][$keyOne][Cau1][$hour]+0;
		$percent=GetPercent($cstaData[0][$keyOne][Cau1][$hour],$cstaData[0][$keyOne][Attempt][$hour]);
		echo "$value<br>";
		echo "$percent";
		echo "</td>";
		
		echo "<td align=\"center\">";
		$value=$cstaData[0][$keyOne][Cau2][$hour]+0;
		$percent=GetPercent($cstaData[0][$keyOne][Cau2][$hour],$cstaData[0][$keyOne][Attempt][$hour]);
		echo "$value<br>";
		echo "$percent";
		echo "</td>";
		
		echo "<td align=\"center\">";
		$value=$cstaData[0][$keyOne][Cau3][$hour]+0;
		$percent=GetPercent($cstaData[0][$keyOne][Cau3][$hour],$cstaData[0][$keyOne][Attempt][$hour]);
		echo "$value<br>";
		echo "$percent";
		echo "</td>";
		
		echo "<td align=\"center\">";
		$value=$cstaData[0][$keyOne][Cau4][$hour]+0;
		$percent=GetPercent($cstaData[0][$keyOne][Cau4][$hour],$cstaData[0][$keyOne][Attempt][$hour]);
		echo "$value<br>";
		echo "$percent";
		echo "</td>";
		
		echo "<td align=\"center\">";
		$value=$cstaData[0][$keyOne][Cau5][$hour]+0;
		$percent=GetPercent($cstaData[0][$keyOne][Cau5][$hour],$cstaData[0][$keyOne][Attempt][$hour]);
		echo "$value<br>";
		echo "$percent";
		echo "</td>";
		
		echo "<td align=\"center\">";
		$value=$cstaData[0][$keyOne][Duration][$hour]+0;
		$timestamp=SecondToTimeStamp($value);
		echo "$value<br>";
		echo "$timestamp";
		echo "</td>";
		
		echo "<td align=\"center\">";
		$value=$cstaData[0][$keyOne][Success][$hour]+0;
		if($value == 0)
			$tmp=0;
		else
			$tmp=ceil($cstaData[0][$keyOne][Duration][$hour]/$value);
		echo "$tmp";
		echo "</td>";
	}
	echo "</table>";
}

//Graphics-1
{
	if($flag0 == "on")
	{
		$tmpColor[]=array(0x66,0xff,0x33);
		for($i=0;$i<$total_hour_num;$i++)
			$suc_data[$i]=$cstaData[0][$keyOne][Success][$i]+0;
		$tmpData[]=$suc_data;
	}
	if($flag1 == "on")
	{
		$tmpColor[]=array(0x66,0xff,0xff);
		for($i=0;$i<$total_hour_num;$i++)
			$cau1_data[$i]=$cstaData[0][$keyOne][Cau1][$i]+0;
		$tmpData[]=$cau1_data;
	}
	if($flag2 == "on")
	{
		$tmpColor[]=array(0,0x66,0x66);
		for($i=0;$i<$total_hour_num;$i++)
			$cau2_data[$i]=$cstaData[0][$keyOne][Cau2][$i]+0;
		$tmpData[]=$cau2_data;
	}
	if($flag3 == "on")
	{
		$tmpColor[]=array(0x66,0,0xcc);
		for($i=0;$i<$total_hour_num;$i++)
			$cau3_data[$i]=$cstaData[0][$keyOne][Cau3][$i]+0;
		$tmpData[]=$cau3_data;
	}
	if($flag4 == "on")
	{
		$tmpColor[]=array(0x66,0x33,0);
		for($i=0;$i<$total_hour_num;$i++)
			$cau4_data[$i]=$cstaData[0][$keyOne][Cau4][$i]+0;
		$tmpData[]=$cau4_data;
	}
	if($flag5 == "on")
	{
		$tmpColor[]=array(0xcc,0,0x66);
		for($i=0;$i<$total_hour_num;$i++)
			$cau5_data[$i]=$cstaData[0][$keyOne][Cau5][$i]+0;
		$tmpData[]=$cau5_data;
	}
	
	echo "<br><b>Hourly Statistic Chart</b><br>";
	
	if(0)
	{
		echo "flag0=$flag0,flag1=$flag1,flag2=$flag2,flag3=$flag3,flag4=$flag4,flag5=$flag5,flag6=$flag6,flag7=$flag7,flag8=$flag8,flag9=$flag9,flag10=$flag10,flag11=$flag11,flag12=$flag12,flag13=$flag13,flag14=$flag14,flag15=$flag15,flag16=$flag16<br>";
	}
	$boldline=$select_checkbox_id;
	for($i=0;$i<$select_checkbox_id;$i++)
	{
		$param="flag$i";
		if($$param != "on")
			$boldline--;
	}
	
	$image=CreateImage(820,390);
	$image=DrawLineHistogram(
		$image,
		$tmpData,
		$tmpColor,
		750/(24*$slot),
		24,
		array(50,20),
		array(750,280),
		$boldline,1,0,1
	);
	
	$remarkBlock=array(
		"Success",
		"No Resource",
		"Negotiation Fail",
		"Media Error",
		"No Response",
		"General Error"
	);
	$pointArr=array(
		array(60,367),
		array(180,367),
		array(300,367),
		array(420,367),
		array(540,367),
		array(660,367)
	);
	
	unset($tmpColor);
	$tmpColor[]=array(0x66,0xff,0x33);
	$tmpColor[]=array(0x66,0xff,0xff);
	$tmpColor[]=array(0,0x66,0x66);
	$tmpColor[]=array(0x66,0,0xcc);
	$tmpColor[]=array(0x66,0x33,0);
	$tmpColor[]=array(0xcc,0,0x66);
	
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,$tmpColor,0,2);
	DrawTimeStamp($image,$sel_date_from,$sel_date_to,array(50,355),750/24);
	
	echo "<table border=0 width=\"820\">";
	echo "<tr>";
	for($i=0;$i<sizeof($remarkBlock);$i++)
	{
		echo "<td>";
		AddCheckbox($remarkBlock[$i]);
		echo "</td>";
	}
	echo "</tr>";
	echo "</table>";
	
	DrawImage($image);
}

//Graphics-2
{
	for($i=0;$i<$total_hour_num;$i++)
	{
		$value=$cstaData[0][$keyOne][Success][$i]+0;
		$duration_value[$i]=$cstaData[0][$keyOne][Duration][$i]+0;
		if($value > 0)
			$aveduration_value[$i]=ceil($duration_value[$i]/$value);
		else
			$aveduration_value[$i]=0;
	}
	unset($tmpColor);
	unset($tmpData);
	
	if($flag6 == "on")
	{
		$tmpColor[]=array(0x66,0xff,0xff);
		$tmpData[]=$duration_value;
	}
	if($flag7 == "on")
	{
		$tmpColor[]=array(0,0x66,0x66);	
		$tmpData[]=$aveduration_value;
	}
	if(0)
	{
		echo "<pre>";
		print_r($tmpData);
		echo "</pre>";
	}
	echo "<br><br><b>Duration Statistic Chart</b><br>";
	$image=CreateImage(820,390);
	
	$boldline=$select_checkbox_id-6;
	for($i=6;$i<$select_checkbox_id;$i++)
	{
		$param="flag$i";
		if($$param != "on")
			$boldline--;
	}
	
	$image=DrawLineHistogram(
		$image,
		$tmpData,
		$tmpColor,
		750/(24*$slot),
		24,
		array(50,20),
		array(750,280),
		$boldline,1,0,1
	);
	
	unset($tmpColor);
	$tmpColor[]=array(0x66,0xff,0xff);
	$tmpColor[]=array(0,0x66,0x66);	
	
	$remarkBlock=array(
		"Total Call Duration",
		"Average Call Duration"
	);
	$pointArr=array(
		array(60,367),
		array(260,367)
	);
	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,$tmpColor,0,2);
	DrawTimeStamp($image,$sel_date_from,$sel_date_to,array(50,355),750/24);
	
	echo "<table border=0 width=\"820\">";
	echo "<tr>";
	for($i=0;$i<sizeof($remarkBlock);$i++)
	{
		echo "<td>";
		AddCheckbox($remarkBlock[$i]);
		echo "</td>";
	}
	echo "</tr>";
	echo "</table>";
	
	DrawImage($image);
}
?>
