<!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>&nbsp;&nbsp;<b><span id="anote">xxx</span></b>
  </div>
  <div style='border: #B0B7BF 1px solid'>
    <table id='tbDef' width='100%'>
    <tr bgcolor='#E5E9EC'><th><span>字段</span></th><th>描述</th><th>值模式</th><th>系数</th><th>值1</th><th>值2</th><th>提示1</th><th>提示2</th><th>操作</th></tr>
    </table>
  </div>
</div>
<div>
  <table width='100%'><tr>
    <th><a href='javascript:history.back()' class='back' style='color: #fff'>返回</a></th>
  </tr></table>
</div>

<script>
var itype=<% write(type); %>;

<% db_Open("/NandFlash/dbCfg.db3"); %>
var jtype=<% db_QryAdv("select Type,Tag1,t.Name,k.Name as KName from tbSMType t left join tbSMKind k on k.Kind=t.Kind where Type="+type,0); %>;
var jdef=<% db_QryAdv("select * from tbSMDef where Type="+type+" order by FldDept,Fld",0); %>;
<% db_Close(); %>

$(function(){
  var sv="";
  $.each(jtype.recs,function(i,n){
    if(i==0) sv+=n.KName+" -";
    if(i<2) sv+=" "+n.Name;
  })
  if(jtype.reccnt>2) sv+=" ...";
  $("#anote").html(sv);
  listDef();
});

function listDef(){
  if (jdef.reccnt==0){
    return;
  }
  var stbl="<tr bgcolor='#E5E9EC'><th><span>字段</span></th><th>描述</th><th>系数</th><th>值模式</th><th>值1</th><th>值2</th><th>提示1</th><th>提示2</th><th>操作</th></tr>";
  $.each(jdef.recs,function(i,n){
    if(n.Fld==0){
    stbl+="<tr bgcolor='#E5E9EC''><th></th><th>"+n.Note+"</th><th>"+n.vkind+"</th><th></th><th></th><th></th><th></th><th></th><th></th></tr>";
    }
    else{
    stbl+="<tr'><th>"+n.Fld+"</th><th><input id='dnote"+n.Fld+"' style='width:100px' value='"+n.Note;
    stbl+="'/></th><th><input id='dfac"+n.Fld+"' style='width:40px' value='"+n.vfactor
    stbl+="'/></th><th><select id='dmode"+n.Fld+"'><option value='-1'>不做告警参数<option value='0'>不检测<option value='1'>等于<option value='2'>不等于<option value='3'>大于<option value='4'>小于<option value='10'>双值 不检测<option value='11'>双值 范围内<option value='12'>双值 范围外";
    stbl+="</select></th><th><input id='dref"+n.Fld+"' style='width:60px' value='"+n.vref
    stbl+="'/></th><th><input id='drefb"+n.Fld+"' style='width:60px' value='"+n.vref2
    stbl+="'/></th><th><input id='dtip"+n.Fld+"' style='width:120px' value='"+n.vtip;
    stbl+="'></th><th><input id='dtipb"+n.Fld+"' style='width:120px' value='"+n.vtip2;
    stbl+="'/></th><th>"+getOps(n)+"&nbsp;&nbsp;"+getDeleteOps(n)+"</th><tr>";
    }
  })
    stbl+="<tr'><th><input id='addfld' style='width:50px' value='";
    stbl+="'/></th><th><input id='adddnote' style='width:100px' value='";
    stbl+="'/></th><th><input id='adddfac' style='width:40px' value='";
    stbl+="'/></th><th><select id='adddmode'><option value='-1'>不做告警参数<option value='0'>不检测<option value='1'>等于<option value='2'>不等于<option value='3'>大于<option value='4'>小于<option value='10'>双值 不检测<option value='11'>双值 范围内<option value='12'>双值 范围外";
    stbl+="</select></th><th><input id='adddref' style='width:60px' value='";
    stbl+="'/></th><th><input id='adddrefb' style='width:60px' value='";
    stbl+="'/></th><th><input id='adddtip' style='width:120px' value='";
    stbl+="'></th><th><input id='adddtipb' style='width:120px' value='";
    stbl+="'/></th><th><a href='javascript:addFld()'>增加</a></th><tr>";
  $("#tbDef").html(stbl);
  $.each(jdef.recs,function(i,n){
    if(n.Fld>0) $("#dmode"+n.Fld).val(n.vmode);
  })
}
function getOps(n){
  sv="<a href='javascript:saveFld("+n.Fld+")'>更新</a>";
  return sv;
}
function getDeleteOps(n){
  sv="<a href='javascript:deleteFld("+n.Fld+")'>删除</a>";
  return sv;
}

function addFld(){
  var ifld = $("#addfld").attr("value");
  if(ifld=="")
  {
	alert("告警定义项字段不能为空!");
	return;
  }
  ifld = parseInt($("#addfld").attr("value"));
  if((ifld==0) || isNaN(ifld))
  {
	alert("告警定义项字段只能为非0数字!");
	return;
  }
  var sfac=$("#adddfac").attr("value");
  if(sfac=="") sfac="1";
  var sref=$("#adddref").attr("value");
  if(sref=="") sref="0";
  var sref2=$("#adddrefb").attr("value");
  if(sref2=="") sref2="0";
  var spost="";
  spost+="&fld="+ifld;
  spost+="&Note='"+$("#adddnote").attr("value");
  spost+="'&vfactor="+sfac;
  spost+="&vmode="+$("#adddmode").val();
  spost+="&vref="+sref;
  spost+="&vref2="+sref2;
  spost+="&vtip='"+$("#adddtip").attr("value");
  spost+="'&vtip2='"+$("#adddtipb").attr("value")+"'";
  $.ajax({
    type: "POST",
    url: "omdev/smdef.asp",
    data: "op=addfld&type="+itype+spost,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok")
	  {
        alert("增加定义项 "+itype+"-"+ifld+" 成功!");
		window.location.reload();
	  }
      else
	  {
        alert("增加定义项 "+itype+"-"+ifld+" 失败!");
	  }
    }
  });
}

function deleteFld(ifld){
  var s="确定要删除 编号"+itype+"-"+ifld+" 吗?";
  if(!confirm(s)) return;

  $.ajax({
    type: "POST",
    url: "omdev/smdef.asp",
    data: "op=delfld&type="+itype+"&fld="+ifld,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok")
	  {
        alert("删除定义项 "+itype+"-"+ifld+" 成功!");
		window.location.reload();
	  }
      else
	  {
        alert("删除定义项 "+itype+"-"+ifld+" 失败!");
	  }
    }
  });
}
function saveFld(ifld){
  var sfac=$("#dfac"+ifld).attr("value");
  if(sfac=="") sfac="1";
  var sref=$("#dref"+ifld).attr("value");
  if(sref=="") sref="0";
  var sref2=$("#drefb"+ifld).attr("value");
  if(sref2=="") sref2="0";
  var spost="";
  spost+="&setval=Note='"+$("#dnote"+ifld).attr("value");
  spost+="',vfactor="+sfac;
  spost+=",vmode="+$("#dmode"+ifld).val();
  spost+=",vref="+sref;
  spost+=",vref2="+sref2;
  spost+=",vtip='"+$("#dtip"+ifld).attr("value");
  spost+="',vtip2='"+$("#dtipb"+ifld).attr("value")+"'";
  $.ajax({
    type: "POST",
    url: "omdev/smdef.asp",
    data: "op=upd2&type="+itype+"&fld="+ifld+spost,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok")
        alert("保存定义项 "+itype+"-"+ifld+" 成功!");
      else
        alert("保存定义项 "+itype+"-"+ifld+" 失败!");
    }
  });
}
</script>
</body>
</html>