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

<script>
import * as echarts from 'echarts'

export default {
  name: 'CountTrendChart',
  props: {
    data: {
      type: Object,
      default: () => ({
        dates: [],
        inFactory: [],
        checked: [],
        outFactory: []
      })
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    data: {
      handler(val) {
        this.initChart()
      },
      deep: true
    }
  },
  mounted() {
    this.initChart()
    window.addEventListener('resize', this.resizeChart)
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
    }
    window.removeEventListener('resize', this.resizeChart)
  },
  methods: {
    initChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$refs.chart)
      }
      
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          data: ['入厂衣服数', '检查衣服数', '出厂衣服数']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.data.dates || []
        },
        yAxis: {
          type: 'value',
          name: '件数',
          axisLabel: {
            formatter: '{value} 件'
          }
        },
        series: [
          {
            name: '入厂衣服数',
            type: 'line',
            stack: 'Total',
            areaStyle: {
              opacity: 0.3
            },
            emphasis: {
              focus: 'series'
            },
            data: this.data.inFactory || []
          },
          {
            name: '检查衣服数',
            type: 'line',
            stack: 'Total',
            areaStyle: {
              opacity: 0.3
            },
            emphasis: {
              focus: 'series'
            },
            data: this.data.checked || []
          },
          {
            name: '出厂衣服数',
            type: 'line',
            stack: 'Total',
            areaStyle: {
              opacity: 0.3
            },
            emphasis: {
              focus: 'series'
            },
            data: this.data.outFactory || []
          }
        ]
      }
      
      this.chart.setOption(option)
    },
    resizeChart() {
      if (this.chart) {
        this.chart.resize()
      }
    }
  }
}
</script> 