<template>
  <a-card size="small" :bordered="false" class="accountManagementList-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 :xl="6" :lg="6" :md="12" :sm="24">
              <a-form-item label="交易时间">
                <a-range-picker v-model="time" allowClear :disabledDate="disabledDate" format="YYYY-MM-DD" @change="dateChange"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="6" :md="6" :sm="6">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="outboundOrderList-refresh" >查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="outboundOrderList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div style="margin:10px 0">
        <div style="display: flex;align-items: center;">
          账户余额：<span>{{ pageInfo?toThousands(pageInfo.totalAmount,2):'--' }}</span>
        </div>
      </div>
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+77+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y:tableHeight }"
        :defaultLoadData="false"
        bordered>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
import { STable } from '@/components'
import { queryTradeFlowPage, merchantCashOutDetail } from '@/api/merchant'
import { toThousands } from '@/libs/tools.js'
export default {
  mixins: [commonMixin],
  components: { STable },
  data () {
    return {
      toThousands,
      spinning: false,
      disabled: false,
      tableHeight: 0,
      showModal: false,
      currentData: null,
      pageInfo: null,
      time: [],
      queryParam: {
        beginDate: '',
        endDate: ''
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '交易时间', dataIndex: 'tradeTime', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '交易说明', dataIndex: 'bizInfo', width: '30%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '交易类型', dataIndex: 'bizType', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '交易金额', dataIndex: 'amount', width: '10%', align: 'right', customRender: function (text) { return toThousands(text, 2) || '--' } },
        { title: '手续费', dataIndex: 'totalChannelFee	', width: '10%', align: 'center', customRender: function (text) { return toThousands(text, 2) || '--' } },
        { title: '账户变动', dataIndex: 'tradeAmount', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '变动后余额', dataIndex: 'afterBalance', width: '10%', align: 'right', customRender: function (text) { return toThousands(text, 2) || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const _this = this
        _this.disabled = true
        _this.spinning = true
        const paramsPage = Object.assign(parameter, this.queryParam) //  有分页
        paramsPage.merchantNo = this.pageInfo ? this.pageInfo.merchantNo : ''
        return queryTradeFlowPage(paramsPage).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            // this.getCount(paramsPage)
            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
        })
      }
    }
  },
  methods: {
    // 时间改变
    dateChange (date) {
      console.log(date, 'date')
      this.queryParam.beginDate = moment(date[0]).format('YYYY-MM-DD')
      this.queryParam.endDate = moment(date[1]).format('YYYY-MM-DD')
    },
    // 禁止选择今天之后的日期
    disabledDate (current) {
      return current && current > moment().endOf('day')
    },
    getPageInfo () {
      merchantCashOutDetail({}).then(res => {
        console.log(res, '===========')
        if (res.status == 200) {
          this.pageInfo = res.data
          this.$refs.table.refresh(true)
        } else {
          this.pageInfo = null
        }
      })
    },
    // 重置
    resetSearchForm () {
      this.time = []
      this.$refs.table.refresh(true)
    },
    // 申请提现
    handleTX () {
      this.showModal = true
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 265
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      _this.getPageInfo()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      setTimeout(() => {
        _this.setTableH()
      }, 400)
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .accountManagementList-wrap{

  }
</style>
