<template>
    <div class="data_body">
        <div style="width: 100%; height: 100%" v-if="this.ooption.xdata.length">
            <div id="charts_canvas" class="echart" style="width: 100%; height: 100%; "></div>
        </div>
        <div style="width: 100%; height: 100%;display: flex;align-items: center;justify-content: center;"
            v-show="!this.ooption.xdata.length">暂无数据</div>
    </div>
</template>

<script>
export default {
    props: {
        evCourseArray: {
            type: Object,
            default: function () {
                return {}
            }
        },
        evCourseArray2: {
            type: Array,
            default: []
        },
    },
    data() {
        return {
            chartObj: null,
            ooption: {
                xdata: [],
                xdata2: [],
                sdata: [],
            },
            option: {
                tooltip: {},
                radar: {
                    axisName: {
                        color: "#000"
                    },
                    // shape: 'circle',
                    indicator: [
                        // { name: '目标一', max: 5 },
                        // { name: '目标二', max: 5 },
                        // { name: '目标三', max: 5 },
                        // { name: '目标四', max: 5 },
                        // { name: '目标五', max: 5 }
                    ]
                },
                series: [
                    {
                        name: '教师考核',
                        type: 'radar',
                        data: [
                            // {
                            //     areaStyle: {
                            //         opacity: 0.2,
                            //         color: '#0061FF'
                            //     },
                            //     itemStyle: {
                            //         color: '#0061FF',
                            //         lineStyle: {
                            //         color: '#0061FF'
                            //         }
                            //     },
                            //     value: [],
                            //     name: ''
                            // }
                        ]
                    }
                ]
            },
        };
    },
    methods: {
        setChart(option) {
            // 雷达图显示的标签
            let newPromise = new Promise((resolve) => {
                resolve();
            });
            //然后异步执行echarts的初始化函数
            newPromise.then(() => {
                const chartObj = this.$echarts.init(
                    //劳动课程
                    this.$el.querySelector("#charts_canvas")
                );
                this.option.radar.indicator = option.sdata;
                if (option.xdata.length > 0) {
                    this.option.series[0].data[0] = {
                        // areaStyle: {
                        //     opacity: 0.2,
                        //     color: '#0061FF'
                        // },
                        itemStyle: {
                            color: '#0061FF',
                            lineStyle: {
                                color: '#0061FF'
                            }
                        },
                        value: option.xdata,
                        name: '全体教师平均得分率'
                    }
                }
                if (option.xdata2.length > 0) {
                    this.option.series[0].data[1] = {
                        // areaStyle: {
                        //     opacity: 0.2,
                        //     color: '#68bbc4'
                        // },
                        itemStyle: {
                            color: '#68bbc4',
                            lineStyle: {
                                color: '#68bbc4'
                            }
                        },
                        value: option.xdata2,
                        name: '平均得分率'
                    }
                }
                // this.option.series[0].data[0].value = option.xdata;
                // 初始化雷达图
                this.chartObj = chartObj;
                this.chartObj.setOption(this.option);
            });
        },
        setArray(array, type) {
            this.ooption.sdata = Object.keys(this.evCourseArray).map((item) => {
                return {
                    name: item,
                    max: 100,
                };
            });
            if (type == 1) {
                this.ooption.xdata = Object.keys(array).map((item) => {
                    return array[item].cogScore;
                })
            }
            if (this.evCourseArray2.length) {
                let xdata2 = []
                for(var i = 0; i < this.ooption.sdata.length; i++) {
                    let name = this.ooption.sdata[i]
                    for(var j = 0; j < this.evCourseArray2.length; j++) {
                        if(this.evCourseArray2[j].name == name.name) {
                            xdata2.push(this.evCourseArray2[j].cogScore)
                            break
                        }
                    }
                }
                this.ooption.xdata2 = xdata2
            }
            if (!this.chartObj) {
                this.setChart(this.ooption);
            } else {
                this.option.radar.indicator = this.ooption.sdata;
                if (this.ooption.xdata.length > 0) {
                    this.option.series[0].data[0] = {
                        // areaStyle: {
                        //     opacity: 0.2,
                        //     color: '#0061FF'
                        // },
                        itemStyle: {
                            color: '#0061FF',
                            lineStyle: {
                                color: '#0061FF'
                            }
                        },
                        value: this.ooption.xdata,
                        name: '全体教师平均得分率'
                    }
                }
                if (this.ooption.xdata2.length > 0) {
                    this.option.series[0].data[1] = {
                        // areaStyle: {
                        //     opacity: 0.2,
                        //     color: '#68bbc4'
                        // },
                        itemStyle: {
                            color: '#68bbc4',
                            lineStyle: {
                                color: '#68bbc4'
                            }
                        },
                        value: this.ooption.xdata2,
                        name: '平均得分率'
                    }
                }
                this.chartObj.setOption(this.option);
            }
            setTimeout(() => {
                if (this.chartObj) {
                    this.chartObj.resize();
                }
            }, 200);
            this.$forceUpdate();
        },
    },
    watch: {
        evCourseArray: {
            immediate: true,
            deep: true,
            handler(newValue, oldValue) {
                this.ooption.xdata = [];
                this.setArray(newValue, 1)
                this.$forceUpdate();
            },
        },
        evCourseArray2: {
            immediate: true,
            deep: true,
            handler(newValue, oldValue) {
                this.ooption.xdata2 = [];
                this.setArray(newValue, 2)
                this.$forceUpdate();
            },
        },
    },
    mounted() {
        this.ooption = {
            xdata: [],
            xdata2: [],
            sdata: [],
        }
        this.setArray(this.evCourseArray, 1)
        // this.setChart(this.ooption);
        var _this = this;
        window.addEventListener("resize", () => {
            if (_this.chartObj) {
                _this.chartObj.resize();
            }
        });
    },
};
</script>

<style scoped>
.data_body {
    height: 100%;
    /* display: flex; */
    position: relative;
    border-radius: 5px;
    /* border: 1px solid #eee; */
    margin: 0 auto;
    box-sizing: border-box;
    padding: 0;
    width: 90%;
    background: #fff;
}
</style>