<template>
  <div style="width: 100%;">
    <template>
      <div class="search">
        <div class="left"></div>
        <div class="right" style="width: auto;">
          <div>
            <el-input
              v-model="search.name"
              placeholder="请输入企业名称/统一社会信用代码"
              clearable
              style="width: 260px !important; margin-right: 10px;"
            ></el-input>
            <el-select
              v-model="search.status"
              placeholder="请选择准入状态"
              clearable
              style="width: 200px !important;"
            >
              <el-option
                v-for="item in constant.enterpriseEntryStatus"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </div>
          <div class="leftBtn">
            <el-button type="primary" @click="fetchData">查询</el-button>
          </div>
        </div>
      </div>
      <cy-comm-table
        ref="commTable"
        v-loading="loading"
        :columns="columns"
        :table-options="tableOptions"
        :data="tableData"
        :count="total"
        parent-component="EnterpriseList"
      />
    </template>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getEnterpriseList } from '@/api/myStoreEnterprise'

export default {
  name: 'EnterpriseList',
  data() {
    return {
      search: {
        name: '',
        status: ''
      },
      transCodeList: [],
      total: 0,
      page: 1,
      size: 10,
      loading: false,
      tableData: [],
      tableOptions: {
        sortTable: true,
        maxHeight: 'calc(100vh - 395px)',
        height: 'calc(100vh - 395px)'
      },
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '企业名称',
          prop: 'name',
          showTooltip: true
        },
        {
          label: '准入状态',
          prop: 'zrStatus',
          render: (h, row) => {
            return <div>{ this.getZrName(row.zrStatus) }</div>
          },
          showTooltip: true
        },
        {
          label: '阶段',
          prop: 'taskName',
          showTooltip: true
        },
        {
          label: '失败原因',
          showTooltip: true,
          prop: 'failReason'
        },
        {
          label: '基础额度（万元）',
          prop: 'amount',
          width: 140,
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.zrStatus === 1 ? '300' : '/' }</div>
          }
        },
        {
          label: '创建时间',
          prop: 'gmtCreate',
          showTooltip: true
        },
        {
          label: '操作',
          show: true,
          render: (h, row) => {
            const btnList = [
              {
                msg: '查看',
                icon: 'iconfont icon-a-Group8141',
                power: '', // viewFinancingLoanInfo
                rowPower: [1],
                category: 'see'
              },
              {
                msg: '补充材料',
                icon: 'iconfont icon-a-Group779',
                power: '', // viewFinancingLoanInfo
                rowPower: [],
                category: 'supInformation'
              },
              {
                msg: '修改',
                icon: 'iconfont icon-a-Group8151',
                power: '', // viewFinancingLoanInfo
                rowPower: [],
                category: 'edit'
              },
              {
                msg: '重新准入',
                icon: 'iconfont icon-a-Group8131',
                power: '', // viewFinancingLoanInfo
                rowPower: [],
                category: 'reAdd'
              }
            ]
            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: 160
        }
      ]
    }
  },
  mounted() {
    this.fetchData()
  },
  computed: {
    ...mapGetters(['constant']),
    tablePower() {
      return ({ item, row }) => {
        const { category } = item
        if (category === 'see') {
          if (row.companyZrId !== null) {
            return true
          }
          return false
        } else if (category === 'supInformation') {
          if (row.redNo === null && row.zrStatus === null && row.taskName === null) {
            return true
          } else if (row.redNo === null && row.zrStatus === null && row.taskName === '签署征信授权书') {
            return true
          }
          return false
        } else if (category === 'edit') {
          if (row.taskName === '填写企业资料' && row.taskId !== null) {
            return true
          }
          return false
        } else if (category === 'reAdd') {
          // TODO: 是否超过30天状态
          if (row.redNo === 1 && row.zrStatus === 0 && row.taskId === null) {
            return true
          }
          return false
        }
        return true
      }
    }
  },
  methods: {
    async fetchData() {
      const params = {
        page: this.page,
        rows: this.size,
        name: this.search.name
      }

      if (this.search.status !== '') {
        params.status = this.search.status
      }

      this.loading = true

      const res = await getEnterpriseList(params)

      this.loading = false

      if (res.success) {
        this.tableData = res.rows || []
        this.total = res.records
      }
    },
    async queryAllTransCode() {
      // const res = await getAllTransCode()

      // if (res.data.length) {
      //   this.transCodeList = res.data
      // }
    },
    handlerOperate(type, row, msg) {
      if (type === 'supInformation') {
        if (row.taskName === '签署征信授权书') {
          this.$router.push({
            name: 'StoreEnterpriseAuth',
            query: {
              zrId: row.companyZrId,
              taskId: row.taskId
            }
          })
        } else {
          this.$router.push({
            name: 'StoreEnterpriseSupplement',
            query: {
              type: 'supInfo',
              id: row.id,
              name: row.name,
              code: row.code,
              taskId: row.taskId || '',
              companyZrId: row.companyZrId
            }
          })
        }
      } else if (type === 'edit') {
        this.$router.push({
          name: 'StoreEnterpriseEdit',
          query: {
            type: 'edit',
            id: row.id,
            name: row.name,
            code: row.code,
            taskId: row.taskId || '',
            companyZrId: row.companyZrId
          }
        })
      } else if (type === 'reAdd') {
        this.$router.push({
          name: 'StoreEnterpriseReInvite',
          query: {
            type: 'reAdd',
            id: row.id,
            name: row.name,
            code: row.code,
            taskId: row.taskId || '',
            companyZrId: row.companyZrId
          }
        })
      } else if (type === 'see') {
        this.$router.push({
          name: 'StoreEnterpriseView',
          query: {
            type: 'see',
            id: row.id,
            name: row.name,
            code: row.code,
            companyZrId: row.companyZrId
          }
        })
      }
    },
    getZrName(status) {
      const map = {
        0: '不准入',
        1: '准入'
      }

      return map[status] || ''
    }
  }
}
</script>

<style lang="scss">

</style>
