<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";
import {hourBillCount} from "@/api/system/dashboard";
import {parseTime} from "@/utils/ruoyi";

export default {
  name: 'dailyProfitReport',
  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: {
    // 获取图表数据
    getChartData(data, key) {
      if (data == null) {
        return [];
      }
      return data.map(item => item[key]);
    },
    // 初始化图表
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOption();
    },
    // 设置数据
    setOption() {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 30,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: this.getChartData(this.billData, 'date'),
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis:{
          type: 'value',
          axisTick: {
            show: false
          }
        },
        legend: {
          show: true,
        },
        series: [{
          name: '订单服务费收入（元）',
          type: 'line',
          data: this.getChartData(this.billData, 'serviceAmount'),
          itemStyle: {
            normal: {
              color: '#313CA9',
            }
          },
        },{
          name: '提现服务费收入（元）',
          type: 'line',
          data: this.getChartData(this.billData, 'withdrawServiceAmount'),
          itemStyle: {
            normal: {
              color: '#246EFF',
            }
          },
        }, {
          name: '月费收入（元）',
          type: 'line',
          data: this.getChartData(this.billData, 'monthAmount'),
          itemStyle: {
            normal: {
              color: '#31d697',
            }
          },
        },  {
          name: '订单手机号收入（元）',
          type: 'line',
          data: this.getChartData(this.billData, 'billMobileAmount'),
          itemStyle: {
            normal: {
              color: '#d6316b',
            }
          },
        }, {
          name: '渠道成本（元）',
          type: 'line',
          data: this.getChartData(this.billData, 'channelCost'),
          itemStyle: {
            normal: {
              color: '#ffb731',
            }
          },
        },
        ]
      })
    }
  }
}
</script>
