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

<script>
import * as echarts from 'echarts'

export default {
  name: 'OrderTrendChart',
  props: {
    data: {
      type: Array,
      default: () => []
    }
  },
  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.map(item => item.statisticsDate)
        },
        yAxis: {
          type: 'value',
          name: '金额',
          axisLabel: {
            formatter: '{value} 元'
          }
        },
        series: [
          {
            name: '洗衣金额',
            type: 'line',
            stack: 'Total',
            areaStyle: {
              opacity: 0.3
            },
            emphasis: {
              focus: 'series'
            },
            data: this.data.map(item => item.clothOrderAmount || 0)
          },
          {
            name: '实付金额',
            type: 'line',
            stack: 'Total',
            areaStyle: {
              opacity: 0.3
            },
            emphasis: {
              focus: 'series'
            },
            data: this.data.map(item => item.clothReceiveAmount || 0)
          }
        ]
      }
      
      this.chart.setOption(option)
    },
    resizeChart() {
      if (this.chart) {
        this.chart.resize()
      }
    }
  }
}
</script> 