<template>
    <div class="max-w max-h">
        <div class="max-w max-h" id="chartbox"></div>
    </div>
</template>
<script>
import { defineComponent, reactive, onMounted, toRefs } from "vue";
import { postMethodCommon, getMethodCommon } from "../../../api/common";
export default defineComponent({
    components: {
        
    },
    setup(props, context) { 
        const data = reactive({
            roleInfo:{
                listDatas: [],
                yearsArr: [],
                goodsArr: []
            }
        })
        onMounted(() => {           
            methods.getListData()
        });
        const methods = {
            getListData(){
                let param = {
                    id: window.goodsNumId || "28fa8415-1113-4cbe-9a46-96009e2d2a42",
                    mid: window.goodsNumMid || "4af7613c-7741-45f5-a8ab-2e87856a8d0d",
                    paginationInputDto: {
                        page: 1,
                        rows: 10000
                    }
                }
                data.roleInfo.yearsArr = []
                postMethodCommon("/FormScheme/GetFormDataPage?id=" + param.id + "&mid="+param.mid, param).then(res =>{
                    let rel = res.data.result.items;
                    let years = [],goods = [],yearsArr = [];
                    rel.forEach(item =>{
                        years.push(item.year)
                        goods.push({
                            name: item.name,
                            child: []
                        })
                    })
                    let newYears = [...new Set(years)].sort(function(a, b) {
                        return a.year - b.year;
                    });
                    data.roleInfo.yearsArr = newYears
                    data.roleInfo.goodsArr = goods.filter((val, ind, self) => {
                        return self.findIndex(t => JSON.stringify(t.name) === JSON.stringify(val.name)) === ind;
                    });
                    data.roleInfo.listDatas = rel.sort(function(a, b) {
                        return a.year - b.year;
                    });
                    data.roleInfo.goodsArr.forEach(item =>{
                        newYears.forEach(val =>{
                            item.child.push({
                                name: val,
                                value: null
                            })
                        })
                    })
                    data.roleInfo.goodsArr.forEach(item =>{
                        item.child.forEach(val =>{
                            rel.forEach(rel =>{
                                if(val.name == rel.year && item.name == rel.name){
                                    val.value = rel.num
                                }
                            })
                        })
                    })
                    methods.renderCharts()
                })                  
            },
            renderCharts(){
                let chartBox = echarts.init(
                    document.getElementById("chartbox")
                );
                const colorList = ["#9E87FF", '#73DDFF', '#fe9a8b', '#F56948', '#9E87FF', '#CF87FF', '#F7CF69', '#EEEF54', '#CD5BFA']
                let seriesdataList = []
                data.roleInfo.goodsArr.forEach((item,index) =>{  
                    seriesdataList.push({
                        name: item.name,
                        type: 'line',
                        data: item.child,
                        symbolSize: 1,
                            symbol: 'circle',
                            smooth: true,
                            yAxisIndex: 0,
                            showSymbol: false,
                            lineStyle: {
                                width: 5,
                                color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
                                        offset: 0,
                                        color: colorList[index]
                                    },
                                    {
                                        offset: 1,
                                        color: colorList[index]
                                    }
                                ]),
                            },
                            itemStyle: {
                                normal: {
                                    color: colorList[index],
                                    borderColor: colorList[index]
                                }
                            }
                    })        
                })    
               
                
                
                let option = {
                    legend: {
                        show: false
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            label: {
                                show: true,
                                backgroundColor: '#fff',
                                color: '#556677',
                                borderColor: 'rgba(0,0,0,0)',
                                shadowColor: 'rgba(0,0,0,0)',
                                shadowOffsetY: 0
                            },
                            lineStyle: {
                                width: 0
                            }
                        },
                        backgroundColor: '#fff',
                        textStyle: {
                            color: '#5c6c7c'
                        },
                        padding: [10, 10],
                        extraCssText: 'box-shadow: 1px 0 2px 0 rgba(163,163,163,0.5)'
                    },
                    grid: {
                        top: '15%'
                    },
                    xAxis: [{
                        type: 'category',
                        data: data.roleInfo.yearsArr,
                        axisLine: {
                            lineStyle: {
                                color: '#DCE2E8'
                            }
                        },
                        axisTick: {
                            show: false
                        },
                        axisLabel: {
                            interval: 0,
                            textStyle: {
                                color: '#556677'
                            },
                            // 默认x轴字体大小
                            fontSize: 12,
                            // margin:文字到x轴的距离
                            margin: 15
                        },
                        axisPointer: {
                            label: {
                                // padding: [11, 5, 7],
                                padding: [0, 0, 10, 0],
                                margin: 15,
                                fontSize: 12,
                                backgroundColor: {
                                    type: 'linear',
                                    x: 0,
                                    y: 0,
                                    x2: 0,
                                    y2: 1,
                                    colorStops: [{
                                        offset: 0,
                                        color: '#fff' // 0% 处的颜色
                                    }, {
                                        // offset: 0.9,
                                        offset: 0.86,
                                        color: '#fff' // 0% 处的颜色
                                    }, {
                                        offset: 0.86,
                                        color: '#33c0cd' // 0% 处的颜色
                                    }, {
                                        offset: 1,
                                        color: '#33c0cd' // 100% 处的颜色
                                    }],
                                    global: false // 缺省为 false
                                }
                            }
                        },
                        boundaryGap: false
                    }],
                    yAxis: [{
                        type: 'value',
                        axisTick: {
                            show: false
                        },
                        axisLine: {
                            show: true,
                            lineStyle: {
                                color: '#DCE2E8'
                            }
                        },
                        axisLabel: {
                            textStyle: {
                                color: '#556677'
                            }
                        },
                        splitLine: {
                            show: false
                        }
                    }, {
                        type: 'value',
                        position: 'right',
                        axisTick: {
                            show: false
                        },
                        axisLabel: {
                            textStyle: {
                                color: '#556677'
                            },
                            formatter: '{value}'
                        },
                        axisLine: {
                            show: true,
                            lineStyle: {
                                color: '#DCE2E8'
                            }
                        },
                        splitLine: {
                            show: false
                        }
                    }],
                    series: seriesdataList
                };

                chartBox.setOption(option);
                // 根据页面大小自动响应图表大小
                window.addEventListener("resize", function () {
                    chartBox.resize();
                });
            }
        };
        return {
            ...data,
            ...methods,
        };
    },
});
</script>
<style scoped>

</style>