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

<script>
import * as echarts from 'echarts'

export default {
  name: 'AreaDistribution',
  props: {
    chartsData: {
      type: Object,
      default: () => ({
        labels: ['T1-A区', 'T1-B区', 'T2-A区', 'T2-B区', 'T3-A区', 'T3-B区'],
        data: [320, 280, 350, 380, 320, 332]
      })
    }
  },
  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.areaChart) return
      this.chart = echarts.init(this.$refs.areaChart)
      const option = {
        responsive: true,
        maintainAspectRatio: false,
        grid: {
          left: '15%',
          right: '5%',
          bottom: '15%',
          top: '10%'
        },
        xAxis: {
          type: 'category',
          data: this.chartsData.labels,
          axisLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.1)'
            }
          },
          axisLabel: {
            color: 'rgba(255, 255, 255, 0.5)',
            fontSize: 10,
            // 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: 10
          }
        },
        series: [{
          type: 'bar',
          data: this.chartsData.data,
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0, color: '#A78BFA'
              }, {
                offset: 1, color: '#6366F1'
              }]
            }
          },
          barWidth: '60%'
        }]
      }
      this.chart.setOption(option)
    }
  },
  beforeDestroy() {
    if (this.chart) this.chart.dispose()
  }
}
</script>

<style lang="scss" scoped>
.area-distribution {
  .chart-container {
    height: 280rpx;
  }
}
</style>
