<template>
  <div class="seizure-info">
    <div class="total-section">
      <div class="donut-container">
        <div class="chart-container" ref="seizureDonut"></div>
      </div>
      <div class="dept-bar-container">
        <div class="chart-container" ref="deptSeizureBar"></div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'SeizureInfo',
  props: {
    chartsData: {
      type: Object,
      default: () => ({
        total: 1982,
        depts: {
          labels: ['旅检一部', '旅检二部', '旅检三部'],
          data: [620, 680, 682]
        }
      })
    }
  },
  data() {
    return {
      charts: {}
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initCharts()
    })
  },
  watch: {
    chartsData: {
      deep: true,
      handler() {
        this.$nextTick(() => {
          this.disposeCharts()
          this.initCharts()
        })
      }
    }
  },
  methods: {
    disposeCharts() {
      Object.values(this.charts).forEach(chart => {
        if (chart) chart.dispose()
      })
      this.charts = {}
    },
    initCharts() {
      this.initSeizureDonut()
      this.initDeptSeizureBar()
    },
    initSeizureDonut() {
      if (!this.$refs.seizureDonut) return
      this.charts.seizureDonut = echarts.init(this.$refs.seizureDonut)
      const option = {
        responsive: true,
        maintainAspectRatio: false,
        series: [{
          type: 'pie',
          radius: ['60%', '80%'],
          data: [
            { value: this.chartsData.total, name: '查获总数' }
          ],
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 1,
              y2: 1,
              colorStops: [{
                offset: 0, color: '#A78BFA'
              }, {
                offset: 1, color: '#6366F1'
              }]
            }
          },
          label: {
            show: false
          }
        }],
        graphic: [
          {
            type: 'text',
            left: 'center',
            top: '38%',
            style: {
              text: String(this.chartsData.total),
              fill: 'rgba(255, 255, 255, 0.9)',
              fontSize: 14,
              fontWeight: 'bold',
              textAlign: 'center'
            },
            z: 100
          },
          {
            type: 'text',
            left: 'center',
            top: '58%',
            style: {
              text: '查获总数',
              fill: 'rgba(255, 255, 255, 0.5)',
              fontSize: 15,
              textAlign: 'center'
            },
            z: 100
          }
        ]
      }
      this.charts.seizureDonut.setOption(option)
    },
    initDeptSeizureBar() {
      if (!this.$refs.deptSeizureBar) return
      this.charts.deptSeizureBar = echarts.init(this.$refs.deptSeizureBar)
      const option = {
        responsive: true,
        maintainAspectRatio: false,
        grid: {
          left: '15%',
          right: '5%',
          bottom: '15%',
          top: '10%'
        },
        xAxis: {
          type: 'category',
          data: this.chartsData.depts.labels,
          axisLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.1)'
            }
          },
          // axisLabel: {
          //   color: 'rgba(255, 255, 255, 0.5)',
          //   fontSize: 9,
          //   rotate: 30
          // }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.05)'
            }
          },
          axisLabel: {
            color: 'rgba(255, 255, 255, 0.5)',
            fontSize: 9
          }
        },
        series: [{
          type: 'bar',
          data: this.chartsData.depts.data,
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0, color: '#60A5FA'
              }, {
                offset: 1, color: '#3B82F6'
              }]
            }
          },
          barWidth: '50%'
        }]
      }
      this.charts.deptSeizureBar.setOption(option)
    }
  },
  beforeDestroy() {
    Object.values(this.charts).forEach(chart => {
      if (chart) chart.dispose()
    })
  }
}
</script>

<style lang="scss" scoped>
.seizure-info {
  .total-section {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  
  .donut-container {
    position: relative;
    width: 240rpx;
    height: 240rpx;
    margin-bottom: 24rpx;
    
    .chart-container {
      min-height: 240rpx;
      width: 100%;
      height: 100%;
    }
  }
  
  .dept-bar-container {
    width: 100%;
    height: 240rpx;

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