<?php
/*********************************************************      
 程序说明：         
    功能说明：CNF参数配置

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

     返回值：无

 作者：   
      
修改注释：      
NO.1      
姓名：            
时间：      
修改说明：      
*********************************************************/  
require("../../inc/header.inc");
$helpId = "2.1.6"; 
$DEBUG = 0;
$sysInfoTable = 'OMC_PUB.sysInfo';
?>
<script LANGUAGE="JavaScript">           
function openSet(oType,tNum){
	var x,y;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;

	URL="./cnfEdit.php?oType="+oType+"&tNum="+tNum+"&scroll_x="+x+"&scroll_y="+y;
	var msgWindow = open(URL, 'newWin', 'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,status=yes,width=400,height=420');
	msgWindow.focus();
}


function delFirm(i)
{
	var delValue,x,y;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;

	if(confirm('Do you want to delete the selected?')!=true){
		window.location.href='<?php echo $PHP_SELF;?>'+"?scroll_x="+x+"&scroll_y="+y;
	}else{
		window.location.href='<?php echo $PHP_SELF;?>?delete=1&delValue='+i+"&scroll_x="+x+"&scroll_y="+y;
	}
}

</script>
<?php
$sysTypeNo_0 = "111";
$sysTypeNo_1 = "270";

$db_0 = "OBJ_".$sysTypeNo_0;
$db_1 = "OBJ_".$sysTypeNo_1;

$tb_0 = "param_0";
$tb_1 = "param_1";
$tb_2 = "param_99";

$maxNum = 20;

$nPubFlag = 99;
$nSubSysParam		= 0;
$nGlobalSubSysParam = 1;
$nGlobalSysParam	= 2;

$nGetComm = 0;
$nSetComm = 3;

$nInteger = 2;
$nOctStr = 4;

$nProcFail = 0;
$nWaitProc = 1;
$nInProc   = 2;	
$nOutProc  = 3;


//Location management
$oidTg = "2.2"; 
$pTg = explode(".",$oidTg);
$i = 0;
$wTg = '';
while(isset($pTg[$i]))
{
	$lNum = $i+1;
	if($DEBUG) echo "<br>pTg[$i]=$pTg[$i]";
	$wTg = $wTg." AND level_$lNum = $pTg[$i] ";
	$i++;
}


/************get default value**************/
$defSql = " SELECT initValue FROM paramConf WHERE keyId > 0 ".$wTg;
$defRes = @mysqli_query($pubConn,$defSql);
$defRow = @mysqli_fetch_array($defRes);
$defaultValue = $defRow[initValue];

$statusArr=array(
		array('num' => '0',  'remark' => 'Not executable', 'color' => '#FF0000'),
		array('num' => '1',  'remark' => 'Booking', 'color' => '#00FF00'),
		array('num' => '2',  'remark' => 'On going', 'color' => '#0000FF'),
		array('num' => '3',  'remark' => 'Finished', 'color' => '#000000'),
		array('num' => '3',  'remark' => 'Draft', 'color' => '#000000')
);

//****************************************************************
//函数
//	ChComm($sysId)
//函数说明:检查命令处理结果
//	
//变量说明: 
//	无
//	
//                                                             
//返回值：$paramType
//$nSubSysParam			= 0;
//$nGlobalSubSysParam	= 1;
//$nGlobalSysParam		= 2;	
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function ChComm($sysId,$chkType=1)
{     
	global $omc_db;
	global $remark,$nProcFail,$nWaitProc,$nInProc,$nOutProc;

	$commTable = "parameterComm";
	$DEBUG = 0;

	$sysIdArr = explode('_',$sysId);
	$sysTypeNo	= $sysIdArr[0];
	$sysNo		= $sysIdArr[1];
	$subSysNo	= $sysIdArr[2];
	
	if($chkType==0)
		$selComm = " SELECT commState,remark FROM $commTable WHERE sysTypeNo = '$sysTypeNo' ";
	else
		$selComm = " SELECT commState,remark FROM $commTable WHERE sysTypeNo = '$sysTypeNo' AND sysNo = '$sysNo' ";
	
	if($DEBUG)echo "<br>selComm=$selComm";
	$selRes  = @mysqli_query($pubConn,$selComm);
	$selRow  = @mysqli_fetch_array($selRes);
	echo mysqli_error($pubConn);
	do
	{
		$temCommState = $selRow[commState];
		if($selRow[remark]){
			$remark = $selRow[remark];
			//echo "<br>remark=$remark";
		}
		//echo "<br>temCommState=$temCommState";
		if($temCommState == $nWaitProc)
			return($nWaitProc);
		elseif($temCommState == $nInProc)
			return($nInProc);
		elseif($temCommState == $nOutProc)
			$commState = $nOutProc;
		else
			$commState = 0;
	}while($selRow  = @mysqli_fetch_array($selRes));

	return($commState);
}



//****************************************************************
//函数
//	从Buffer_2拷贝到$buffer_1
//函数说明:
//	
//变量说明: 
//	无
//	
//                                                             
//返回值：$paramType
//
//
//	
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function copyBuffer($buffer_1,$buffer_2,$wDef,$type='1')
{     
	global $sysTypeNo_0,$sysTypeNo_1,$db_0,$db_1,$tb_0,$tb_1,$tb_2,$wTg;
	$DEBUG = 0;
	$addWer = "	AND (LENGTH($buffer_2) > 0) AND $buffer_2 != 'Null' AND $buffer_2 NOT LIKE 'Error%' ";

	//Location management
	if($type){
		$uTgSql = " UPDATE $tb_0 set $buffer_1=$buffer_2 WHERE subSysNo = '0' ".$wTg.$addWer; 
	}else{
		$uTgSql = " UPDATE $tb_0 set $buffer_1=$buffer_2 WHERE subSysNo = '0' ".$wTg.$wDef.$addWer; 
	}
	if($DEBUG) echo " <br>uTgSql=$uTgSql ";
	$uTgRes = @mysqli_query($pubConn,$uTgSql);

	echo mysqli_error($pubConn);

	if($uTgRes)
		return 1;
	else
		return 0;
}

//****************************************************************
//函数
//	get 或 set
//函数说明:
//	
//变量说明: 
//	无
//	
//                                                             
//返回值：$commType
//
//
//	
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function eSetGet($commType)
{     
	global $sysTypeNo_0,$sysTypeNo_1,$db_0,$db_1,$tb_0,$tb_1,$tb_2,$wTg,$nProcFail,$nWaitProc,$nInProc,$nOutProc;
	global $maxNum,$remark,$nGetComm,$nSetComm;
	$DEBUG = 0;

	if($commType == $nGetComm)
		$showType = "Get ";
	else
		$showType = "Set ";


	//2.2.8
	$sysId = "270_0";
	$objIdStr = "2.2.*";
	$cic = sysCommSend($sysId,$commType,$objIdStr);

	$i = 0;
	while(1)
	{
		//echo "<br>i=$i";
		if($i == 0)
			echo "<br>$showType Conference parameter&nbsp;&nbsp;";
		if($i > $maxNum){
			echo "<br><font size=\"2\" color=\"#FF0000\">Timeout</font>";
			flush();
			break;
		}
		echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
		flush();	

		$sysId = "270";
		$chkType = "0";
		$chRes=ChComm($sysId,$chkType);
		//echo "<br>chRes=$chRes";
		if($chRes == $nOutProc || $chRes == $nProcFail){
			$pNext2 = 1;
			if($chRes == $nProcFail)
				echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
			elseif($remark == 'response timeout')
				echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
			else
				echo "<br><font size=\"2\" color=\"#0000FF\">$remark</font><br>";

			flush();
			break;
		}
	
		sleep(1);
		$i++;
	}
}//end of function



if($saveSys)
{

	/****************copy initValue to editValue******************/
	$buffer_1 = "editValue";
	$buffer_2 = "initValue";
	$edCin=copyBuffer($buffer_1,$buffer_2,$wDef);
	
	eSetGet($nSetComm);
	//sleep(1);

	//added by QShuang in 06-24
	eSetGet($nGetComm);//get value
	/****************copy readValue to initValue******************/
	$buffer_1 = "initValue";
	$buffer_2 = "readValue";
	$inCre=copyBuffer($buffer_1,$buffer_2,$wDef);
	if($DEBUG) echo "<br>inCre=$inCr";

	sleep(1);
	echo "<script language=\"javascript\">";
	//echo "window.location.href='$PHP_SELF';";
	echo "</script>";


}//end of set sys
elseif($readSys)
{	
	eSetGet($nGetComm);//get value

	/****************copy readValue to initValue******************/
	$buffer_1 = "initValue";
	$buffer_2 = "readValue";
	$inCre=copyBuffer($buffer_1,$buffer_2,$wDef);
	if($DEBUG) echo "<br>inCre=$inCr";

	sleep(1);
	echo "<script language=\"javascript\">";
	//echo "window.location.href='$PHP_SELF';";
	echo "</script>";
}//end of $readSys
elseif($saveDisk)
{
	/****************copy initValue to editValue******************/
	$buffer_1 = "editValue";
	$buffer_2 = "initValue";
	$inCed=copyBuffer($buffer_1,$buffer_2,$wDef);
	if($DEBUG) echo "<br>$inCed=$$inCed";

	if($inCed)
		echo "<br><font color=\"#0000FF\">Save success.</font>";
	else
		echo "<br><font color=\"#FF0000\">Save Failure.</font>";

	flush();
	sleep(1);
	echo "<script language=\"javascript\">";
	echo "window.location.href='$PHP_SELF';";
	echo "</script>";
}//end of $saveDisk
elseif($readDisk)
{
	/****************copy editValue to initValue******************/
	$buffer_1 = "initValue";
	$buffer_2 = "editValue";
	$loadType = 1;
	$edCin=copyBuffer($buffer_1,$buffer_2,$wDef,$loadType);
	if($DEBUG) echo "<br>edCin=$edCin";

	if($edCin)
		echo "<br><font color=\"#0000FF\">Read success.</font>";
	else
		echo "<br><font color=\"#FF0000\">Read failure.</font>";
	
	flush();
	sleep(1);
	echo "<script language=\"javascript\">";
	echo "window.location.href='$PHP_SELF';";
	echo "</script>";

}//end of $comSys
elseif($delete)
{
	$delSql = " UPDATE $tb_0 set initValue = '$defaultValue' WHERE subSysNo = '0' ".$wTg. " AND instanceNo = $delValue ";
	//echo "<br>delSql=$delSql";
	$delRes = @mysqli_query($pubConn,$delSql);
	echo mysqli_error($pubConn);
}
if($readSys || $saveSys || $saveComm){
	echo "<br>";
	echo "<p align=\"left\"><a href=\"$PHP_SELF\" target=\"contents\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">$strBack</a>";
	exit();
}


/************select value to see************/
//Location management
$tgSql = " SELECT instanceNo,initValue,editValue,readValue FROM $tb_0 WHERE subSysNo = '0' ".$wTg." ORDER BY instanceNo "; 
if($DEBUG) echo "<br>tgSql=$tgSql";
$tgRes = @mysqli_query($pubConn,$tgSql);
$tgRow = @mysqli_fetch_array($tgRes);
echo mysqli_error($pubConn);
$wDef = " AND (";
$defaultValue_1 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE000000EE";
do
{
	$instanceNo = $tgRow[instanceNo];
	$initValue = $tgRow[initValue];
	if($initValue == $defaultValue || $initValue == $defaultValue_1){
		$cnfShow[$instanceNo] = 0;
		continue;
	}else{
		$cnfShow[$instanceNo] = 1;
		
		$wDef = $wDef." instanceNo = $instanceNo OR ";
	}
	
	//echo "<br>tgShow[$instanceNo]=$cnfShow[$instanceNo]";

	$grpNum = $cnfArr[$instanceNo][0] = HexDec(substr($initValue,0,2));
	if($DEBUG)echo "<br>grpNum=$grpNum";

	$cphNum = substr($initValue,2,16);
	$p1 = explode("E",$cphNum);
	$cphNum = $cnfArr[$instanceNo][1] = $p1[0];
	if($DEBUG)echo "<br>cphNum=$cphNum";

	$pwd = $cnfArr[$instanceNo][2] = substr($initValue,18,4);

	$aphNum = substr($initValue,22,16);
	$p3 = explode("E",$aphNum);
	$aphNum = $cnfArr[$instanceNo][3] = $p3[0];
	if($DEBUG)echo "<br>aphNum=$aphNum";

	$month = omc_keep_length(HexDec(substr($initValue,38,2)),0,2);
	$day = omc_keep_length(HexDec(substr($initValue,40,2)),0,2);
	$hour = omc_keep_length(HexDec(substr($initValue,42,2)),0,2);
	$minute = omc_keep_length(HexDec(substr($initValue,44,2)),0,2);
	$bgnTime = $cnfArr[$instanceNo][4] = $month."-".$day." ".$hour.":".$minute;
	//echo "cnfArr[$instanceNo][4]={$cnfArr[$instanceNo][4]}<br>";

	$drt = $cnfArr[$instanceNo][5] = HexDec(substr($initValue,46,4))*15;

	$maxNum = $cnfArr[$instanceNo][6] = HexDec(substr($initValue,50,2));

	$sttNum = HexDec(substr($initValue,52,2));
	$status = $cnfArr[$instanceNo][7] = $statusArr[$sttNum][remark];
	$colorArr[$instanceNo][7] = $statusArr[$sttNum][color];
	//echo "<br>status=$status";

}while($tgRow = @mysqli_fetch_array($tgRes));

$wDef = substr($wDef,0,-3);
$wDef = $wDef.")";
if(strlen($wDef) < 10){
	$wDef = 'AND instanceNo < 0';
}
if($DEBUG)echo "<br>wDef=$wDef";
//$cnfShow[0] = "1";
$scroll_x = isset($scroll_x)?$scroll_x:'0';
$scroll_y = isset($scroll_y)?$scroll_y:'0';
echo "<body topmargin=\"5\" leftmargin=\"12\" onLoad='self.scrollBy($scroll_x,$scroll_y)'>";
echo "<form method=\"POST\" name=\"confForm\" action=\"$PHP_SELF\">";
?>
<?php
echo "<table border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\">";
echo "$strConfiguration > CNF";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";
?>
<!--
<table border="0" cellpadding="0" cellspacing="0" width="100%">           
    <tr>      
	<?php
		echo "<td width=200 valign=\"middle\"><nobr><b>System:</b>";
		echo "<input type=\"submit\" value=Get class=\"button\" name=\"readSys\" style=\"left: 1; top: 2; width: 70; height: 25; position: relative\">";
		echo "<input type=\"submit\" value=Set class=\"button\" name=\"saveSys\" style=\"left: 1; top: 2; width: 70; height: 25; position: relative\">";
	?>
	</nobr>
	</td>        
	<?php
		echo "<td width=200 valign=\"middle\"><nobr><b>Disk:</b>";
		echo "<input type=\"submit\" value=Load class=\"button\" name=\"readDisk\" style=\"left: 1; top: 2; width: 70; height: 25; position: relative\">";
		echo "<input type=\"submit\" value=Save class=\"button\" name=\"saveDisk\" style=\"left: 1; top: 2; width: 70; height: 25; position: relative\">";
	?>
	</nobr>
	</td>    
	<td width=200 valign="middle"><nobr><b>New:</b><input type="button" value="Add" onclick="javascript:openSet(1,0);" name="add" style="left: 3; top: 2; width: 70; height: 25; position: relative">
	</nobr>
	</td>    
  </tr>          
</table>         
-->
<table border="0" cellpadding="0" cellspacing="0" width="100%">           
    <tr>      
	<?php
		echo "<td width=200 valign=\"middle\"><nobr><b>System:</b>";
		echo "<input type=\"submit\" value=Get class=\"button\" name=\"readSys\" style=\"left: 1; top: 2; width: 70; height: 25; position: relative\">";
		echo "<input type=\"submit\" value=Set class=\"button\" name=\"saveSys\" style=\"left: 1; top: 2; width: 70; height: 25; position: relative\">";
	?>
	</nobr>
	</td>        
	<td width=200 valign="middle"><nobr><b>New:</b><input type="button" value="Add" onclick="javascript:openSet(1,0);" name="add" style="left: 3; top: 2; width: 70; height: 25; position: relative">
	</nobr>
	</td>    
  </tr>          
</table>         

<p></p>
<TABLE  border="1" width="100%" cellpadding="2" cellspacing="0" bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF">     
<tr bgcolor="#E6E6E6">
<td width="6%" >No.</td>     
<td width="6%" >Conf ID</td>     
<td width="8%" >Conf number</td>     
<td width="8%" >Password</td>     
<td width="8%" >Applicant</td> 
<td width="8%" >Start time</td>  
<td width="8%" >Duration(M)</td>  
<td width="8%" >Party</td>  
<td width="8%" >Status</td>  
<td width="6%" >Delete</td>
</tr>
<?php
for($i=0;$i<400;$i++)
{
	$pNum = floor($i%16);

	if($cnfShow[$i]){
		if($pNum == 0 && $i > 0)
			echo "<tr bgcolor=\"#E6E6E6\">";
		else
			echo "<tr>";
		
		if($cnfArr[$i][7] == "Unseted"){		
			echo "<td><a href=javascript:openSet(0,$i)>$i</a></td>";
		}else{
			echo "<td>$i</a></td>";
		}
		for($j=0;$j<8;$j++){
			echo "<td><nobr><font color=\"{$colorArr[$i][$j]}\">{$cnfArr[$i][$j]}&nbsp;</font></nobr></td>";
		}
		if($cnfArr[$i][7] == "Using"){
			echo "<td><input type=\"checkbox\" name=\"C$i\" value=\"ON\" DISABLED onClick=\"javascript:delFirm($i)\" ></td>";
			//echo "<td>&nbsp;</td>";
		}else{
			echo "<td><input type=\"checkbox\" name=\"C$i\" value=\"ON\" onClick=\"javascript:delFirm($i)\" ></td>";
		}
		echo "</tr>";

	}
}
echo "</table>";
echo "<input type=\"hidden\" name=\"wDef\" value=\"$wDef\">";
echo "</form>";
?>
</body>
</html>
