<template>
  <div :class="className" :style="{height:height,width:width}" v-loading="loading"/>
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getTimeArray } from '@/utils'

export default {
  name: 'dailyBillAmountReport',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    billData: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      loading: false,
      chart: null,
      // x轴配置
      x: {
        start: 0,
        end: 22,
        step: 2
      },
    }
  },
  watch: {
    billData:{
      deep: true,
      handler(nv, ov) {
        this.$nextTick(() => {
          this.setOption();
        })
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    // 初始化图表
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOption();
    },
    // 设置数据
    setOption() {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          show: true
        },
        grid: {
          top: 30,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: this.billData.map(item => item.date),
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '支付金额',
          type: 'line',
          data: this.billData.map(item => item.total),
          itemStyle: {
            normal: {
              color: '#246EFF',
            }
          },
        }, {
          name: '退款金额',
          type: 'line',
          data: this.billData.map(item => item.refund),
          itemStyle: {
            normal: {
              color: '#31d697',
            }
          },
        }, {
          name: '实收金额',
          type: 'line',
          data: this.billData.map(item => item.received),
          itemStyle: {
            normal: {
              color: '#6831d6',
            }
          },
        }]
      })
    }
  }
}
</script>
