<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";

const animationDuration = 6000

export default {
  name: 'dailyBillReport',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '275px'
    }
  },
  data() {
    return {
      loading: false,
      chart: null,
      billData: [], // 记录统计数据
      // x轴配置
      x: {
        start: 0,
        end: 22,
        step: 2
      },
      // 查询条件
      queryParams: {
        createDate: parseTime(new Date(), "{y}-{m}-{d}")
      }
    }
  },
  watch: {
    billData:{
      deep: true,
      handler(nv, ov) {
        this.$nextTick(() => {
          this.setOption();
        })
      }
    }
  },
  created() {
    this.getBillList();
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    // 获取分时数据
    getHourData(data, key) {
      if (data == null) {
        return [];
      }
      // 将数据分组求和
      let list = [];
      for(let i = this.x.start; i <= this.x.end; i += this.x.step) {
        let sum = 0;
        data.filter(item => item.createHour >= i && item.createHour < i + this.x.step).forEach(item => {
          sum += item[key] == null ? 0 : item[key];
        })
        list.push(sum);
      }
      return list;
    },
    // 获取分时记录统计
    getBillList() {
      this.loading = true;
      hourBillCount(this.queryParams).then(res => {
        this.billData = res.data;
      }).finally(() => {
        this.loading = false;
      })
    },
    // 初始化图表
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOption();
    },
    // 设置数据
    setOption() {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: getTimeArray(this.x.start, this.x.end, this.x.step),
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '充值',
          type: 'bar',
          stack: 'vistors',
          barWidth: '30%',
          data: this.getHourData(this.billData, 'recharge'),
          animationDuration,
          itemStyle: {
            normal: {
              color: '#246EFF',
            }
          },
        }, {
          name: '提现',
          type: 'bar',
          stack: 'vistors',
          barWidth: '30%',
          data: this.getHourData(this.billData, 'withdraw'),
          animationDuration,
          itemStyle: {
            normal: {
              color: '#81E2FF',
            }
          },
        }]
      })
    }
  }
}
</script>
