<!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>
    <script type="text/javascript" src="js/omara.js"></script>

</head>
<body class="you_bd" style="width:95%">
<div >
    <div style="margin: 10px;margin-top: 15px; width:100%">
        <span>&nbsp;&nbsp;&nbsp;查询模式：
			<select id="IsQryMode" style="width:80px;" onchange="onChangeQryMode(this)">
			<option value="0">4小时</option>
			<option value="1">24小时</option>
			<option value="2">一周</option>
			<option value="3">一个月</option>
			<option value="4">一年</option>
			<option value="5">自定义</option>
			</select>
		</span>
		<span class="datesel" id="datesel" style="display:none"><span>&nbsp;&nbsp;&nbsp;<input type="text" id="date_3" value="" /></span>
        <span>&nbsp;--&nbsp;&nbsp;<input type="text" id="date_4" value="" /></span></span> 
        <!--<span>选择查询日期:</span> <span>开始 <input type="text" id="date_3" value="" /></span>
        <span>结束 <input type="text" id="date_4" value="" /></span>-->
        <span>&nbsp;&nbsp;&nbsp;监控设备：
			<select id="IsSID" style="width:180px;">
			</select></span>
        <span><input type="button" id="btn_qry" class="dateBtn2" value="查询"></span>
        <span>&nbsp;&nbsp;<input type="button" id="btn_save" value="保存到excel" onclick="javascript:saveToExcel('almList')"></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="aImg" 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 jname = <% db_QryAdv("select SID,Name,ASubSys from tbSM",0); %>
//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 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 mSMName = new Map();
var mSMSubSys = new Map();
$.each(jname.recs,function(i,n){
	mSMName.set(n.SID, n.Name); 
	mSMSubSys.set(n.SID, n.ASubSys)
});

listSM();
function listSM()
{
	var bshow = 0;
	var stbl = "";
	stbl+=" <option value=\"all\">全部</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)
			{
				stbl+="";
			}
			else if (n.SID==2)
			{
				stbl+="";
			}
			else
			{
				stbl+=" <option value=\""+(n.SID)+"\">"+n.Name+"</option>";
			}
		}
	});
    $("#IsSID").html(stbl);
}
var szShowData = "";
var iDataCount = 0;
function saveToExcel(tableid){
	if (iDataCount > 60000)
	{
        alert("查询的数据量过大，无法导出到Excel！\n当前数据量："+iDataCount+"条记录\n最大只能导出60000条记录");
        return false
	}
	$("#"+tableid).table2excel({
		exclude: ".noExl",
		name: "Excel Document Name",
		filename: "AlarmData" + new Date().toISOString().replace(/[\-\:\.]/g, "")+".xls",
		fileext: ".xls",
		exclude_img: true,
		exclude_links: true,
		exclude_inputs: true
	});
}
$(document).on("click",".almbtn",function(){
  var src=$(this).attr("op");
  $("#aImg").attr("src",src);
  $(".Almimg").fadeToggle();
});
$(".btn_img_guanbi").click(function(){
    $(".Almimg").fadeOut()
});

$("#date_3").datetimepicker({
  format:"Y-m-d H:i:00",      //格式化日期
});
$("#date_4").datetimepicker({
  format:"Y-m-d H:i:00",      //格式化日期
});
$.datetimepicker.setLocale('ch');

function onChangeQryMode(obj){
    var usel=document.getElementById('IsQryMode');
    var uselidx=usel.selectedIndex; //序号，取当前选中选项的序号
    var imode = usel.options[uselidx].value;
	if (imode==0)//<option value="0">4小时</option>
	{
		$("#datesel").hide();
	}
	else if (imode==1)//<option value="1">24小时</option>
	{
		$("#datesel").hide();
	}
	else if (imode==2)//<option value="2">一周</option>
	{
		$("#datesel").hide();
	}
	else if (imode==3)//<option value="3">一个月</option>
	{
		$("#datesel").hide();
	}
	else if (imode==4)//<option value="4">一年</option>
	{
		$("#datesel").hide();
	}
	else if (imode==5)//<option value="5">自定义</option>
	{
		$("#datesel").show();
	}
	return;
}

$(".dateBtn2").click(function(){
    index=1;
    var sDate;
    var eDate;
    var usel=document.getElementById('IsQryMode');
    var uselidx=usel.selectedIndex; //序号，取当前选中选项的序号
    var imode = usel.options[uselidx].value;
    var nRDate = new Date();
	var df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	if (imode==0)//<option value="0">4小时</option>
	{
		eDate=df.format(nRDate);
		nRDate.setHours(nRDate.getHours()-4);
		sDate=df.format(nRDate);
		//sDate=nRDate.getFullYear()+"-"+(nRDate.getMonth()+1)+"-"+nRDate.getDate()+" "+nRDate.getHours()+":"+nRDate.getMinutes()+":"+nRDate.getSeconds();
		//eDate=nRDate.getFullYear()+"-"+(nRDate.getMonth()+1)+"-"+nRDate.getDate()+" "+nRDate.getHours()+":"+nRDate.getMinutes()+":"+nRDate.getSeconds();
	}
	else if (imode==1)//<option value="1">24小时</option>
	{
		eDate=df.format(nRDate);
		nRDate.setDate(nRDate.getDate()-1);
		sDate=df.format(nRDate);
	}
	else if (imode==2)//<option value="2">一周</option>
	{
		eDate=df.format(nRDate);
		nRDate.setDate(nRDate.getDate()-7);
		sDate=df.format(nRDate);
	}
	else if (imode==3)//<option value="3">一个月</option>
	{
		eDate=df.format(nRDate);
		nRDate.setMonth(nRDate.getMonth()-1);
		sDate=df.format(nRDate);
	}
	else if (imode==4)//<option value="4">一年</option>
	{
		eDate=df.format(nRDate);
		nRDate.setFullYear(nRDate.getFullYear()-1);
		sDate=df.format(nRDate);
	}
	else if (imode==5)//<option value="5">自定义</option>
	{
		sDate=$("#date_3").val();
		eDate=$("#date_4").val();
	}
    dataarr=[];
    dataarr2=[];
    datatime=[];
    //var sDate=$("#date_3").val();
    //var eDate=$("#date_4").val();
	//alert("时间："+sDate+","+eDate);
    //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);
	//alert("时间差："+difftime);
    var result = (eRDate-sRDate)/(24*60*60*1000);
    //if(result>7){
    //    alert("最多只能查询7天以内连续区间的数据！");
    //    return false
    //}
    if(sDate==""||eDate==""||sDate>eDate){
        alert("请输入正确的查询时间！");
        return false
    }
    var usel=document.getElementById('IsSID');
    var uselidx=usel.selectedIndex; //序号，取当前选中选项的序号
    var isid = usel.options[uselidx].value;
	
	szShowData = "";
	iDataCount = 0;
	{
		szShowData+="<div style='overflow: auto;height: 500px;'>";
		szShowData+="<table id='almList' width='100%' style='border: #B0B7BF 1px solid'>";
		szShowData+="<tr><td style='width: 200px;'>报警时间</td><td style='width: 100px;'>报警原因</td><td style='width: 100px;'>报警方式</td><td style='width: 100px;'>报警对象</td><td style='width: 100px;'>类别</td><td style='width: 200px;'>报警描述</td><td style='width: 150px;'>设备名称</td><td style='width: 50px;'>AID</td><td style='width: 40px;'>截图</td></tr>";
	}
	$(".lond").show();
	//if (difftime < 1)
	{
		$.ajax({
			type: "POST",
			url: "../omdev/smalarm-t.asp",
			data: "starttime="+sDate+"&endtime="+eDate+"&isid="+isid,
			dataType: "json",
			//async: false,
			success: function(msg){
				showData(msg);
			}
		});
	}
    $.ajax({
        type: "POST",
        url: "../omdev/smalarm.asp",
        data: "starttime="+sDate+"&endtime="+eDate+"&isid="+isid,
        dataType: "json",
		//async: false,
        beforeSend: function() {
            $(".dateBtn").attr('disabled', 'disabled');
            $(".lond").show();
        },
        complete: function() {
            $(".lond").hide();
            $(".dateBtn").removeAttr("disabled");
			$("#container3").html(szShowData);
        },
        success: function(msg){
			showData(msg);
        }
    });
});

function showData(data)
{
	var q='';
	if (data.qry == "err")
		return;
	iDataCount += data.reccnt;
	$.each(data.recs,function(i,n){
		var name='未知';var note='告警';var mask='';var recv='';
		var kind=(n.Type>>8);
		
		if(n.Type > 4096 && n.Type < 5631)
		{
			kind = mSMSubSys.get(n.SID);
		}
		
		if(kind==0x01) name="市电";
		else if(kind==0x02) name="配电";
		else if(kind==0x03) name="UPS";
		else if(kind==0x04) name="蓄电池";
		else if(kind==0x05) name="温湿度";
		else if(kind==0x06) name="漏水";
		else if(kind==0x07) name="空调";
		else if(kind==0x08) name="精密空调";
		else if(kind==0x09) name="开关量";
		else if(kind==0x0a) name="报警器";
		else if(kind==0x0b) name="显示屏";
		else if(kind==0x0c) name="相机视频";
		else if(kind==0x0d) name="IC门禁";
		else if(n.Type<16){
			if(n.Type==6) name="漏水检测";
			else if(n.Type==9) name="烟感";
			else if(n.Type==10) name="红外";
			else if(n.Type==11) name="门磁";
		}
		if(n.Alm&4){note+=" 解除"}
		if(n.Alm&8){note+=" 重发"}
		if(n.Alm&16){note+=" 故障"}
		if(n.Alm&0xf00){note+=" 未发出"}
		if(n.AlmMask&1){mask+=" 声光"}
		if(n.AlmMask&2){mask+=" 语音"}
		if(n.AlmMask&4){mask+=" 短信"}
		if(n.AlmMask&8){mask+=" 彩信"}
		if(n.AlmMask&0x10){mask+=" 电话"}
		if(n.AlmMask&0x20){mask+=" 邮件"}
		if(n.AlmMask&0x40){mask+=" POST"}
		if(n.AlmMask&0x100){mask+=" 联动1"}
		if(n.AlmMask&0x200){mask+=" 联动2"}
		for (var x=0; x<16; x++)
			if ((n.AlmRecv & (1 << x))>0) recv+=" #"+(x+1);
		if(n.AlmMask&0x40){mask+=" POST"}
		//if(i==0){
		//	q+="<div style='overflow: auto;height: 500px;'>";
		//	q+="<table id='almList' width='100%' style='border: #B0B7BF 1px solid'>";
		//	q+="<tr><td style='width: 200px;'>报警时间</td><td style='width: 100px;'>报警原因</td><td style='width: 100px;'>报警方式</td><td style='width: 100px;'>报警对象</td><td style='width: 100px;'>类别</td><td style='width: 200px;'>报警描述</td><td style='width: 150px;'>设备名称</td><td style='width: 50px;'>AID</td><td style='width: 40px;'>截图</td></tr>";
		//}
		if(n.AID<0) {
		  note="简报";
		  name="系统";
		  n.AlmBrief="定时简报";
		}
		q+="<tr><td>"+n.LastUpd+"</td><td>"+note+"</td><td>"+mask+"</td><td>"+recv+"</td><td>"+name+"</td><td>"+n.AlmBrief+"</td><td>"+mSMName.get(n.SID)+"</td><td>"+n.AID+"</td><td>";
		if( n.AlmImg!=undefined && n.AlmImg.length>2 ){
			var istart=n.AlmImg.indexOf('wwwpub');
			if (istart>=0) n.AlmImg=n.AlmImg.substring(istart+7);
			q+="<span><img src='img/ico_img.png' op='"+ n.AlmImg+"' class='almbtn'></span>";
		}
		q+="</td></tr>";
	});
	szShowData += q;
}

</script>
</body>
</html>