<template>
  <div v-loading="loading">
    <el-row type="flex" style="justify-content: space-between">
      <el-date-picker
        v-model="computedDate"
        type="daterange"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"
        :clearable="false"
        @change="getData"
      >
      </el-date-picker>
      <div>
        合计
        <span style="font-size: 26px">{{totalIncome | money}}</span>
        元
      </div>
    </el-row>
    <single-line-chart name="收入(元)" :chart-data="chartData" :labels="labels" height="250px"/>
  </div>
</template>

<script>
import SingleLineChart from '@/components/SingleLineChart/index.vue'
import RangePicker from '@/components/RangePicker/index.vue'
import { appBonusDailyAmount } from '@/api/app/dashboard'
import { getLastDateStr } from '@/utils'

export default {
  name: 'MchRechargeCountReport',
  components: { RangePicker, SingleLineChart },
  data() {
    return {
      queryParams: {
        payDateStart: getLastDateStr(6),
        payDateEnd: getLastDateStr(0)
      },
      data: [],
      loading: false,
    }
  },
  computed: {
    // 数据列表
    chartData() {
      return this.data.map(item => item.sum);
    },
    labels() {
      return this.data.map(item => item.key);
    },
    computedDate: {
      get() {
        return [this.queryParams.payDateStart, this.queryParams.payDateEnd]
      },
      set(val) {
        this.queryParams.payDateStart = val[0];
        this.queryParams.payDateEnd = val[1];
      }
    },
    totalIncome() {
      if (this.data == null) {
        return 0;
      }
      return this.data.reduce((pre, cur) => pre + cur.sum, 0);
    }
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.loading = true;
      appBonusDailyAmount(this.queryParams).then(res => {
        this.data = res.data;
      }).finally(() => {
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
