<!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>
  <div class='list2' style='margin: 10px;'>
    <a class='you_p' href='#' style='color: #fff'>开关量设备</a>
  </div>
  <div style='border: #B0B7BF 1px solid'>
    <table id='tbList' width='100%'>
    <tr bgcolor='#E5E9EC'><th style='padding: 10px'><span>SID</span> </th><th style='padding: 10px'><span>监测项描述</span></th><th style='padding: 10px'><span>传感器类型</span></th><th style='padding: 10px'><span>基本信息</span></th><th style='padding: 10px'><span>操作</span></th></tr>
    </table>
  </div>
</div>
<div>
  <table width='100%'>
    <tr>
    <td width="50%" style="border:none">传感器类型: <select id="lsType" style="width:350px"></select></td>
    <td style="border:none"><span style="display:none">端 口: <select id="lsPort" style="width:100px"></select></span></td>
    </tr>
    <tr>
    <td><a href='javascript:sidAdd()' class='back' style='color: #fff;background: #ffa225'>+添加监测项</a></td>
    <td><a href='javascript:history.back()' class='back' style='color: #fff'>返回</a></td>
    </tr>
  </table>
</div>
<div  style="color:red">
  <br>温馨提示：
  <br>添加传感器的方法：选择需要添加的“传感器”后，
  <br>点击“+添加监测项”，即可完成对传感器的添加。
</div>
<script>

<% db_Open("/NandFlash/dbCfg.db3"); %>
var jtype=<% db_QryAdv("select Type,t.Kind,t.Name,t.Note,Tag1,Ext1,Port,k.Name as KName from tbSMType t left join tbSMKind k on k.Kind=t.Kind where Port=1 order by Type,Tag1",0); %>;
var jport=<% db_QryAdv("select * from tbPort where Type=1 order by Type,Name",0); %>;
<% db_Close(); %>

listT();
function listT(){
  if(jtype.reccnt==0) return;
  var s="";
  $.each(jtype.recs, function(i,n){
    s+="<option value='"+n.Type+"'>"+n.Note+" ("+n.Name+")";
  });
  $("#lsType").html(s);
}

listP();
function listP(){
  if(jport.reccnt==0) return;
  var s="";
  $.each(jport.recs, function(i,n){
    s+="<option value='"+n.Val+"'>"+n.Name;
  });
  $("#lsPort").html(s);
}

$(function(){
    qryDio();
});

function qryPort(){
  $.ajax({
    type: "POST",
    url: "omdev/port.asp",
    data: "op=qryok&port="+3,
    contentType:"application/x-www-form-urlencoded; charset=gb2312",
    dataType:"json",
    success: function(msg){
      jport=msg;
      listP();
    }
  });
}

function qryDio(){
  $.ajax({
    type: "POST",
    url: "omdev/smbyport.asp",
    data: "port="+1,
    contentType:"application/x-www-form-urlencoded; charset=gb2312",
    dataType:"json",
    success: function(msg){
      listDio(msg)
    }
  });
}
function listDio(data){
  var s='';
  s+="<tr bgcolor='#E5E9EC'><th style='padding: 10px'><span>SID</span> </th><th style='padding: 10px'><span>监测项描述</span></th><th style='padding: 10px'><span>传感器类型</span></th><th style='padding: 10px'><span>基本信息</span></th><th style='padding: 10px'><span>操作</span></th></tr>";
  $.each(data.recs,function(i,n){
    s+="<tr id='tr"+i+"'><th>"+n.SID+"</th><th  style='text-align: left'>"+n.Name+"</th><th style='text-align: left'>"+n.TNote+" ("+n.TName+")</th><th style='text-align: left'>"+getNotes(n,i)+"</th><th>"+getOps(n,i)+"</th></tr>";
  })
  $("#tbList").html(s);
  $("a:contains('启用')").addClass("red");
}
function fmtPort(iv,tag){
  var s="";
  if(iv & 0xf00000)
     s+="&nbsp;DI"+(iv&0xff);
  else 
  {
    if(tag==1)
	{
	  s+="&nbsp;SID"+((iv & 0xfff00)>>8)+"-"+"AI"+(iv&0xff);
	}
	else
    s+="&nbsp;SID"+((iv & 0xfff00)>>8)+"-"+"DI"+(iv&0xff);
  }
  return s;
}
function getNotes(n, i){
  var s="";
  s+="端口:"+fmtPort(n.SerType,n.Tag1);
  s+="&nbsp;";
  if(n.Tag1==0)
  {
   if(n.Ext1==0) s+="禁用告警";
   if(n.Ext1==1) s+="闭合告警";
   if(n.Ext1==2) s+="断开告警";
  }
  //s+="&nbsp;附注:"+n.Note;
  return s;
}
function getOps(n, i){
  var s="";
  s+="<a href='C0-1x.asp?sid="+n.SID+"&type="+n.Type+"&port="+n.SerType+"&tag1="+n.Tag1+"'>编辑</a>";
  s+="&nbsp;&nbsp;<a id='sid_st"+i+"' href='javascript:sidSt("+n.SID+","+i+","+n.SerType+")'>";
  if(n.State==0) s+="停用"; else s+="启用";
  s+="</a>";
  s+="&nbsp;&nbsp;<a href='javascript:sidDel("+n.SID+","+i+")'>删除</a>";
  return s;
}
function sidSt(isid, itr, iser){
  var s=$("#sid_st"+itr).text()
  var ist=0;
  if(s=="停用"){
    s="启用"; ist=1;
  }
  else {
    s="停用"; ist=0;
  }
  $.ajax({
    type: "POST",
    url: "omdev/smx.asp",
    data: "op=stx&sid="+isid+"&ser="+iser+"&st="+ist,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok"){
        $("#sid_st"+itr).html(s);
        $("a:contains('启用')").addClass("red");
        $("a:contains('停用')").removeClass("red");
        //qryPort();
      }
      else
        if (ist==0) alert("启用失败,端口已经使用!");
    }
  });
}

function sidDel(isid, itr){
  var s="确定要删除 "+$("#tr"+itr+" th:eq(1)").text()+" SID="+isid+" 吗?";
  if(!confirm(s)) return;
  $.ajax({
    type: "POST",
    url: "omdev/smx.asp",
    data: "op=del&sid="+isid,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok")
        $("#tr"+itr).remove();
      else
        alert("删除 "+$("#tr"+itr+" th:eq(0)").text()+" 编号"+itype+"-"+itag+" 失败!");
    }
  });
}

function sidAdd(){
 var tag1 = 0;
  for(var i = 0;i< jtype.reccnt;i++)
  {
    if(jtype.recs[i].Type==$("#lsType").val())
	{
	  tag1 = jtype.recs[i].Tag1;
	  break;
	}
  }
  $.ajax({
    type: "POST",
    url: "omdev/port.asp",
    data: "op=qryok&port=1",
    dataType:"json",
    success: function(msg){
      if(msg.qry=="ok" && msg.reccnt>0)
        location.href="C0-1x.asp?sid=0&type="+$("#lsType").val()+"&port=0"+"&tag1="+tag1;
      else
        alert("没有端口可用(已经全部使用)!");
    }
  });
}
</script>
</body>
</html>