<template>
  <div>
    <el-form :model="searchForm" inline @submit.prevent>
      <el-form-item>
        <el-input
          v-model="searchForm.userName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchForm.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchForm.idcard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchForm.carTypeId"
          placeholder="选择考试类型"
          clearable
          filterable
          @change="handleQuery"
        >
          <el-option
            v-for="item in carTypeOptions"
            :key="item.carTypeId"
            :label="item.carName"
            :value="item.carTypeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchForm.subject"
          placeholder="选择科目"
          clearable
          filterable
          @change="handleQuery"
        >
          <el-option label="科一" :value="1" />
          <el-option label="科四" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="searchForm.examTime"
          type="daterange"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          range-separator="-"
          start-placeholder="考试日期"
          end-placeholder="日期"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" v-hasPermi="['customer:exam-record:list']">搜索</el-button>
        <el-button @click="handleExport" v-hasPermi="['customer:exam-record:export']">
          导出
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableList" border stripe>
      <el-table-column type="index" width="50" />
      <el-table-column label="姓名" prop="userName" />
      <el-table-column label="手机号码" prop="phone" width="120" />
      <el-table-column label="身份证号" prop="idcard" min-width="120" />
      <el-table-column label="考试类型" prop="carTypeName" min-width="120" />
      <el-table-column label="考试科目" prop="subject" min-width="120" />
      <el-table-column label="考试得分" prop="score" min-width="120" />
      <el-table-column label="交卷时间" prop="createTime" min-width="120" />
      <el-table-column label="考试时长" prop="testTimeStr" min-width="120" />
    </el-table>
    <Pagination
      :total="total"
      v-model:page="searchForm.pageNo"
      v-model:limit="searchForm.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="ExamRecord">
import { removeNullField } from '@/utils'
import * as ExamRecordApi from '@/api/customer/examRecord.js'
import download from '@/utils/download'

import { getCustomerExamCarType } from '@/api/customer/customer.js'

const carTypeOptions = ref([])

const searchForm = ref({
  userName: undefined,
  carTypeId: undefined,
  pageNo: 1,
  pageSize: 20
})

onMounted(() => {
  getCustomerExamCarType().then((res) => {
    carTypeOptions.value = res
  })

  handleQuery()
})

/** 搜索按钮操作 */
const handleQuery = () => {
  searchForm.value.pageNo = 1
  getList()
}

const loading = ref(false)
const tableList = ref([])
const total = ref(0)
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    if (searchForm.value.examTime && searchForm.value.examTime.length > 0) {
      searchForm.value.startTime = searchForm.value.examTime[0] + ' 00:00:00'
      searchForm.value.endTime = searchForm.value.examTime[1] + ' 23:59:59'
    } else {
      searchForm.value.startTime = undefined
      searchForm.value.endTime = undefined
    }
    const data = await ExamRecordApi.getRecordPage(removeNullField(searchForm.value))
    tableList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

function handleExport() {
  const params = removeNullField(searchForm.value)
  if (params.examTime && params.examTime.length > 0) {
    params.startTime = params.examTime[0] + ' 00:00:00'
    params.endTime = params.examTime[1] + ' 23:59:59'
  } else {
    params.startTime = undefined
    params.endTime = undefined
  }
  ExamRecordApi.exportRecord(params).then((res) => {
    download.excel(res, '考试成绩.xls')
  })
}
</script>

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