<!--待办事项列表-->
<template>
  <div>
    <TableHeader date-picker>
      <template v-slot:select>
        <el-form-item label='任务名称'>
          <el-input v-model='tableStore.table.params.searchValue' placeholder='请输入任务名称' />
        </el-form-item>
      </template>
    </TableHeader>
    <!--表格-->
    <Table ref='tableRef'></Table>
  </div>
</template>

<script setup lang='ts'>
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { onMounted, provide, ref,watch } from 'vue'
import { useRouter } from 'vue-router'
import { dateFormatter, formatPast2 } from '@/utils/formatTime'

defineOptions({
  name: 'businessUser'
})

const { push, options, currentRoute } = useRouter()
const flag = ref(false)
const tableStore = new TableStore({
  url: '/bpm-boot/bpm/task/doneList',
  method: 'POST',
  publicHeight: 65,
  column: [
    {
      field: 'index',
      title: '序号',
      width: '60',
      formatter: (row: any) => {
        return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
      }
    },
    { title: '任务名称', field: 'processInstance.name', minWidth: 130 },
    { title: '发起人', field: 'processInstance.startUser.name', minWidth: 130 },
    { title: '发起部门', field: 'processInstance.startUser.deptName', minWidth: 130 },
    { title: '当前任务', field: 'name', minWidth: 130 },
    { title: '发起时间', field: 'createTime', minWidth: 170 },
    { title: '结束时间', field: 'endTime', minWidth: 170 },
    {
      title: '审批状态',
      field: 'status',
      minWidth: 130,
      render: 'tag',
      custom: {
        1: 'primary',
        2: 'success',
        3: 'danger',
        4: 'warning'
      },
      replaceValue: {
        1: '审批中',
        2: '审批通过',
        3: '审批不通过',
        4: '已取消'
      }
    },
    { title: '审批建议', field: 'reason', minWidth: 150 },
    {
      title: '耗时',
      field: 'durationInMillis',
      minWidth: 150,
      formatter: (obj: any) => {
        return formatPast2(obj.row.durationInMillis)
      }
    },
    {
      title: '操作',
      align: 'center',
      minWidth: '150',
      fixed: 'right',
      render: 'buttons',
      buttons: [
        {
          name: 'productSetting',
          title: '流程详情',
          type: 'primary',
          icon: 'el-icon-EditPen',
          render: 'basicButton',
          click: row => {
            flag.value = true
            handleAudit(row.processInstance.id)
          }
        }
      ]
    }
  ],
  beforeSearchFun: () => {
    for (let key in tableStore.table.params) {
      if (tableStore.table.params[key] === '') {
        delete tableStore.table.params[key]
      }
    }
  }
})

onMounted(() => {
  // 加载数据
  tableStore.index()
})
tableStore.table.params.searchValue = ''
provide('tableStore', tableStore)

/** 处理审批按钮 */
const handleAudit = (instanceId: any) => {
  push({
    name: 'BpmProcessInstanceDetail',
    query: {
      id: instanceId
    }
  })
}

watch(
    () => currentRoute.value.path,
    () => {
        if (flag.value && options.history.state.forward?.split('/')[1] == 'bpm') {
            tableStore.index()
            flag.value = false
        }
    },
    {
        deep: true
    }
)
</script>
