<template>
  <div class="loan-money">
    <router-view />
    <template v-if="!$route.query.type">
      <div class="search">
        <div />
        <div class="right">
          <div>
            <el-input v-model="search.fastSearch" placeholder="请输入供应商名称/用户唯一标识/合同编号/借据编号" clearable></el-input>
          </div>
          <div>
            <el-input v-model="search.payoutNo" placeholder="请输入申请编号" clearable></el-input>
          </div>
          <div>
            <el-date-picker
              v-model="search.payoutDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择放款日期"
            />
          </div>
          <div>
            <el-date-picker
              v-model="search.maturityDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择到期日期"
            />
          </div>
          <div>
            <el-select
              v-model="search.payoutStatus"
              placeholder="请选择放款状态"
              clearable
              style="width: 100%"
            >
              <el-option v-for="item in []" :key="item.label"></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 { queryLoanMoneyList } from '@/api/capitalFinancialManage'

export default {
  name: 'CapitalAuth',
  data() {
    return {
      loading: false,
      activeName: 'pending',
      tableData: [],
      page: 1,
      size: 10,
      total: 0,
      search: {
        fastSearch: '',
        payoutNo: '',
        payoutDate: '',
        maturityDate: '',
        payoutStatus: ''
      },
      tableOptions: {
        maxHeight: 'calc(100vh - 336px)',
        height: 'calc(100vh - 336px)'
      },
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '申请编号',
          prop: 'payoutNo',
          width: 150,
          showTooltip: true
        },
        {
          label: '供应商名称',
          prop: 'supplierName',
          width: 150,
          showTooltip: true
        },
        {
          label: '合同编号',
          prop: 'contractNo',
          width: 150,
          showTooltip: true
        },
        {
          label: '支用批次',
          showTooltip: true,
          render: (h, row) => {
            return (
              <div>{ row.quotaDisburseBatch }</div>
            )
          }
        },
        {
          label: '资方',
          showTooltip: true,
          prop: 'financingInstitutionName'
        },
        {
          label: '放款日期',
          showTooltip: true,
          prop: 'payoutDate'
        },
        {
          label: '到期日',
          showTooltip: true,
          prop: 'maturityDate'
        },
        {
          label: '放款金额',
          showTooltip: true,
          render: (h, row) => {
            return (
              <div>{ formatMoney(row.loanAmount) }</div>
            )
          }
        },
        {
          label: '放款状态',
          prop: 'payoutStatus',
          showTooltip: true,
          render: (h, row) => {
            return (
              <div>{ this.getPayoutStatus(row.payoutStatus) }</div>
            )
          }
        },
        {
          label: '借据编号',
          prop: 'dueBillNo',
          showTooltip: true,
          width: 120
        },
        {
          label: '用户唯一标识',
          prop: 'payeeInnerCardFlag',
          showTooltip: true,
          width: 120
        },
        {
          label: '失败原因',
          prop: 'failMsg',
          showTooltip: true
        },
        {
          label: '提款申请时间',
          prop: 'loanApplyDate',
          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'
              }
            ]
            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 true
      // return ({ item, row }) => {
      //   const { rowPower } = item
      //   if (rowPower) {
      //     // entryOutType 出入帐类型 1--入账 2--出账 出账只存在查看按钮
      //     if (rowPower.includes(row.cashStatus) || row.entryOutType === '2') {
      //       return false
      //     }
      //     // 入账且状态为已分配不展示编辑按钮
      //     if (row.entryOutType === '1' && row.cashStatus !== '1') {
      //       return false
      //     }
      //     return true
      //   } else {
      //     return true
      //   }
      // }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    async fetchData(type) {
      console.log(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 queryLoanMoneyList(params)

      this.loading = false
      if (res.success) {
        this.tableData = res.rows
        this.total = res.records
      }
    },
    handlerOperate(type, row, msg) {
      if (type === 'see') {
        console.log('see')
        this.$router.push({
          name: 'CapitalLoanMoneyDetail',
          query: {
            type: 'see',
            id: row.id
          }
        })
      }
    },
    getPayoutStatus(status) {
      const map = {
        '01': '处理中',
        '02': '放款成功',
        '03': '放款失败'
      }
      return map[status] || ''
    }
  }
}
</script>

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

}
</style>
