<template>
  <div>
    <el-table v-loading="loading" :data="rechargeList">
      <el-table-column align="center" type="index" label="#"></el-table-column>
      <el-table-column align="center" label="时间" prop="createTime"></el-table-column>
      <el-table-column align="center" label="充值用户" prop="userName"></el-table-column>
      <el-table-column align="center" label="套餐名称" prop="suitName"></el-table-column>
      <el-table-column align="center" label="金额" prop="money">
        <template slot-scope="d">{{d.row.money | money}} 元</template>
      </el-table-column>
      <el-table-column align="center" label="时长">
        <template slot-scope="d">
          <span>{{d.row.suitTime}} {{suitTimeUnit(d.row.suitTimeUnit)}}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :auto-scroll="false"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getChargeList"
    />
  </div>
</template>

<script>
import {listBill} from "@/api/system/recharge";
import { findLabel } from '@/utils'

export default {
  name: 'rechargeRecord',
  dicts: ['time_unit'],
  props: {
    // 设备id
    deviceId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      rechargeList: [], // 充值记录列表
      loading: false,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        type: 1,
        deviceId: null,
      }
    }
  },
  computed: {
    // 时长单位
    suitTimeUnit() {
      return (unit) => {
        return findLabel(this.dict.type.time_unit, unit);
      }
    }
  },
  watch: {
    deviceId(nv, ov) {
      this.getChargeList(nv);
    }
  },
  created() {
    this.getChargeList(this.deviceId);
  },
  methods: {
    // 获取充值记录
    getChargeList(deviceId) {
      if(deviceId == null) {
        this.recordList = [];
        this.total = 0;
        return;
      }
      this.loading = true;
      this.queryParams.deviceId = deviceId | this.deviceId;
      listBill(this.queryParams).then(response => {
        this.rechargeList = response.rows;
        this.total = response.total;
      }).finally(() => {
        this.loading = false;
      });
    }
  }
}
</script>
