<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="GB2312">
    <title></title>
    <link href="css/style.css" title="style" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
    <script src="js/jquery-3.3.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/jquery.datetimepicker.min.css" />
    <script type="text/javascript" src="js/jquery.datetimepicker.full.min.js"></script>
    <script type="text/javascript" src="js/jquery.table2excel.js"></script>

</head>
<body style="background: #F3F3F3">
<div >
    <div style="margin: 10px;margin-top: 15px; width:100%">
        <span></span> <span>&nbsp;&nbsp;&nbsp;开始：<input type="text" id="date_3" value="" /></span>
        <span>&nbsp;&nbsp;结束：<input type="text" id="date_4" value="" /></span>
        <span>&nbsp;&nbsp;&nbsp;监控设备：
			<select id="IsSID" style="width:150px;" onchange="onchangeSID(this)">
			</select>
		</span><span>&nbsp;&nbsp;&nbsp;监控项：
			<select id="IsTestPoint" style="width:150px;">
			</select>
		</span>
        <span><input type="button" id="btn_qry" class="dateBtn2" value="查询"></input></span>
        <span>&nbsp;&nbsp;<input type="button" id="btn_save" value="保存到excel" onclick="javascript:saveToExcel('almList')"></input></span>
		<br>
		<!--<span style="color:red">实时查询只能查询一小时以内的数据，历史查询只能查询一小时以后的数据</span>-->
    </div>
    <span class="lond" id="lond" style="text-align: center;margin-left: 200px;display:none" ><img  src="img/loding.gif" ><span>正在查询数据库....</span></span>
    <div id="container3" style="width:100%;min-height: 500px;overflow: auto"></div>
    <div style="position: fixed;left: 30px;top: 50px;" class="Almimg hide">
        <img src="img/ico_guanbi.png" class="btn_img_guanbi" style="position: absolute;right: 5px;top: 5px;cursor:pointer">
        <img id="AlmImg" src="img/loginBG.jpg" width="640" height="480" style="box-shadow: 8px 5px 5px #888888;">
    </div>
</div>
<a href='javascript:history.back()' class='back' style='color: #fff'>返回</a>
<script>

<% db_Open("/NandFlash/dbCfg.db3"); %>
//var jtypes=<% db_QryAdv("select sm.Type,smt.Name, smt.Note from tbSM sm left join tbSMType smt on smt.Type=sm.Type where sm.State=0 and sm.DataMode=1",1); %>;
var jsids=<% db_QryAdv("select * from tbSM where State=0 and DataMode=1 and SerDef<>'' order by SID",1); %>;
var jdis=<% db_QryAdv("select s.*,k.Name as KName,t.Note as TNote,t.Name as TName,t.Port from tbSM s left join tbSMKind k on k.Kind=s.Kind left join tbSMType t on (t.Type=s.Type and t.Tag1=s.Tag1) where Port=1 and State=0 and DataMode=1",1); %>;
<% db_Close(); %>

var jsCfg = "";
$(function(){
	listSM();
	$("#date_3").datetimepicker({
      format:"Y-m-d H:m:s",      //格式化日期
	});
	$("#date_4").datetimepicker({
      format:"Y-m-d H:m:s",      //格式化日期
	});
	$.datetimepicker.setLocale('ch');
})

function listSM()
{
	var bshow = 0;
	var stbl = "";
	stbl+=" <option value=\"0\">请选择监控设备</option>";
	$.each(jsids.recs,function(i,n){
		if (n.Kind==2560 || n.Kind==2816 || n.Kind==3072)
			stbl+="";//" <option value=\""+((n.Type<<16)+(n.Tag1<<8)+n.SID)+"\">"+n.Name+"</option>";
		else
		{
			if (n.SID==1)
			{
				bshow = 0;
				$.each(jdis.recs,function(i,m){
					if (m.SerKind == 0x800000)
					{
						bshow = 1;
						return;
					}
				});
				if (bshow)
				{
					n.Name = "内置开关量";
					stbl+=" <option value=\""+((n.Type<<16)+(n.Tag1<<8)+n.SID)+"\">"+n.Name+"</option>";
				}
			}
			else if (n.SID==2)
			{
				bshow = 0;
				$.each(jdis.recs,function(i,m){
					if (m.SerKind == (n.SID<<8))
					{
						bshow = 1;
						return;
					}
				});
				if (bshow)
				{
					n.Name = "内置模拟量";
					stbl+=" <option value=\""+((n.Type<<16)+(n.Tag1<<8)+n.SID)+"\">"+n.Name+"</option>";
				}
			}
			else
			{
				if (n.Type==1280)
					stbl+=" <option value=\""+((n.Type<<16)+(n.Tag1<<8)+n.SID)+"\">"+n.Name+"</option>";
				else
					stbl+="";
			}
		}
	});
    $("#IsSID").html(stbl);
}

function saveToExcel(tableid){
    var usel=document.getElementById('IsSID');
    var uselidx=usel.selectedIndex; //序号，取当前选中选项的序号
    var sname = usel.options[uselidx].text;
    usel=document.getElementById('IsTestPoint');
    uselidx=usel.selectedIndex; //序号，取当前选中选项的序号
    var sTestPoint = usel.options[uselidx].text;
	$("#"+tableid).table2excel({
		exclude: ".noExl",
		name: "Excel Document Name",
		filename: sname + "_"+sTestPoint+ "_HistoryData_" + new Date().toISOString().replace(/[\-\:\.]/g, ""),
		fileext: ".xls",
		exclude_img: true,
		exclude_links: true,
		exclude_inputs: true
	});
}

function onchangeSID(obj){
    var usel=document.getElementById('IsSID');
    var uselidx=usel.selectedIndex; //序号，取当前选中选项的序号
    var sid = usel.options[uselidx].value % 256;
	var itype = (usel.options[uselidx].value >> 16) & 0xffff;
	var itag1 = (usel.options[uselidx].value >> 8) & 0xff;
	
	jsCfg = "";
	var url = "../jsn/A3-"+itype+".js"
	if (((itype==256) && ((itag1==30) || (itag1==35))) || (itype==1792) || (itype==2317) || (itype==2320))
		url = "../jsn/A3-"+itype+"-"+itag1+".js"
	$.ajax({
		type: "POST",
		url: url,
		async: false,
		dataType: "text",
		error: function(e) {
			console.log("Get ../jsn/A3-"+itype+".js error");
		},
		complete: function(){
			//console.log("Get ../jsn/A3-"+itype+".js complete");
		},
		success: function(msg){
			jsCfg=JSON.parse(msg);
			console.log("Get ../jsn/A3-"+itype+".js success");
			//console.log("Js Conf: "+"\""+msg+"\"");
			//console.log("Js Conf: "+JSON.parse(msg));
		}
	});
	
	if (jsCfg=="")
	{
        alert("数据未支持，请检查数据配置是否正确！");
		$("#IsTestPoint").html("");
		return;
	}
	
	var idx = 0;
	var stbl = "";
	if (((itype&0xff00)==2304))
	{
		if (itype==2304)
		{
			$.each(jdis.recs,function(i,n){
				if (n.SerKind == 0x800000)
				{
					var ivx = n.SerType - n.SerKind;
					var key = "D0"+ivx;
					if (ivx >= 10)
						key = "D"+ivx;
						stbl+=" <option value=\""+key+"\">"+n.Name+"</option>";
				}
			});
		}
		else 
		{
			$.each(jdis.recs,function(i,n){
				if (n.SerKind == (sid<<8))
				{
					var ivx = n.SerType - n.SerKind;
					var key = "D0"+ivx;
					if (ivx >= 10)
						key = "D"+ivx;
					stbl+=" <option value=\""+key+"\">"+n.Name+"</option>";
				}
			});
		}
	}
	else
	{
		stbl+=" <option value=\"\">全部</option>";
		for (var key in jsCfg)
		{
			if (idx >= 2)
				stbl+=" <option value=\""+key+"\">"+jsCfg[key].title+"</option>";
			idx++;
		}
	}
	$("#IsTestPoint").html(stbl);
}

$(".almbtn").on("click",function(){
  var src=$(this).attr("op");
  $("#AlmImg").attr("src",src);
  $(".Almimg").fadeToggle();
});
$(".btn_img_guanbi").click(function(){
    $(".Almimg").fadeOut()
});

var szShowData = "";
var TestPoint="";
$(".dateBtn2").click(function(){
    index=1;
    var sDate=$("#date_3").val();
    var eDate=$("#date_4").val();
    //var sArr = sDate.split("-");
    //var eArr = eDate.split("-");
    //var sRDate = new Date(sArr[0], sArr[1], sArr[2]);
    //var eRDate = new Date(eArr[0], eArr[1], eArr[2]);
    var sRDate = new Date(sDate);
    var eRDate = new Date(eDate);
    var nRDate = new Date();
    var difftime = (nRDate-eRDate)/(60*60*1000);
    var result = (eRDate-sRDate)/(24*60*60*1000);
    if(result>7){
        alert("最多只能查询7天以内连续区间的数据！");
        return false
    }
    if(sDate==""||eDate==""||sDate>eDate){
        alert("请输入正确的查询时间！");
        return false
    }
	var op="qry";
    var usel=document.getElementById('IsSID');
    var uselidx=usel.selectedIndex; //序号，取当前选中选项的序号
    var sname = usel.options[uselidx].text;//名称
    var sid = usel.options[uselidx].value % 256;
	var itype = (usel.options[uselidx].value >> 16) & 0xffff;
	var itag1 = (usel.options[uselidx].value >> 8) & 0xff;
	
    usel=document.getElementById('IsTestPoint');
    uselidx=usel.selectedIndex; //序号，取当前选中选项的序号
    var sTestPoint = usel.options[uselidx].text;//测点
	
    if(sid<=0){
        alert("请选择传感器！");
        return false
    }

	if (jsCfg=="")
	{
        alert("数据未支持，请检查数据配置是否正确！");
        return false
	}
	
	TestPoint=$("#IsTestPoint").val();
	
	szShowData = "";
	szShowData+="<div style='overflow: auto;height: 500px;'>";
	szShowData+="<table id='almList' width='100%' style='border: #B0B7BF 1px solid'>";
	szShowData+="<tr>";
	
	szShowData += "<td>监控设备</td>";
	if (TestPoint=="")
	{
		for (var key in jsCfg)
		{
			szShowData += "<td>"+jsCfg[key].title+"</td>";
		}
	}
	else
	{
		var idx = 0;
		for (var key in jsCfg)
		{
			idx++;
			szShowData += "<td>"+jsCfg[key].title+"</td>";
			if (idx >= 2)
				break;
		}
		if (((itype&0xff00)==2304))
		{
			//szShowData += "<td>"+$("#IsTestPoint option:selected").text();+"</td>"
		}
		else
		{
			szShowData += "<td>"+jsCfg[TestPoint].title+"</td>";
		}
	}
	
	$(".lond").show();
	if (difftime < 1)
	{
		$.ajax({
			type: "POST",
			url: "../omdev/smdat-t.asp",
			data: "op="+op+"&imode=0"+"&starttime="+sDate+"&endtime="+eDate+"&itype="+itype+"&sid="+sid+"&TestPoint="+TestPoint,
			dataType: "json",
			async: false,
			beforeSend: function() {
				$(".dateBtn2").attr('disabled', 'disabled');
				//$(".lond").show();
				$("#container3").html("");
			},
			success: function(msg){
				$(".dateBtn2").removeAttr("disabled");
				//$(".lond").hide();
				showHistryData(jsCfg, msg);
			}
		});
	}
	
    $.ajax({
        type: "POST",
        url: "../omdev/smdat.asp",
        data: "op="+op+"&imode=0"+"&starttime="+sDate+"&endtime="+eDate+"&itype="+itype+"&sid="+sid+"&TestPoint="+TestPoint,
        dataType: "json",
		async: false,
        beforeSend: function() {
            $(".dateBtn2").attr('disabled', 'disabled');
        },
        complete: function() {
            //$(".lond").hide();
            $(".dateBtn2").removeAttr("disabled");
        },
		error: function(e) { 
            //$(".lond").hide();
            $(".dateBtn2").removeAttr("disabled");
			alert("查询失败！"); 
            $("#container3").html("");
		},
        success: function(msg){
            $(".dateBtn2").removeAttr("disabled");
            showHistryData(jsCfg, msg);
        }
    });
    $(".lond").hide();
});

function showHistryData(jsCfg, data)
{
	var q='';
	var val='';
	var stbl = '';
	var idx = 0;
    var usel=document.getElementById('IsSID');
    var uselidx=usel.selectedIndex; //序号，取当前选中选项的序号
    var sname = usel.options[uselidx].text;//名称
    var sid = usel.options[uselidx].value % 256;
	var itype = (usel.options[uselidx].value >> 16) & 0xffff;
	var itag1 = (usel.options[uselidx].value >> 8) & 0xff;
	
    usel=document.getElementById('IsTestPoint');
    uselidx=usel.selectedIndex; //序号，取当前选中选项的序号
    var sTestPoint = usel.options[uselidx].text;//测点
	
	$.each(data.recs,function(i,n){
		q = "<tr>";
		idx = 0;
		if (TestPoint=="")
		{
			q += "<td>"+sname+"</td>";
			for (var key in jsCfg)
			{
				if (jsCfg[key].vType==0)
					val = n[key] / jsCfg[key].vFactor + jsCfg[key].unit;
				else if (jsCfg[key].vType==1)
					val = jsCfg[key].vStatus[ n[key] ];
				else //if (jsCfg[key].vType==2)
					val = n[key];
				q += "<td>"+val+"</td>";
				idx++;
			}
		}
		else
		{
			var idx = 0;
			if (((itype&0xff00)==2304))
			{
				q += "<td>"+sTestPoint+"</td>";
				for (var key in jsCfg)
				{
					idx++;
					if (jsCfg[key].vType==0)
						val = n[key] / jsCfg[key].vFactor + jsCfg[key].unit;
					else if (jsCfg[key].vType==1)
						val = jsCfg[key].vStatus[ n[key] ];
					else //if (jsCfg[key].vType==2)
						val = n[key];
					q += "<td>"+val+"</td>";
					if (idx >= 1)
						break;
				}
			}
			else
			{
				q += "<td>"+sname+"</td>";
				for (var key in jsCfg)
				{
					idx++;
					if (jsCfg[key].vType==0)
						val = n[key] / jsCfg[key].vFactor + jsCfg[key].unit;
					else if (jsCfg[key].vType==1)
						val = jsCfg[key].vStatus[ n[key] ];
					else //if (jsCfg[key].vType==2)
						val = n[key];
					q += "<td>"+val+"</td>";
					if (idx >= 2)
						break;
				}
			}
			if (jsCfg[TestPoint].vType==0)
				val = n[TestPoint] / jsCfg[TestPoint].vFactor + jsCfg[TestPoint].unit;
			else if (jsCfg[TestPoint].vType==1)
				val = jsCfg[TestPoint].vStatus[ n[TestPoint] ];
			else //if (jsCfg[TestPoint].vType==2)
				val = n[TestPoint];
			q += "<td>"+val+"</td>";
		}
		q += "</tr>";
		if(i==0){
			stbl+="</tr>";
		}
		stbl += q;
	});
	szShowData += stbl;
	$("#container3").html(szShowData);
}

function onqrymodechange(obj)
{
  var iv=$("#qrymode").val();
  if (iv != 0)//按时间段
  {
	listSMType();
  }
  else//按前端点位
  {
	listSM()
  }
}
</script>
</body>
</html>
