export const pieChartData = (opts = {}) => {
    const groupByType = opts.countItemList;
    const color = ["#7ED321", "#ff9900", "#F5A623", "#DED11A", "#2A7E1D", '#2A7E1D', '#85C700', '#F7C137', '#F57723', '#00C1D4', '#3D97FF', '#8C54FF']
    const valueArray = {}
    const data = groupByType && groupByType.length > 0 && groupByType.map(item => {
        valueArray[item.name] = item.count
        return {
            name: item.name,
            value: item.count,
        };
    });

    const getArrayValue = (array, key) => {
        const keyIndex = key || 'value';
        const res = [];
        if (array) {
            array.forEach(t => {
                res.push(t[keyIndex]);
            });
        }
        return res;
    }

    const getPercent = (name) => {
        const value = valueArray[name];
        return (value && (value / opts.total) * 100 || 0).toFixed(2) + '%'
    }
    const arrName = getArrayValue(data, 'name');
    const getLegend = (name) => {
        let string = ''
        const arr = data?.map(item=>item?.name?.length);
        if(arr?.length > 0){
            const maxName = Math.max(...arr)
            if(maxName > name?.length){
                const length = maxName - name?.length;
                string = new Array(length).fill('   ').join('')
            }
        }
        return string
    }
    return {
        tooltip: {
            trigger: 'item',
            formatter: '{b}: {c} {d}%',
        },
        color,
        legend: {
            type: 'scroll',
            pageIconColor: '#85C700',
            pageIconSize: 7,
            orient: 'vertical',
            left: 'left',
            top: 'middle',
            icon: 'circle',
            // itemWidth: 7,
            formatter: (name) => {
                return `${name}     | ${getPercent(name)}`
            },
        },
        series: [
            {
                type: 'pie',
                radius: '85%',
                center: ['70%', '50%'],
                avoidLabelOverlap: false,
                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                data
            }
        ]
    }
};

const dataList = (data) => {
    const array = []
    if(data && data.length > 0){
        data.forEach(i => {
            array.push(i.yvalue)
        })
    }
    // console.log(array , '22222====')
    return array;
}

// 能耗统计
export const lineBarOption = (opts = {}) => {
    const {data} = opts;
    const legendData = [];
    const seriesData = [];
    const xData = []
    if (data && data.length > 0) {
        data.map((item, index) => {
            legendData.push({
                icon: 'circle',
                name: item.name || ''
            })
            seriesData.push({
                name: item.name,
                type: index === 0 ? 'bar' : 'line',
                itemStyle: {
                    color: index === 0 ? "#7ED321" : '#1890FF'
                },
                lineStyle: {
                    color: index === 0 ? "#7ED321" : '#1890FF'
                },
                data: dataList(item.trendIndexList || [])
            })
        })
        data[0].trendIndexList.map(item=>{
            xData.push(item.xvalueText)
        })
    }
    return {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#283b56'
                }
            }
        },
        // grid:{
        //    top:'15%',
        //    left:'5%',
        //    right:'5%'
        // },
        legend: {
            data: legendData
        },
        xAxis: [
            {
                axisLabel: {
                    margin: 30
                },
                type: 'category',
                boundaryGap: true,
                axisTick: {
                    alignWithLabel: true
                },
                data: xData
            },
            {
                show: false,
                type: 'category',
                boundaryGap: true,
                axisTick: {
                    alignWithLabel: true
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                name: "能耗趋势",
                nameGap: 40,
                nameTextStyle: {
                    fontWeight: 600,
                    fontSize: 14
                },
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                splitLine: {
                    show: false
                }
            },
            {
                show: true,
                type: 'value',
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                name: `单位:${(data && data.length > 0 && data[0].unit) || ''}`,
                splitLine: {
                    show: false
                },
                // axisLabel: {
                //     show: false
                // }
            }
        ],
        series: seriesData
    }
}

// 报警统计
export const alarmOption = (opts = {}) => {
    const {data , total , percentData} = opts;
    const color = ["#7ED321", "#ff9900", "#F5A623", "#DED11A", "#2A7E1D", '#2A7E1D', '#85C700', '#F7C137', '#F57723', '#00C1D4', '#3D97FF', '#8C54FF']
    const getPercent = (name) => {
        const value = percentData[name];
        return (value && (value / total) * 100 || 0).toFixed(2) + '%'
    }
    return {
        tooltip: {
            trigger: 'item',
            formatter: '{b}: {c} {d}%',
        },
        color,
        legend: {
            type: 'scroll',
            pageIconColor: '#85C700',
            pageIconSize: 7,
            orient: 'vertical',
            left: 'left',
            top: 'middle',
            icon: 'circle',
            itemWidth: 7,
            formatter: (name) => {
                return `${name}     | ${getPercent(name)}`
            },
            data: ['已处理' , '已派单' , '未处理']
        },
        series: [
            {
                type: 'pie',
                radius: '85%',
                center: ['70%', '50%'],
                avoidLabelOverlap: false,
                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                data
            }
        ]
    }
}