<template>
  <div class="divide-accounts-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">
            <div>
              <el-input v-model="search.searchNumber" style="width: 250px" placeholder="请输入业务编号/保理合同编号" clearable />
            </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>
          </div>
        </div>
        <cy-comm-table
          ref="commTable"
          v-loading="loading"
          :columns="columns"  
          :data="tableData"
          parentComponent="DivideAccountsManage"
          :count="total"
          :table-options="tableOptions"
        />
      </el-tabs>
      <cy-dialog ref="timeAxisDialog" title="进度时间轴" top="15vh">
        <cy-time-axis ref="timeAxis" :time-axis-params="timeAxisParams"></cy-time-axis>
      </cy-dialog>
    </template>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { ledgerdoList, ledgerPendingList } from '@/api/divideAccountsManage/divideAccountsManage'
export default {
  name: 'DivideAccountsManage',
  data() {
    return {
      activeName: 'pending',
      search: {
        searchNumber: '',
        fastSearch: ''
      },
      timeAxisParams: {
        businessKey: '',
        businessType: 'LedgerInfoManage'
      },
      total: 0,
      loading: false,
      tableData: [],
      tableOptions: {
        maxHeight: 'calc(100vh - 406px)',
        height: 'calc(100vh - 406px)'
      },
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '分账单号',
          prop: 'ledgerNumber',
          showTooltip: true
        },
        {
          label: '业务编号',
          showTooltip: true,
          prop: 'businessNumber',
          render: (h, row) => {
            const btnList = row.assignVoList || []
            const options = btnList.map(item => {
              return (
                <el-tooltip class='item' effect='dark' content={item.businessNumber} placement='top'>
                  <div class='one-ellipsis'>
                    {item.businessNumber}
                  </div>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          }
        },
        {
          label: '保理合同编号',
          showTooltip: true,
          prop: 'userName',
          render: (h, row) => {
            const btnList = row.assignVoList || []
            const options = btnList.map(item => {
              return (
                <el-tooltip class='item' effect='dark' content={item.contractNo} placement='top'>
                  <div class='one-ellipsis'>
                    {item.contractNo}
                  </div>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          }
        },
        {
          label: '支用批次',
          showTooltip: true,
          prop: 'userName',
          render: (h, row) => {
            const btnList = row.assignVoList || []
            const options = btnList.map(item => {
              return (
                <el-tooltip class='item' effect='dark' content={item.quotaDisburseBatch} placement='top'>
                  <div class='one-ellipsis'>
                    {item.quotaDisburseBatch}
                  </div>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          }
        },
        {
          label: '项目名称',
          prop: 'userTypeName',
          render: (h, row) => {
            const btnList = row.assignVoList || []
            const options = btnList.map(item => {
              return (
                <el-tooltip class='item' effect='dark' content={item.projectName} placement='top'>
                  <div class='one-ellipsis'>
                    {item.projectName}
                  </div>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          }
        },
        {
          label: '主企业名称',
          prop: 'otherDutyName',
          render: (h, row) => {
            const btnList = row.assignVoList || []
            const options = btnList.map(item => {
              return (
                <el-tooltip class='item' effect='dark' content={item.mainEnterprise} placement='top'>
                  <div class='one-ellipsis'>
                    {item.mainEnterprise}
                  </div>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          }
        },
        {
          label: '核心企业名称',
          prop: 'otherDutyName',
          render: (h, row) => {
            const btnList = row.assignVoList || []
            const options = btnList.map(item => {
              return (
                <el-tooltip class='item' effect='dark' content={item.enterprise} placement='top'>
                  <div class='one-ellipsis'>
                    {item.enterprise}
                  </div>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          }
        },
        {
          label: '贸易平台',
          prop: 'taskName',
          render: (h, row) => {
            const btnList = row.assignVoList || []
            const options = btnList.map(item => {
              return (
                <el-tooltip class='item' effect='dark' content={item.platformName} placement='top'>
                  <div class='one-ellipsis'>
                    {item.platformName}
                  </div>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          }
        },
        {
          label: '入账金额（元）',
          prop: 'ledgerAmount',
          showTooltip: true,
          width: 120
        },
        {
          label: '当前阶段',
          prop: 'taskName'
        },
        {
          label: '执行人',
          prop: 'executor'
        },
        {
          label: '状态',
          prop: 'ledgerStatusDict.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: 'editLedger',
                rowPower: ['待处理', '处理中'],
                tabPower: ['pending'],
                category: 'edit'
              },
              {
                msg: '查看',
                icon: 'iconfont icon-Magnifier',
                power: 'viewLedger',
                rowPower: ['已完成', '分账审核', '待处理', '处理中'],
                tabPower: ['pending', 'all'],
                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([
      'name'
    ]),
    tablePower() {
      return ({ item, tab, row }) => {
        const { tabPower, rowPower } = item
        if (rowPower) {
          if (tabPower.includes(tab) && (rowPower.includes(row.ledgerStatusDict.name) || rowPower.includes(row.taskName))) {
            return true
          }
        } else {
          if (tabPower.includes(tab)) {
            return true
          }
        }
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      if (type === 'search') {
        this.page = 1
        this.size = 10
      }
      this.loading = true
      const params = {
        ...this.search,
        page: this.page || 1,
        rows: this.size || 10
      }
      if (this.activeName === 'pending') {
        ledgerPendingList({ ...params, simpleClassName: 'LedgerInfoManage' }).then(response => {
          this.tableData = response.rows
          this.loading = false
          this.total = response.records
        })
      } else {
        ledgerdoList(params).then(response => {
          this.tableData = response.rows
          this.loading = false
          this.total = response.records
        })
      }
    },
    handlerOperate(type, row) {
      const obj = {
        params: {
          id: row.id
        },
        query: {
          type,
          taskId: row.taskId
        }
      }
      if (type === 'timeAxis') {
        this.timeAxisClick(row)
        return
      }
      if (type === 'see') {
        this.$router.push({
          name: 'DivideAccountSee',
          ...obj
        })
      } else {
        if (row.taskName === '分账审核') {
          this.$router.push({
            name: 'DivideAccountApproval',
            ...obj
          })
        } else {
          this.$router.push({
            name: 'DivideAccountEdit',
            ...obj
          })
        }
      }
    },
    handleTabClick(tab) {
      this.activeName = tab.name
      this.fetchData('search')
    },
    timeAxisClick(row) {
      this.timeAxisParams.businessKey = row.id
      this.$refs.timeAxisDialog.show = true
      this.$nextTick(() => {
        this.$refs.timeAxis.fetchData()
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
