<template>
  <div ref="chart" class="chart-container"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'NestedPieChart',
  props: ['alldata','subject'],
  data() {
    return {
      chart: null,
      innerData: [
        { value: 0, name: 'AI模式' },
        { value: 0, name: '普通模式' }
      ],
      outerData: []
    };
  },
  watch: {
     alldata:{
          handler(val) {                
              if (val && val.length) {
                  this.startCom(val);
              }
          },
          deep:true,
          immediate: true  // 组件创建时立即执行
      }
  },
  // mounted() {
  //   this.startCom(this.alldata);
  // },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
    this.chart && this.chart.dispose();
  },
  methods: {
    startCom(val) {
      let data = JSON.parse(JSON.stringify(val));
      // console.log('subject',this.subject);
      
      // console.log('aidedDesigndata',data);

      this.innerData[0].value = data.filter(e=> e.state == 5 || e.state == 6).length
      this.innerData[1].value = data.filter(e=> e.state != 5 && e.state != 6).length

      // 为每个对象添加一个 value 属性
      let kk = this.subject.map(item => ({
        ...item, // 保留原有属性
        value: 0 // 添加 value 属性，设为默认值
      }));

      this.outerData = kk.map(category => {
        const count = data.reduce((acc, item) => {
          return acc + ((item.typeids && item.typeids.includes(category.id)) ? 1 : 0);
        }, 0);
        return {
          ...category,
          value: count // 添加计算的数量
        };
      });
      this.outerData = this.outerData.filter(e=> e.value)
      this.pio()
    },
    pio(){
      this.$nextTick(() => {
        this.chart = echarts.init(this.$refs.chart);
        this.initChart();
      })
    },
    initChart() {
      const options = {
        tooltip: {
          trigger: 'item'
        },
        // grid: {
        //   top: '5%', // 调整图表的上边距
        //   // left: '3%',
        //   // right: '4%',
        //   // bottom: '3%',
        //   containLabel: true
        // },
        legend: {
          bottom:'0%',
          left:'5%',
          right:'5%'
        },
        series: [
          {
            name: '模式分类',
            type: 'pie',
            center: ['50%', '40%'], // 调整饼图的中心位置
            radius: ['0%', '30%'],
            label: {
              show: false,
              position: 'center'
            },
            data: this.innerData,
            itemStyle: {
              borderRadius: 8,
              borderColor: '#fff',
              borderWidth: 2
            }
          },
          {
            name: '学科分类',
            type: 'pie',
            center: ['50%', '40%'], // 调整饼图的中心位置
            radius: ['40%', '65%'],
            label: {
              show: false
            },
            data: this.outerData,
            itemStyle: {
              borderRadius: 8,
              borderColor: '#fff',
              borderWidth: 2
            }
          }
        ]
      };

      this.chart.setOption(options);
      window.addEventListener('resize', this.chart.resize);

    },
    handleResize() {
      this.chart && this.chart.resize();
    }
  }
};
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 100%;
}
</style>