<?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);
if(0)
{
	echo "<pre>";
	print_r($cstaConf);
	echo "</pre>";
}

$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>";

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

//Table-1
{
	$datafield[]="Attempt";
	$datafield[]="Success";
	
	$showfield[]="Attempt";
	$showfield[]="Success";
	$showfield[]="Normal";
	switch($keyOne)
	{
	case 1:
		$datafield[]="lu_normal";
		$datafield[]="lu_res_not_ok";
		$datafield[]="lu_ser_not_ok";
		$datafield[]="lu_ser_no_imp";
		$datafield[]="lu_msg_error";
		$datafield[]="lu_pro_error";
		
		$showfield[]="No Resource";
		$showfield[]="No Service";
		$showfield[]="Invalid Service";
		$showfield[]="Invalid Message";
		$showfield[]="Signal Error";
		break;
	case 2:
	case 3:
		$datafield[]="cc_normal";
		$datafield[]="cc_call_deny";
		$datafield[]="cc_net_busy";
		$datafield[]="cc_caller_clear";
		$datafield[]="cc_sub_busy";
		$datafield[]="cc_no_answer";
		$datafield[]="cc_sig_error";
		
		$showfield[]="Call Deny";
		$showfield[]="Net Busy";
		$showfield[]="Caller Clear";
		$showfield[]="Sub Busy";
		$showfield[]="No Answer";
		$showfield[]="Signal Error";
		break;
	case 4:
	case 5:
		$datafield[]="sm_normal";
		$datafield[]="sm_error_sub";
		$datafield[]="sm_ser_not_ok";
		$datafield[]="sm_net_busy";
		$datafield[]="sm_msg_error";
		$datafield[]="sm_pro_error";
		$datafield[]="sm_res_not_ok";
		
		$showfield[]="Sub Error";
		$showfield[]="No Service";
		$showfield[]="Net Busy";
		$showfield[]="Message Error";
		$showfield[]="Signal Error";
		$showfield[]="No Resource";
		break;
	}
	$datafield[]="Duration";
	$showfield[]="Duration";
	$showfield[]="Average Duration";
	
	$datafieldnum=sizeof($datafield);
	$showfieldnum=sizeof($showfield);
	for($hour=0;$hour<$total_hour_num;$hour++)
	{
		for($i=0;$i<$datafieldnum;$i++)
		{
			for($j=1;$j<=256;$j++)
				$hourData[$datafield[$i]][$hour]+=$cstaData[$j][$datafield[$i]][$hour];
		}
	}
	
	if(0)
	{
		echo "<pre>";
		print_r($hourData);
		echo "</pre>";
	}
	
	$typeLabel=array(1=>"LU",2=>"Call MO",3=>"Call MT",4=>"SM MO",5=>"SM MT");
	$table_title="{$typeLabel[$keyOne]} 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")));
	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\">";
	$fieldwidth=90/($datafieldnum+2);
	echo "<td align=\"center\" width=\"10%\">Time</td>";
	for($i=0;$i<$showfieldnum;$i++)
		echo "<td align=\"center\" width=\"{$fieldwidth}%\">{$showfield[$i]}</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>";
		
		for($i=0;$i<$datafieldnum;$i++)
		{
			$value=$hourData[$datafield[$i]][$hour];
			if(($keyOne == 1 && $i == 8) || ($keyOne == 4 && $i == 9) || ($keyOne == 5 && $i == 9))
				echo "<td align=\"center\">";
			else
				echo "<td align=\"center\">$value";
			if($i>0 && $i< ($datafieldnum-1))
			{
				$percent=GetPercent($value,$hourData[Attempt][$hour]);
				echo "<br>$percent";
			}
			else if($i == ($datafieldnum-1))
			{
				$timestamp=SecondToTimeStamp($value);
				if($keyOne == 1 || $keyOne == 4 || $keyOne == 5)
					echo "--";
				else
					echo "<br>$timestamp";
			}
			echo "</td>";
		}
		
		if($hourData[Success][$hour] > 0)
			$value=ceil($hourData[Duration][$hour]/$hourData[Success][$hour]);
		else
			$value=0;
		if($keyOne == 1 || $keyOne == 4 || $keyOne == 5)
			echo "<td align=\"center\">--</td>";
		else
			echo "<td align=\"center\">$value</td>";

		echo "";
		echo "</tr>";
	}
	echo "</table>";
}

//Graphics-1
{
	unset($tmpColor);
	unset($tmpData);
	if($flag0 == "on")
	{
		$tmpColor[]=array(0x66,0xff,0x33);
		$tmpData[]=$hourData[$datafield[1]];
	}
	
	if($flag1 == "on")
	{
		$tmpColor[]=array(0x66,0xff,0xff);
		$tmpData[]=$hourData[$datafield[2]];
	}
	if($flag2 == "on")
	{
		$tmpColor[]=array(0,0x66,0x66);
		$tmpData[]=$hourData[$datafield[3]];
	}
	if($flag3 == "on")
	{
		$tmpColor[]=array(0x66,0,0xcc);
		$tmpData[]=$hourData[$datafield[4]];
	}
	if($flag4 == "on")
	{
		$tmpColor[]=array(0x66,0x33,0);
		$tmpData[]=$hourData[$datafield[5]];
	}
	if($flag5 == "on")
	{
		$tmpColor[]=array(0xcc,0,0x66);
		$tmpData[]=$hourData[$datafield[6]];
	}
	if($flag6 == "on")
	{
		$tmpColor[]=array(0xcc,0xee,0x66);
		$tmpData[]=$hourData[$datafield[7]];
	}
	if($keyOne > 1)
	{
		if($flag7 == "on")
		{
			$tmpColor[]=array(0xff,0,0);
			$tmpData[]=$hourData[$datafield[8]];
		}
	}
	
	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
	);
	
	if($keyOne == 1)
	{
		$remarkBlock=array(
			"Success",
			"Normal",
			"No Resource",
			"No Service",
			"Invalid Service",
			"Invalid Message",
			"Signal Error"
		);
	}
	else if($keyOne == 2 || $keyOne == 3)
	{
		$remarkBlock=array(
			"Success",
			"Normal",
			"Call Deny",
			"Net Busy",
			"Caller Clear",
			"Sub Busy",
			"No Answer",
			"Signal Error"
		);
	}
	else if($keyOne == 4 || $keyOne == 5)
	{
		$remarkBlock=array(
			"Success",
			"Normal",
			"Sub Error",
			"No Service",
			"Net Busy",
			"Message Error",
			"Signal Error",
			"No Resource"
		);
	}
	$pointArr=array(
		array(60,367),
		array(160,367),
		array(260,367),
		array(360,367),
		array(460,367),
		array(560,367),
		array(660,367)
	);
	
	if($keyOne > 1)
		$pointArr[]=array(760,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);
	$tmpColor[]=array(0xcc,0xee,0x66);
	if($keyOne > 1)
		$tmpColor[]=array(0xff,0,0);
	$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
if(!($keyOne == 1 || $keyOne == 4 || $keyOne == 5))
{
	for($i=0;$i<$total_hour_num;$i++)
	{
		if($hourData[Success][$i] > 0)
			$hourData[AveDuration][$i]=ceil($hourData[Duration][$i]/$hourData[Success][$i]);
		else
			$hourData[AveDuration][$i]=0;
	}

	unset($tmpColor);
	unset($tmpData);
	
	if($keyOne == 1)
	{
		if($flag7 == "on")
		{
			$tmpColor[]=array(0x66,0xff,0xff);
			$tmpData[]=$hourData[Duration];
		}
		if($flag8 == "on")
		{
			$tmpColor[]=array(0,0x66,0x66);	
			$tmpData[]=$hourData[AveDuration];
		}
		$lineStart=7;
	}
	else
	{
		if($flag8 == "on")
		{
			$tmpColor[]=array(0x66,0xff,0xff);
			$tmpData[]=$hourData[Duration];
		}
		if($flag9 == "on")
		{
			$tmpColor[]=array(0,0x66,0x66);	
			$tmpData[]=$hourData[AveDuration];
		}
		$lineStart=8;
	}
	
	echo "<br><br><b>Duration Statistic Chart</b><br>";
	$image=CreateImage(820,390);
	
	$boldline=$select_checkbox_id-$lineStart;
	for($i=$lineStart;$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);
}
?>
