<template>
    <div id="chart">
        <div ref="echartRef" class="chart" :style="{ height: `${height}px` }"></div>
    </div>
</template>

<script>
import * as echarts from "echarts";
export default {
    name: "ZrPieChart",
    data() {
        return {
            chart: null, 
            options: {
                title: {
                    text: '',
                    subtext: '',
                    left: 'center',
                    padding: [15, 0, 0, 0]
                },
                tooltip: {
                    trigger: 'item',
                    
                },
                legend: {},
                series: [
                    {
                        name: '',
                        type: 'pie',
                        radius: '50%',
                        label: {
                            show: true
                        },
                        roseType: '',
                        data: [],
                        itemStyle: {},
                        emphasis: {}
                    }
                ]
            },
            height: 300,
        };
    },
    props: {
        // 图表数据
        seriesData: {
            type: Array,
            default: () => [],
            require: true
        },
        // 图表数据配置项
        chartOptions: {
            type: Object,
            default: () => {},
            require: true
        },
        // 暗黑模式
        dark: {
            type: Boolean,
            default: false,
        }
    },
    mounted() {
        if(this.chartOptions.dark) {
            this.chart = echarts.init(this.$refs.echartRef, 'dark');
        }else {
            this.chart = echarts.init(this.$refs.echartRef);
        }
        this.chart.setOption(this.options);
    },
    watch: {  
        chartOptions: {
            handler: function(newChartOptions) {
                if(newChartOptions.title.text) {
                    this.options.title.text = newChartOptions.title.text
                    this.options.series[0].name = newChartOptions.title.text
                }
                if(newChartOptions.title.subtext) this.options.title.subtext = newChartOptions.title.subtext
                if(newChartOptions.height) this.height = newChartOptions.height
                if(newChartOptions.legend) this.options.legend = newChartOptions.legend
                if(newChartOptions.series) {
                    if(newChartOptions.series[0].radius) this.options.series[0].radius = newChartOptions.series[0].radius
                    if(newChartOptions.series[0].itemStyle) this.options.series[0].itemStyle = newChartOptions.series[0].itemStyle
                    if(newChartOptions.series[0].roseType) this.options.series[0].roseType = newChartOptions.series[0].roseType
                }
                
            },
            immediate: true,
            deep: true
        },
        "chartOptions.dark": {
            handler: function(newTheme) {
                this.chart.dispose(); //销毁
                if(newTheme){
                    this.chart = echarts.init(this.$refs.echartRef, 'dark')
                }else {
                    this.chart = echarts.init(this.$refs.echartRef)    
                }
                this.chart.setOption(this.options);
            },
        },
        seriesData: {
            handler: function (newSeries) {  
                if(this.chartOptions.props) {
                    let name = this.chartOptions.props.name
                    let value = this.chartOptions.props.value
                    this.options.series[0].data = newSeries.map((item,index) => {
                        let obj = {
                            name: '',
                            value: 0
                        }
                        if(item[name]) {
                            obj.name = item[name]
                        }
                        obj.value = item[value]
                        return obj
                    });
                }
            },
            immediate: true,
            deep: true,
        },
        options: {
            handler: function (newOption) {
                this.chart.setOption(newOption);
            },
            deep: true,
        },
    },
    methods: {

    },
};
</script>

<style lang="less" scoped>
.chart {
    width: 100%;
}
</style>