<template>
  <a-card size="small" :bordered="false" class="withdrawalManagementList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="提现时间">
                <rangeDateTime ref="rangeDate" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="withdrawalManagementList-refresh">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearchForm()" :disabled="disabled" id="withdrawalManagementList-reset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作按钮 -->
      <div class="table-operator">
        <a-button v-if="$hasPermissions('B_withdrawalManagement_cashOut')" id="withdrawalManagementList-tx" type="primary" class="button-warning" @click="handleCashOut">申请提现</a-button>
        <div style="display: inline-block;margin-left: 80px;">可提现余额： <strong>{{ accountInfo&&(accountInfo.balance || accountInfo.balance==0) ? '￥'+accountInfo.balance : '--' }}</strong> 元</div>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1320 }"
        :defaultLoadData="false"
        bordered>
      </s-table>
      <!-- 提现说明 -->
      <div class="descriptions">
        <p class="descriptions-tit">提现说明：</p>
        <ul class="descriptions-list">
          <li><em>1、</em>提现手续费为元/笔，从可提现余额中扣除。</li>
          <li><em>2、</em>提现到账时间正常为T+1工作日（例如当天提现，则第二天到账。如果遇到周末或法定节假日，则依次顺延）。</li>
          <li><em>3、</em>页面上的【可提现余额】包含当天收款但支付公司未清算金额，该部分金额只能在第二天清算后才可提现，如果提示【提现失败】和【账户余额不足】，则表明【可提现金额】中会存在该部分金额，遇到这种情况时，请减少当日提现金额或在次日进行提现。</li>
          <li><em>4、</em>提现账户为申请开通支付结算账户时所提供的账户信息。</li>
        </ul>
      </div>
    </a-spin>
    <!-- 申请提现 -->
    <cashOutModal :openModal="openModal" :nowData="accountInfo" @ok="handleOk" @close="closeModal" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDateTime from '@/views/common/rangeDateTime.vue'
import cashOutModal from './cashOutModal.vue'
import { satelliteWHWithdrawalList, satelliteWHWithdrawalAccount } from '@/api/satelliteWH'
export default {
  name: 'SatelliteWHWithdrawalList',
  components: { STable, VSelect, rangeDateTime, cashOutModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      advanced: true, //  高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      // 查询参数
      queryParam: {
        beginDate: '',
        endDate: ''
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '提现时间', dataIndex: 'cashOutDate', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '提现单号', dataIndex: 'withdrawalNo', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '申请提现金额', dataIndex: 'amount', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '开户名', dataIndex: 'bankAccount', width: 150, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '银行卡号', dataIndex: 'bankAccountNo', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'stateDictValue', width: 150, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'resultDesc', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return satelliteWHWithdrawalList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      openModal: false, //  弹框
      itemId: '', //  当前id
      nowData: null, //  当前记录数据
      accountInfo: null
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 账户信息
    getAccount () {
      satelliteWHWithdrawalAccount().then(res => {
        if (res.status == 200) {
          this.accountInfo = res.data
        } else {
          this.accountInfo = null
        }
      })
    },
    // 申请提现
    handleCashOut () {
      if (this.accountInfo && this.accountInfo.balance && this.accountInfo.serviceCharge) {
        const balance = Number(this.accountInfo.balance) * 100
        const serviceCharge = Number(this.accountInfo.serviceCharge) * 100
        if ((balance - serviceCharge) <= 0) {
          this.$message.warning('余额不足，暂不能提现！')
        } else {
          this.openModal = true
        }
      }
    },
    handleOk () {
      this.$refs.table.refresh(true)
      this.getAccount()
    },
    closeModal () {
      this.openModal = false
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.getAccount()
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235 - 217.5
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .withdrawalManagementList-wrap{
    .descriptions{
      margin-top: 45px;
      .descriptions-tit{
        font-size: 14px;
        font-weight: bold;
      }
      .descriptions-list{
        list-style: none;
        padding: 0;
        margin: 0;
        li{
          position: relative;
          padding: 5px 0 5px 20px;
          line-height: 1.5em;
          font-size: 13px;
          em{
            position: absolute;
            left: 0;
            top: 4px;
            font-style: normal;
          }
        }
      }
    }
  }
</style>
