<template>
  <div class="relation-disburse-main">
    <div class="search">
      <div class="left"></div>
      <div class="right">
        <div>
          <el-input v-model="search.fastSearch" placeholder="请输入资方名称" style="width: 200px" clearable></el-input>
        </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="count"
    />
    <cy-dialog ref="dialog" :title="dialog.title" width="50vw" top="15vh">
      <add-number v-if="dialog.type ==='addNumber'" ref="addNumber" :row-info="dialog.row" @modify-click="handlerOperate"></add-number>
      <relation-company v-if="dialog.type ==='company'" ref="relationCompany" :row-info="dialog.row" @modify-click="handlerOperate"></relation-company>
      <relation-disburse-list v-if="dialog.type ==='disburse'" ref="relationDisburseList" :row-info="dialog.row" @modify-click="handlerOperate"></relation-disburse-list>
    </cy-dialog>
  </div>
</template>

<script>
import { getRelationDisburseList } from '@/api/capitalManage/relationDisburse'
export default {
  components: {
    AddNumber: () => import('./components/AddNumber'),
    RelationCompany: () => import('./components/RelationCompany'),
    RelationDisburseList: () => import('./components/RelationDisburseList.vue')
  },
  data() {
    return {
      search: {
        fastSearch: ''
      },
      deepSearch: {},
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '资方名称',
          prop: 'institutionName',
          showTooltip: true
        },
        {
          label: '客户经理',
          showTooltip: true,
          prop: 'customerManager'
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                msg: '修改资方账号',
                icon: 'iconfont icon-xiugai',
                rowPower: [],
                power: 'addAccount',
                category: 'addNumber'
              },
              {
                msg: '关联企业',
                icon: 'iconfont icon-qiye',
                rowPower: [],
                power: 'addEnterprise',
                category: 'company'
              },
              {
                msg: '关联支用',
                icon: 'iconfont icon-tubiao11',
                rowPower: [],
                power: 'addEnterpriseDisburse',
                category: 'disburse'
              },
              {
                msg: '详情',
                icon: 'iconfont icon-xiangqing',
                rowPower: [],
                power: 'getDetailList',
                category: 'details'
              }
            ]
            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' v-power={item.power}>
                    <i class={item.icon} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 200
        }
      ],
      loading: false,
      tableData: [{
        institutionName: '1111'
      }],
      count: 0,
      dialog: {
        title: '',
        type: '',
        row: {}
      }
    }
  },
  computed: {
    tablePower() {
      return ({ item, tab, row }) => {
        const { tabPower, rowPower } = item
        if (rowPower) {
          if (rowPower.includes(row.currentStage) || rowPower.includes(row.financingStatus)) {
            if (rowPower.includes(null)) {
              return false
            }
            return true
          }
        } else {
          if (tabPower.includes(tab)) {
            return true
          }
        }
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      if (type === 'search') {
        this.page = 1
        this.size = 10
        this.deepSearch = JSON.parse(JSON.stringify(this.search))
      }
      if (Object.keys(this.deepSearch).length !== 0) {
        this.search = JSON.parse(JSON.stringify(this.deepSearch))
      }
      this.loading = true
      const params = {
        ...this.search,
        page: this.page || 1,
        rows: this.size || 10
      }
      getRelationDisburseList(params).then(({ data }) => {
        const { rows, records } = data
        this.loading = false
        this.tableData = rows
        this.count = records
      })
    },
    handlerOperate(type, row) {
      if (type === 'close') {
        this.$refs.dialog.show = false
      } else if (type === 'comfirm') {
        this.fetchData()
        this.$refs.dialog.show = false
      } else {
        if (type === 'details') {
          const { id, institutionName } = row
          this.$router.push({
            name: 'RelationDisburseDetails',
            params: { id: 'details' },
            query: {
              id,
              institutionName
            }
          })
        } else {
          this.$refs.dialog.show = true
        }
        if (type === 'addNumber') {
          this.dialog = { title: '添加资方登录账号', type, row }
        }
        if (type === 'company') {
          this.dialog = { title: '关联企业', type, row }
          setTimeout(() => {
            this.$refs.relationCompany.resetData()
          }, 300)
        }
        if (type === 'disburse') {
          this.dialog = { title: '关联支用', type, row }
          setTimeout(() => {
            this.$refs.relationDisburseList.getCreditData()
          }, 100)
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
