var allData;
$(document).ready(function() {
    //初始化区域
    $.ajax({
        type : "POST",
        url : "/pqs9200/user/getAreasInfo",
        dataType : "json",
        success : function(info) {
            if (info != null) {
                for (var i = 0; i < info.length; i++) {
                    $("#area").append(
                        "<option value='"+info[i].dicIndex+"'>"
                        + info[i].city + "</option>");
                }
                //初始化页面内容
                var area = $("#area").val();
                var startTime = $("#startTime").val();
                var endTime = $("#endTime").val();
                refresh(area, startTime, endTime);
            }
        }
    });
});


$("#query").click(function(){
    var area = $("#area").val();
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    refresh(area, startTime, endTime);
});

function refresh(area, startTime, endTime){
    var i ;
    //初始化区域
    $.ajax({
        type : "POST",
        url : "/pqs9200/area/queryLevel",
        data:{
            area:area,
            startTime:startTime,
            endTime:endTime
        },
        dataType:"json",
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success : function(data) {
            if(data.code===500){
                allData=null;
            }else{
                allData=data.body;
            }
            startQuery()
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    });
}
var totalEvents=0;
//初始化区域内容
function initArea(areaList){
    if(areaList==null){
        areaList=[];
    }else{
        //计算总计
        var name='总计';
        var value=0;
        var monitor=0;
        var node='/';
        for(var i=0;i<areaList.length;i++){
            value=Number(areaList[i].value)+value;
            monitor=Number(areaList[i].countAll)+monitor
        }
        var area={};
        area.name=name;
        area.value=value;
        area.countAll=monitor;
        totalEvents=value;
        area.node=node;
        areaList.push(area);
        areaList.reverse();
    }
    $("#areaTable").DataTable({
        data: areaList,
        autoWidth:false,
        order:[1,"desc"],
        columnDefs : [ {
            orderable : false,
            targets : [0]
        } ,
            { "width": "25%", "targets": 0 },
            { "width": "25%", "targets": 1 },
            { "width": "25%", "targets": 2 },
            { "width": "25%", "targets": 3 }
        ],
        paging:true,
        lengthChange: false,
        searching:false,
        destroy : true,
        pageLength:10,
        language : {
            url:'/pqs9200/json/chine.json',
        },
        columns: [
            { data: 'name' },
            { data: 'countAll' },
            { data: 'value'},
            { data: 'node'},
        ],
    })
}
//初始化变电站内容
function initSub(subList){
    if(subList==null){
        subList=[];
    }else{
        //计算总计
        var name='总计';
        var value=0;
        for(var i=0;i<subList.length;i++){
            value=Number(subList[i].value)+value;
        }
        var sub={};
        sub.name=name;
        sub.value=value;
        subList.push(sub);
        subList.reverse();
    }
    $("#subTable").DataTable({
        data: subList,
        autoWidth:false,
        columnDefs : [
            { "width": "50%", "targets": 0 },
            {
                orderable : false,
                targets : [0]
            }
        ],
        order:[1,"desc"],
        paging:true,
        lengthChange: false,
        searching:false,
        destroy : true,
        pageLength:10,
        language : {
            url:'/pqs9200/json/chine.json',
        },
        columns: [
            { data: 'name' },
            { data: 'value'}
        ],
    })
}
//初始化监测点内容
function initMonitor(monitorList){
    if(monitorList==null){
        monitorList=[];
    }else{
        var name='总计';
        var monitor={};
        monitor.name=name;
        monitor.value=totalEvents;
        monitorList.push(monitor);
        monitorList.reverse();
    }
    $("#monitorTable").DataTable({
        data: monitorList,
        autoWidth:false,
        columnDefs : [
            { "width": "50%", "targets": 0 },
            {
                orderable : false,
                targets : [0]
            }
        ],
        order:[1,"desc"],
        paging:true,
        lengthChange: false,
        searching:false,
        destroy : true,
        pageLength:10,
        language : {
            url:'/pqs9200/json/chine.json',
        },
        columns: [
            { data: 'name' },
            { data: 'value'}
        ],
    })
}

//特殊处理，表头紧缩问题--start
var it = null;

function startQuery() {
    //实时刷新时间单位为毫秒
    it = setInterval('refreshQuery()', 100);

}

/* 刷新查询 */
function refreshQuery() {
    var width = $("#areaTable").width();
    if (width > 100) {
        if (allData===500) {
            initArea(null);
            initSub(null);
            initMonitor(null);
        } else {
            initArea(allData.areaList);
            initSub(allData.subList);
            initMonitor(allData.monitorList);
        }
        window.clearInterval(it)
    }
}
//特殊处理，表头紧缩问题--end

//点击导出报表数据
$("#exportData").on("click",function () {
    var area = $("#area").val();
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var name = $("#area").find("option:selected").text();
    var xhr = new XMLHttpRequest();
    xhr.open('post', '/pqs9200/area/downloadLevelData', true);
    xhr.responseType = 'blob';
    xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
    var commontParamter={};
    commontParamter.area=area;
    commontParamter.startTime=startTime;
    commontParamter.endTime=endTime;
    var i = ityzl_SHOW_LOAD_LAYER();
    xhr.send(JSON.stringify(commontParamter))
    xhr.onload = function () {
        if (this.status === 200) {
            var blob = this.response;
            var a = document.createElement('a');
            var url = window.URL.createObjectURL(blob);
            a.href = url;
            //设置文件名称
            a.download = name+'严重度统计_'+startTime+'_'+endTime+'.xlsx';
            a.click();
            a.remove();
        }
        //***********关闭loading
        ityzl_CLOSE_LOAD_LAYER(i);
    }

})