<?php
/*************************************************************************      
* 文件说明：         
*    功能说明:OMC状态显示页面，
*    调用关系：        
*
*
* 作者：Cui Ticun    2006-10-30     
*      
*修改注释：      
*NO.1      
*Name：            
*Time：
*Modify description：      
*************************************************************************/
require("../sub_inc/header.inc");
$showingTypeDefine=array(			
						array('needDragElement'=>'1','typeOffset'=>'20480','maxPageNum'=>'1','maxLedNum'=>'512','bg_img'=>'map.jpg','title'=>'MAP'),
						array('needDragElement'=>'1','typeOffset'=>'20489','maxPageNum'=>'1','maxLedNum'=>'512','bg_img'=>'bg_shelf.jpg','title'=>'Central Office'),
						array('needDragElement'=>'1','typeOffset'=>'20577','maxPageNum'=>'12','maxLedNum'=>'512','bg_img'=>'bss.jpg','title'=>'BSS')
						);

if(!isset($pageNum)||strlen($pageNum)<=0)
	$pageNum=0;
if(!isset($showingPageType)||strlen($showingPageType)<=0)
	$showingPageType=0;

$needDragElement=$showingTypeDefine[$showingPageType][needDragElement];
$typeOffset		=$showingTypeDefine[$showingPageType][typeOffset];
$maxPageNum		=$showingTypeDefine[$showingPageType][maxPageNum];
$maxLedNum		=$showingTypeDefine[$showingPageType][maxLedNum];
$bg_img			=$showingTypeDefine[$showingPageType][bg_img];

if($DEBUG)
{
	echo "<br>showingPageType=$showingPageType";
	echo "<br>needDragElement=$needDragElement";
	echo "<br>typeOffset=$typeOffset";
	echo "<br>pageNum=$pageNum";
	echo "<br>maxPageNum=$maxPageNum";
	echo "<br>maxLedNum=$maxLedNum";
	echo "<br>bg_img=$bg_img";
}

echo "<script language=\"JavaScript\">\n";
echo "var showingPageType=$showingPageType;\n";
echo "var needDragElement=$needDragElement;\n";
echo "var typeOffset=$typeOffset;\n";
echo "var pageNum=$pageNum;\n";
echo "var maxPageNum=$maxPageNum;\n";
echo "var maxLedNum=$maxLedNum;\n";
echo "var bg_img='$bg_img';\n";
echo "</script>";

if($needDragElement==1)
	require("./dragElement_test.php");
?>

<?php
if($language_version == 'eng'){
	$language = eng;
}else{
	$language = gb;
}

$dbName='OMC_PUB';
$ledConfTable='sysLedConf';
$ledStatTable='sysLedStat';

$nLedNoUsed	='00';
$nLedOk		='01';
$nLedWarn	='02';
$nLedError	='03';
$nLedAlarm  ='04';
$NotShowLED ='15';
$shapeArr=array("circle","square","rectangle","triangle","rectangle_v","star");
$suffixArr=array(".gif","_f.gif");
$maxY=540;
$maxX=760;
$divideNumber='20489';
$routerURL = "./status_router.php?";
$htmlPageType=50;
?>

<script language="JavaScript">

var request_o=null; //declare the variable to hold the object.

var pageNum=0;
var totalLedNum=0;

/*
*--------------- GetResult() -----------------
* GetResult() 
* 功能:通过XMLHTTP发送请求,返回结果.
* 参数:str,字符串,发送条件.
* 实例:GetResult();
*--------------- GetResult() -----------------
*/

function GetResult()
{
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
		//new XMLHttpRequest() 
	}
	//alert(request_o);
	var url="mapStatusServer.php?showingPageType="+showingPageType+"&typeOffset="+typeOffset+"&pageNum="+pageNum;
	request_o.open("GET",url,"false");

	request_o.onreadystatechange = handleMsgs; 

	request_o.send(null);
}

function handleMsgs(){
	//alert(totalLedNum);
	
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	//alert(request_o.readyState);

	if(request_o.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		//var strResult = unescape(request_o.responseText);
		var strResult = request_o.responseText;
		//alert(strResult);
		var arrResult = strResult.split("@@@");
				
		if(arrResult.length-1!=totalLedNum)
			window.location.href="./mapStatus_test.php?showingPageType="+showingPageType+"&pageNum="+pageNum;
		for(var i=0;i<arrResult.length-1;i++){
			statusImg=document.getElementById(i);
			//alert(arrResult[i]);
			statusImg.src=arrResult[i];
		}
	}
}

function MyShow(ledNum)
{
	//10秒自动刷新一次,10秒取得一次数据.
	timer = window.setInterval("GetResult()",10000);
}

function ChangeImgId()
{
	var idNumber = 0;
	for (var i=0;i<document.images.length;i++)
	{
		if(document.images[i].id != 'helpId'){
			document.images[i].id = idNumber;
			idNumber++;
		}
	}
}	

function openPage(newPage_showingPageType,newPage_pageNum,backurl)
{
	window.location.href=backurl+"showingPageType="+newPage_showingPageType+"&pageNum="+newPage_pageNum;
}

</script>
<?php
function getLedStatus($status_str,$led_num){
	global $NotShowLED,$divideNumber;
	$status_num = $NotShowLED;

	$offset = 0;
	if($led_num < $divideNumber){
		$offset = ($led_num % 16)*2;
	}else{
		$offset = 0;
	}
	if($DEBUG) 
	echo "<br>led_num=$led_num,status_str=$status_str,offset=$offset";
	$status_num = substr($status_str,$offset,2);
	if($DEBUG) 
	echo "<br>status_num=$status_num";

	if($status_num >=0 && $status_num <=$NotShowLED){
		return($status_num&0x07);
	}else{
		return($NotShowLED);
	}
}
?>

<?php
//save the result of dragging
if($command=='save'){
	$tmpArr=explode("_",$drag_key_led_num);
	$updateSql="UPDATE $ledConfTable SET point_x='$drag_x',point_y='$drag_y' WHERE key_led_num='$tmpArr[1]'";
	if($DEBUG){
		echo "<font size=\"2\">";
		echo "<br>updateSql=$updateSql";
		echo "</font>";
	}
	mysqli_query($pubConn,$updateSql);
}
?>

<?php
$pageIndex=$typeOffset+$pageNum;
$selSql = "SELECT T1.key_led_num,T2.key_status,T1.point_x,T1.point_y,T1.shape,T1.remark,T1.dst_page_type,T1.dst_page_num 
					FROM $ledConfTable AS T1,$ledStatTable AS T2  
					WHERE (
					if(T1.sync_from_led < 0 && T2.key_led_num < $divideNumber,floor(T1.key_led_num/16) = T2.key_led_num,T1.key_led_num = T2.key_led_num) 
					OR 
					if(T1.sync_from_led >= 0 && T2.key_led_num < $divideNumber,floor(T1.sync_from_led/16) = T2.key_led_num,T1.sync_from_led = T2.key_led_num) 
					)
					AND T1.key_src_page_type ='$showingPageType' 
					AND T1.key_src_page_num = '$pageIndex' 
					";
if($DEBUG)
	echo "<br>selSql=$selSql";
$selResult=mysqli_query($pubConn,$selSql);
echo mysqli_error($pubConn);
$i = 0;
while($selRows=mysqli_fetch_array($selResult))
{
	$key_led_num_Arr[$i] = $selRows[key_led_num];
	$X_Arr[$i]			 = $selRows[point_x];
	$Y_Arr[$i]			 = $selRows[point_y];
	$status[$i]			 = getLedStatus($selRows[key_status],$selRows[key_led_num]);
	if(strchr($selRows[remark],":")){
		$tmpRemarkArr		 = explode(":",$selRows[remark]);
		$remark[$i]			 = $tmpRemarkArr[1];
	}else
		$remark[$i]			 = $selRows[remark];
	$dst_page_type[$i]	 = $selRows[dst_page_type];
	$dst_page_num[$i]	 = $selRows[dst_page_num];
	$shape[$i]			 = $selRows[shape];
	$type[$i]			 = $selRows[type];

	/*
	$tmpX=$selRows[point_x]+10;
	$tmpY=$selRows[point_y]+60;
	
	$updateSql="UPDATE $ledConfTable SET point_x='$tmpX',point_Y='$tmpY' where key_led_num='$key_led_num_Arr[$i]'";
	//	echo "<br>updateSql=$updateSql";
	mysqli_query($pubConn,$updateSql);
	*/
	if($DEBUG)
	{
		echo "<br>key_led_num_Arr[$i]=$key_led_num_Arr[$i]";
		echo "<br>X_Arr[$i]=$X_Arr[$i]";
		echo "<br>Y_Arr[$i]=$Y_Arr[$i]";
		echo "<br>type[$i]=$type[$i]";
		echo "<br>status[$i]=$status[$i]";
		echo "<br>remark[$i]=$remark[$i]";
	}
	$i++;
}
$ledNum = $i;
?>

<BODY  background="./images/<?php echo $bg_img;?>" style="background-repeat:no-repeat;background-position:10 60" onload="ChangeImgId();">

<?php
echo "<table boder=\"0\" width=\"100%\" height=\"50\">";
echo "<tr>";
echo "<td width=\"50%\">";
echo "<font size=\"2\">";
echo "Status > ";
for($i=0;$i<sizeof($showingTypeDefine);$i++){	
	echo "<a href=\"JavaScript:openPage($i,0,'$PHP_SELF?')\">{$showingTypeDefine[$i][title]}</a>";
	
	if($showingTypeDefine[$i][title]=='BSS'&&$showingTypeDefine[$showingPageType][title]=='BSS'){
		if($pageNum>0){
			$tmpPageNum=$pageNum-1;
			echo "<a href=\"JavaScript:openPage($i,$tmpPageNum,'$PHP_SELF?')\"><</a>";
		}else{
			echo "<";
		}
		echo "&nbsp;$pageNum&nbsp;";

		if($pageNum<11){
			$tmpPageNum=$pageNum+1;
			echo "<a href=\"JavaScript:openPage($i,$tmpPageNum,'$PHP_SELF?')\">></a>";
		}else{
			echo ">";
		}
	}
	echo "|";
}
echo "</font>";
echo "</td>";
echo "</tr>";
echo "</table>";

//display all led status
$totalImgY=0;
for($j=0;$j<sizeof($Y_Arr);$j++)
{
	//echo "<font size=\"2\">status = $status[$j]<br>";
	switch($status[$j]){
	case $nLedOk:
		$tmpImage="./images/".$shapeArr[$shape[$j]]."_green";
		break;
	case $nLedWarn:
		$tmpImage="./images/".$shapeArr[$shape[$j]]."_yellow";
		break;
	case $nLedError:
		$tmpImage="./images/".$shapeArr[$shape[$j]]."_red";
		break;
	case $nLedNoUsed:
		$tmpImage="./images/".$shapeArr[$shape[$j]]."_gray";
		break;
	default :
		
		$tmpImage="./images/".$shapeArr[$shape[$j]]."_gray";
		break;
	}

	if($continueFlag==1){
		$ledNum--;
		$continueFlag=0;
		continue;
	}
	
	$tmpImage = $tmpImage.$suffixArr[0];
	$tmpId='statusLed_'.$key_led_num_Arr[$j];

	echo "\n<div id=\"$tmpId\" style=\"position:absolute; top:{$Y_Arr[$j]}px; left:{$X_Arr[$j]}px;\">";
	if($needDragElement){
		echo "\n<div onmousedown=\"beginDrag(this.parentNode,event);\">";
		echo "<img VSPACE=\"0\" align = \"top\" border=\"0\" src=\"$tmpImage\"></a>";
		echo "\n</div>";
		
		$tmpPageNum=$dst_page_num[$j]-$showingTypeDefine[$dst_page_type[$j]][typeOffset];
	
		if($dst_page_type[$j] >= $htmlPageType){
			$linkURL=$routerURL."page_type=$dst_page_type[$j]&page_num=$dst_page_num[$j]&src_page_type=$showingPageType&src_page_num=$pageNum";
			if($DEBUG) 
				echo "<br>linkURL=$linkURL";
			echo "<a href=\"$linkURL\">$remark[$j]</a>";
		}else{
			$i=$dst_page_type[$j];
			echo "<a href=\"JavaScript:openPage($i,$tmpPageNum,'$PHP_SELF?')\">$remark[$j]</a>";
		}
	}else{
		$tmpPageNum=$dst_page_num[$j]-$showingTypeDefine[$dst_page_type[$j]][typeOffset];
		if($dst_page_type[$j] >= $htmlPageType){
			$linkURL=$routerURL."page_type=$dst_page_type[$j]&page_num=$dst_page_num[$j]&src_page_type=$showingPageType&src_page_num=$pageNum";
			if($DEBUG) 
				echo "<br>linkURL=$linkURL";
			echo "<a href=\"$linkURL\"><img VSPACE=\"0\" align = \"top\" border=\"0\" src=\"$tmpImage\"></a>";
		}else{
			$i=$dst_page_type[$j];
			echo "<a href=\"JavaScript:openPage($i,$tmpPageNum,'$PHP_SELF?')\"><img VSPACE=\"0\" align = \"top\" border=\"0\" src=\"$tmpImage\"></a>";
		}
	}
	echo "\n</div>";	
}

echo "<script language=\"JavaScript\">";
echo "pageNum=$pageNum;";
echo "totalLedNum=$ledNum;";
echo "</script>";
?>
</body>
</html>
