<!DOCTYPE html>
<% language=javascript %>
<html>
<head lang="en">
    <meta charset="GB2312">
    <title></title>
    <link href="css/style.css" title="style" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.6.3.min.js"></script>

    <style type="text/css">
        #main div{position:absolute;width:55px;height:37px;text-align:center;line-height: 37px;font-weight: bold;color:blue  }
        .subsysbox{background:#f0f0f0; border:2px solid #999;}
        .green_{background: #CCFF99;border: 1px solid black;}
        .border{border:solid 2px black}
        .nav{position: absolute;right: 10px;top: 0px;}
        .nav li{float: left;width: 90px;margin-left: 5px;margin-top: 0px}
        .nav a{display: block;color: #636464;font-size: 1em;line-height: 30px;text-align: center;border: 2px solid #636464; }
        .nav a:hover{background: #2A303E;color: #fff;}
		.tips {background:#f0f0f0; border:2px solid #999;;color: #1C86EE;font-weight:bold;font-size: 15px;position:absolute;z-index: 9999;}
    </style>
</head>
<body style="background: #F3F3F3">
	<div class="divbox" style="margin-left:0px;margin-top: 0px;display: none">
		<input type="checkbox" class="int_check" id="check" disabled="disabled">启用
		<input type="text" value=""  class="name_input" disabled="disabled">
		<input type="button" class="int_bc" value="保存">
	</div>
<div class="nav" style="display: none">
    <ul>
            <li><a class="Custom" href="javascript:void(0)">打开设计</a></li>
    </ul>
</div>
	<div class="system_status">
        <table width='100%'>
        <tr> <td style='border: none;'> <h1 style='font-size: 18px;'>系统当前状态</h1></td> </tr>
        <tr> <td style='width: 200px;'>设备当前时间：</td><td><span class='timeIdex'></span></td> </tr>
        <tr style='display:none;'> <td style='width: 200px;'>GSM信号质量：</td><td></td> </tr>
        <tr> <td style='width: 200px;'>当前传感器数量：</td><td><span class='length'></span></td> </tr>
		</table>
        <table width='100%' class='index_table' style='margin-top: 30px'>
        <tr> <td style='border: none'> <h1 style='font-size: 18px;'>设备监测信息</h1></td> </tr></table>
	</div>
	<div class="append" style='background:url(img/bg-status.jpg);background-size: 100% 100%;background-repeat:no-repeat;position:absolute; width:100%; height:100%; z-index:-1'>
	</div>
	<div class="tips" id="tips"></div>
	<!--<a href='javascript:history.back()' class='back' style='color: #fff'>返回</a>-->
<script>
var tescape= 5000,time;
var _settings = 0;
var ismousedown = 0;
var possave = 1;
var axisx = new Array();
var axisy = new Array();
var sidmax = 0;
var position = <%
db_Open("/NandFlash/dbCfg.db3");
write("{\"items\":");
db_QryAdv("select * from tbSMLocation order by SID",0);
write("}");
db_Close();
%>;

<% db_Open("/NandFlash/tmp/dbDat0.db3"); %>
var json=<%
write("{\"items\":");
db_QryAdv("select ss.[SubSys], ssd.SID, ssd.Type, ssd.Fld, sm.Name, ss.Note, sm.Tag1, sm.State as AlmSt, sm.SerDef, sm.Ext2, sm.AID, ss.Count, ssd.[LastUpd], ssd.State from tbSubSysDetail ssd left join tbSubSys ss on ssd.[SubSys] = ss.SubSys left join tbSM sm on ssd.[SID] = sm.SID",0);
write(",\"gsm\":");
db_QryAdv("select * from tbAlmGsm",0);
write(",\"dt\":{\"dt\":");
cfg_GetCfg(5);
write("}}");
%>;
var jcount = <% db_QryAdv("select count(SID) as sidnum from tbSubSysDetail where State>=0 and State<=1") ;%>;
var smst="";
<% db_Close(); %>

var isadmin = (getCookie("user").indexOf("|OK1") >= 0);
if (isadmin) {
  $(".nav").show();
} else {
  $(".nav").hide();
}

$(".tips").hide();
  
$(".back,.you_p").live("click",function(){window.history.go(-1);});
$(function(){
	$("span:contains('故障'),span:contains('告警'),span:contains('闭合'),span:contains('关闭')").addClass("red").removeClass("ger");
	$("span:contains('正常')").addClass("ger").removeClass("red");
	time=setInterval(ajax,tescape);
});
function ajax(){
	$.ajax({
		type: "POST",
        url: "omdev/allstatus-loc.asp",
		async: true,
		dataType:"json",
		success: function(msg){
			list3(msg);
		}
	});
}
listPosition(position);
list3(json);
function list3(data){
	var q='';
	var almcnt=0;
	$.each(data.items.recs,function(i,n){
		if(i==0){
		}
		almcnt++;
		if(((n.SubSys) != 0x0C) && ((n.SubSys) != 0x0D))
		{
			sidmax = (sidmax>=n.SID) ? sidmax : n.SID;
			q += "<div class='subsysbox indexSubSys' id='drag" + n.SID + "' style='"+GetSenStyle(n.SID)+"'";
			q += " onmousedown='mousedown(this,event,"+n.SID+")' onmousemove='mousemove(event,"+n.SID+")' onmouseup='mouseup(event,"+n.SID+")'";
			q += "onmouseover='ontips(event,"+n.State+")' onmouseout='outtips()'";
			q += ">";
			q += "<span class='sphref' style='cursor:pointer;color:"+GetSenColor(n.State)+";font-weight:bold;' onclick='OnSIDMouseClick("+n.SubSys+","+n.SID+","+n.Type+","+n.Tag1+",\""+n.SerDef+"\")'>&nbsp;"+ (n.Name)+"</span>";
			if (n.AID > 0)
			{
				if (n.AlmSt==1)
				{
					q += "&nbsp;&nbsp;<span>告警已解除 </span>";
				}
				else if(n.State==1)
				{
					//q += "<br><span class='statehome' >&nbsp;"+ GetSenState(n.State)+"&nbsp;</span>";
					q += "&nbsp;&nbsp;<span><a class='indexSubSys'  href='javascript:RemoveAlarm(" + n.SID + "," + n.Type + ",0" + ",\"" + n.Name + "\")'>解除告警</a> </span>";
				}
			}
			q+="</div>";
		}
	});
	sidmax = (sidmax > 128) ? 128 : sidmax;
	q += smst;
	$(".append").html(q);
	$.each(data,function(i,n){
		$(".timeIdex").html(n.dt)
	});
	$(".length").html(almcnt);
	$("span:contains('故障'),span:contains('告警'),span:contains('闭合')").addClass("red").removeClass("ger");
	$("span:contains('正常')").addClass("ger").removeClass("red");
}


function listPosition(data){
	$.each(data.items.recs,function(i,n){
		axisx[n.SID] = n.axisx;
		axisy[n.SID] = n.axisy;
	});
}

function RemoveAlarm(sid, type, fld, name)
{
	var str='确定要解除 '+name+' 告警吗？';
	var wParam = 0x11;//0x10
	var lParam = sid;
	if(!window.confirm(str)) return;
	/*if((type & 0xff00) == 0x0900)//开关量
	{
	}
	else
	{
	}*/
	$.ajax({
		type: "POST",
		url: "action/doctrl",
		data: "op="+wParam+"&value="+lParam+"",
		dataType: "json",
		success: function(msg){
			alert("ok!");
		}
	});
}

function GetSenStyle(isid)
{
	var strstyle = "position: absolute;z-index: 2;left: " + axisx[isid] + "%;top: " + axisy[isid] + "%;color: #1C86EE;";
	return strstyle;
}


function GetSenColor(istate)
{
	var strstyle = "green";
	if(istate==0){strstyle="green"}
	else if(istate==1){strstyle="red"}
	else{strstyle="red"}
	return strstyle;
}

function GetSenState(istate)
{
	var strstyle = "正常";
	if(istate==0){strstyle="正常"}
	else if(istate==1){strstyle="告警"}
	else{strstyle="故障"}
	return strstyle;
}

function GetSIDIdstr(isid)
{
	switch(isid){
	case 1: return "Elec";//市电监测
	case 2: return "Onoff";//配电监测
	case 3: return "UPS";//UPS监测
	case 4: return "BAT";//电池监测
	case 5: return "Envir";//温度湿度
	case 6: return "Leak";//漏水监测
	case 7: return "Airc";//空调监测
	case 8: return "Cyber";//精密空调
	case 9: return "Smoke";//烟雾监测
	case 10: return "Infrared";//红外监测
	case 11: return "Card";//门禁门磁
	}
	return "Unknown";
}

function ontips(event, st)
{
	var s = "";
	var color = "red"
	if (st == 0)
	{
		s += "正常";
		color = "green"
	}
	else if (st == 1)
		s += "告警";
	else
		s += "故障";
	var offset = $(".tips").offset();
	offset.left = event.pageX;
	offset.top = event.pageY-35;
	$(".tips").html(s);
	$(".tips").css({"color":color,"left":offset.left,"top":offset.top, "font-size":"18px","font-weight":"bold"});
	$(".tips").show();
}

function outtips()
{
	$(".tips").hide();
}

function OnSIDMouseClick(isys, isid, itype, itag1, sSer)
{
	if (_settings == 0)
	{
		var url = "A3-" + itype;
		if(itype==256 && itag1 == 35) 
			url+="-"+itag1;
        else if(itype==1792 && itag1 == 1) 
			url+="-"+itag1;
		else if(itype==2050 && itag1 == 1) 
			q+="-"+itag1;
        else if(itype==2056 && itag1 == 1) 
			url+="-"+itag1;
		else if((n.Type==3331) && (n.Tag1==0)) 
			q+="-"+n.Tag1;
		url += ".asp?sid=" + isid + "&tag1=" + itag1;
		if (sSer == "")
			url = "C2.asp?subsys="+isys;
		self.location = url;
	}
}

var a;
var _x,_y;//鼠标离控件左上角的相对位置
var _move = 0;
function mousedown(o, e, isys) {
	if (_settings) {
		a = o;
		document.all ? a.setCapture() : window.captureEvents(Event.MOUSEDOWN);
		_x=e.pageX-parseInt($("#drag"+isys).css("left"));
		_y=e.pageY-parseInt($("#drag"+isys).css("top"));
		ismousedown = 1;
	}
}

function mouseup(e, isys) {
	if (_settings) {
		document.all ? a.setCapture() : window.captureEvents(Event.MOUSEUP);
		ismousedown = 0;
		if (_move)
		{
			var l = parseInt((parseInt(a.style.left)) / parseInt($(".append").css("width") + 0.5) * 100);
			var t = parseInt((parseInt(a.style.top)) / parseInt($(".append").css("height") + 0.5) * 100);
			
			//alert("axisx=" + axisx[isys] + "&nbsp;axisy=" + axisy[isys] + "<br>curx=" +l+ "&nbsp;cury="+t);
			axisx[isys] = l;
			axisy[isys] = t;
			saveSMPos(isys);
			_move = 0;
		}
		a = "";
	}
}

function mousemove(e, isys){
	if (_settings && ismousedown) {
		document.all ? a.setCapture() : window.captureEvents(Event.MOUSEMOVE);
		var x=e.pageX-_x;//移动时根据鼠标位置计算控件左上角的绝对位置
		var y=e.pageY-_y;
		
		var xx=parseInt($("#drag"+isys).css("left"));
		var yy=parseInt($("#drag"+isys).css("top"));
		
		if ((Math.abs(x-xx) >= 2) || (Math.abs(y-yy) >= 2))
			possave = 0;
		//alert("x=" + x + "&nbsp;y=" + y + "<br>xx=" +xx+ "&nbsp;yy="+yy);
		$("#drag"+isys).css({top:y,left:x});//控件新位置
		_move = 1;
	}
}

function saveSMPos(sid)
{
	if (sid<=0)
	{
		for (var i=0; i <= sidmax; i++)
		{
			var sdat = "op=upd";
			sdat += "&sid=" + i;
			sdat += "&axisx=" + axisx[i];
			sdat += "&axisy=" + axisy[i];
			$.ajax({
			  type: "POST",
			  url: "omdev/smlocation.asp",
			  data: sdat,
			  success: function(msg) {
			  }
			});
		}
		alert("数据已经保存！");
	}
	else
	{
		var sdat = "op=upd";
		sdat += "&sid=" + sid;
		sdat += "&axisx=" + axisx[sid];
		sdat += "&axisy=" + axisy[sid];
		$.ajax({
		  type: "POST",
		  url: "omdev/smlocation.asp",
		  data: sdat,
		  success: function(msg) {
		  }
		});
	}
}

//保存按钮
$(".int_bc").click(function() {
	saveSMPos(-1);
});

//开关设计切换
$(".Custom").live('click', function() {
	if ($(".Custom").text() == "打开设计") {
	  $(".Custom").css("background", "#2A303E");
	  $(this).text("关闭设计");
	  $(".Custom").css("color", "#fff");
	  $(".divbox").show("500");
	  _settings = 1;
		ismousedown = 0;
		possave = 1;
	  clearInterval(time);
	} else {
	  $(".Custom").css("background", "#E5E9EC");
	  $(this).text("打开设计");
	  $(".Custom").css("color", "#636464");
	  $(".divbox").hide("500");
	  if (possave == 0)
	  {
		if(window.confirm("是否保存位置信息？")) 
			saveSMPos(-1);
	  }
	  possave = 1;
	  _settings = 0;
		ismousedown = 0;
		time=setInterval(ajax,tescape);
	}
});

function getCookie(c_name) {
  if (document.cookie.length > 0) {
	c_start = document.cookie.indexOf(c_name + "=")
	if (c_start != -1) {
	  c_start = c_start + c_name.length + 1
	  c_end = document.cookie.indexOf(";", c_start)
	  if (c_end == -1) c_end = document.cookie.length
	  return unescape(document.cookie.substring(c_start, c_end))
	}
  }
  return ""
}
</script>
</body>
</html>