<template>
  <div class="unsafe-types-chart">
    <div class="chart-container" ref="typesChart"></div>
    <div class="types-list">
      <div v-for="(item, index) in chartsData.types" :key="index" class="list-item">
        <div class="item-name">{{ item.name }}</div>
        <div class="item-value">{{ item.value }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'UnsafeTypesChart',
  props: {
    chartsData: {
      type: Object,
      default: () => ({
        types: [
          { name: '一类', value: 56 },
          { name: '二类', value: 57 },
          { name: '三类', value: 56 },
          { name: '四类', value: 55 },
          { name: '五类', value: 55 }
        ]
      })
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  watch: {
    chartsData: {
      deep: true,
      handler() {
        this.$nextTick(() => {
          if (this.chart) this.chart.dispose()
          this.initChart()
        })
      }
    }
  },
  methods: {
    initChart() {
      if (!this.$refs.typesChart) return
      this.chart = echarts.init(this.$refs.typesChart)
      const option = {
        responsive: true,
        maintainAspectRatio: false,
        series: [{
          type: 'pie',
          radius: ['50%', '70%'],
          data: this.chartsData.types.map(item => ({
            name: item.name,
            value: item.value
          })),
          itemStyle: {
            color: function(params) {
              const colors = ['#A78BFA', '#6366F1', '#8B5CF6', '#7C3AED', '#6D28D9']
              return colors[params.dataIndex]
            }
          },
          label: {
            show: true,
            color: 'rgba(255, 255, 255, 0.7)',
            fontSize: 10,
            formatter: '{b}\n{d}%'
          }
        }]
      }
      this.chart.setOption(option)
    }
  },
  beforeDestroy() {
    if (this.chart) this.chart.dispose()
  }
}
</script>

<style lang="scss" scoped>
.unsafe-types-chart {
  .chart-container {
    height: 220rpx;
  }
  
  .types-list {
    margin-top: 16rpx;
  }
  
  .list-item {
    display: flex;
    justify-content: space-between;
    font-size: 24rpx;
    padding: 8rpx 0;
    
    .item-name {
      color: rgba(255, 255, 255, 0.7);
    }
    
    .item-value {
      color: rgba(255, 255, 255, 0.9);
    }
  }
}
</style>
