<?php
require("../../inc/header.inc");
require("../generalCsta/function.inc");
global $OMC_server;
$cstaDbConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], "CSTA_DB");

if($scroll_y > 0){
	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;\">";
}	
?>

<script language="JavaScript">
function select_page_up(sysTypeNo,sel_date_from,keyOne,keyTwo)
{
	var page_id=document.myform.page_select_up.value;
	var newurl="./detail_csta_down.php?sysTypeNo="+sysTypeNo+"&sel_date_from="+sel_date_from+"&keyOne="+keyOne+"&keyTwo="+keyTwo+"&page_id="+page_id;
	window.location.href=newurl;
}
function select_page_down(sysTypeNo,sel_date_from,keyOne,keyTwo)
{
	var page_id=document.myform.page_select_down.value;
	var newurl="./detail_csta_down.php?sysTypeNo="+sysTypeNo+"&sel_date_from="+sel_date_from+"&keyOne="+keyOne+"&keyTwo="+keyTwo+"&page_id="+page_id;
	window.location.href=newurl;
}
</script>
<form name="myform" action="detail_csta_down.php" method="POST">
<?php
adjust_head_frame("down");
adjust_title_head_frame("down");
adjust_title_tail_frame("down");
adjust_content_head_frame("down");	

$sel_date_from = $_REQUEST['sel_date_from'];
$sysTypeNo = $_REQUEST['sysTypeNo'];

$keyOne = $_REQUEST['keyOne'];
$keyTwo = $_REQUEST['keyTwo'];
$page_id = $_REQUEST['page_id'];

if(!isset($keyOne))
	$keyOne=-1;
if(!isset($keyTwo))
	$keyTwo=-1;		
if(!isset($page_id))
	$page_id=0;


if(0)
{
	echo "sysTypeNo=$sysTypeNo<br>";
	echo "sel_date_from=$sel_date_from<br>";
	echo "keyOne=$keyOne,keyTwo=$keyTwo<br>";
	echo "page_id=$page_id<br>";
}

$generalConf=parse_ini_file("general.conf",true);
$cstaConf=GetCstaConf($sysTypeNo);
if(0)
{
	echo "sysTypeNo=$sysTypeNo<br>";
	echo "<pre>";
	print_r($cstaConf);
	echo "</pre>";
}


$sql="show fields from {$generalConf[$sysTypeNo][DataTable]} where Field <> 'key_sys_id' and Field <> 'key_subsys_id' and Field <> 'key_type' and Field <> 'key_e1_tg' and Field <> 'csta_datetime' ";
$res=mysqli_query($cstaDbConn,$sql);
$num=mysqli_num_rows($res);
$fieldnum=$num;
if(0)
{
	echo "sql:$sql<br>";
	echo "num:$num<br>";
}
for($i=0;$i<$fieldnum;$i++)
{
	$row=@mysqli_fetch_array($res);
	$csta_fields[]=$row[Field];
}
$cstafieldstr=implode(",",$csta_fields);
$csvData=GetRowCsv("./fieldConf/{$sysTypeNo}_flag.csv");
$csvFieldName=GetRowCsv("./fieldConf/{$sysTypeNo}_name.csv");
if(0)
{
	echo "cstafieldstr=$cstafieldstr<br>";
	echo "<pre>";
	print_r($csta_fields);
	print_r($csvFieldName);
	echo "</pre>";
}



$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_from,date("Y")));
//echo "sel_date_from=$sel_date_from,startTime=$startTime,endTime=$endTime<br>";

//echo "keyOne=$keyOne,keyTwo=$keyTwo<br>";
$condition="";
if($keyOne != -1)
{
	$condition.=" and key_type=$keyOne ";
}
if($keyTwo != -1)
{
	$condition.=" and key_e1_tg=$keyTwo ";
}
// echo "<pre>";
// echo print_r($generalConf);
// echo "</pre>";

$start_record=$generalConf[MaxRow]*$page_id;
$end_record=$generalConf[MaxRow]*($page_id+1);

$sql="select * from {$generalConf[$sysTypeNo][DataTable]} where csta_datetime>='$startTime' and csta_datetime <= '$endTime' $condition order by csta_datetime desc ";
//echo "$sql<br>";
$res=mysqli_query($cstaDbConn,$sql);
$num=mysqli_num_rows($res);
echo mysqli_error($cstaDbConn);
$totalrecordnum=$num;
if(0)
{
	echo "sql2:$sql<br>";
	echo "num2:$num<br>";
}

$sql="select * from {$generalConf[$sysTypeNo][DataTable]} where csta_datetime>='$startTime' and csta_datetime <= '$endTime' $condition order by csta_datetime desc LIMIT $start_record, {$generalConf[MaxRow]} ";
//echo "$sql<br>";
$res=mysqli_query($cstaDbConn,$sql);
$num=mysqli_num_rows($res);
$recordnum=$num;
$pagenum=ceil($totalrecordnum/$generalConf[MaxRow]);
if(0)
{
	echo "recordnum=$recordnum,max={$generalConf[MaxRow]},pagenum=$pagenum<br>";
}

$start_recordstr=$start_record++;
if($start_recordstr >= $totalrecordnum)
	$start_recordstr=$totalrecordnum;
else
	$start_recordstr=$start_record;
	
if($end_record > $totalrecordnum)
	$end_recordstr=$totalrecordnum;
else
	$end_recordstr=$end_record;
	
$prev_page_id=$page_id-1;
$next_page_id=$page_id+1;

echo "<table border=0 width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\" align=\"left\">";
echo "Record: $start_recordstr - $end_recordstr, Total: $totalrecordnum";
echo "</td>";
echo "<td width=\"50%\" align=\"right\">";
if($page_id > 0)
{
	echo "<a href=\"$PHP_SELF?sysTypeNo=$sysTypeNo&sel_date_from=$sel_date_from&keyOne=$keyOne&keyTwo=$keyTwo&page_id=$prev_page_id\">";
	echo "Previous<img src=\"../../images/left.gif\" alt=$strNextPage align=absBottom border=0></img></a>";
}
else
{
	echo "<font color=\"#808080\">Previous</font><img src=\"../../images/left_1.gif\" alt=$strNextPage align=absBottom border=0></img>";
}
echo "<select name=\"page_select_up\" onchange=\"javascript:select_page_up('$sysTypeNo','$sel_date_from','$keyOne','$keyTwo');\">";
for($i=0;$i<$pagenum;$i++)
{
	$showpage=$i+1;
	if($page_id == $i)
		echo "<option value=\"$i\" selected>$showpage</option>";
	else
		echo "<option value=\"$i\">$showpage</option>";
}
echo "</select>";
if($page_id < ($pagenum-1))
{
	echo "<a href=\"$PHP_SELF?sysTypeNo=$sysTypeNo&sel_date_from=$sel_date_from&keyOne=$keyOne&keyTwo=$keyTwo&page_id=$next_page_id\">";
	echo "<img src=\"../../images/right.gif\" alt=$strNextPage align=absBottom border=0>Next</a>";
}
else
{
	echo "<img src=\"../../images/right_1.gif\" alt=$strNextPage align=absBottom border=0><font color=\"#808080\">Next</font>";
}
echo "</td>";
echo "</tr>";
echo "</table>";

echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
echo "<tr bgcolor=\"#E6E6E6\">";
echo "<td>System</td>";
echo "<td>Type</td>";
echo "<td>TG</td>";
for($i=0;$i<$fieldnum;$i++)
{
	//{$csta_fields[$i]} 
	if($csvData[$i] != 0)
		echo "<td>{$csvFieldName[$i]}</td>";
}
echo "<td>Time</td>";
echo "</tr>";

if($recordnum == 0)
{
	echo "<tr>";
	echo "<td>--</td>";
	echo "<td>--</td>";
	echo "<td>--</td>";
	for($j=0;$j<$fieldnum;$j++)
	{
		if($csvData[$j] != 0)
			echo "<td>--</td>";
	}
	echo "<td>--</td>";
	echo "</tr>";
}


for($i=0;$i<$cstaConf[DataObjectCount];$i++)
{
	$doConf=$cstaConf[DataObject][$i];
	for($keyIndex=$doConf[KeyOneStart];$keyIndex<=$doConf[KeyOneEnd];$keyIndex++)
	{
		$keyCountConf[$keyIndex]=$doConf[KeyFieldCount];
	}
}
if(0)
{
	echo "<pre>";
	print_r($keyCountConf);
	echo "</pre>";
}

for($i=0;$i<$recordnum;$i++)
{
	$row=@mysqli_fetch_array($res);
	echo "<tr>";
	$system_id="{$generalConf[$sysTypeNo][Module]}_".($row[key_sys_id]+0);
	echo "<td>$system_id</td>";
	
	$typestr=$generalConf[KeyOneName][$sysTypeNo][$row[key_type]];
	if($keyCountConf[$row[key_type]] > 0)
		echo "<td>$typestr</td>";
	else
		echo "<td>--</td>";
	
	if($keyCountConf[$row[key_type]] == 2)
		echo "<td>{$row[key_e1_tg]}</td>";
	else
		echo "<td>--</td>";
		
	for($j=0;$j<$fieldnum;$j++)
	{
		if($csvData[$j] != 0)
			echo "<td>{$row[$csta_fields[$j]]}</td>";
	}
	
	echo "<td>{$row[csta_datetime]}</td>";
	echo "</tr>";
}

echo "</table>";
if($recordnum >= 25)
{
	echo "<table border=0 width=\"100%\">";
	echo "<tr>";
	echo "<td width=\"50%\" align=\"left\">";
	echo "Record: $start_recordstr - $end_recordstr, Total: $totalrecordnum";
	echo "</td>";
	echo "<td width=\"50%\" align=\"right\">";
	if($page_id > 0)
	{
		echo "<a href=\"$PHP_SELF?sysTypeNo=$sysTypeNo&sel_date_from=$sel_date_from&keyOne=$keyOne&keyTwo=$keyTwo&page_id=$prev_page_id\">";
		echo "Previous<img src=\"../../images/left.gif\" alt=$strNextPage align=absBottom border=0></img></a>";
	}
	else
	{
		echo "<font color=\"#808080\">Previous</font><img src=\"../../images/left_1.gif\" alt=$strNextPage align=absBottom border=0></img>";
	}
	echo "<select name=\"page_select_down\" onchange=\"javascript:select_page_down('$sysTypeNo','$sel_date_from','$keyOne','$keyTwo');\">";
	for($i=0;$i<$pagenum;$i++)
	{
		$showpage=$i+1;
		if($page_id == $i)
			echo "<option value=\"$i\" selected>$showpage</option>";
		else
			echo "<option value=\"$i\">$showpage</option>";
	}
	echo "</select>";
	if($page_id < ($pagenum-1))
	{
		echo "<a href=\"$PHP_SELF?sysTypeNo=$sysTypeNo&sel_date_from=$sel_date_from&keyOne=$keyOne&keyTwo=$keyTwo&page_id=$next_page_id\">";
		echo "<img src=\"../../images/right.gif\" alt=$strNextPage align=absBottom border=0>Next</a>";
	}
	else
	{
		echo "<img src=\"../../images/right_1.gif\" alt=$strNextPage align=absBottom border=0><font color=\"#808080\">Next</font>";
	}
	echo "</td>";
	echo "</tr>";
	echo "</table>";
}
adjust_content_tail("down");
echo "</form>";
?>
