<?php
require("../../inc/header.inc");
?>
<script LANGUAGE="JavaScript">  
function goPage(numberOfRec){
	
	
	
		var pageUrl,bgnNo,numberPerPage;
		pageNo=document.selectform.pageNo.value;

		numberPerPage='<?php echo "$numberPerPage";?>';
		numberPerPage=100;

		bgnNo=numberPerPage*(pageNo-1);
		var selSysTypeNo=document.selectform.selSysTypeNo.value;

		baseUrl='<?php echo "$PHP_SELF?$basePageLink";?>';
		
		pageUrl="&bgnNo="+bgnNo+"&numberOfRecords="+numberOfRec+"&selSysTypeNo="+selSysTypeNo;
		window.location.href=baseUrl+pageUrl;     
		
		
}      
</script>
<body leftmargin="15" rightmargin="10" onload="javascript:adjust_frame('down');" onresize="javascript:adjust_frame('down');" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<?php
//&bgnNo=$bgnNo&numberOfRecords=numberOfRecords$basePageLink=$_REQUEST['basePageLink'];
$bgnNo=$_REQUEST['bgnNo'];
$numberOfRecords=$_REQUEST['numberOfRecords'];
$selSysTypeNo=$_REQUEST['selSysTypeNo'];
$nextPageLink=$_REQUEST['nextPageLink'];
$prePageLink=$_REQUEST['prePageLink'];
$actMode=$_REQUEST['actMode'];
echo "<meta http-equiv=\"refresh\" content=\"10; url=$PHP_SELF?selSysTypeNo=$selSysTypeNo&noop=$noop&nolog=1\">";
adjust_head_frame("down");
adjust_title_head_frame("down");
adjust_title_tail_frame("down");

adjust_content_head_frame("down");	
echo "<br>";



//*****
//$url_temp=$_SERVER["REQUEST_URI"];
//echo "This page URL:$url_temp<br>";
/*********************************************************      
 程序说明：         
    功能说明：告警查询的下部页面，查询显示
    调用关系：
    变量说明：

    作者：   
      
修改注释：      
NO.1      
姓名：            
时间：      
修改说明：      
*********************************************************/

$DEBUG = 1;

//if(!isset($selSysTypeNo)) $selSysTypeNo='all';
//==========================================================
//变量定义
//==========================================================
$dbName		= 'OMC_PUB';
$logTable	= 'sysAlarmEvent';
$numberPerPage	= 100;		//每页纪录数
$orderByStr = "alarmTime DESC,sysTypeNo,sysNo,subSysNo";//排序顺序

//确定告警内容显示语言种类
if($language=='chn'){
	$alarmDesc	= alarmDesc_1; //chinese
}else{
	$alarmDesc	= alarmDesc_2;
}
 
//初始化设备名和设备编号数组，
//由于三张表一起查时，速度太慢，占用系统资源太大。
//所以系统类型编号对应的系统类型名称不从数据库中直接取。
//将数据到如数组，显示时，从数组中来取。
$device_sql ="SELECT sysTypeNo,sysTypeName 
			  FROM  $sysconf_table 
			  WHERE needHeartBeat >'0' 
			  ORDER BY sysTypeNo  
			  ";
$device_result = @mysqli_query($pubConn,$device_sql);
$device_rows = @mysqli_fetch_array($device_result);
echo mysqli_error($pubConn);
if($DEBUG)echo "<br>device_sql:$device_sql";
$i=0;
do{
	$sysTypeDef[$i][sysTypeNo]	= $device_rows[sysTypeNo];
	$sysTypeDef[$i][sysTypeName]= $device_rows[sysTypeName];
	$i++;
}while($device_rows = mysqli_fetch_array($device_result));

//需要被查询的字段名
$selectFieldList="$logTable.sysTypeNo,sysNo,subSysNo,$logTable.compCode,$logTable.alarmCode,$logTable.alarmInfo,$logTable.alarmId,alarmLevel,$alarmDesc ,alarmTime,clearTime,clearMode,clearInfo";

//显示列表时的表头标题
$noop=0;
if($noop!=1)
{
	if ($selectedLanguage == 'eng')
		$showFieldArr=array('System','Alarm Info','Alarm time');
	elseif ($selectedLanguage == 'chn')
		$showFieldArr=array('系统号','详细内容','告警时间');
}
else
{
	if ($selectedLanguage == 'eng')
		$showFieldArr=array('System','Alarm Info','Alarm time');
	elseif ($selectedLanguage == 'chn')
		$showFieldArr=array('系统号','详细内容','告警时间');
}
 
//==========================================================
//输入变量
//==========================================================
//==========================================================
//数据处理
//==========================================================
//告警手动清除



	//根据上部页面传来的变量，生成查询条件
	if( strlen($selSysTypeNo) > 0){
		if( $selSysTypeNo != 'all'){
			$whereStr .=" $logTable.sysTypeNo = '$selSysTypeNo'";
		}
			if(strlen($whereStr) > 0){
				$whereStr .=" AND  clearMode= '2'";
			}else{
				$whereStr .=" clearMode= '2'";
			}	
		
	}else{
		if(strlen($whereStr) < 1){	
			$whereStr =" clearMode= '2' ";
		}
	}
	
	if($DEBUG) echo "<br>whereStr=$whereStr";
	
	//查询基本条件 test
	// if(strlen($whereStr) > 10){
	// 	$bascWhereStr ="
	// 				AND $logTable.sysTypeNo = $defTable.sysTypeNo 
	// 				AND $logTable.compCode  = $defTable.compCode 
	// 				AND $logTable.alarmCode = $defTable.alarmCode
	// 				";
	// }else{
	// 	$bascWhereStr ="
	// 				$logTable.sysTypeNo = $defTable.sysTypeNo 
	// 				AND $logTable.compCode  = $defTable.compCode 
	// 				AND $logTable.alarmCode = $defTable.alarmCode
	// 				";
	// }
	


	if(!$numberOfRecords){
		if(!isset($bgnNo))
			$bgnNo=0;
		$countSql="SELECT  count(*) as totalNum
					FROM $logTable
					WHERE $whereStr
					";
		if($DEBUG) echo "<br>countSql=$countSql";
		$result=mysqli_query($pubConn,$countSql);
		echo mysqli_error($pubConn);
		$record = @mysqli_fetch_array($result);
		$numberOfRecords = $record[0];
		
		if($DEBUG) 
		echo "<br>numberOfRecords=$numberOfRecords";

				
		//插入日志
		if($nolog != 1)
			insertLog("View Alarm Log");
	}

	//找到需要显示的纪录
	if($numberOfRecords > 0){ 
		$showSql=" SELECT  sysTypeNo,sysNo,subSysNo,alarmInfo,alarmTime
					FROM $logTable
					WHERE $whereStr
					ORDER BY $orderByStr
					LIMIT $bgnNo, $numberPerPage
				";
		if($DEBUG) echo "<br>showSql=$showSql";
		$result=@mysqli_query($pubConn,$showSql);
		echo mysqli_error($pubConn);
		$thisPageOtherNum = @mysqli_num_rows($result);
		$rows=@mysqli_fetch_array($result);
	
		if($DEBUG)echo "<br>thisPageOtherNum:$thisPageOtherNum";

		if($selSysTypeNo=='all' || !isset($selSysTypeNo)|| strlen($selSysTypeNo)==0)
		if($thisPageOtherNum==0 || $thisPageOtherNum< $numberPerPage)
		{
			//echo "$thisPageOtherNum< $numberPerPage";
			$countSql="SELECT  count(*) as totalNum
					FROM $logTable
					WHERE $whereStr
						";
			if($DEBUG) echo "<br>countSql=$countSql";
			$cntResult=mysqli_query($pubConn,$countSql);
			echo mysqli_error($pubConn);
			$record = @mysqli_fetch_array($cntResult);
			$otherTotalNum = $record[0];
			
		}
		
		//下面是控制分页显示需要传递的变量传递变量  
		if ($numberOfRecords >= 1) {
			
			$nextBgnNo = $bgnNo + $numberPerPage;  //本页的结束数目  
			
			if ($nextBgnNo > $numberOfRecords){  
				$nextBgnNo = $numberOfRecords;  
			}  
			$t1 = $bgnNo + 1;  
			$prevBgnNo = $bgnNo - $numberPerPage;  
			if ($prevBgnNo < 0) {  
				$prevBgnNo = 0;  
			}
			$basePageLink	="selSysTypeNo=$selSysTypeNo&numberOfRecords=$numberOfRecords";
			$prePageLink	="bgnNo=$prevBgnNo";
			$nextPageLink	="bgnNo=$nextBgnNo";
			
		}  
		$showBgnNo=$bgnNo+1;
	}

//==========================================================
//输出并显示数据
//==========================================================

?>



<form name="selectform" action="./alarmEvent_down.php" method="post">
<?php  
echo "<input type=\"hidden\" value=\"$selSysTypeNo\" name=\"selSysTypeNo\">";
echo "<input type=\"hidden\" value=\"$noop\" name=\"noop\">";
if($numberOfRecords == 0){
	echo "<br><font size =\"3\" color=\"#000000\">Currently there is no alarm!</font>";  
	exit();
}

?>

<TABLE  border="0" width="100%" cellpadding="2" cellspacing="0" bordercolor="#666666" bordercolordark="#FFFFFF">      
  <TR>
	<TD >
		<?php 
			echo "$strDisplayRecords<FONT color=#0000ff>&nbsp;$showBgnNo - $nextBgnNo</FONT>, ";
			echo "($strTotal:$numberOfRecords)";   
		?>
	</TD>
	<TD align="right">   
		<?php 
			// NEW: Display a link for the previous set of records.  
			if ($bgnNo != 0) { 
				echo "<a href=\"$PHP_SELF?$basePageLink&$prePageLink\">"; 
				echo "Previous&nbsp;<IMG alt=$strPreviousPage align=absBottom border=0  src=../../images/left.gif ></a>&nbsp;";  
			}else{
				echo "<font color=\"#808080\">Previous&nbsp;</font><IMG alt=$strPreviousPage align=absBottom border=0  src=../../images/left_1.gif ></a>&nbsp;"; 
			}  
		  
			//根据纪录条数，算出页码数，本页页码
			if($numberOfRecords > 0){
				$totalPageNum = ceil($numberOfRecords / $numberPerPage );
				$pageNo = $bgnNo / $numberPerPage + 1;
			}

			echo "<SELECT size=\"1\" name=\"pageNo\" onChange=\"javasript:goPage($numberOfRecords)\">";
			for($i=1;$i<=$totalPageNum;$i++){
				if($i== $pageNo){
					echo "<option  selected  value=\"$i\" >$i</option>";
				}else{
					echo "<option value=\"$i\">$i</option>";
				}
			}
			echo "</SELECT>";

			// NEW: Display a link for the next set of records.  
			if ($nextBgnNo != $numberOfRecords && $numberOfRecords >0) {  
				echo "<a href=\"$PHP_SELF?$basePageLink&$nextPageLink\">";
				echo "<IMG alt=$strNextPage align=absBottom border=0  src=../../images/right.gif>&nbsp;Next</a>"; 
			}else{
				echo "<IMG alt=$strNextPage align=absBottom border=0  src=../../images/right_1.gif ><font color=\"#808080\">&nbsp;Next</font>";
			} 
		?>
	</TD>
  </TR>  
</TABLE> 
<table border="1" width="100%"  cellpadding="1" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF">
  <tr bgcolor="#E6E6E6">
    <?php
	$i=0;
	for($i=0;$i<sizeof($showFieldArr);$i++){
		echo "<td align=\"center\">";
		echo "$showFieldArr[$i]";
		echo "</td>";
	}
	?>
  </tr>
 
  <?php  
if($thisPageOtherNum>0)
do
{
		for($i=0; $i < sizeof($sysTypeDef); $i++){
			if($rows[sysTypeNo] == $sysTypeDef[$i][sysTypeNo]){
				$sysName=$sysTypeDef[$i][sysTypeName].'_'.$rows[sysNo].'_'.$rows[subSysNo];
				$sysId=$rows[sysTypeName].'_'.$rows[sysNo].'_'.$rows[subSysNo];
				
				break;
			}
		}
		if(strlen($sysName) < 3){
			$sysName='unknown';
			//continue;
		}
	
	$sysTypeNo	=$rows[sysTypeNo];
	$sysNo		=$rows[sysNo];
	$subSysNo	=$rows[subSysNo];
	$compCode	=$rows[compCode];
	$alarmCode	=$rows[alarmCode];
	$descript	=$rows[$alarmDesc];
	$alarmInfo	=$rows[alarmInfo];
	$alarmId	=$rows[alarmId];
	$alarmTime	=$rows[alarmTime];
	$clearTime	=$rows[clearTime];
	$clearMode	=$rows[clearMode];
	$sysName=getSystemNameBySysTypeNo($sysTypeNo).'_'.$rows[sysNo].'_'.$rows[subSysNo];;
	echo "<tr>";
	echo "<td align=\"center\">";
	echo "&nbsp; $sysName";
	echo "</td>";


	// echo "<td align=\"left\">";
	// echo "&nbsp; $descript";
	// echo "</td>";
	
	echo "<td align=\"center\">";
	echo "&nbsp; $alarmInfo";
	echo "</td>";

	echo "<td align=\"center\">";
	echo "&nbsp; $alarmTime";
	echo "</td>";


	echo "</tr>";
}while($rows=mysqli_fetch_array($result));


?>
</table>
</form> 
<?php 
adjust_content_tail("down");
?>
</body>
</html>