<template>
  <div class="financing-main">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div>
          <el-input v-model="search.fastSearch" placeholder="请输入企业名称/合同编号" />
        </div>
        <!-- <div>
          <el-input v-model="search.searchNumber" placeholder="请输入合同编号" />
        </div> -->
        <div>
          <el-select v-model="search.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="item in constant.projectInitiationStatus"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </div>

        <div class="right-btn">
          <el-button type="primary" @click="fetchData('search')">查询</el-button>
          <el-button type="primary" @click="applyFinancingClick">申请</el-button>
        </div>
      </div>
    </div>
    <cy-comm-table
      ref="commTable"
      v-loading="loading"
      :columns="columns"
      :data="tableData"
      :count="count"
      parent-component="FinancingMain"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getList } from '@/api/myFinancing'
export default {
  name: 'FinancingMain',
  data() {
    return {
      count: 0,
      search: {
        fastSearch: '',
        status: ''
      },
      tableData: [{ id: 111, businessNumber: '2131231' }],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '客户名称',
          prop: 'businessNumber',
          showTooltip: true,
          width: 150
        },
        {
          label: '合同编号',
          showTooltip: true,
          prop: 'contractNumber',
          width: 150
        },
        {
          label: '支用批次',
          prop: 'quotaDisburseBatch',
          width: 120
        },
        {
          label: '申请融资金额（元）',
          prop: 'mainEnterpriseName',
          showTooltip: true
        },
        {
          label: '实际融资金额（元）',
          prop: 'mainEnterpriseName',
          showTooltip: true
        },
        {
          label: '当前阶段',
          prop: 'currentStage'
        },
        {
          label: '说明',
          prop: 'currentStage',
          showTooltip: true
        },
        {
          label: '状态',
          prop: 'financingStatus'
        },
        {
          label: '提交时间',
          prop: 'currentStage',
          showTooltip: true
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                msg: '查看',
                icon: 'iconfont icon-a-Group957',
                power: '',
                rowPower: ['额度支用', '支用审核', '合同制作', '合同签署', '合同审核', '付款申请', '付款审核', '已完成'],
                category: 'see'
              },
              // {
              //   msg: '编辑',
              //   icon: 'iconfont icon-a-Group8151',
              //   rowPower: ['合同签署', null],
              //   power: 'projectNumber:delete',
              //   category: 'edit'
              // },
              {
                msg: '还款计划',
                icon: 'iconfont icon-a-Group1087',
                power: '',
                // '已完成'
                rowPower: [],
                category: 'play'
              }
            ]
            const options = btnList.map(item => {
              // this.tablePower({ item, row }) &&
              return (
                <el-tooltip class='item' effect='dark' content={item.msg} placement='top'>
                  <span class='table-icon-box'>
                    <i class={item.icon} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 120
        }
      ],
      loading: false
    }
  },
  computed: {
    ...mapGetters(['constant']),
    tablePower() {
      return ({ item, tab, row }) => {
        const { tabPower, rowPower } = item
        if (rowPower) {
          if (rowPower.includes(row.currentStage) || rowPower.includes(row.financingStatus)) {
            if (rowPower.includes(null) && row.financingStatus === '已完成') {
              return false
            }
            return true
          }
        } else {
          if (tabPower.includes(tab)) {
            return true
          }
        }
      }
    }
  },
  created() {
    this.$store.dispatch('getConstant', ['projectInitiationStatus'])
  },
  mounted() {
    // this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type === 'search') {
        this.page = 1
        this.size = 10
      }
      const params = {
        ...this.search,
        page: this.page || 1,
        rows: this.size || 10
      }
      getList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.rows
        this.count = data.records
      })
    },
    applyFinancingClick() {
      this.$router.push({
        name: 'ApplyFinancing',
        params: {
          id: 'add'
        }
      })
    },
    handlerOperate(type, row) {
      if (type === 'play') {
        this.$router.push({
          name: 'RepaymentPlay',
          params: {
            id: row.id
          }
        })
      } else if (type === 'edit') {
        const { currentStage } = row
        if (currentStage === '合同签署') {
          this.$router.push({
            name: 'myFinancingContractSign',
            params: {
              id: row.id
            }
          })
        } else if (currentStage === null) {
          this.$router.push({
            name: 'ApplyFinancing',
            params: {
              id: row.id
            },
            query: {
              type: 'edit'
            }
          })
        }
      } else if (type === 'see') {
        this.$router.push({
          name: 'MyFinancingSee',
          params: {
            id: row.id
          },
          query: {
            type: 'see'
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.financing-main {

}
</style>
<style lang="scss">
.financing-main {
  .icon-a-Group957, .icon-a-Group1087 {
    font-size: 28px;
  }
}
</style>
