<template>
  <cacp-complex-table class="cacp-pd-20" :actions="actions" :loading="loading" :data="tableData" :pagination="tablePagination"
    @on-page-change="onPageChange" stripe>
    <el-table-column prop="formTitle" label="标题"></el-table-column>
    <el-table-column prop="issueUserName" 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>
      <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 { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

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

import * as apis from '@/apis/government'
import config from '@/config'
import type { GovernmentInfo, GovernmentQueryInfo } from '@/types/government'

const actions: Array<TableAction> = [
  {
    key: 'insert',
    tooltip: '新增',
    type: 'primary',
    limit: 'none',
    position: 'left',
    icon: 'Plus',
    onclick: onInsert
  },
  {
    key: 'show',
    tooltip: '查看',
    type: 'default',
    limit: 'one',
    icon: 'View',
    onclick: onShow
  }
]

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

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

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

  const query: GovernmentQueryInfo = {
    title: '',
    startDate: '2024-01-01',
    endDate: '2024-12-31',
    pageSize: tablePagination.pageSize,
    pageIndex: tablePagination.currentPage
  }

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

  setLoading(false)
}

function onShow(info: GovernmentInfo): void {
  const r = router.resolve({
    path: 'gov-detail-show',
    query: { formId: info.formId }
  })
  window.open(r.href)
}

function onInsert(): void {
  const r = router.resolve({
    path: 'gov-detail-oper',
    query: { mode: 'create' }
  })
  window.open(r.href)
}

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

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