$(function () {

    // 获取近七天日期
    let dateArray = dateOfTheLastWeek();

    /**
     * 加载上传趋势图
     */
    trendLineChart(dateArray);

    /**
     * 所属行业
     */
    // industryChart();
    industryTypeChart();
    /**
     * 数据分类
     */
    classificationChart(dateArray);

    /**
     * 数据级别
     */
    dataLevelChart(dateArray)

    /**
     * 数据审核通过率
     */
    dataAuditChart();

    /**
     * 数据类别
     */
    dataCategoryChart()
});

/**
 * 数据类别
 */
function dataCategoryChart() {
    //数据类别
    let pieChart = echarts.init(document.getElementById("echarts-types-chart"));

    let nameData = []
    let data = []
    // 加载数据
    $.ajax({
        method: 'get',
        data: {},
        url: "/dashboard/data/dataCategory",
        success: function (result) {
            data = result
            if (result != undefined && result.length > 0) {
                for (let i = 0; i < result.length; i++) {
                    nameData.push(result[i].name)
                }
            }

            // ------------------------
            let pieOption = {
                title: {
                    text: '',
                    subtext: '',
                    x: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    x: 'right',
                    data: nameData
                },
                calculable: true,
                series: [
                    {
                        name: '数据类别',
                        type: 'pie',
                        radius: '60%',
                        center: ['44%', '50%'],
                        data: data,
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.9)'
                            }
                        }
                    }
                ]
            };
            pieChart.setOption(pieOption);
            $(window).resize(pieChart.resize);
        }
    })
}


/**
 * 数据审核通过率
 */
function dataAuditChart() {
    // 初始化
    let approvalChart = echarts.init(document.getElementById("echarts-approval-chart"));

    // 加载数据
    $.ajax({
        method: 'get',
        data: {},
        url: "/dashboard/data/dataAudit",
        success: function (result) {
            //数据审核
            let approvalOption = {
                tooltip: {
                    formatter: '{a} <br/>{b} : {c}%'
                },
                toolbox: {
                    feature: {
                        restore: {},
                        saveAsImage: {}
                    }
                },
                series: [
                    {
                        name: '审核通过率',
                        type: 'gauge',
                        radius: '100%',
                        detail: {formatter: '{value}%'},
                        data: [{value: result, name: '通过率'}]
                    }
                ]
            };
            approvalChart.setOption(approvalOption);
            $(window).resize(approvalChart.resize);
        }
    })

}


/**
 * 近七天日期
 */
function dateOfTheLastWeek() {
    //最近一周日期
    var myDate = new Date(); //获取今天日期
    myDate.setDate(myDate.getDate() - 6);
    var dateArray = [];
    var flag = 1;
    for (let i = 0; i < 7; i++) {
        // let dateTemp = (myDate.getMonth() + 1) + "-" + myDate.getDate();
        // if (dateTemp.length == 4) {
        //     dateTemp = "0" + dateTemp
        // }
        var month = myDate.getMonth()+1;
        var mday = myDate.getDate()
        var months = month >=10?month :'0'+ month;
        var mdays = mday>=10?mday:'0'+mday;
        dateTemp = months+"-"+mdays
        dateArray.push(dateTemp);
        myDate.setDate(myDate.getDate() + flag);
    }

    return dateArray;
}


/**
 * 数据等级
 * @param dateArray
 */
function dataLevelChart(dateArray) {
    //数据级别
    let bgColorList = ['#FBB730', '#31BDF2'];
    let importantDataArray = []
    let coreDataArray = []

    let lineChart = echarts.init(document.getElementById("echarts-line-chart"));

    // 获取数据
    $.ajax({
        method: 'get',
        data: {},
        url: "/dashboard/data/dataLevelData",
        success: function (result) {
            for (let i = 0; i < dateArray.length; i++) {
                // 重要数据
                let flag = false
                for (let j = 0; j < result.importantData.length; j++) {
                    // 截取掉年份 只留月份和日期
                    let date = result.importantData[j].date.slice(5)
                    if (dateArray[i] == date) {
                        flag = true
                        importantDataArray.push(result.importantData[j].amount)
                    }
                }
                if (!flag) {
                    importantDataArray.push(0)
                }
                // 核心数据
                flag = false
                for (let j = 0; j < result.coreData.length; j++) {
                    // 截取掉年份 只留月份和日期
                    let date = result.coreData[j].date.slice(5)
                    if (dateArray[i] == date) {
                        flag = true
                        coreDataArray.push(result.coreData[j].amount)
                    }
                }
                if (!flag) {
                    coreDataArray.push(0)
                }
            }

            // 加载chart
            let lineOption = {
                title: {
                    text: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['重要数据', '核心数据']
                },
                grid: {
                    x: 40,
                    x2: 40,
                    y2: 24
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        data: dateArray
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '重要数据',
                        type: 'bar',
                        itemStyle: {
                            normal: {
                                show: true,//鼠标悬停时显示label数据
                                barBorderRadius: [10, 10, 10, 10],//柱形图圆角，初始化效果
                                color: bgColorList[0]
                            }
                        },
                        data: importantDataArray,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        /*markLine: {
                            lineStyle: {
                                type: 'dashed'
                            },
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }*/
                    },
                    {
                        name: '核心数据',
                        type: 'bar',
                        itemStyle: {
                            normal: {
                                show: true,//鼠标悬停时显示label数据
                                barBorderRadius: [10, 10, 10, 10],//柱形图圆角，初始化效果
                                color: bgColorList[1]
                            }
                        },
                        data: coreDataArray,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        /*markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }*/
                    }
                ]
            };

            lineChart.setOption(lineOption);
            $(window).resize(lineChart.resize);
        }
    })
}

/**
 * 数据分类
 */
function classificationChart(dateArray) {
    // 数据  因暂时没有电信数据 只有工业数据 所以电信数据都设置默认值 0
    let dateArray2 = []
    //数据分类
    let barChart = echarts.init(document.getElementById("echarts-bar-chart"));

    $.ajax({
        method: 'get', // 服务器数据的请求方式 get or post
        data: {},
        url: "/dashboard/data/trendData", // 服务器数据的加载地址
        success: function (result) {
            for (let i = 0; i < dateArray.length; i++) {
                let flag = false
                for (let j = 0; j < result.length; j++) {
                    // 截取掉年份 只留月份和日期
                    let date = result[j].date.slice(5)
                    if (dateArray[i] == date) {
                        flag = true
                        dateArray2.push(result[j].amount)
                    }
                }
                if (!flag) {
                    dateArray2.push(0)
                }
            }

            // -------------------------

            let barOption = {
                title: {
                    text: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['工业数据', '电信数据']
                },
                grid: {
                    x: 30,
                    x2: 40,
                    y2: 24
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        data: dateArray
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '工业数据',
                        type: 'bar',
                        data: dateArray2,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        /*markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }*/
                    },
                    {
                        name: '电信数据',
                        type: 'bar',
                        data: [0, 0, 0, 0, 0, 0, 0],
                        markPoint: {
                            data: [
                                {name: '年最高', value: 0, xAxis: 4, yAxis: 1,},
                                {name: '年最低', value: 0, xAxis: 2, yAxis: 1}
                            ]
                        },
                        /*markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }*/
                    }
                ]
            };
            barChart.setOption(barOption);
            window.onresize = barChart.resize;
        }
    })

}

/**
 * 所属行业
 */
function industryChart() {
    //所属行业 echarts-industry-chart
    let industryChart = echarts.init(document.getElementById("echarts-industry-chart"));
    let nameData = []
    let data = []
    $.ajax({
        method: 'get', // 服务器数据的请求方式 get or post
        data: {},
        url: "/dashboard/data/industryData", // 服务器数据的加载地址
        success: function (result) {
            data = result
            if (result != undefined && result.length > 0) {
                for (let i = 0; i < result.length; i++) {
                    nameData.push(result[i].name)
                }
            }

            // ----------------------------
            let industryoption = {
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b}: {c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    x: 'right',
                    right: 1,
                    data: nameData
                },
                series: [
                    {
                        name: '访问来源',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            show: false,
                            position: 'center'
                        },
                        emphasis: {
                            label: {
                                show: true,
                                fontSize: '20',
                                fontWeight: 'bold'
                            }
                        },
                        labelLine: {
                            show: false
                        },
                        data: data
                    }
                ]
            };
            industryChart.setOption(industryoption);
            window.onresize = industryChart.resize;
        }
    })


}

/**
 * 行业类别
 */
function industryTypeChart() {
    //所属行业 echarts-industry-chart
    let industryChart = echarts.init(document.getElementById("echarts-industry-chart"));
    let nameData = []
    let data = []
    $.ajax({
        method: 'get', // 服务器数据的请求方式 get or post
        data: {},
        url: "/dashboard/data/industryType", // 服务器数据的加载地址
        success: function (result) {
            data = result
            if (result != undefined && result.length > 0) {
                for (let i = 0; i < result.length; i++) {
                    nameData.push(result[i].name)
                }
            }

            // ----------------------------
            let industryoption = {
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b}: {c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    x: 'right',
                    right: 1,
                    data: nameData
                },
                series: [
                    {
                        name: '所属行业',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            show: false,
                            position: 'center'
                        },
                        emphasis: {
                            label: {
                                show: true,
                                fontSize: '20',
                                fontWeight: 'bold'
                            }
                        },
                        labelLine: {
                            show: false
                        },
                        data: data
                    }
                ]
            };
            industryChart.setOption(industryoption);
            window.onresize = industryChart.resize;
        }
    })


}

/**
 * 数据上传趋势图
 * @param dateArray
 */
function trendLineChart(dateArray) {
    // 数据
    let dateArray2 = []
    $.ajax({
        method: 'get', // 服务器数据的请求方式 get or post
        data: {},
        url: "/dashboard/data/trendData", // 服务器数据的加载地址
        success: function (result) {
            for (let i = 0; i < dateArray.length; i++) {
                let flag = false
                for (let j = 0; j < result.length; j++) {
                    // 截取掉年份 只留月份和日期
                    let date = result[j].date.slice(5)
                    if (dateArray[i] == date) {
                        flag = true
                        dateArray2.push(result[j].amount)
                    }
                }
                if (!flag) {
                    dateArray2.push(0)
                }
            }

            //折线图
            let lineoptiondata = {
                tooltip: {
                    trigger: 'axis',
                    formatter: function (params) {
                        let val = 0;
                        if (params[0].data != undefined) {
                            val = params[0].data
                        }
                        return "今日上报:" + val + '<br>';
                    }
                },
                legend: {
                    data: ['数据上报趋势图']
                },
                grid: {
                    x: 40,
                    x2: 40,
                    y2: 40
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: dateArray
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        axisLabel: {
                            formatter: '{value}'
                        }
                    }
                ],
                series: [
                    {
                        name: '数据上报趋势图',
                        type: 'line',
                        data: dateArray2,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        /*markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }*/
                    }

                ]
            };

            let lineDataChart = echarts.init(document.getElementById("echarts-line-data"));
            lineDataChart.setOption(lineoptiondata);
            $(window).resize(lineDataChart.resize);

        }
    })

}


/**
 * 地图chart
 */
function mapChart() {
    $.ajax({
        method: 'get', // 服务器数据的请求方式 get or post
        data: {},
        url: "/dashboard/data/queryData", // 服务器数据的加载地址
        success: function (result) {
            var maxData = result.maxData + ''
            var dataArray1 = [];
            var dataArray2 = [];

            if (result.mapData1 != null && result.mapData1.length > 0) {
                for (var i = 0; i < result.mapData1.length; i++) {
                    var key = result.mapData1[i].cityName
                    key = key.replace("市", "");
                    key = key.replace("省", "");
                    dataArray1.push({name: key, value: result.mapData1[i].amount});
                }
            }

            if (result.mapData2 != null && result.mapData2.length > 0) {
                for (var i = 0; i < result.mapData2.length; i++) {
                    var key = result.mapData2[i].cityName
                    key = key.replace("市", "");
                    key = key.replace("省", "");
                    dataArray2.push({name: key, value: result.mapData2[i].amount});
                }
            }

            //中国地图
            var mapChart = echarts.init(document.getElementById("echarts-map-chart"));
            var optionMap = {
                title: {
                    text: '',
                    subtext: '',
                    x: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: function (parms) {
                        let res = parms.data.name + '<br>';
                        let flag = false
                        for (let i = 0; i < optionMap.series[0].data.length; i++) {
                            if (parms.data.name == optionMap.series[0].data[i].name) {
                                res += "重要数据: " + optionMap.series[0].data[i].value + '<br>';
                                flag = true
                            }
                        }
                        if (!flag) {
                            res += "重要数据: 0<br>";
                        }
                        flag = false
                        for (let i = 0; i < optionMap.series[1].data.length; i++) {
                            if (parms.data.name == optionMap.series[1].data[i].name) {
                                res += "核心数据: " + optionMap.series[1].data[i].value + '<br>';
                                flag = true
                            }
                        }
                        if (!flag) {
                            res += "核心数据: 0<br>";
                        }
                        return res;
                    }
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    data: ['重要数据', '核心数据']
                },
                dataRange: {
                    min: 0,
                    max: maxData,
                    x: 'left',
                    y: 'bottom',
                    text: ['高', '低'],           // 文本，默认为数值文本
                    calculable: true
                },
                series: [
                    {
                        name: '重要数据',
                        type: 'map',
                        mapType: 'china',
                        roam: false,
                        itemStyle: {
                            normal: {label: {show: true}},
                            emphasis: {label: {show: true}}
                        },
                        data: dataArray1
                    },
                    {
                        name: '核心数据',
                        type: 'map',
                        mapType: 'china',
                        roam: false,
                        itemStyle: {
                            normal: {label: {show: true}},
                            emphasis: {label: {show: true}}
                        },
                        data: dataArray2
                    }

                ]
            };
            //使用制定的配置项和数据显示图表
            mapChart.setOption(optionMap);
            $(window).resize(mapChart.resize);
        }
    });
}

