<!-- 工作流，抄送我的流程 -->
<template>
  <ContentWrap>
    <avue-crud
      ref="crudRef"
      v-model="tableForm"
      v-model:page="tablePage"
      v-model:search="tableSearch"
      :data="tableData"
      :option="tableOption"
      :permission="permission"
      @search-change="searchChange"
      @search-reset="resetChange"
      @refresh-change="getTableData"
      @size-change="sizeChange"
      @current-change="currentChange"
    >
      <template #menu="{ row }">
        <el-button link type="primary" @click="handleAudit(row)">流程</el-button>
      </template>
    </avue-crud>
  </ContentWrap>
</template>
<script lang="ts" setup>
import {dateFormatter} from '@/utils/formatTime'
import * as ProcessInstanceApi from '@/api/bpm/processInstance'

defineOptions({
  name: 'BpmCopyTask'
})

// 路由
const {push} = useRouter()
// 列表的加载中
const loading = ref(true)

const {getCurrPermi} = useCrudPermi()

// 表格的配置
const tableOption = reactive({
  addBtn: false,
  editBtn: false,
  delBtn: false,
  viewBtn: true,
  viewBtnText: '详情',
  viewBtnIcon: 'none',
  align: 'center',
  headerAlign: 'center',
  searchMenuSpan: 6,
  searchMenuPosition: 'left',
  labelSuffix: ' ',
  span: 24,
  dialogWidth: '50%',
  column: {
    taskId: {
      label: '任务编号'
    },
    taskName: {
      label: '任务名称'
    },
    processInstanceId: {
      label: '流程编号',
      search: true,
    },
    processInstanceName: {
      label: '所属流程',
      search: true,
    },
    startUserNickname: {
      label: '流程发起人',
    },
    reason: {
      label: '抄送原因'
    },
    creatorNickname: {
      label: '抄送人'
    },
    createTime: {
      label: '抄送时间',
      type: 'datetime',
      width: 180,
      formatter: (row: any, value: any, rowv: any, column: any) => {
        return dateFormatter(row, column, value)
      }
    },
    searchCreateTime: {
      label: '抄送时间',
      display: false,
      hide: true,
      search: true,
      searchType: 'datetimerange',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      startPlaceholder: '开始时间',
      endPlaceholder: '结束时间',
      searchRange: true,
    }
  }
})

const tableForm = ref<any>({})
const tableData = ref([])
const tableSearch = ref<any>({})
const tablePage = ref({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

const permission = getCurrPermi(['bpm:task'])

const crudRef = ref()
useCrudHeight(crudRef)

const getTableData = async () => {
  // 列表的加载中...
  loading.value = true
  // 拼接查询参数
  let searchObj = {
    ...tableSearch.value,
    pageNo: tablePage.value.currentPage,
    pageSize: tablePage.value.pageSize
  }
  // 处理时间参数，如果为空则去掉这个属性
  if (!searchObj.createTime?.length) delete searchObj.createTime
  // 去掉空字符串属性
  for (let key in searchObj) if (searchObj[key] === '') delete searchObj[key]

  // 请求我的待阅数据
  try {
    const data = await ProcessInstanceApi.getProcessInstanceCCPage(searchObj)
    tableData.value = data.list
    tablePage.value.total = data.total
  } finally {
    // 无论请求成功或者失败，这里都需要将加载中关闭
    loading.value = false
  }

}

const searchChange = (params: any, done: any) => {
  tablePage.value.currentPage = 1
  // 获取数据
  getTableData().finally(() => done())
}

const resetChange = () => {
  searchChange({}, () => {
  })
}

const sizeChange = (pageSize) => {
  tablePage.value.pageSize = pageSize
  resetChange()
}

const currentChange = (currentPage) => {
  tablePage.value.currentPage = currentPage
  getTableData()
}

const handleAudit = (row: any) => {
  push({
    name: 'BpmProcessInstanceDetail',
    query: {
      id: row.processInstanceId
    }
  })
}

/** 初始化 **/
onMounted(async () => {
  await getTableData()
})

</script>
