<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="答卷id" prop="examineeId">
        <el-input v-model="queryParams.examineeId" placeholder="请输入答卷id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="学生用户id" prop="studentId">
        <el-input v-model="queryParams.studentId" placeholder="请输入学生用户id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="题id" prop="questionId">
        <el-input v-model="queryParams.questionId" placeholder="请输入题id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="题号" prop="seq">
        <el-input v-model="queryParams.seq" placeholder="请输入题号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="知识点" prop="knowledgeId">
        <el-input v-model="queryParams.knowledgeId" placeholder="请输入知识点" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="批改答案" prop="correct">
        <el-input v-model="queryParams.correct" placeholder="请输入批改答案" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="最长时间" prop="duration">
        <el-input v-model="queryParams.duration" placeholder="请输入最长时间" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="更新次数" prop="count">
        <el-input v-model="queryParams.count" placeholder="请输入更新次数" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态 1 正确" prop="state">
        <el-input v-model="queryParams.state" placeholder="请输入状态 1 正确" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="评级" prop="scoreLevel">
        <el-input v-model="queryParams.scoreLevel" placeholder="请输入评级" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="评分" prop="score">
        <el-input v-model="queryParams.score" placeholder="请输入评分" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="得分率" prop="scoreRate">
        <el-input v-model="queryParams.scoreRate" placeholder="请输入得分率" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['learn:answer:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['learn:answer:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['learn:answer:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['learn:answer:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="answerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <el-table-column label="答题id" align="center" prop="answerId" fixed="left" />
      <el-table-column label="答卷id" align="center" prop="examineeId" />
      <el-table-column label="学生用户id" align="center" prop="studentId" min-width="100" />
      <el-table-column label="题id" align="center" prop="questionId" min-width="100" />
      <el-table-column label="题号" align="center" prop="seq" />
      <el-table-column label="知识点" align="center" prop="knowledgeId" />
      <el-table-column label="学生答案" align="center" prop="answer" />
      <el-table-column label="批改答案" align="center" prop="correct" />
      <el-table-column label="最长时间" align="center" prop="duration" />
      <el-table-column label="更新次数" align="center" prop="count" />
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="评级" align="center" prop="scoreLevel" />
      <el-table-column label="评分" align="center" prop="score" />
      <el-table-column label="得分率" align="center" prop="scoreRate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" min-width="140">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['learn:answer:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['learn:answer:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改答卷答案对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="answerRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="答卷id" prop="examineeId">
          <el-input v-model="form.examineeId" placeholder="请输入答卷id" />
        </el-form-item>
        <el-form-item label="学生用户id加速" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生用户id加速" />
        </el-form-item>
        <el-form-item label="题id" prop="questionId">
          <el-input v-model="form.questionId" placeholder="请输入题id" />
        </el-form-item>
        <el-form-item label="题号" prop="seq">
          <el-input v-model="form.seq" placeholder="请输入题号" />
        </el-form-item>
        <el-form-item label="知识点" prop="knowledgeId">
          <el-input v-model="form.knowledgeId" placeholder="请输入知识点" />
        </el-form-item>
        <el-form-item label="学生答案" prop="answer">
          <el-input v-model="form.answer" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="批改答案" prop="correct">
          <el-input v-model="form.correct" placeholder="请输入批改答案" />
        </el-form-item>
        <el-form-item label="最长时间" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入最长时间" />
        </el-form-item>
        <el-form-item label="更新次数" prop="count">
          <el-input v-model="form.count" placeholder="请输入更新次数" />
        </el-form-item>
        <el-form-item label="状态 1 正确" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态 1 正确" />
        </el-form-item>
        <el-form-item label="评级" prop="scoreLevel">
          <el-input v-model="form.scoreLevel" placeholder="请输入评级" />
        </el-form-item>
        <el-form-item label="评分" prop="score">
          <el-input v-model="form.score" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="得分率" prop="scoreRate">
          <el-input v-model="form.scoreRate" placeholder="请输入得分率" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Answer">
import { listAnswer, getAnswer, delAnswer, addAnswer, updateAnswer } from "@/api/learn/answer"

const { proxy } = getCurrentInstance()

const answerList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    examineeId: null,
    studentId: null,
    questionId: null,
    seq: null,
    knowledgeId: null,
    answer: null,
    correct: null,
    duration: null,
    count: null,
    state: null,
    scoreLevel: null,
    score: null,
    scoreRate: null,
  },
  rules: {
    examineeId: [
      { required: true, message: "答卷id不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询答卷答案列表 */
function getList() {
  loading.value = true
  listAnswer(queryParams.value).then(response => {
    answerList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    answerId: null,
    examineeId: null,
    studentId: null,
    questionId: null,
    seq: null,
    knowledgeId: null,
    answer: null,
    correct: null,
    duration: null,
    count: null,
    state: null,
    scoreLevel: null,
    score: null,
    scoreRate: null,
    createTime: null,
    updateTime: null
  }
  proxy.resetForm("answerRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.answerId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加答卷答案"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _answerId = row.answerId || ids.value
  getAnswer(_answerId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改答卷答案"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["answerRef"].validate(valid => {
    if (valid) {
      if (form.value.answerId != null) {
        updateAnswer(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addAnswer(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _answerIds = row.answerId || ids.value
  proxy.$modal.confirm('是否确认删除答卷答案编号为"' + _answerIds + '"的数据项？').then(function () {
    return delAnswer(_answerIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('learn/answer/export', {
    ...queryParams.value
  }, `answer_${new Date().getTime()}.xlsx`)
}

getList()
</script>
