var picHeight, endTime, startTime, allData;
var tiggleValue = "";
var buttonname;
var companyname;
$(function () {
    var end = getEndTime();
    var start = getMonthStartTime(end);

	$.ajax({
        type: "POST",
        data:{
            start: start,
            end: end
        },
		async:false,
        url: "/pqs9000/device/getMsgInfo",
        dataType: 'json',
        success: function (data) {
        	buttonname = data.body.name;
        	tiggleValue = data.body.name;
        }
	})

	$.ajax({
        type: "POST",
        url: "/pqs9000/theme/getThemeInfo",
        async: false,
        dataType: 'json',
        success: function (data) {
            companyname = data.body.name;
        }
    })

    //数据完整性开关触发的函数
    $(".wanzheng").bootstrapSwitch({
        onText: buttonname,      // 设置ON文本
        offText: companyname,    // 设置OFF文本
        onColor: "primary",// 设置ON文本颜色     (info/success/warning/danger/primary)
        offColor: "primary",  // 设置OFF文本颜色        (info/success/warning/danger/primary)
        size: "small",    // 设置控件大小,从小到大  (mini/small/normal/large)
        handleWidth: "60",//设置控件宽度
        // 当开关状态改变时触发
        onSwitchChange: function (event, state) {
            if (state == true) {
                tiggleValue = buttonname;
            } else {
                tiggleValue = companyname;
            }
            //重新画图
            reDrawAllPic();
        }
    });

    var i;
    picHeight = $(window).height() * 0.5 - 5;
    endTime = getEndTime();
    startTime = getMonthStartTime(endTime);
    $.ajax({
        url: '/pqs9000/overview/dataIntegrality',
        data: {
            startTime: startTime,
            endTime: endTime
        },
        type: 'post',
        dataType: 'json',
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            if (data.code === 500 | data.body === null) {
                allData = null;
                drawAreaPic(null);
                drawVoltagePic(null)
                drawCompanyPic(null)
            } else {
                allData = data.body;
                drawAreaPic(data.body.area);
                drawVoltagePic(data.body.vol)
                drawCompanyPic(data.body.interval)
            }
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    })
})

//开关触发图片更新
function reDrawAllPic() {
    if (allData === null) {
        drawAreaPic(null);
        drawVoltagePic(null)
        drawCompanyPic(null)
    } else {
        drawAreaPic(allData.area);
        drawVoltagePic(allData.vol)
        drawCompanyPic(allData.interval)
    }
}

//绘画区域数据
function drawAreaPic(area) {
    $("#area").css("height", picHeight);
    var areaOption = echarts.init($("#area")[0]);
    var inte = [];
    var areas = [];
    var route;
    var lineFeedBar;
    if(area.length>barUpperLimit12){
        route=barRouteIncline;
        lineFeedBar=noLineFeed;
    }else{
        route=barRouteZero;
        lineFeedBar=lineFeed;
    }
    if (null != area) {
        for (var i = 0; i < area.length; i++) {
            var integralityRate;
            if (tiggleValue === buttonname) {
                areas[i] = area[i].name + lineFeedBar+"(" + area[i].amounts + ")";
                integralityRate = Number(area[i].dataIntegrity);
            } else {
                areas[i] = area[i].name + lineFeedBar+"(" + area[i].gwamounts + ")";
                integralityRate = Number(area[i].gwdataIntegrity);
            }
            var singleData = {};
            var item = {};
            item.color = getColor(integralityRate);
            if(integralityRate<3.1414){
                singleData.value = 3.1415;
                item.actual = integralityRate;
            }else{
                singleData.value = integralityRate;
            }
            singleData.itemStyle = item;
            singleData.areaIndex=area[i].deptsIndex;
            inte[i] = singleData;
        }
    }
    var option = {
        backgroundColor: canvasBG,//背景色
        title: {
            text: '区域',
            x: 'center'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            formatter: function (params) {
                var color = params[0].color;
                var tips = "";
                tips += params[0].name;
                if (color === noMonitor) {
                    tips += '<br/>' + params[0].seriesName + ' : /';
                } else if (color === noData) {
                    tips += '<br/>' + params[0].seriesName + ' : （暂无数据）';
                } else {
                    if (params[0].data.itemStyle.actual !== undefined) {
                        tips += '<br/>' + params[0].seriesName + ' : ' + params[0].data.itemStyle.actual;
                    } else {
                        tips += '<br/>' + params[0].seriesName + ' : ' + params[0].value;
                    }
                }
                return tips;
            }
        },
        //控制图标在dataroom中的位置大小
        grid: {
            left: '1%',
            right: '7%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [{
            type: 'category',
            data: areas,
            name: '地区\n(监测点数)\n',
            axisLabel: {
                interval: 0,
                show: true,
                rotate:route,
                fontSize:10
            }
        }],
        yAxis: [{
            type: 'value',
            name: '%',
            min: '0',
            max: '100'
        }],
        series: [{
            name: '完整性',
            type: 'bar',
            barMaxWidth: barMax,
            data: inte
        }]
    };
    areaOption.setOption(option);
    areaOption.on('click',function (params) {
        if(getRole("/pqs9000/area/integralitytable")){
            var city=params.data.areaIndex;
            window.top.addTab('/pqs9000/area/integralitytable?startTime='+startTime+'&endTime='+endTime+'&area='+city+'&interval=月份','完整性列表')
        }
    });
}

//绘制电压等级图
function drawVoltagePic(vol) {
    $("#voltage").css("height", picHeight);
    var volOption = echarts.init($("#voltage")[0]);
    var inte = [];
    var areas = [];
    if (null != vol) {
        for (var i = 0; i < vol.length; i++) {
            var integralityRate;
            if (tiggleValue === buttonname) {
                areas[i] = vol[i].name + "\n(" + vol[i].amounts + ")";
                integralityRate = Number(vol[i].dataIntegrity);
            } else {
                areas[i] = vol[i].name + "\n(" + vol[i].gwamounts + ")";
                integralityRate = Number(vol[i].gwdataIntegrity);
            }
            var singleData = {};
            var item = {};
            item.color = getColor(integralityRate);
            if(integralityRate<3.1414){
                singleData.value = 3.1415;
                item.actual = integralityRate;
            }else{
                singleData.value = integralityRate;
            }
            singleData.itemStyle = item;
            inte[i] = singleData;
        }
    }
    var option = {
        backgroundColor: canvasBG,//背景色
        title: {
            text: '电压等级',
            x: 'center'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            formatter: function (params) {
                var color = params[0].color;
                var tips = "";
                tips += params[0].name;
                if (color === noMonitor) {
                    tips += '<br/>' + params[0].seriesName + ' : /';
                } else if (color === noData) {
                    tips += '<br/>' + params[0].seriesName + ' : （暂无数据）';
                } else {
                    if (params[0].data.itemStyle.actual !== undefined) {
                        tips += '<br/>' + params[0].seriesName + ' : ' + params[0].data.itemStyle.actual;
                    } else {
                        tips += '<br/>' + params[0].seriesName + ' : ' + params[0].value;
                    }
                }
                return tips;
            }
        },
        //控制图标在dataroom中的位置大小
        grid: {
            left: '3%',
            right: '13%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [{
            type: 'category',
            data: areas,
            name: '电压等级\n(监测点数)\n',
        }],
        yAxis: [{
            type: 'value',
            name: '%',
            min: '0',
            max: '100'
        }],
        series: [{
            name: '完整性',
            type: 'bar',
            barMaxWidth: barMax,
            data: inte
        }]
    };
    volOption.setOption(option);
    volOption.on('click',function (params) {
        if(getRole("/pqs9000/area/integralitytable")) {
            window.top.addTab('/pqs9000/area/integralitytable?startTime=' + startTime + '&endTime=' + endTime + '&interval=月份', '完整性列表')
        }
    });
}

//绘制设备厂家图
function drawCompanyPic(company) {
    $("#company").css("height", picHeight);
    var comOption = echarts.init($("#company")[0]);
    var inte = [];
    var areas = [];
    if (null != company) {
        for (var i = 0; i < company.length; i++) {
            var integralityRate;
            if (tiggleValue === buttonname) {
                areas[i] = company[i].name + "\n(" + company[i].amounts + ")";
                integralityRate = Number(company[i].dataIntegrity);
            } else {
                areas[i] = company[i].name + "\n(" + company[i].gwamounts + ")";
                integralityRate = Number(company[i].gwdataIntegrity);
            }
            var singleData = {};
            var item = {};
            item.color = getColor(integralityRate);
            if(integralityRate<3.1414){
                singleData.value = 3.1415;
                item.actual = integralityRate;
            }else{
                singleData.value = integralityRate;
            }
            singleData.itemStyle = item;
            inte[i] = singleData;
        }
    }
    var option = {
        backgroundColor: canvasBG,//背景色
        title: {
            text: '终端厂家',
            x: 'center'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            formatter: function (params) {
                var color = params[0].color;
                var tips = "";
                tips += params[0].name;
                if (color === noMonitor) {
                    tips += '<br/>' + params[0].seriesName + ' : /';
                } else if (color === noData) {
                    tips += '<br/>' + params[0].seriesName + ' : （暂无数据）';
                } else {
                    if (params[0].data.itemStyle.actual !== undefined) {
                        tips += '<br/>' + params[0].seriesName + ' : ' + params[0].data.itemStyle.actual;
                    } else {
                        tips += '<br/>' + params[0].seriesName + ' : ' + params[0].value;
                    }
                }
                return tips;
            }
        },
        //控制图标在dataroom中的位置大小
        grid: {
            left: '3%',
            right: '13%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [{
            type: 'category',
            data: areas,
            name: '终端厂家\n(监测点数)\n',
        }],
        yAxis: [{
            type: 'value',
            name: '%',
            min: '0',
            max: '100'
        }],
        series: [{
            name: '完整性',
            type: 'bar',
            barMaxWidth: barMax,
            data: inte
        }]
    };
    comOption.setOption(option);
    comOption.on('click',function (params) {
        if(getRole("/pqs9000/area/integralitytable")) {
            window.top.addTab('/pqs9000/area/integralitytable?startTime=' + startTime + '&endTime=' + endTime + '&interval=月份', '完整性列表')
        }
    });
}

/**
 * 获取指定区域下终端的在线率具体数据（最多10条）
 */
function showDetailData(content, title) {
    layer.open({
        type: 2,
        title: title + '监测点数据完整性详情（前20）',
        shadeClose: true,
        resize: false,
        area: '700px',
        scrollbar: false,
        content: content
    })
}

//根据数据完整性获取颜色
function getColor(data) {
    if (data >= 90) {
        return green;
    } else if (data === 3.1415) {
        return noMonitor;
    } else if (data === 3.14159) {
        return noData;
    } else if (data < 60) {
        return red;
    } else {
        return yellow;
    }
}
