<template>
  <div class="part-box is-filter">
    <el-form inline>
      <el-form-item label="姓名">
        <el-input
          v-model.trim="searchModel.name"
          placeholder="请输入"
          clearable
          style="width: 120px"
        />
      </el-form-item>
      <el-form-item label="准考证号">
        <el-input
          v-model.trim="searchModel.examNumber"
          placeholder="请输入"
          clearable
          style="width: 120px"
        />
      </el-form-item>
      <el-form-item label="学号">
        <el-input
          v-model.trim="searchModel.studentCode"
          placeholder="请输入"
          clearable
          style="width: 120px"
        />
      </el-form-item>
      <el-form-item label="科目">
        <select-subject v-model="searchModel.subjectCode"></select-subject>
      </el-form-item>
    </el-form>
    <el-divider class="form-divider" />
    <div class="part-action">
      <el-space wrap>
        <el-button type="primary" @click="toPage(1)">异常查询</el-button>
        <el-button @click="onExport">导出</el-button>
        <el-button @click="startCheck">开始检查</el-button>
      </el-space>
    </div>
  </div>
  <div class="part-box">
    <el-table
      class="page-table"
      :data="dataList"
      :loading="loading"
      border
      stripe
    >
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="examNumber" label="准考证号" width="120" />
      <el-table-column prop="name" label="姓名" min-width="100" />
      <el-table-column prop="studentCode" label="学号" width="120" />
      <el-table-column prop="subjectCode" label="科目" min-width="100" />
      <el-table-column prop="batchCode" label="扫描批次" width="120" />
      <el-table-column prop="uploadTime" label="上传时间" width="160" />
    </el-table>
    <el-pagination
      v-model:current-page="pagination.pageNumber"
      v-model:page-size="pagination.pageSize"
      :layout="pagination.layout"
      :total="pagination.total"
      @size-change="pageSizeChange"
      @current-change="toPage"
    />
  </div>
</template>

<script setup lang="ts">
  import { reactive } from 'vue';
  import {
    getImageCheckDataList,
    startImageCheck,
    getImageCheckStatus,
  } from '@/api/check';
  import { ElMessage } from 'element-plus';
  import type {
    ImageCheckDataListItem,
    ImageCheckDataListFilter,
  } from '@/api/types/check';
  import useTable from '@/hooks/table';
  import { downloadExport } from '@/utils/download-export';

  defineOptions({
    name: 'ImageCheck',
  });

  const searchModel = reactive<ImageCheckDataListFilter>({
    name: '',
    examNumber: '',
    studentCode: '',
    subjectCode: null,
  });

  const { dataList, pagination, loading, toPage, pageSizeChange } =
    useTable<ImageCheckDataListItem>(getImageCheckDataList, searchModel, false);

  async function onExport() {
    await downloadExport('exportImageCheckData', searchModel);
  }

  async function startCheck() {
    const checkStatus = await getImageCheckStatus();
    if (checkStatus) {
      ElMessage.error('检查已启动');
      return;
    }
    const res = await startImageCheck();
    if (res.success) {
      ElMessage.success('检查已启动');
    } else {
      ElMessage.error(res.message);
    }
  }
</script>
