<template>
  <div class="financing-manage">
    <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('search')">查询</el-button>
              </div>
            </template>
          </div>
        </div>
        <cy-comm-table
          ref="commTable"
          v-loading="loading"
          :columns="columns"
          :data="tableData"
          :count="total"
          :table-options="getTableOptions"
          parent-component="FinancingManage"
        />
      </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, getAllList } from '@/api/financingManage'

export default {
  name: 'FinancingManage',
  data() {
    return {
      total: 0,
      loading: false,
      activeName: 'pending',
      getStatusList: [],
      search: {
        status: '',
        searchNumber: '',
        fastSearch: ''
      },
      tableData: [],
      timeAxisParams: {
        businessKey: '',
        businessType: 'FinancingApply'
      },
      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: 'projectName',
          showTooltip: true
        },
        {
          label: '主企业名称',
          prop: 'supplierName',
          width: '100',
          showTooltip: true
        },
        {
          label: '核心企业名称',
          prop: 'customerName',
          width: '120',
          showTooltip: true
        },
        {
          label: '贸易平台',
          prop: 'platformName',
          showTooltip: true
        },
        {
          label: '融资金额（元）',
          prop: 'financingAmount',
          showTooltip: true,
          width: '120'
        },
        {
          label: '当前阶段',
          prop: 'stageStr',
          showTooltip: true
        },
        {
          label: '执行人',
          prop: 'executor',
          showTooltip: true,
          render: (h, row) => {
            return <div>{this.activeName === 'all' ? row.assignee?.name : row.executor}</div>
          }
        },
        {
          label: '状态',
          prop: 'statusStr',
          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'],
                category: 'edit'
              },
              {
                msg: '查看',
                icon: 'iconfont icon-Magnifier',
                power: 'code',
                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'>
                    <i class={item.icon} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 120
        }
      ]
    }
  },
  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 {
          maxHeight: 'calc(100vh - 406px)',
          height: 'calc(100vh - 406px)'
        }
      }
      return {
        maxHeight: 'calc(100vh - 366px)',
        height: 'calc(100vh - 366px)'
      }
    }
  },
  mounted() {
    this.$store.dispatch('getConstant', ['projectInitiationStatus'])
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      if (type === 'search') {
        this.page = 1
        this.size = 10
      }
      const params = {
        page: this.page || 1,
        rows: this.size || 10,
        simpleClassName: 'FinancingApply',
        // difference: this.activeName,
        ...this.search
      }
      if (this.activeName === 'all') {
        getAllList(params).then(({ rows, records }) => {
          this.tableData = rows
          this.total = records
        })
      } else {
        getList(params).then(({ rows, records }) => {
          this.tableData = rows
          this.total = records
        })
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          return false
        }
      })
    },
    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: 'FinancingView',
          params: { id },
          query: {
            type: 'see'
          }
        })
      }
      if (type === 'edit') {
        if (row.taskName === '额度支用') {
          this.$router.push({
            name: 'LimitExpenditure',
            params: { id },
            query: {
              taskId
            }
          })
        } else if (row.taskName === '支用审核') {
          this.$router.push({
            name: 'DisburseApproval',
            params: { id },
            query: {
              taskId
            }
          })
        } else if (row.taskName === '合同制作') {
          this.$router.push({
            name: 'ContractPreparation',
            params: { id },
            query: {
              taskId
            }
          })
        } else if (row.taskName === '合同审核') {
          this.$router.push({
            name: 'ContractApproval',
            params: { id },
            query: {
              taskId
            }
          })
        } else if (row.taskName === '付款申请') {
          this.$router.push({
            name: 'PaymentApplication',
            params: { id },
            query: {
              taskId
            }
          })
        } else if (row.taskName === '豁免-业务审核') {
          this.$router.push({
            name: 'FinancingBusinessReview',
            params: { id },
            query: {
              taskId,
              approvalType: 'business'
            }
          })
        } else if (row.taskName === '豁免-风控审核') {
          this.$router.push({
            name: 'FinancingRiskReview',
            params: { id },
            query: {
              taskId,
              approvalType: 'risk'
            }
          })
        } else if (row.taskName === '付款审核') {
          this.$router.push({
            name: 'PaymentApprove',
            params: { id },
            query: {
              taskId
            }
          })
        }
      }
    },
    handleTabClick(tab) {
      this.activeName = tab.name
      this.fetchData('search')
    }
  }
}
</script>

<style lang="scss" scoped>
.sales-contract-management {

}
</style>
