<template>
  <div id="card-1">
    <el-row>
      <el-col :span="24" class="main">
        <span v-if="view === 'list'">
          <el-col :span="24" class="one">
            <el-col :span="24">
              <p>
                账户余额：<span>{{ info.total }}</span
                >元
              </p>
            </el-col>
            <el-col :span="24">
              可提现金额：<span style="color: red">{{ info.canGet }}</span
              >元
            </el-col>
            <el-col :span="24">
              <el-button type="success" @click="toCash()"> 提现 </el-button>
              <el-button type="success" @click="toRecord()"> 申请记录 </el-button>
            </el-col>
          </el-col>
          <el-col :span="24" class="two">
            <data-table :fields="fields" @query="search" :data="list" :total="total"> </data-table>
          </el-col>
        </span>
        <detail v-if="view === 'info'" :canGet="canGet" @toBack="toBack"></detail>
        <record v-if="view === 'record'" @toBack="toBack"></record>
      </el-col>
    </el-row>
  </div>
</template>
<script>
const _ = require('lodash');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('shopInBill');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
export default {
  name: 'card-1',
  props: {},
  components: {
    detail: () => import('./detail.vue'),
    record: () => import('./record.vue'),
  },
  data: function () {
    return {
      view: 'list',
      // 账户余额
      info: {},
      // 账户流水
      list: [],
      // 来源列表
      sourceList: [],
      fields: [
        {
          label: '来源',
          model: 'source',
          format: (i) => {
            let data = this.sourceList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
        { label: '净金额', model: 'receipts' },
        { label: '总金额', model: 'total' },
        { label: '抽成比例(%)', model: 'cut' },
        { label: '时间', model: 'time' },
      ],
      canGet: 0,
      total: 0,
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...mapActions(['query', 'fetch', 'create', 'update', 'delete', 'bill']),
    ...dictData({ dictQuery: 'query' }),
    // 查询
    async search({ skip = 0, limit = this.$limit, ...info } = {}) {
      let arr = await this.bill(this.user.shop.id);
      if (this.$checkRes(arr)) {
        this.$set(this, `info`, arr.data);
      }
      let res = await this.query({ skip, limit, ...info, shop: this.user.shop._id });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    toCash() {
      this.$set(this, `canGet`, this.info.canGet);
      if (this.info.canGet > 0) {
        this.$set(this, `view`, 'info');
      } else {
        this.$message({ type: `warning`, message: `您没有可提现金额` });
      }
    },
    toRecord() {
      this.$set(this, `view`, 'record');
    },
    // 执行返回
    toBack() {
      this.view = 'list';
    },
    // 查询其他信息
    async searchOther() {
      let res = await this.dictQuery({ code: 'cashBack_source' });
      if (this.$checkRes(res)) this.$set(this, `sourceList`, res.data);
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  text-align: center;
}
.one {
  margin: 0 0 10px 0;
  p {
    font-size: 24px;
    font-weight: 700;
    span {
      color: red;
    }
  }
}
.two {
  span {
    color: red;
  }
  .sp1 {
    font-size: 18px;
    font-weight: 500;
  }
}
.title {
  text-align: center;
}
.el-col {
  margin: 10px 0;
}
</style>
