<?php
/*********************************************************      
 程序说明：         
    功能说明：本程序定义系统配置信息

    调用关系：调用:header.inc     
              被调用:      
 
    变量说明：

     返回值：无

 作者：  
      
修改注释：      
NO.1      
姓名        
时间
修改说明
*********************************************************/    
require("../../inc/header.inc");

$max_menu = 4;
$db=$omc_db;
$sysTypeNo=isset($sysTypeNo)?$sysTypeNo:'0';
$selCompCode=isset($selCompCode)?$selCompCode:'new';
$selAlarmCode=isset($selAlarmCode)?$selAlarmCode:'new';
if($language=='eng'){
	$alarmDesc = 'alarmDesc_1';
}else{
	$alarmDesc = 'alarmDesc_2';
}


/**************************************删除一个系统类型***********************************************/
if($delete){
	$del_sql = "DELETE FROM $alarmconf_table WHERE sysTypeNo='$sysTypeNo' AND compCode = '$selCompCode' AND alarmCode='$selAlarmCode' ";
	$del_result = mysqli_query($pubConn,$del_sql);
	//echo "<br>del_sql=$del_sql";
	//----------------
	//插入日志
	if ($del_result)
		$log_name = $user_info["account"]." delete".$selAlarmCode." successful";
	else
		$log_name = $user_info["account"]." delete".$selAlarmCode." fail";
	insertLog("$log_name");
	//------------
	if($del_result){
		echo "<font color=\"#0000ff\">$strDeleteSuccess</font>";
       	 }else{
		echo "<font color=\"#ff0000\">$strDeleteFailure</font>";
         }
} //end if($delete) 


/*****************************添加,保存一个系统*****************************/
if($save){
	if($selCompCode == 'new')//插入一个新组件
	{
		//判断新添加的组件是否存在
		$judCompSql = " SELECT COUNT(*) FROM $alarmconf_table WHERE sysTypeNo='$sysTypeNo' AND compCode ='$compCode' AND alarmCode = '0' ";
		$judCompRes = @mysqli_query($pubConn,$judCompSql);
		$judCompRow = @mysqli_fetch_array($judCompRes);
		echo mysqli_error($pubConn);
		if(!$judCompRow[0])
		{
		  $saveSql = "REPLACE INTO $alarmconf_table 
			(sysTypeNo, compCode, alarmCode, alarmDesc_1, alarmDesc_2, alarmType, alarmLevel, alarmNote_1,   alarmNote_2) 
		    VALUES 
			('$sysTypeNo', '$compCode', '0', '$alarmDesc_1', '$alarmDesc_2', '$alarmType', '$alarmLevel', '$alarmNote_1', '$alarmNote_2') 
		    ";
			$selCompCode=$compCode;
		}
		else
		{
			echo "<br><font color=\"#FF0000\">Component always is existed!</font>";
		}
	}
	elseif($selAlarmCode == 'new')//插入一个新告警类型
	{  
		$judCodeSql = " SELECT COUNT(*) FROM $alarmconf_table WHERE sysTypeNo='$sysTypeNo' AND compCode ='$selCompCode' AND alarmCode='$alarmCode' ";
		$judCodeRes = @mysqli_query($pubConn,$judCodeSql);
		$judCodeRow = @mysqli_fetch_array($judCodeRes);
		echo mysqli_error($pubConn);
		if(!$judCodeRow[0])
		{
		  $saveSql = "REPLACE INTO $alarmconf_table 
			(sysTypeNo, compCode, alarmCode, alarmDesc_1, alarmDesc_2, alarmType, alarmLevel, alarmNote_1,   alarmNote_2) 
		    VALUES 
			('$sysTypeNo', '$selCompCode', '$alarmCode', '$alarmDesc_1', '$alarmDesc_2', '$alarmType', '$alarmLevel', '$alarmNote_1', '$alarmNote_2') 
		    ";
			$selAlarmCode=$alarmCode;
		}
		else
		{
			echo "<br><font color=\"#FF0000\">Alarm code always is used!</font>";
		}
	}
	else//修改已有组件或告警类型
	{  
		if($selAlarmCode==0){
			$chgCompCode=$compCode;
		}else{
			$chgCompCode=$selCompCode;
		}
		$saveSql = "
			UPDATE $alarmconf_table 
			SET 
			sysTypeNo='$sysTypeNo',
			compCode='$chgCompCode',	
			alarmCode='$alarmCode',	
			alarmDesc_1='$alarmDesc_1',
			alarmDesc_2='$alarmDesc_2', 
			alarmType='$alarmType',
			alarmLevel='$alarmLevel',
			alarmNote_1='$alarmNote_1',
			alarmNote_2='$alarmNote_2'
			WHERE  sysTypeNo='$sysTypeNo' AND compCode='$selCompCode' AND alarmCode='$selAlarmCode'
			";
		if($selAlarmCode==0){
			$selCompCode=$compCode;
		}
		$selAlarmCode=$alarmCode;
	}
	//echo "<br>$save_sql";
	$saveRes = @mysqli_query($pubConn,$saveSql);
	echo mysqli_error($pubConn);
	//------------插入日志------------
	//------------
	if($saveRes > 0){
		$log_name = $user_info["account"]." set".$selAlarmCode." successful.";
		echo "<br><font color=\"#0000ff\" >$strSaveSuccess</font>";
    }else{
		$log_name = $user_info["account"]." set".$selAlarmCode." fail.";
		echo "<br><font color=\"#ff0000\">$strSaveFailure</font>";
    }
	insertLog("$log_name");
} //end if($save) 
?>
<body>

<script LANGUAGE="JavaScript">  
function selType()      
 {      
    var base_url,sysTypeNo,url;      
    base_url='<?php echo"$PHP_SELF";?>?sysTypeNo=';
    sysTypeNo=document.selectform.sysTypeNo.value;   
    url=base_url+sysTypeNo;
    window.location=url;      
 }      

function selComp()      
 {      
    var base_url,sysTypeNo,selCompCode,url;      
    base_url='<?php echo"$PHP_SELF";?>?sysTypeNo=';
    sysTypeNo=document.selectform.sysTypeNo.value;   
	selCompCode=document.selectform.selCompCode.value;   
    url=base_url+sysTypeNo+"&selCompCode="+selCompCode;
    window.location=url;      
 }      

function selCode()      
 {      
    var base_url,sysTypeNo,selCompCode,selAlarmCode,url;  
    base_url='<?php echo"$PHP_SELF";?>?sysTypeNo=';
    sysTypeNo=document.selectform.sysTypeNo.value; 
	selCompCode=document.selectform.selCompCode.value;   
	selAlarmCode=document.selectform.selAlarmCode.value;   
    url=base_url+sysTypeNo+"&selCompCode="+selCompCode+"&selAlarmCode="+selAlarmCode;
    window.location=url;      
 }
 
 function alarmInit(initAll) //add by hewd 2002-06-18     
 {      
    var base_url,sysTypeNo,selCompCode,selAlarmCode,url;  
    if(initAll > 0){
		base_url='alarmInit.php?initAll=1';
		url=base_url;
	}else{
		base_url='alarmInit.php?initAll=0&sysTypeNo=';
		sysTypeNo=document.selectform.sysTypeNo.value; 
		url=base_url+sysTypeNo;
	}
    window.location=url;      
 }  
</script>
<form method="POST" name=selectform action="<?php echo"$PHP_SELF";?>" >
  <table  border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF" > 
<?php  
echo "<tr bgcolor=\"#E6E6E6\">";    
echo "<td width=\"100%\" colspan=\"2\">";
echo "<p align=\"center\"><b>$strAlarmConfiguration</b></p></td></tr>";
echo "<tr>";    
echo "<td width=\"40%\">$strSystemType</td>";
echo "<td width=\"60%\">";


$typeSql		= " SELECT sysTypeNo,sysTypeName FROM $sysconf_table WHERE needHeartBeat = '1' 
					ORDER BY sysTypeNo ";
$typeRes		= @mysqli_query($pubConn,$typeSql);
$typeRow		= @mysqli_fetch_array($typeRes);
echo mysqli_error($pubConn);

$compSql		= " SELECT * FROM $alarmconf_table WHERE sysTypeNo = '$sysTypeNo' AND alarmCode = '0'
					ORDER BY compCode ";
$compRes		= @mysqli_query($pubConn,$compSql);
$compRow		= @mysqli_fetch_array($compRes);
echo mysqli_error($pubConn);

$codeSql		= " SELECT * FROM $alarmconf_table WHERE sysTypeNo = '$sysTypeNo' AND compCode = 							'$selCompCode' ORDER BY alarmCode ";
$codeRes		= @mysqli_query($pubConn,$codeSql);
$codeRow		= @mysqli_fetch_array($codeRes);
echo mysqli_error($pubConn);

if($selCompCode != 'new' && $selAlarmCode != 'new')
{   
	$confSql		= " SELECT * FROM $alarmconf_table WHERE sysTypeNo = '$sysTypeNo'  AND compCode = 						'$selCompCode' AND alarmCode = '$selAlarmCode' ";
	$confRes		= @mysqli_query($pubConn,$confSql);
	$confRow		= @mysqli_fetch_array($confRes);
	echo mysqli_error($pubConn);
	$sysTypeNo		= $confRow[sysTypeNo];
	$compCode 		= $confRow[compCode];
	$alarmCode		= $confRow[alarmCode];
	$alarmDesc_1 	= $confRow[alarmDesc_1];
	$alarmDesc_2 	= $confRow[alarmDesc_2];
	$alarmType 		= $confRow[alarmType];
	$alarmLevel 	= $confRow[alarmLevel];
	$alarmNote_1 	= $confRow[alarmNote_1];
	$alarmNote_2 	= $confRow[alarmNote_2];
}	


echo "<select size=\"1\" name=\"sysTypeNo\" onChange=\"javascript:selType()\">";
do
{
	if($sysTypeNo == $typeRow[sysTypeNo]){
		echo "<option value=\"$typeRow[sysTypeNo]\" selected>$typeRow[sysTypeName]</option>";
		$sysTypeName = $typeRow[sysTypeName];
	}else{
		echo "<option value=\"$typeRow[sysTypeNo]\">$typeRow[sysTypeName]</option>";
	}
}while($typeRow = @mysqli_fetch_array($typeRes));
echo "</select></td></tr>";


//显示选择组件下拉菜单
echo "<tr bgcolor=\"#E6E6E6\">";    
echo "<td width=\"40%\">$strExistedComponent</td>";
echo "<td width=\"60%\">";
echo " <select size=\"1\" name=\"selCompCode\" onChange=\"javascript:selComp()\">";
echo "<option value=\"new\">----------NEW----------</option>";
do{
	if($selCompCode == $compRow[compCode] && isset($compRow[compCode]))
		echo "<option value=\"$compRow[compCode]\" selected>$compRow[$alarmDesc]($compRow[compCode])</option>";
	elseif(isset($compRow[compCode]))
		echo "<option value=\"$compRow[compCode]\">$compRow[$alarmDesc]($compRow[compCode])</option>";
}while($compRow = @mysqli_fetch_array($compRes));


//显示选择告警类型下拉菜单
if($selCompCode!='new')
{
	echo "<tr bgcolor=\"#E6E6E6\">";    
	echo "<td width=\"40%\">$strExistedAlarm</td>";
	echo "<td width=\"60%\">";
	echo " <select size=\"1\" name=\"selAlarmCode\" onChange=\"javascript:selCode()\">";
	echo "<option value=\"new\">----------NEW----------</option>";
	do{
		if($selAlarmCode == $codeRow[alarmCode] && isset($codeRow[alarmCode]))
			echo "<option value=\"$codeRow[alarmCode]\" selected>$codeRow[$alarmDesc]($codeRow[alarmCode])</option>";
		elseif(isset($codeRow[alarmCode]))
			echo "<option value=\"$codeRow[alarmCode]\">$codeRow[$alarmDesc]($codeRow[alarmCode])</option>";
	}while($codeRow = @mysqli_fetch_array($codeRes));
}

if(($selAlarmCode!='new' && $selAlarmCode==0) || $selCompCode=='new')
{
	echo "<tr>";
	echo "<td width=\"40%\">$strComponentCode (0-255)</td>";    
	echo "<td width=\"60%\"><input name=\"compCode\" maxlength=\"3\" size=\"3\" value=\"$compCode\">";  
	echo "</td>";    
	echo "</tr>"; 
	echo "<input type=\"hidden\" name=\"alarmCode\" maxlength=\"3\" size=\"3\" value=\"$alarmCode\">";
}
elseif($selCompCode!='new')
{
	echo "<tr>";    
	echo "<td width=\"40%\">$strAlarmCode (0-255)</td>";
	echo "<td width=\"60%\"><input name=\"alarmCode\" maxlength=\"3\" size=\"3\" value=\"$alarmCode\">";
	echo "</td>";    
	echo "</tr>"; 
}
?>
    <tr bgcolor="#E6E6E6">
    <td width="40%"><?php echo "$strDescription ($strChinese)";?></td>
    <td width="60%"><input name="alarmDesc_1" size="40" value="<?php echo $alarmDesc_1; ?>">
    </td>
    </tr>
    <tr bgcolor="#E6E6E6">
    <td width="40%"><?php echo "$strDescription ($strEnglish)"; ?></td>
    <td width="60%"><input name="alarmDesc_2" size="40" value="<?php echo $alarmDesc_2; ?>">
	</textarea>
    </td>
    </tr>
    <tr>    
    <td width="40%"><?php echo "$strAlarmType"; ?></td>
	<td>
	<select name="alarmType">
	<?php  
	for($j=0; $j<count($arrayAlarmType); $j++)
	{
		if($j == $alarmType)
		{
			echo "<option value=\"$j\" selected>{$arrayAlarmType[$j][remark]}</option>";
		}
		else
		{
			echo "<option value=\"$j\">{$arrayAlarmType[$j][remark]}</option>";
		}
	}
	?>
	</select>
	</td>
	</tr>
    <tr>    
    <td width="40%"><?php echo "$strAlarmLevel"; ?></td>
	<td>
	<select name="alarmLevel">
	<?php
	echo count($arrayAlarmLevel);
	for($j=0; $j<count($arrayAlarmLevel); $j++)
	{
		if($j == $alarmLevel)
		{
			echo "<option value=\"$j\" selected><font color=\"{$arrayAlarmLevel[$j][color]}\">{$arrayAlarmLevel[$j][remark]}</font></option>";
		}
		else
		{
			echo "<option value=\"$j\"><font color=\"{$arrayAlarmLevel[$j][color]}\">{$arrayAlarmLevel[$j][remark]}</font></option>";
		}
	}
	?>
	</select>
	</td>
	</tr>
    <tr bgcolor="#E6E6E6">
    <td width="40%"><?php echo "$strRemark ($strChinese)";?></td>
    <td width="60%"><textarea rows="2" name="alarmNote_1" cols="40"><?php echo $alarmNote_1; ?></textarea>
    </td>
    </tr>
    <tr bgcolor="#E6E6E6">
    <td width="40%"><?php echo "$strRemark ($strEnglish)"; ?></td>
    <td width="60%"><textarea rows="2" name="alarmNote_2" cols="40"><?php echo $alarmNote_1; ?></textarea>
    </td>
    </tr>
    </table>
  <p><input type="submit" value="<?php echo "$strSave"; ?>" name="save"> 
     <input type="reset"  value="<?php  echo "$strCancel"; ?>" name="reset"> 
     <input type="submit" value="<?php echo "$strDeleteMode"; ?>" name="delete">
	 <input type="button" value="<?php echo "Init $sysTypeName 's alarm"?>" name="Init" onclick=alarmInit(0)>
	 <input type="button" value="Init ALL alarm" name="Init" onclick=alarmInit(1)>
  </p>                
</form>                 
</body>                
</html>

