<template>
  <div class="capital-main-content">
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleTabClick">
      <el-tab-pane label="待处理" name="pending"> </el-tab-pane>
      <el-tab-pane label="全部" name="all"></el-tab-pane>
      <div class="search">
        <div class="left"></div>
        <div class="right">
          <div>
            <el-input v-model="search.keyword" style="width: 250px" placeholder="请输入资方名称 /授信编号" clearable />
          </div>
          <div>
            <el-select
              v-model="search.capitalStatus"
              placeholder="请选择资方状态"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in constant.capitalStatus"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              >
              </el-option>
            </el-select>
          </div>
          <div>
            <el-date-picker
              v-model="search.creditStartDate"
              type="date"
              placeholder="请选择授信生效日"
              value-format="yyyy-MM-dd"
              clearable
            >
            </el-date-picker>
          </div>
          <div>
            <el-date-picker
              v-model="search.creditEndDate"
              type="date"
              placeholder="请选择授信到期"
              value-format="yyyy-MM-dd"
              clearable
            >
            </el-date-picker>
          </div>
          <div class="leftBtn">
            <el-button type="primary" @click="fetchData">查询</el-button>
          </div>
        </div>
      </div>
      <cy-comm-table
        ref="commTable"
        v-loading="loading"
        parent-component="CapitalManage"
        :columns="columns"
        :data="tableData"
        :count="total"
        :table-options="tableOptions"
      />
    </el-tabs>
  </div>

</template>

<script>
import { mapGetters } from 'vuex'
import { getList } from '@/api/capitalManage/index'
export default {
  name: 'CapitalManage',
  data() {
    return {
      search: {
        keyword: '',
        capitalStatus: '',
        creditStartDate: '',
        creditEndDate: ''
      },
      total: 0,
      loading: false,
      activeName: 'pending',
      tableData: [],
      tableOptions: {
        maxHeight: 'calc(100vh - 410px)',
        height: 'calc(100vh - 410px)'
      },
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '授信编号',
          prop: 'creditNo',
          showTooltip: true
        },
        {
          label: '资方名称',
          prop: 'capitalName',
          showTooltip: true
        },
        {
          label: '机构类型',
          prop: 'institutionType',
          showTooltip: true
        },
        {
          label: '业务类型',
          prop: 'businessType',
          showTooltip: true
        },
        {
          label: '业务类型编号',
          prop: 'businessTypeNo',
          showTooltip: true,
          width: '120'
        },
        {
          label: '客户经理',
          prop: 'belongName',
          showTooltip: true
        },
        {
          label: '部门',
          prop: 'orgName',
          showTooltip: true
        },
        {
          label: '授信金额（元）',
          prop: 'creditAmount',
          showTooltip: true,
          width: '120',
          type: 'num'
        },
        {
          label: '用信金额（元）',
          prop: 'amount',
          showTooltip: true,
          width: '120',
          type: 'num'
        },
        {
          label: '可用余额（元）',
          prop: 'availableBalance',
          showTooltip: true,
          width: '120',
          type: 'num'
        },
        {
          label: '授信生效日',
          prop: 'creditStartDate',
          showTooltip: true,
          width: '120'
        },
        {
          label: '授信到期日',
          prop: 'creditEndDate',
          showTooltip: true,
          width: '120'
        },
        {
          label: '授信状态',
          prop: 'creditStatus',
          showTooltip: true
        },

        {
          label: '资方状态',
          prop: 'capitalStatus',
          showTooltip: true
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                msg: '修改',
                icon: 'iconfont icon-xiugai',
                power: 'capitalUpdate',
                tabPower: ['all', 'pending'],
                rowPower: ['待生效', '生效'],
                category: 'edit'
              },
              {
                msg: '查看',
                icon: 'iconfont icon-Magnifier',
                power: 'capitalView',
                tabPower: ['all', 'pending'],
                rowPower: [],
                category: 'see'
              }
            ]
            const options = btnList.map(item => {
              return (
                this.tablePower({ item, tab: this.activeName, 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) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 100
        }
      ]
    }
  },
  computed: {
    ...mapGetters(['constant']),
    tablePower() {
      return ({ item, tab, row }) => {
        const { tabPower, rowPower } = item
        if (rowPower && rowPower.length) {
          if (tabPower.includes(tab) && rowPower.includes(row.capitalStatus)) {
            return true
          }
        } else {
          if (tabPower.includes(tab)) {
            return true
          }
        }
      }
    }
  },
  created() {
    this.$store.dispatch('getConstant', ['capitalStatus'])
    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,
        flag: this.activeName
      }
      getList(params).then(({ rows, records }) => {
        this.tableData = rows
        this.total = records
        this.loading = false
      })
    },
    handleTabClick(tab) {
      this.activeName = tab.name
      this.page = 1
      this.size = 10
      this.fetchData()
    },
    handlerOperate(type, row) {
      this.$router.push({
        name: 'CapitalManageDetails',
        params: { id: row.id },
        query: {
          type
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
