//根据完整率获取颜色
function getColor(data) {
    if (data>=90){
        return green;
    }else if (data<60){
        return red;
    }else{
        return yellow;
    }
}
/**
 * 数据缺失情况
 */
function load() {
    var i ;
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    $.ajax({
        method : "post",
        url : "/pqs9900/dataloss/getDataLoss",
        data : {
            startTime:startTime,
            endTime:endTime
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        error:function(data) {
            layer.msg("暂无数据", {
                icon : 5,
                time : 3000
            });
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        },
        success : function(data) {
            if (data.code == 500) {
                //没有数据
                initTable(null);
            } else {
                initTable(data.body);
            }
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    });
}
$(document).ready(function() {
    load();
});

$("#query").click(function(){
    load();
});
function getMyDate(str){
    var oDate = new Date(str),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth()+1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getSeconds(),
        oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay);//最后拼接时间
    return oTime;
};
//补0操作
function getzf(num){
    if(parseInt(num) < 10){
        num = '0'+num;
    }
    return num;
}
function initTable(data){
    var ide;
    var height=$(window).height();
    var heightTemp=screen.height;
    var length;
    if(heightTemp===768){
        length=3;
    }else if(heightTemp<=733){
        length=2;
    }else if(heightTemp===1050){
        length = 6;
    }
    else{
        length=4;
    }
    var p=$("#roleJ").val();
    $('#vdlist').dataTable().fnClearTable();
    if(p=="Npro"){
        var table=$('#vdlist').DataTable({
            "scrollY": "200px",
            destroy : true,
            data : data,
            paging: false,
            alengthMenu:false,
            language : {
                url:'/pqs9900/json/chine.json'
            },
            columnDefs:
                [ {
                    "targets": [ 1 ], //隐藏第1列，从第0列开始
                    "visible": false
                } ],
            createdRow : function(row, data, index) {
                if (data[7] == "警告") {
                    $('td', row).css('color', '#696969')
                } else if (data[7] == "注意") {
                    $('td', row).css('color', '#FFD700')
                } else if(data[7]=="合格"){
                    $('td', row).css('color', '#2E8B57')
                }
            },
        });
        table.on('order.dt search.dt',
            function() {
                table.column(0, {
                    search: 'applied',
                    order: 'applied'
                }).nodes().each(function(cell, i) {
                    cell.innerHTML = i + 1;
                });
            }).draw();
        new $.fn.dataTable.Buttons(table, {
            buttons: [
                {
                    extend: 'csv',//导出格式
                    title:'数据库列表',//导出csv名称
                    exportOptions:{
                        columns :[0,1,2,3,4,5],//导出需要的列
                        format:{
                            body:function(data){//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                                return data;
                            }
                        }
                    }
                }
            ]
        } );
        modifyCss(table);
    }else{
        var table=$('#vdlist').DataTable({
            destroy : true,
            scrollY: 120,
            scroller: {
                loadingIndicator: true,
            },
            data : data,
            paging: false,
            info:false,
            language : {
                url:'/pqs9900/json/chine.json'
            },
            columnDefs:
                [ {
                    "targets": [ 1 ], //隐藏第1列，从第0列开始
                    "visible": false
                } ],
            createdRow : function(row, data, index) {
                if (data[7] == "警告") {
                    $('td', row).css('color',red)

                } else if (data[7] == "注意") {
                    $('td', row).css('color', yellow)
                } else if(data[7]=="合格"){
                    $('td', row).css('color', green)
                }
            },
        });
        table.on('order.dt search.dt',
            function() {
                table.column(0, {
                    search: 'applied',
                    order: 'applied'
                }).nodes().each(function(cell, i) {
                    cell.innerHTML = i + 1;
                });
            }).draw();
        new $.fn.dataTable.Buttons(table, {
            buttons: [
                {
                    extend: 'csv',//导出格式
                    title:'数据库列表',//导出csv名称
                    exportOptions:{
                        columns :[0,1,2,3,4,5,6,7,8,9,10,11,12],//导出需要的列
                        format:{
                            body:function(data){//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                                return data;
                            }
                        }
                    }
                }
            ]
        } );
        modifyCss(table);
        var lineIndex=table.row( 0 ).data()[1];
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        var lineName=table.row( 0 ).data()[5];
        $.ajax({
            method: "post",
            url: "/pqs9900/dataloss/getTj",
            data: {
                startTime: startTime,
                endTime: endTime,
                lineIndex:lineIndex
            },
            beforeSend: function () {
                ide = ityzl_SHOW_LOAD_LAYER();
            },
            success : function(data) {
                var x=[];
                var y=[];
                $("#allArea").css("height",height-220);
                var areaOption = echarts.init($("#allArea")[0]);
                var areas=[];
                if(null!=data){
                    for(var i=0;i<data.length;i++){
                        var eValue=Number(data[i].sc*100);
                        var eSingleData={};
                        eSingleData.value=eValue.toFixed(2);
                        var eItem={};
                        eItem.color=getColor(eValue);
                        eSingleData.itemStyle=eItem;
                        y[i]=eSingleData;

                        var xA=getMyDate(data[i].timeId);
                        var xASingleData={};
                        xASingleData.value=xA;
                        x[i]=xASingleData;
                    }
                }
                var labelOption = {
                    normal: {
                        show: true,
                        position: 'top'
                    }
                };
                var option = {
                    backgroundColor: canvasBG,//背景色
                    title: {
                        text: lineName+'  数据缺失率',
                        x: 'center'
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    //控制图标在dataroom中的位置大小
                    grid: {
                        left: '3%',
                        right: '7%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [{
                        type: 'category',
                        data: x,
                        name: '时间',
                    }],
                    yAxis: [{
                        type: 'value',
                        name: '%',
                        min:'0',
                        max:'100'
                    }],
                    series: [{
                        name:'完整性(%)' ,
                        type: 'bar',
                        barGap: 0.2,
                        barMaxWidth:barMax,
                        label: labelOption,
                        data: y
                    }
                    ]
                };
                areaOption.setOption(option);
                //***********关闭loading
                ityzl_CLOSE_LOAD_LAYER(ide);
            }
        })
        $('#vdlist').on('dblclick', 'tr', function () {
            var x=[];
            var y=[];
            var lineIndex=table.row( this ).data()[1];
            var startTime = $("#startTime").val();
            var endTime = $("#endTime").val();
            var lineName= table.row( this ).data()[5];
            $.ajax({
                method: "post",
                url: "/pqs9900/dataloss/getTj",
                data: {
                    startTime: startTime,
                    endTime: endTime,
                    lineIndex:lineIndex
                },
                beforeSend: function () {
                    ide = ityzl_SHOW_LOAD_LAYER();
                },
                success : function(data) {
                    $("#allArea").css("height",height-220);
                    var areaOption = echarts.init($("#allArea")[0]);
                    var areas=[];
                    if(null!=data){
                        for(var i=0;i<data.length;i++){
                            var eValue=Number(data[i].sc*100);
                            var eSingleData={};
                            eSingleData.value=eValue.toFixed(2);
                            var eItem={};
                            eItem.color=getColor(eValue);
                            eSingleData.itemStyle=eItem;
                            y[i]=eSingleData;

                            var xA=getMyDate(data[i].timeId);
                            var xASingleData={};
                            var d=new Date(xA)
                            xASingleData.value=xA;
                            x[i]=xASingleData;
                        }
                    }
                    var labelOption = {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    };
                    var option = {
                        backgroundColor: canvasBG,//背景色
                        title: {
                            text: lineName+'  数据缺失率',
                            x: 'center'
                        },
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'shadow'
                            }
                        },
                        //控制图标在dataroom中的位置大小
                        grid: {
                            left: '3%',
                            right: '7%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: [{
                            type: 'category',
                            data: x,
                            name: '时间',
                        }],
                        yAxis: [{
                            type: 'value',
                            name: '%',
                            min:'0',
                            max:'100'
                        }],
                        series: [{
                            name:'完整性(%)' ,
                            type: 'bar',
                            barGap: 0.2,
                            barMaxWidth:barMax,
                            label: labelOption,
                            data: y
                        }
                        ]
                    };
                    areaOption.setOption(option);
                    //***********关闭loading
                    ityzl_CLOSE_LOAD_LAYER(ide);
                }
            })
        })

    }
}
function modifyCss(table){
    table.buttons().container().css("display","inline-block");
    table.buttons().container().css("float","right");
    table.buttons().container().css("left","4px");
    table.buttons().container().find("a").prepend("<i class='glyphicon glyphicon-share'></i>");
    table.buttons().container().appendTo('.header');
}