<template>
  <div class="main-conetent">
    <div v-if="activeName === 'all'" class="search">
      <div />
      <div class="right">
        <div>
          <el-select v-model="search.status" placeholder="请选择状态">
            <el-option
              v-for="item in constant.c_supplier_status"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </div>
        <div>
          <el-input v-model="search.fastSearch" placeholder="请输入主企业名称" />
        </div>
        <div>
          <el-button type="primary" @click="fetchData('all')">查询</el-button>
        </div>
      </div>
    </div>
    <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>
      <cy-comm-table
        ref="commTable"
        v-loading="loading"
        parent-component="SupplierManage"
        :columns="columns"
        :data="tableData"
        :count="total"
        :table-options="getTableOptions"
      />
    </el-tabs>
    <cy-dialog ref="timeAxisDialog" title="进度时间轴" top="15vh">
      <cy-time-axis ref="timeAxis" :time-axis-params="timeAxisParams"></cy-time-axis>
    </cy-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { exportFile } from '@/utils/index'
import { supplierPendingList, supplierAllList, download } from '@/api/creditManage/supplierManage'
export default {
  name: 'SupplierManage',
  data() {
    return {
      total: 0,
      loading: false,
      activeName: 'pending',
      search: {
        status: '',
        fastSearch: ''
      },
      timeAxisParams: {
        businessKey: '',
        businessType: 'SupplierInfo'
      },
      tableData: [
        // { userName: '1111', statusName: '2131231', type: '尽调分配' },
        // { userName: '1111', statusName: '2131231', type: '尽调审核' },
        // { userName: '1111', statusName: '2131231', type: '尽调时间确认' },
        // { userName: '1111', statusName: '2131231', type: '视频尽调' },
        // { userName: '1111', statusName: '2131231', type: '准入报告' }

      ],
      tableOptions: {
        maxHeight: 'calc(100vh - 406px)',
        height: 'calc(100vh - 406px)'
      },
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '供应商企业名称',
          prop: 'supplierName',
          showTooltip: true
        },
        {
          label: '建工行业',
          prop: 'constructionIndustry',
          showTooltip: true,
          render: (h, row) => {
            return <span>{ row.constructionIndustry ? '是' : '否' }</span>
          }
        },
        {
          label: '供应商准入状态',
          showTooltip: true,
          render: (h, row) => {
            return <span>{ row?.statusDictionary?.name }</span>
          }
        },
        {
          label: '执行人',
          prop: 'assigneeName'
        },
        {
          label: '最近提交时间',
          prop: 'gmtModified'
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                msg: '时间进度轴',
                icon: 'iconfont icon-timeAxis',
                power: '',
                tabPower: ['all', 'pending'],
                category: 'timeAxis'
              },
              {
                msg: row.statusDictionary?.name,
                icon: 'iconfont icon-xiugai',
                power: '',
                tabPower: ['pending'],
                category: 'edit'
              },
              {
                msg: '查看',
                icon: 'iconfont icon-Magnifier',
                power: 'viewSupplierInfo',
                tabPower: ['all', 'pending'],
                // rowPower: [],
                category: 'see'
              },
              { msg: '下载报告', icon: 'iconfont icon-xiazai', power: 'downloadSupplierInfoReport', rowPower: ['授信成功'], tabPower: ['all'], category: 'download' }
            ]
            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) {
          if (tabPower.includes(tab) && rowPower.includes(row.statusName)) {
            return true
          }
        } else {
          if (tabPower.includes(tab)) {
            return true
          }
        }
      }
    },
    getTableOptions() {
      if (this.activeName === 'all') {
        return {
          sortTable: true,
          maxHeight: 'calc(100vh - 406px)',
          height: 'calc(100vh - 406px)'
        }
      }
      if (this.total > 0) {
        return {
          sortTable: true,
          maxHeight: 'calc(100vh - 360px)',
          height: 'calc(100vh - 360px)'
        }
      }
      return {
        sortTable: true,
        maxHeight: 'calc(100vh - 306px)',
        height: 'calc(100vh - 306px)'
      }
    }
  },
  created() {
    this.$store.dispatch('getConstant', ['c_supplier_status'])
  },
  mounted() {
    this.fetchData('pending')
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type === 'pending' || this.activeName === 'pending') {
        const params = {
          page: this.page,
          rows: this.size,
          simpleClassName: 'SupplierInfo'
        }
        supplierPendingList(params).then(({ rows, records }) => {
          this.loading = false
          this.tableData = rows
          this.total = records
        })
      }
      if (type === 'all' || this.activeName === 'all') {
        const allParams = {
          page: this.page,
          rows: this.size,
          ...this.search
        }
        supplierAllList(allParams).then(({ rows, records }) => {
          this.loading = false
          this.tableData = rows
          this.total = records
        })
      }
    },
    handlerOperate(type, row) {
      if (type === 'timeAxis') {
        this.timeAxisClick(row)
        return
      }
      if (type === 'see') {
        this.$router.push({
          name: 'SupplierManageSee',
          params: { id: row.id },
          query: {
            taskId,
            id: row.id,
            type
          }
        })
        return
      }
      if (type === 'download') {
        download({ supplierId: row.id }).then((data) => {
          exportFile(data)
        })
        return
      }
      const { statusDictionary: { name }, id, taskId, taskDefKey } = row
      if (name === '尽调分配') {
        this.$router.push({
          name: 'DueDistribution',
          params: { id },
          query: {
            taskId
          }
        })
      } else if (name === '尽调时间确认' && taskDefKey === 'due_diligence_confirm') {
        this.$router.push({
          name: 'TimeConfirm',
          params: { id },
          query: {
            taskId
          }
        })
      } else if (name === '视频尽调') {
        this.$router.push({
          name: 'VideoDue',
          params: { id },
          query: {
            taskId
          }
        })
      } else if (name === '尽调审核') {
        if (row.taskDefKey === 'second_review') {
          this.$router.push({
            name: 'SupplierAuditingTwo',
            params: { id },
            query: { taskId }
          })
        } else {
          this.$router.push({
            name: 'SupplierAuditing',
            params: { id },
            query: {
              taskId
            }
          })
        }
      } else if (name === '准入报告') {
        this.$router.push({
          name: 'AdmissionReport',
          params: { id },
          query: {
            taskId
          }
        })
      }
    },
    handleTabClick(tab) {
      this.activeName = tab.name
      this.page = 1
      this.size = 10
      this.fetchData(tab.name)
    },
    timeAxisClick(row) {
      this.timeAxisParams.businessKey = row.id
      this.$refs.timeAxisDialog.show = true
      this.$nextTick(() => {
        this.$refs.timeAxis.fetchData()
      })
    }
  }
}
</script>

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

}
</style>
