<template>
  <div style="width: 100%; height: 100%;">
    <div ref="chart" style="width: 100%; height: 100%;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import { fungong } from '@/api/ManageApi/index';

export default {
  name: 'FunctionChart',
  props: {
    years: {
      type: String,
      default: new Date().getFullYear().toString()
    }
  },
  data() {
    return {
      functionnumber: 0,
      chartData: [
        { value: 54, name: '高端制造与国际贸易区', itemStyle: { color: '#36C3FB' } },
        { value: 65, name: '独墅湖科教创新区', itemStyle: { color: '#5B76F9' } },
        { value: 32, name: '阳澄湖半岛旅游度假区', itemStyle: { color: '#F08445' } },
        { value: 25, name: '金鸡湖商务区', itemStyle: { color: '#F6B600' } },
        { value: 15, name: '苏相合作区', itemStyle: { color: '#50DFB3' } }
      ]
    };
  },
  mounted() {
    this.getData();
  },
  watch: {
    years: {
      immediate: true,
      handler(newVal) {
        this.getData();
      }
    }
  },
  methods: {
    async getData() {
      try {
        const response = await fungong({ years: this.years });
        if (response && response.code === 200 && response.data) {
          const totalnumber = response.data.reduce((sum, item) => sum + item.count, 0);
          this.functionnumber = totalnumber;
          this.chartData = this.processData(response.data);
          this.renderChart();
        } else {
        }
      } catch (error) {
      }
    },
    processData(data) {
      const names = ['高端制造与国际贸易区', '独墅湖科教创新区', '阳澄湖半岛旅游度假区', '金鸡湖商务区', '苏相合作区'];
      const counts = data.map(item => item.count);

      const total = counts.reduce((sum, count) => sum + count, 0);

      return names.map((name, index) => ({
        value: counts[index] || 0,
        name: name,
        itemStyle: { color: ['#36C3FB', '#5B76F9', '#F08445', '#F6B600', '#50DFB3'][index] },
        percent: ((counts[index] || 0) / total * 100).toFixed(2)
      }));
    },
    renderChart() {
      const chartDom = this.$refs.chart;
      const myChart = echarts.init(chartDom);

      const option = {
        tooltip: {
          trigger: 'item',
          formatter: function (params) {
            return `${params.name}: ${params.value}个 (${params.percent}%)`;
          }
        },
        legend: {
          orient: 'vertical',
          right: '10%',
          top: 'center',
          itemGap: 10,
          formatter: function (name) {
            const item = this.chartData.find(item => item.name === name);
            return `${name} ${item.value}个`;
          }.bind(this),
        },
        graphic: [
          {
            type: 'text',
            left: '29%',
            top: '40%',
            style: {
              text: this.functionnumber.toString(),
              textAlign: 'center',
              fill: '#292C33',
              fontSize: '1.25rem',
              fontFamily: 'DINbold',
              fontWeight: '500'
            }
          },
          {
            type: 'text',
            left: '26%',
            top: '55%',
            style: {
              text: '项目总数',
              textAlign: 'center',
              fill: '#9E9E9E',
              fontSize: '0.68rem',
              fontFamily: 'alibold',
              fontWeight: '400'
            }
          }
        ],
        series: [
          {
            name: '项目分布',
            type: 'pie',
            radius: ['40%', '60%'],
            center: ['30%', '50%'],
            data: this.chartData,
            label: {
              show: false
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      };

      myChart.setOption(option);
    }
  }
};
</script>

<style scoped>

</style>