<!-- 证书逾期提醒列表 -->
<script name="RemindList" lang="ts" setup>
import { ElMessage } from 'element-plus'
import type { IListQuery } from './remind-interface'
import { getCertList, remindCert } from '@/api/resource/certRemind'
import type { TableColumn } from '@/components/NormalTable/table_interface'

// 定义变量
const { proxy } = getCurrentInstance() as any
const $router = useRouter()
const searchQuery = ref<IListQuery>({
  name: '',
  deptName: '',
  offset: 1,
  limit: 20,
})
const loadingTable = ref<boolean>(false) // 表格loading
const total = ref<number>(0) // 数据总条数
const columns = ref<Array<TableColumn>>([
  { text: '人员编号', value: 'staffNo', align: 'center', width: '160' },
  { text: '姓名', value: 'staffName', align: 'center' },
  { text: '工作部门', value: 'deptName', align: 'center' },
  { text: '计量专业', value: 'major', align: 'center' },
  { text: '证号', value: 'certNo', align: 'center' },
  { text: '发证日期', value: 'grantDate', align: 'center' },
  { text: '证书有效日期', value: 'effectiveDate', align: 'center' },
]) // 表头
const list = ref([]) // 表格数据

// 逻辑

// 获取数据列表
const fetchData = (isNowPage = false) => {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    searchQuery.value.offset = 1
  }
  getCertList(searchQuery.value).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      total.value = Number(res.data.total)
    }
    loadingTable.value = false
  }).catch((_) => {
    loadingTable.value = false
  })
}

const search = () => {
  fetchData()
}

const clearList = () => {
  searchQuery.value = {
    name: '',
    deptName: '',
    offset: 1,
    limit: 20,
  }
  fetchData()
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.value.limit = val.size
  }
  if (val && val.page) {
    searchQuery.value.offset = val.page
  }
  fetchData(true)
}

// 查看详情信息
const detail = (row: any) => {
  sessionStorage.setItem('certInfo', JSON.stringify(row))
  $router.push({
    query: {
      type: 'detail',
      id: row.id,
    },
    path: 'remind/detail',
  })
}

// 证书提醒
const remindCertById = (row: any) => {
  remindCert({ id: row.id }).then((res) => {
    if (res.code === 200) {
      ElMessage.success(`编号 ${row.certNo} 的证书提醒成功`)
    }
    else {
      ElMessage.error(`编号 ${row.certNo} 的证书提醒失败: ${res.message}`)
    }
  })
}

const batchImport = () => {

}

const exportToExcel = () => {

}

onMounted(() => {
  fetchData()
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="clearList">
      <search-item>
        <el-input v-model="searchQuery.name" placeholder="姓名" clearable />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.deptName" placeholder="工作部门" clearable />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container>
      <!-- 表头区域 -->
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm(`/resource/person/remind/import`)" icon="icon-import" title="批量导入" @click="batchImport" />
        <icon-button v-if="proxy.hasPerm(`/resource/person/remind/export`)" icon="icon-export" title="导出" @click="exportToExcel" />
      </template>

      <!-- 表格区域 -->
      <normal-table
        :data="list" :total="total" :columns="columns"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }"
        :list-loading="loadingTable"
        @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column fixed="right" label="操作" align="center" width="130">
            <template #default="{ row }">
              <el-button size="small" type="primary" link @click="detail(row)">
                详情
              </el-button>
              <el-button size="small" type="primary" link @click="remindCertById(row)">
                提醒
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>
