<template>
  <div>
    <el-form :model="searchForm" inline @submit.prevent>
      <el-form-item>
        <el-input
          v-model="searchForm.userNameOrPhone"
          placeholder="输入学员姓名/手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchForm.question"
          placeholder="输入题目"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchForm.answerResult"
          placeholder="选择答题结果"
          @change="handleQuery"
          style="width: 150px"
          clearable
        >
          <el-option label="答对" :value="1" />
          <el-option label="答错" :value="2" />
        </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="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 row.optionList" :key="index">
            <span>{{ item.op }}. {{ item.opDesc }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="正确答案" prop="trueAnswer" width="80" />
      <el-table-column label="答题时间" prop="practiceTime" width="110" />
      <el-table-column label="姓名" prop="userName" width="100" />
      <el-table-column label="手机号" prop="phone" width="120" />
      <el-table-column label="答题选择" prop="answer" width="80" />
      <el-table-column label="答题结果" prop="answerResult" width="100">
        <template #default="{ row }">
          <el-tag v-if="row.answerResult == 2" type="danger">答错</el-tag>
          <el-tag v-else type="success">答对</el-tag>
        </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"
    />
  </div>
</template>

<script setup>
import { searchStudyRecords } from '@/api/uav/record'

const searchForm = ref({
  userNameOrPhone: '',
  practiceType: 1,
  question: '',
  answerResult: undefined,
  pageNo: 1,
  pageSize: 50
})

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

onMounted(() => {
  handleQuery()
})
async function getList() {
  loading.value = true
  try {
    const resp = await searchStudyRecords(searchForm.value)
    tableList.value = resp.list || []
    total.value = resp.total || 0
  } finally {
    loading.value = false
  }
}

function handleQuery() {
  searchForm.value.pageNo = 1
  getList()
}
</script>

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