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

export default {
  name: "balancePeriodReport",
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
  },
  data() {
    return {
      loading: false,
      chart: null,
      recordData: [],
      queryParams: {
        createDate: parseTime(new Date(), "{y}-{m}-{d}")
      },
      // x轴配置
      x: {
        start: 0,
        end: 23,
        step: 1
      },
    }
  },
  watch: {
    recordData: {
      deep: true,
      handler(val) {
        this.setOptions()
      }
    }
  },
  created() {
    this.x.end = new Date().getHours();
    this.getBalancePeriodList();
  },
  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 => {
          let hour = parseTime(item.createTime, "{h}");
          return hour >= i && hour < i + this.x.step
        }).forEach(item => {
          sum += item[key] == null ? 0 : item[key];
        })
        list.push(sum.toFixed(2));
      }
      return list;
    },
    // 获取数据
    getBalancePeriodList() {
      this.loading = true;
      balancePeriod(this.queryParams).then(res => {
        this.recordData = res.data;
      }).finally(() => {
        this.loading = false;
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption({
        xAxis: {
          data: getTimeArray(this.x.start, this.x.end, this.x.step),
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis:[
          {
            name: '平台总余额',
            show: true,
          },
          {
            name: '用户总余额',
            show: true,
          },
          {
            name: '充值',
            show: false,
          },
        ],
        legend: {
          show: true,
        },
        series: [{
          name: '平台总余额（元）',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#313CA9',
              lineStyle: {
                color: '#313CA9',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          yAxisIndex: 0,
          data: this.getHourData(this.recordData, "platformBalance"),
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        },{
          name: '用户总余额（元）',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#249EFF',
              lineStyle: {
                color: '#249EFF',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          yAxisIndex: 1,
          data: this.getHourData(this.recordData, "userBalance"),
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        },{
          name: '充值金额（元）',
          itemStyle: {
            normal: {
              color: '#21CCFF',
              lineStyle: {
                color: '#21CCFF',
                width: 2
              }
            }
          },
          yAxisIndex: 2,
          smooth: true,
          type: 'line',
          data: this.getHourData(this.recordData, "recharge"),
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },]
      })
    }
  }
}
</script>
