<template>
  <div v-loading="loading">
    <el-row type="flex" style="justify-content: space-between">
      <el-date-picker
        v-model="computedMonth"
        type="month"
        placeholder="选择月"
        @change="getData"
        :clearable="false"
      />
      <div>
        {{queryParams.year}}年{{queryParams.month}}月收入
        <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 { mchRechargeCount } from '@/api/mch/recharge'
import SingleLineChart from '@/components/SingleLineChart/index.vue'
import RangePicker from '@/components/RangePicker/index.vue'

export default {
  name: 'MchRechargeCountReport',
  components: { RangePicker, SingleLineChart },
  data() {
    return {
      queryParams: {
        year: new Date().getFullYear(),
        month: new Date().getMonth() + 1,
        groupBy: "create_date"
      },
      data: [],
      loading: false,
    }
  },
  computed: {
    // 数据列表
    chartData() {
      return this.data.map(item => item.recharge);
    },
    labels() {
      return this.data.map(item => item.createDate);
    },
    computedMonth: {
      get() {
        let date = new Date();
        date.setFullYear(this.queryParams.year);
        date.setMonth(this.queryParams.month - 1);
        console.log('get', date)
        return date;
      },
      set(val) {
        console.log('set', val)
        this.queryParams.year = val.getFullYear();
        this.queryParams.month = val.getMonth() + 1;
      }
    },
    totalIncome() {
      if (this.data == null) {
        return 0;
      }
      return this.data.reduce((pre, cur) => pre + cur.recharge, 0);
    }
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.loading = true;
      mchRechargeCount(this.queryParams).then(res => {
        this.data = res.data;
      }).finally(() => {
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
