<template>
  <div class="chart-container">
    <div ref="orderChart" class="chart" />
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'OrderChart',
  data() {
    return {
      chart: null,
      orderData: {
        dates: [],
        orders: [],
        completedOrders: []
      }
    }
  },
  mounted() {
    this.initChart()
    this.getOrderData()
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
      this.chart = null
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.orderChart)
      this.setOptions()
    },
    async getOrderData() {
      // TODO: 调用获取订单数据的API
      // 模拟数据
      this.orderData = {
        dates: ['5-1', '5-2', '5-3', '5-4', '5-5', '5-6', '5-7'],
        orders: [30, 40, 35, 50, 45, 35, 40],
        completedOrders: [28, 35, 30, 45, 40, 30, 35]
      }
      this.setOptions()
    },
    setOptions() {
      const option = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['总订单', '已完成']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.orderData.dates
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '总订单',
            type: 'line',
            data: this.orderData.orders,
            smooth: true,
            itemStyle: {
              color: '#409EFF'
            }
          },
          {
            name: '已完成',
            type: 'line',
            data: this.orderData.completedOrders,
            smooth: true,
            itemStyle: {
              color: '#67C23A'
            }
          }
        ]
      }
      this.chart.setOption(option)
    }
  }
}
</script>

<style lang="scss" scoped>
.chart-container {
  width: 100%;
  height: 100%;
  .chart {
    width: 100%;
    height: 100%;
  }
}
</style> 