<template>
  <div class="capital-auth">
    <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-date-picker
              v-model="search.effectiveDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择放款日期"
            />
          </div>
          <div>
            <el-date-picker
              v-model="search.expiringDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择到期日期"
            />
          </div>
          <div>
            <el-select
              v-model="search.creditDate"
              placeholder="请选择授信状态"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in constant.smbCreditStatus"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </div>
          <div>
            <el-select
              v-model="search.limitStatus"
              placeholder="请选择额度状态"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in constant.smbLimitStatus"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </div>
          <div class="leftBtn">
            <el-button type="primary" @click="fetchData('search')">查询</el-button>
            <el-button type="primary" @click="handleAddCaptical">新增资方授信</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 { queryFinancialList, freshAuthRes } from '@/api/capitalFinancialManage'

export default {
  name: 'CapitalAuth',
  data() {
    return {
      loading: false,
      activeName: 'pending',
      tableData: [],
      page: 1,
      size: 10,
      total: 0,
      search: {
        fastSearch: '',
        effectiveDate: '',
        expiringDate: '',
        creditDate: '',
        limitStatus: ''
      },
      tableOptions: {
        maxHeight: 'calc(100vh - 336px)',
        height: 'calc(100vh - 336px)'
      },
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '额度编号',
          width: 150,
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.financingFromInvestorsNotice ? row.financingFromInvestorsNotice.limitNumber : '' }</div>
          }
        },
        {
          label: '供应商名称',
          width: 150,
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.enterprise ? row.enterprise.corporateName : '' }</div>
          }
        },
        {
          label: '资方',
          showTooltip: true,
          prop: 'refinancingManagementName',
          width: 300
        },
        {
          label: '授信状态',
          prop: 'creditStatus',
          showTooltip: true
          // render: (h, row) => {
          //   return <div>{ row.cashStatus === '1' ? '已创建' : row.cashStatus === '2' ? '已分配' : row.cashStatus === '3' ? '待处理' : '已完成' }</div>
          // }
        },
        {
          label: '资方授信额度',
          showTooltip: true,
          width: 200,
          render: (h, row) => {
            return <div>{ row.financingFromInvestorsNotice ? formatMoney(row.financingFromInvestorsNotice.remainingAvailableCreditLimit) : '' }</div>
          }
        },
        {
          label: '资方授信可用额度',
          showTooltip: true,
          width: 200,
          render: (h, row) => {
            return <div>{ row.financingFromInvestorsNotice ? formatMoney(row.financingFromInvestorsNotice.creditLimit) : '' }</div>
          }
        },
        {
          label: '拒绝原因',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.financingFromInvestorsNotice ? row.financingFromInvestorsNotice.rejectReason : '' }</div>
          }
        },
        {
          label: '额度状态',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.financingFromInvestorsNotice ? row.financingFromInvestorsNotice.limitStatus : '' }</div>
          }
        },
        {
          label: '生效日期',
          prop: 'financingFromInvestorsNotice.effectiveDate',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.financingFromInvestorsNotice ? row.financingFromInvestorsNotice.effectiveDate : '' }</div>
          }
        },
        {
          label: '失效日期',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.financingFromInvestorsNotice ? row.financingFromInvestorsNotice.expiringDate : '' }</div>
          }
        },
        {
          label: '操作',
          show: true,
          render: (h, row) => {
            const btnList = [
              // {
              //   msg: '时间进度轴',
              //   icon: 'iconfont icon-timeAxis',
              //   power: '',
              //   // tabPower: ['all', 'pending'],
              //   category: 'timeAxis'
              // },
              {
                msg: '查看',
                icon: 'iconfont icon-Magnifier',
                power: 'viewSmbRefinancing',
                // tabPower: ['all'],
                // rowPower: [],
                category: 'see'
              },
              {
                msg: '修改',
                icon: 'iconfont icon-xiugai',
                power: 'updateSmbRefinancingDetail',
                category: 'edit',
                rowPower: ['审批拒绝']
              },
              {
                msg: '续授信',
                icon: 'iconfont icon-guanlian',
                power: 'creditSmbRefinancing',
                category: 'delayAuth',
                rowPower: ['已过期']
              },
              {
                msg: '刷新授信结果',
                icon: 'iconfont icon-shuaxin',
                power: 'viewSmbRefinancingDetail',
                category: 'refreshAuth'
              }
            ]
            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'
    ]),
    tablePower() {
      return ({ item, row }) => {
        const { category, rowPower } = item
        if (category === 'edit' && rowPower) {
          if (rowPower.includes(row.creditStatus) || !row.creditStatus) {
            return true
          }
          return false
        }

        if (category === 'delayAuth' && rowPower) {
          if (rowPower.includes(row.creditStatus) || !row.creditStatus) {
            return true
          }
          return false
        }

        return true
      }
    },
    formatMoney() {
      return (val) => {
        return formatMoney(val)
      }
    }
  },
  mounted() {
    this.$store.dispatch('getConstant', ['smbCreditStatus', 'smbLimitStatus'])
    this.fetchData()
  },
  methods: {
    handleAddCaptical() {
      this.$router.push({
        name: 'CapitalAuthAdd',
        query: {
          type: 'add'
        }
      })
    },
    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 queryFinancialList(params)

      this.loading = false
      console.log(res)
      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: 'CapitalAuthDetail',
          query: {
            type: 'see',
            id: row.id
          }
        })
      } else if (type === 'delayAuth') {
        this.$router.push({
          name: 'CapitalAuthEdit',
          query: {
            type: 'edit',
            id: row.id,
            credit: true
          }
        })
      } else if (type === 'edit') {
        this.$router.push({
          name: 'CapitalAuthEdit',
          query: {
            type: 'edit',
            id: row.id
          }
        })
      } else if (type === 'refreshAuth') {
        this.refreshAtuh(row.id)
      }
    },
    async refreshAtuh(id) {
      const res = await freshAuthRes({ id })
      if (res.success) {
        this.$message.success('刷新授信结果成功')
        this.$router.go(0)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.capital-auth {

}
</style>
