<template>
  <div class="pay-money">
    <router-view />
    <template v-if="!$route.query.type">
      <div class="search">
        <div />
        <div class="right">
          <div>
            <el-input v-model="search.keyword" placeholder="请输入供应商名称/用户唯一标识/合同编号/借据编号" clearable></el-input>
          </div>
          <div>
            <el-input v-model="search.repayNo" placeholder="请输入还款编号" clearable></el-input>
          </div>
          <div>
            <el-date-picker
              v-model="search.paymentDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择还款日期"
            />
          </div>
          <div>
            <el-select
              v-model="search.paymentStatus"
              placeholder="请选择还款状态"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in constant.rePayTypeList"
                :key="item.label"
                :value="item.code"
                :label="item.name"
              >
              </el-option>
            </el-select>
          </div>
          <div class="leftBtn">
            <el-button type="primary" @click="fetchData('search')">查询</el-button>
          </div>
        </div>
      </div>
      <cy-comm-table
        ref="commTable"
        v-loading="loading"
        :columns="columns"
        :data="tableData"
        :count="total"
        :table-options="tableOptions"
      />
    </template>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { formatMoney } from '@/utils/index'
import { getPaymentList, repayPayment } from '@/api/capitalFinancialManage/index'

export default {
  name: 'CapitalAuth',
  data() {
    return {
      loading: false,
      tableData: [],
      page: 1,
      size: 10,
      total: 0,
      search: {
        keyword: '',
        repayNo: '',
        paymentDate: '',
        paymentStatus: ''
      },
      tableOptions: {
        maxHeight: 'calc(100vh - 336px)',
        height: 'calc(100vh - 336px)'
      },
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '还款编号',
          prop: 'repayNo',
          width: 150,
          showTooltip: true
        },
        {
          label: '供应商名称',
          prop: 'enterprise.corporateName',
          width: 150,
          showTooltip: true
        },
        {
          label: '合同编号',
          prop: 'contractNo',
          width: 150,
          showTooltip: true
        },
        {
          label: '支用批次',
          showTooltip: true,
          prop: 'quotaDisburseBatch'
        },
        {
          label: '资方',
          showTooltip: true,
          prop: 'financingInstitutionName'
        },
        {
          label: '还款总金额',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.hxAmt ? this.formatMoney(row.hxAmt) : '' }</div>
          }
        },
        {
          label: '归还利息',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.hxInte ? this.formatMoney(row.hxInte) : '' }</div>
          }
        },
        {
          label: '归还本金',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.hxCapi ? this.formatMoney(row.hxCapi) : '' }</div>
          }
        },
        {
          label: '放款状态',
          prop: 'cashStatus',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ this.getRepayStatus(row.repayStatus) }</div>
          }
        },
        {
          label: '还款方式',
          prop: 'repayType',
          showTooltip: true,
          width: 120
        },
        {
          label: '用户唯一标识',
          prop: 'openId',
          showTooltip: true,
          width: 150
        },
        {
          label: '失败原因',
          prop: 'remark',
          showTooltip: true
        },
        {
          label: '还款日期',
          prop: 'repayDate',
          showTooltip: true,
          width: 120
        },
        {
          label: '借据编号',
          prop: 'dueBillNo',
          showTooltip: true,
          width: 120
        },
        {
          label: '操作',
          show: true,
          render: (h, row) => {
            console.log(this)
            const btnList = [
              // {
              //   msg: '时间进度轴',
              //   icon: 'iconfont icon-timeAxis',
              //   power: '',
              //   // tabPower: ['all', 'pending'],
              //   category: 'timeAxis'
              // },
              {
                msg: '查看',
                icon: 'iconfont icon-Magnifier',
                power: '',
                // tabPower: ['all'],
                // rowPower: [],
                category: 'see'
              },
              {
                msg: '重新还款',
                icon: 'iconfont icon-shuaxin',
                power: '',
                // tabPower: ['all'],
                rowPower: ['03'],
                category: 'rePay'
              }
            ]
            const options = btnList.map(item => {
              return (
                this.tablePower({ item, row }) && <el-tooltip class='item' effect='dark' content={item.msg} placement='top' >
                  <span class='table-icon-box' v-power={item.power}>
                    <i class={item.icon} onClick={() => { this.handlerOperate(item.category, row, item.msg) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 150
        }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'constant'
    ]),
    formatMoney() {
      return (val) => {
        return formatMoney(val)
      }
    },
    tablePower() {
      return ({ item, row }) => {
        const { rowPower } = item
        if (rowPower) {
          if (rowPower.includes(row.repayStatus)) {
            return true
          }
          return false
        }
        return true
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    async fetchData(type) {
      if (type === 'search') {
        this.page = 1
        this.size = 10
      }

      this.loading = true
      const params = {
        ...this.search,
        page: this.page || 1,
        rows: this.size || 10
      }

      const res = await getPaymentList(params)

      this.loading = false
      if (res.success) {
        this.tableData = res.rows
        this.total = res.records
      }
    },
    handlerOperate(type, row, msg) {
      if (type === 'see') {
        this.$router.push({
          name: 'CapitalPaymentDetail',
          query: {
            type: 'see',
            id: row.id
          }
        })
      } else if (type === 'rePay') {
        console.log('rePay')
        this.retryPayment(row.id)
      }
    },
    async retryPayment(id) {
      const res = await repayPayment({ id })

      if (res.success) {
        this.$message.success('更新成功')
        this.$router.go(0)
      }
    },
    getRepayStatus(status) {
      const map = {
        '01': '还款成功',
        '02': '还款失败',
        '03': '还款中'
      }

      return map[status] || ''
    }
  }
}
</script>

<style lang="scss" scoped>
.pay-money {

}
</style>
