<template>
  <div class="exemption-follow">
    <router-view />
    <template v-if="!$route.params.id">
      <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">
            <template v-if="activeName === 'all'">
              <div>
                <el-input v-model="search.searchNumber" style="width: 250px" placeholder="请输入业务编号/保理合同编号/付款编号" clearable />
              </div>
              <div>
                <el-select
                  v-model="search.status"
                  placeholder="请选择状态"
                  clearable
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in constant.projectInitiationStatus"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  >
                  </el-option>
                </el-select>
              </div>
              <div>
                <el-input v-model="search.fastSearch" style="width: 300px" placeholder="请输入项目名称/主企业名称/核心企业名称" clearable></el-input>
              </div>
              <div class="leftBtn">
                <el-button type="primary" @click="fetchData">查询</el-button>
              </div>
            </template>
          </div>
        </div>
        <cy-comm-table
          ref="commTable"
          v-loading="loading"
          :columns="columns"
          :data="tableData"
          :count="total"
          :table-options="getTableOptions"
        />
      </el-tabs>
    </template>
    <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 { getList, downloadFile } from '@/api/exemptionFollow'
import { exportFile } from '@/utils'

export default {
  name: 'ExemptionFollow',
  data() {
    return {
      total: 0,
      loading: false,
      activeName: 'pending',
      search: {
        status: '',
        searchNumber: '',
        fastSearch: ''
      },
      tableData: [],
      tableOptions: {
        maxHeight: 'calc(100vh - 406px)',
        height: 'calc(100vh - 406px)'
      },
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '业务编号',
          prop: 'businessNumber',
          showTooltip: true
        },
        {
          label: '保理合同编号',
          showTooltip: true,
          prop: 'contractNumber',
          width: '120'
        },
        {
          label: '支用批次',
          prop: 'quotaDisburseBatch',
          showTooltip: true
        },
        {
          label: '付款编号',
          prop: 'paymentNo',
          width: '100',
          showTooltip: true
        },
        {
          label: '项目名称',
          prop: 'projectName',
          width: '120',
          showTooltip: true
        },
        {
          label: '主企业名称',
          prop: 'supplierName',
          showTooltip: true,
          width: '150'
        },
        {
          label: '核心企业名称',
          prop: 'customerName',
          showTooltip: true,
          width: '120'
        },
        {
          label: '贸易平台',
          prop: 'platformName',
          showTooltip: true,
          width: '120'
        },
        {
          label: '融资金额（元）',
          prop: 'financingAmount',
          showTooltip: true,
          width: '120'
        },
        {
          label: '当前阶段',
          prop: 'taskName',
          showTooltip: true
        },
        {
          label: '执行人',
          prop: 'executor',
          showTooltip: true
        },
        {
          label: '状态',
          prop: 'status.name',
          showTooltip: true
        },
        {
          label: '操作',
          show: true,
          render: (h, row) => {
            const btnList = [
              {
                msg: '时间进度轴',
                icon: 'iconfont icon-timeAxis',
                power: '',
                tabPower: ['all', 'pending'],
                category: 'timeAxis'
              },
              {
                msg: '编辑',
                icon: 'iconfont icon-xiugai',
                power: '',
                tabPower: ['pending'],
                rowPower: ['资料补充', '资料审批'],
                category: 'edit'
              },
              {
                msg: '查看',
                icon: 'iconfont icon-Magnifier',
                power: 'viewExemptionMatters',
                tabPower: ['all', 'pending'],
                // rowPower: [],
                category: 'see'
              },
              {
                msg: '下载',
                icon: 'iconfont icon-xiazai',
                power: 'downloadExemptionMatters',
                tabPower: ['all'],
                rowPower: ['已完成'],
                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'>
                    <i class={item.icon} v-power={item.power} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 120
        }
      ],
      timeAxisParams: {
        businessKey: '',
        businessType: 'ExemptionMatters'
      }
    }
  },
  computed: {
    ...mapGetters(['constant']),
    tablePower() {
      return ({ item, tab, row }) => {
        const { tabPower, rowPower } = item
        if (rowPower) {
          if (tabPower.includes(tab) && rowPower.includes(row.taskName) || rowPower.includes(row.status.name)) {
            return true
          }
        } else {
          if (tabPower.includes(tab)) {
            return true
          }
        }
      }
    },
    getTableOptions() {
      if (this.activeName === 'all') {
        return {
          maxHeight: 'calc(100vh - 406px)',
          height: 'calc(100vh - 406px)'
        }
      }
      return {
        maxHeight: 'calc(100vh - 306px)',
        height: 'calc(100vh - 306px)'
      }
    }
  },
  created() {
    this.$store.dispatch('getConstant', 'projectInitiationStatus')
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type === 'search') {
        this.page === 1
        this.size === 10
      }
      const params = {
        difference: this.activeName,
        page: this.page || 1,
        rows: this.size || 10,
        simpleClassName: 'ExemptionMatters',
        ...this.search
      }
      getList(params).then(({ data }) => {
        this.loading = false
        const { rows, records } = data
        this.tableData = rows
        this.total = records
      })
    },
    handlerOperate(type, row) {
      const { id, taskId } = row
      if (type === 'timeAxis') {
        this.timeAxisParams.businessKey = id
        this.$refs.timeAxisDialog.show = true
        this.$nextTick(() => {
          this.$refs.timeAxis.fetchData()
        })
      }
      if (type === 'see') {
        this.$router.push({
          name: 'ExemptionView',
          params: { id }
        })
      }
      if (type === 'download') {
        downloadFile({ exemptionMattersId: id }).then((data) => {
          exportFile(data)
        })
      }
      if (type === 'edit') {
        if (row.taskName === '资料补充') {
          this.$router.push({
            name: 'ExemptionSupplementaryInfo',
            params: { id },
            query: {
              taskId
            }
          })
        } else if (row.taskName === '资料审批') {
          this.$router.push({
            name: 'ExemptionDataReview',
            params: { id },
            query: {
              taskId
            }
          })
        }
      }
    },
    handleTabClick(tab) {
      this.activeName = tab.name
      this.fetchData('search')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
