<template>
  <cacp-complex-table :actions="actions" :loading="loading" :data="tableData" :pagination="tablePagination" @on-page-change="onPageChange" stripe>
    <el-table-column prop="taskTitle" label="标题"></el-table-column>
    <el-table-column prop="senderName" label="发送人"></el-table-column>
    <el-table-column prop="createTime" label="创建时间">
      <template #default="scope">
        {{ dayjs(scope.row.createTime).format('YYYY-MM-DD') }}
      </template>
    </el-table-column>
    <el-table-column prop="expiredTime" label="过期时间">
      <template #default="scope">
        {{ dayjs(scope.row.expiredTime).format('YYYY-MM-DD') }}
      </template>
    </el-table-column>
    <template #title>
      <div style="font-size: 18px;font-weight: 700; text-align: center">用户待办信息</div>
    </template>
  </cacp-complex-table>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

import { useLoading, useComplexTable, SuccessResultCode, type Result, type RuntimeTask, type TableAction, type PageInfo } from '@cacp/ui'

// import * as apis from '@/apis/workflow/runtime'
import * as apis from '@/apis/government'
import config from '@/config'
import type {UserTaskQueryInfo } from '@/types/government'

const actions: Array<TableAction> = [
  {
    key: 'oper',
    tooltip: '操作',
    type: 'warning',
    limit: 'one',
    icon: 'Edit',
    onclick: onOperate
  }
]

const { loading, setLoading } = useLoading()
const { tableData, tablePagination, setPagination, setPageIndex, setPageSizes } = useComplexTable(config)

onMounted(async () => {
  
  await onQuery()
})

async function onQuery(): Promise<void> {
  setLoading(true)

  const query: UserTaskQueryInfo = {
    type: 'WAITING_TASK',
    pageSize: tablePagination.pageSize,
    pageIndex: tablePagination.currentPage
  }

  const res: Result<PageInfo<RuntimeTask>> = await apis.getUserTaskListByPage(query)
  if (res.code === SuccessResultCode && res.data) {
    setPagination(res.data)
  } else {
    ElMessage.error({
      message: res.message,
      showClose: true
    })
  }

  setLoading(false)
}


function onOperate(task: RuntimeTask): void {
  window.open(task.url)
}

function onPageChange(currentPage: number) {
  setPageIndex(currentPage)
  onQuery()
}

</script>

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