<?php
/*********************************************************      
 程序说明:         
    功能说明:
    调用关系:调用:     
              被调用:      
    变量说明:
    返回值:无
 作者:   
修改注释:      
NO.1      
姓名:            
时间:      
修改说明:      
*********************************************************/    
require("../../inc/header.inc");
?>
<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
adjust_head_frame("down");
adjust_title_head_frame("down");
adjust_title_tail_frame("down");
adjust_content_head_frame("down");	
echo "<br>";

$DEBUG = 0;

if($DEBUG) echo "<br>mode=$mode";
$sysConfTable = 'OMC_PUB.sysConf';
$sysInfoTable = 'OMC_PUB.sysInfo';
$pubDb			='OMC_PUB';
$netIdDefArr=array(
									array('0', "International"),	
				   				array('1', "international Spare"),
									array('2', "National"),
									array('3', "National Spare")
							);

function alertMsg($msg)
{
	echo "<script LANGUAGE=\"JavaScript\">";
	echo "\n alert('$msg');";
	echo "\n window.history.go(-1);";
	echo "\n </script>";
	exit();
}
							
function sendBssCommand($bssid,$ip,$sysAlias,$add=1)
{
	$DEBUG = 0;
	$db = "BssOmcDb";
	$remark = $sysAlias;
	if($add==1)
		$nmicmd="newbss $bssid $ip";
	else
		$nmicmd = "dropbss $bssid $ip";
	
	/* Old MySql */
	$sqlstring = "SELECT row_no FROM OMCR_COMMAND WHERE status=0 " .
	             "ORDER BY seqNum ASC LIMIT 10";
	//echo "$sqlstring<BR>";
	$res = mysqli_query($pubConn,$sqlstring) or
	          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	while (list($row_no[]) = mysqli_fetch_row($res));
	$no_of_rows = mysqli_num_rows($res);
	mysql_free_result($res);
	if ( $no_of_rows <= 0 || ($no_of_rows > 10) )
	{
	    //
	    // No available row in OMCR_COMMAND
	    // Send error to MySQL()
	    //
	    $errorInfo = "Command table is full: Wait and send the command again";
		if($DEBUG) echo "<br>errorInfo=$errorInfo";
		alertMsg($errorInfo);
		
	}
	if($add==1)
	{
		//
		// Save "Name and remark" in OMCR_BSSTABLE
		// A non-null IP field in OMCR_BSSTABLE means the entry is valid
		//
		$sqlstring = "UPDATE OMCR_BSSTABLE SET name='$remark', ip='$ip'," .
		             "remark='$remark' WHERE bssid=$bssid";
		if($DEBUG) echo "$sqlstring<BR>";
		$res = mysqli_query($pubConn,$sqlstring) or
		          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
		
		$sqlstring = "UPDATE OMCR_BssFunc SET Label='$remark' " .
		             "WHERE bssid=$bssid";
		if($DEBUG) echo "$sqlstring<BR>";
		$res = mysqli_query($pubConn,$sqlstring) or
		          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	}
	//
	// Insert 1 commands in OMCR_COMMAND out of 10 possible available rows
	//
	$i=0;
	do
	{
	    if ($row_no[$i] != '')
	    {
	        $sqlstring = "REPLACE INTO OMCR_COMMAND VALUES(" .
	                     $row_no[$i] . ",-1,'$nmicmd',null,255,-1,null,null,null)";
	        if($DEBUG) echo "$sqlstring<BR>";
	        $res       = mysqli_query($pubConn,$sqlstring) or
	                     die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	    }
	    $i++;
	} while ($i<10 && mysql_affected_rows() != 2); //replace=delete+insert (2 task)
	
	$cmd1_row_no = $row_no[$i-1];
	
	//
	// Wait for response: timeout after 500ms x 20 = 10 secs
	//
	$i=0;
	while ($i<20)
	{
	    usleep(500000);
	    $sqlstring = "SELECT resultcode,result,responsecode,response from OMCR_COMMAND WHERE status=0 AND " .
	                 "row_no=$cmd1_row_no ORDER BY seqNum ASC";
	    if($DEBUG) echo "$sqlstring<BR>";
	    $res       = mysqli_query($pubConn,$sqlstring) or
	                 die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	    while ( $mydata = mysqli_fetch_row($res) )
	        list($resultcode,$result,$responsecode,$response) = $mydata;
	        //list($a[],$b[],$c[],$d[],$e[],$f[],$result[],$response[]) = $mydata;
	    $no_of_rows = mysqli_num_rows($res);
	    mysql_free_result($res);
	
	    if ($no_of_rows == 1)
	    {
	    	if($add==1)//add
	    	{
		        if ($result != 'ok')
		        {
		            // Create/Delete failed !!!
					$errorInfo = "$result: $response";
					if($DEBUG) echo "<br>errorInfo=$errorInfo";
					//$errorInfo=rawurlencode($errorInfo);
					alertMsg($errorInfo);
					return 0;
		        }
		    }
		    else//delete
		    {
				if ($resultcode != 0)
				{
					$errorInfo = "dropbss command failed: ".$response;
					if($DEBUG) echo "<br>errorInfo=$errorInfo";
					//$errorInfo=rawurlencode($errorInfo);
					alertMsg($errorInfo);
					return 0;
				}
		    }
	    }
	    echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
		flush();	
	    $i++;
	}
	return 1;
}//end sendBssCommand


?>
<?php
if($mode == 'add'){
	if(isset($save)){
		$tmpSysType = 500;
		if(!isset($sysNo))
			$sysNo = 0;
		$subSysNo = 0;
		$isRemote = 0;
				
		$return_result=sendBssCommand($sysNo,$bssIP,$sysAlias);
/*		if($return_result==1){
?>
			<script LANGUAGE="JavaScript">;
			 window.location.href = "modify.php?url=bssListDown.php" +
                                          "&language=<?=$language?>" +
                                          "&bssid=<?=$sysNo?>" +
                                          "&objectid=BssFunc" +
                                          "&nmicommand=set"+ 
                                          "&modified_data=Label"+
                                          "&tLabel=2"+
                                          "&Label=<?=$sysAlias?>" +
                                          "&timeout=30";
			 </script>;
<?php
		}*/
		//=============================================================
		$addSql = "REPLACE INTO $sysInfoTable (sysTypeNo,sysNo,subSysNo,isRemote,netId,DPC,ip,baseStat,sysAlias,updateTime )
					VALUES('500','$sysNo','$subSysNo','$isRemote','$ni','$dpc','$bssIP','1','$sysAlias',CURRENT_TIMESTAMP)";

		if($DEBUG) echo "<br>addSql = $addSql";
		$result = mysqli_query($pubConn,$addSql);
		echo mysqli_error($pubConn);
		
		echo "<br><font size=3 color=blue>Add ok</font>";
		echo " <meta http-equiv=\"refresh\" content=\"1; url=./bssListDown.php\"> ";
	}
}else if($mode == 'delete'){
	$tmpSysType = 500;
	$tmpSubSysNo = 0;
	$delSql = "DELETE FROM $sysInfoTable WHERE sysTypeNo='$tmpSysType' AND sysNo = '$sysNo'
				AND subSysNo = '$tmpSubSysNo'";
	if($DEBUG) echo "<br>delSql = $delSql";
	mysqli_query($pubConn,$delSql);
	
	$bssTable = "BssOmcDb.OMCR_BSSTABLE";
	$selSql = "select ip from $bssTable where bssid='$sysNo'";
	$result = mysqli_query($pubConn,$selSql);
	if($row = mysqli_fetch_array($result))
	{
		$bssIP = $row[ip];		
		$bssid=$sysNo;
		if(strlen($bssIP)>0)
			sendBssCommand($bssid,$bssIP,'',0);
	}
	echo "<br>Deleted Successful";
	exit();
}
?>

<form method="POST" name=confForm action="<?php echo"$PHP_SELF";?>" >
<table  border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF" > 
 <tr bgcolor="#E6E6E6">    
	<td width="100%" colspan="6" align="center">
		<font size=3><b><?php echo "BSS"; ?></b></font>
	</td>
 </tr>

<?php
echo "<tr bgcolor=#E6E6E6>";
echo "<td  width=\"25%\">System No.</td>";
echo "<td>";

$sysNoSql ="SELECT maxSysNum FROM $sysConfTable WHERE sysTypeNo='500'";
if($DEBUG) echo "<br>$sysNoSql";
$sysNoResult = mysqli_query($pubConn,$sysNoSql);
$sysNoRows = @mysqli_fetch_array($sysNoResult);
$maxSysNum = $sysNoRows[maxSysNum];

//Get the valid BSS object
unset($bssIDList);
$bssTable = "BssOmcDb.OMCR_BSSTABLE";
$selSql = "select * from $bssTable order by bssid";
$result = mysqli_query($pubConn,$selSql);
while($row = mysqli_fetch_array($result))
{
	$bssid_tmp=$row[bssid]+0;
	if(strlen(trim($row[ip])) > 0)
		$bssIDList[$bssid_tmp]=1;
	else
		$bssIDList[$bssid_tmp]=0;
}

echo "<select size=\"1\" name=\"sysNo\">";
for($i=0;$i<$maxSysNum;$i++){
	if($bssIDList[$i] == 0)
		echo "<option value=\"$i\">$i</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";

//===ip==
echo "<tr><td><nobr>IP:</td>";
echo "<td><input type=\"text\" name=\"bssIP\" value=\"\" length=\"15\"></td>";
echo "</tr>";
/*
echo "<tr>";
echo "<td>Alias:</td>";
echo "<td colspan=\"2\"><input type=\"text\" name=\"sysAlias\" value=\"$sysListRows[sysAlias]\" length=\"25\" maxlength=\"20\"></td>";
echo "</tr>";
*/
echo "</table>";
echo "&nbsp;&nbsp;";
echo "<input type=\"submit\" name=\"save\" value=\"Save\">&nbsp;&nbsp;";
echo "<input type=\"hidden\" name=\"mode\" value=\"$mode\">";
echo "</FROM>";
adjust_content_tail("down");
?>