<template>
  <div>
    <el-form :model="searchForm" inline @submit.prevent>
      <el-form-item>
        <el-input
          v-model="searchForm.userName"
          placeholder="输入学员姓名/手机号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchForm.isPass"
          placeholder="选择考试结果"
          @change="handleQuery"
          style="width: 150px"
          clearable
        >
          <el-option label="合格" :value="1" />
          <el-option label="不合格" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 搜索 </el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableList"
      highlight-current-row
      max-height="calc(100vh - 260px)"
    >
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="姓名" prop="userName" />
      <el-table-column label="手机号" prop="phone" />
      <el-table-column label="无人机类型" prop="modelName" />
      <el-table-column label="驾驶员类型" prop="typeName" />
      <el-table-column label="交卷时间" prop="submitTime" />
      <el-table-column label="模考时长" prop="testTimeStr" />
      <el-table-column label="得分" prop="score" />
      <el-table-column label="答题结果" prop="isPass">
        <template #default="{ row }">
          <el-tag v-if="row.isPass" type="success">合格</el-tag>
          <el-tag v-else type="danger">不合格</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button type="text" @click="handleView(row)">查看明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="margin-bottom: 0"
      v-show="total > 0"
      :total="total"
      v-model:page="searchForm.pageNo"
      v-model:limit="searchForm.pageSize"
      @pagination="getList"
    />

    <Dialog v-model="dialogVisible" title="考试详情" width="80%">
      <el-table :data="testDetailList" border stripe max-height="calc(100vh - 180px)">
        <el-table-column type="index" width="55" align="center" />
        <el-table-column label="题目" prop="question" />
        <el-table-column label="图片" width="120">
          <template #default="{ row }">
            <img v-if="row.imageUrl" :src="row.imageUrl" width="100" height="100" />
          </template>
        </el-table-column>
        <el-table-column label="选项">
          <template #default="{ row }">
            <div v-for="(item, index) in optionArr" :key="index">
              <span v-if="row['choose' + item]">{{ item }}. {{ row['choose' + item] }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="正确答案" prop="trueAnswer" width="85" />
        <el-table-column label="答题选择" prop="answer" width="85" />
        <el-table-column label="答题结果" prop="answerResult" width="100">
          <template #default="{ row }">
            <el-tag v-if="!row.answerResult" type="info">未答</el-tag>
            <el-tag v-else-if="row.answerResult == 2" type="danger">答错</el-tag>
            <el-tag v-else type="success">答对</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </Dialog>
  </div>
</template>

<script setup>
import { searchExamRecords, searchExamDetail } from '@/api/uav/record'

const searchForm = ref({
  userName: '',
  pageNo: 1,
  pageSize: 50
})

const loading = ref(false)
const total = ref(0)
const tableList = ref([])

onMounted(() => {
  handleQuery()
})
function getList() {
  loading.value = true
  try {
    searchExamRecords(searchForm.value).then((res) => {
      tableList.value = res.list
      total.value = res.total
    })
  } finally {
    loading.value = false
  }
}

function handleQuery() {
  searchForm.value.pageNo = 1
  getList()
}

const testDetailList = ref([])
const dialogVisible = ref(false)
const optionArr = ['A', 'B', 'C', 'D', 'E']
function handleView(row) {
  try {
    // 跳转到考试记录详情页面
    searchExamDetail({ testId: row.testId }).then((res) => {
      testDetailList.value = res
      dialogVisible.value = true
    })
  } finally {
  }
}
</script>

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