<template>
    <div class="execution">

      <el-card class="box-card header_bar" v-loading="loading">
        <div class="layout_h">
          <div class="layout_v">
            <span class="value_title">商家余额</span>
            <span class="value_content">￥{{balance}}</span>
          </div>
          <div class="layout_v" style="margin-left: 180px;">
            <span class="value_title">可提现金额</span>
            <span class="value_content">￥{{balanceValid}}</span>
          </div>
          <div class="weight_0_full layout_h_r">
            <div class="layout_v">
              <el-button type="primary" @click="onWithdraw">提 现</el-button>
              <el-button type="text" style="margin-top: 10px;" @click="onBindCard">提现银行卡：{{bankCardInfo}}</el-button>
            </div>
          </div>
        </div>
      </el-card>

        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :permission="permissionList"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @selection-change="onSelectionChange"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
                       @size-change="sizeChange"
                       @current-change="currentChange">
            </avue-crud>
        </basic-container>
        <bind-card :show="showAuth" @close="showAuth = false" @success="onAuthSuccess" />
        <apply-withdraw :show="showWithdraw" :wallet="walletInfo" @close="showWithdraw = false" @success="onWithdrawSuccess" />
    </div>
</template>

<script>
    import {fetchList,walletInfo} from '@/api/financial/wallet.js'
    import {tableOption} from '@/const/crud/financial/wallet.js'
    import {mapGetters} from 'vuex'
    import {
      ZmMixin
    } from '@/mixins/ZmMixin.js'

    export default {
        name: 'merchantWallet',
        mixins: [ZmMixin],
        components: {
          'bind-card': () => import('./components/BindCardPopup.vue'),
          'apply-withdraw': () => import('./components/ApplyWithdrawPopup.vue')
        },
        data() {
            return {
                loading: false,
                nowId: '',
                walletInfo: undefined,
                searchForm: {},
                tableData: [],
                selectedData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                showAuth: false,
                showWithdraw: false,
                tableLoading: false,
                tableOption: tableOption
            }
        },
        computed: {
            ...mapGetters(['permissions']),
            permissionList() {
                return {
                    addBtn: this.vaildData(this.permissions.user_withdraw_export, false),
                    delBtn: this.vaildData(this.permissions.general_basesystemmoney_del, false),
                    editBtn: this.vaildData(this.permissions.general_basesystemmoney_edit, false)
                };
            },
            balance() {
              if(this.walletInfo) {
                return this.walletInfo.balance || 0
              }
              return 0
            },
            balanceValid() {
              if(this.walletInfo) {
                return this.walletInfo.valid || 0
              }
              return 0
            },
            bankCardInfo() {
              if(this.walletInfo && this.walletInfo.bankCard) {
                let number = this.walletInfo.bankCard.cardNo
                let count = number.length
                return number.substr(count - 6, 6)
              }
              return '未绑定'
            }
        },
        mounted() {
          this.loadWallet()
        },
        methods: {
          loadWallet() {
            this.loading = true
            walletInfo()
            .then(r => {
              this.loading = false
              this.walletInfo = this.zmRes(r)
            })
            .catch(e => {
              this.loading = false
            })
          },
          queryParams(page, params) {
            let p = {
              current: page.currentPage,
              size: page.pageSize
            }
            if(params) {
              let range = this.parseRange(params)
              p = {
                ...p,
                startTime: range[0],
                endTime: range[1]
              }
            }
            return p
          },
            // 列表查询
            getList(page, params) {
                this.tableLoading = true
                let p = this.queryParams(page, params)
                fetchList(p).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                }).catch(() => {
                    this.tableLoading=false
                })
            },
            // 每页条数改变事件
            sizeChange(pageSize){
                this.page.pageSize = pageSize
            },
            // 当前页发生改变事件
            currentChange(current){
                this.page.currentPage = current
            },
            // 查询事件
            searchChange(form, done) {
                this.searchForm = form
                this.page.currentPage = 1
                this.getList(this.page, form)
                done()
            },
            // 刷新事件
            refreshChange() {
                this.getList(this.page)
            },
            //选择事件
            onSelectionChange(r) {
              this.selectedData = r
            },
            //点击提现
            onWithdraw() {
              if(this.walletInfo.bankCard) {
                this.showWithdraw = true
              } else {
                this.showAuth = true
              }
            },
            //点击银行卡
            onBindCard() {
              this.showAuth = true
            },
            //完成绑定
            onAuthSuccess() {
              this.showAuth = false
              this.loadWallet()
            },
            //完成提现申请
            onWithdrawSuccess() {
              this.showWithdraw = false
              this.loadWallet()
            }
        }
    }
</script>

<style scoped>

  .value_title {
    font-size: 16px;
  }

  .value_content {
    font-size: 32px;
    font-weight: 700;
    margin-top: 10px;
  }

  .header_bar {
    margin-left: 10px;
    margin-right: 10px;
  }

</style>
