<!DOCTYPE html>
<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>

</head>
<body style="background: #F3F3F3">
<div id="divDevCtrl">
<table width='100%' class="ofBTN">
    <tr><td style='border: none'> <h1>设备控制</h1></td></tr>
     <!--<tr><td style='width: 200px;'>自动控制</td><td><a class="gray" zt="1" op="zdkzBtn" href="javascript:void(0)">开启</a><a op="zdkzBtn" style="margin-left: 10px;" zt="0" class="gray" href="javascript:void(0)">关闭</a><span style="line-height: 35px;background: #F3F3F3"> <span id="msgg" style="margin-left: 50px">手动控制前请关闭自动控制</span></span></td></tr>-->
    <tr><td style='width: 200px;'>DO2</td><td><a id="aon3" class="gray" zt="1" op="jdc3Btn" href="javascript:void(0)">开启</a><a id="aoff3" op="jdc3Btn" style="margin-left: 10px;" zt="0" class="gray" href="javascript:void(0)">关闭</a></td></tr>
    <!--<tr><td style='width: 200px;'>DO2</td><td><a id="aon2" class="gray" zt="1" op="jdc2Btn" href="javascript:void(0)">开启</a><a id="aoff2" op="jdc2Btn" style="margin-left: 10px;" zt="0" class="gray" href="javascript:void(0)">关闭</a></td></tr>-->
    <tr><td style='width: 200px;'>DO1</td><td><a id="aon1" class="gray" zt="1" op="jdc1Btn" href="javascript:void(0)">开启</a><a id="aoff1" op="jdc1Btn" style="margin-left: 10px;" zt="0" class="gray" href="javascript:void(0)">关闭</a></td></tr>
</table>
</div>
<a href='javascript:history.back()' class='back' style='color: #fff'>返回</a>

<script>
<% db_Open("/NandFlash/dbCfg.db3"); %>
var diCnt = <% db_QryVal("select Count(*) from tbPort where Type=1 and SID=1"); %>
var doName = <% db_QryAdv("select Name from tbPort where Type=2",0); %>
<% db_Close(); %>
var doCnt = doName.reccnt;

$(function(){
    var arr=["green","redd"];
    $("a[op=zdkzBtn]").live("click",function(){
        var i=$("a[op=zdkzBtn]").index(this);
        $("a[op=zdkzBtn]:eq("+i+")").attr("class",arr[i]).siblings().attr("class","gray");
        var q=$(this).attr("zt");
        ajaxBtnof(3,q);
        if(i==0){
            $("#msgg").html("")
        }else{
            $("#msgg").html("提示：<span  style='color: red;'>自动控制已经关闭！</span>退出该页面前请确认 打开 自动控制，否则自动控制失效！")
        }
    });
	
    ajaxSt();
});

function ajaxSt(){
    $.ajax({
        type: "POST",
        url: "omdev/smdat0.asp",
        data: "itype=2304&sid=0",
        dataType:"json",
        success: function(msg){
            updSt(msg);
        }
    });
}
function updSt(data){
	if (data.qry!="ok")
	  return;
	if (data.reccnt<=0)
	  return;
  for(fld in data.recs[0]) {
	  if(fld.indexOf('D') == 0){
	    var iv = parseInt(fld.substr(1));
	    //console.log("doCnt="+doCnt);
	    if (iv>diCnt && iv<=diCnt+doCnt) {
	    	var idx = iv-diCnt;
				if (data.recs[0][fld]==1)
				{
					$("#aon"+idx).attr("class","green");
					$("#aoff"+idx).attr("class","gray");
				}
				else
				{
					$("#aon"+idx).attr("class","gray");
					$("#aoff"+idx).attr("class","redd");
				}
	    }
		}
	}
}

list0(doName);
function list0(data){
	var tbX="";
	tbX+="<table width='100%' class=\"ofBTN\">	\n";
	tbX+="<tr><td style='border: none'><h1>设备控制</h1></td></tr> \n ";
	//tbX+="<tr><td style='width: 200px;'>自动控制</td><td><a class=\"gray\" zt=\"1\" op=\"zdkzBtn\" href=\"javascript:void(0)\">开启</a><a op=\"zdkzBtn\" style=\"margin-left: 10px;\" zt=\"0\" class=\"gray\" href=\"javascript:void(0)\">关闭</a><span style=\"line-height: 35px;background: #F3F3F3\"> <span id=\"msgg\" style=\"margin-left: 50px\">手动控制前请关闭自动控制</span></span></td></tr>\n";

    $.each(data.recs,function(i,n){
		var index = i+1;
		
		tbX+="<tr><td style='width: 200px;'>";
		tbX+=n.Name;
		tbX+="</td><td><a id=\"aon";
		tbX+=index;
		tbX+="\" class=\"gray\" href=\"javascript:btnclk("+index+",1)\">开启</a><a id=\"aoff";
		tbX+=index;
		tbX+="\" style=\"margin-left: 10px;\" class=\"gray\" href=\"javascript:btnclk("+index+",0)\">关闭</a></td></tr>\n";
    })
	
	tbX+="</table>";
	document.getElementById("divDevCtrl").innerHTML = tbX;
	//console.log("tbX="+tbX);
}


function btnclk(idx, op) {
	if (op) {
		$("#aoff"+idx).attr("class","gray");
		$("#aon"+idx).attr("class","green");
	}
	else {
		$("#aon"+idx).attr("class","gray");
		$("#aoff"+idx).attr("class","redd");
	}
  if (idx >=3) idx++;
  ajaxBtnof(idx,op);
}
function ajaxBtnof(wParam,lParam){
    $.ajax({
        type: "POST",
        url: "action/doctrl",
        data: "op="+wParam+"&value="+lParam+"",
        dataType: "json",
        success: function(msg){
          alert("ok!");
					//var id1 = setTimeout(ajaxSt(), 3000);
					//clearTimeout(id1);
        }
    });
}
</script>
</body>
</html>