<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="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="source">
        <el-input v-model="queryParams.source" placeholder="请输入来源,练习,试卷，刷题" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input v-model="queryParams.state" placeholder="请输入状态" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="知识点id" prop="knownledgeId">
        <el-input v-model="queryParams.knownledgeId" placeholder="请输入知识点id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="科目" prop="subjectId">
        <el-input v-model="queryParams.subjectId" placeholder="请输入科目" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="试卷id" prop="paperId">
        <el-input v-model="queryParams.paperId" placeholder="请输入试卷id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="总分" prop="scoreTotal">
        <el-input v-model="queryParams.scoreTotal" 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="scoreLevel">
        <el-input v-model="queryParams.scoreLevel" 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 label="错误次数" prop="wrongCount">
        <el-input v-model="queryParams.wrongCount" placeholder="请输入错误次数" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="正确次数" prop="rightCount">
        <el-input v-model="queryParams.rightCount" placeholder="请输入正确次数" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="总次数" prop="totalCount">
        <el-input v-model="queryParams.totalCount" placeholder="请输入总次数" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker clearable v-model="queryParams.createdTime" type="date" value-format="YYYY-MM-DD"
          class="w-[170px]!" placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedTime">
        <el-date-picker clearable v-model="queryParams.updatedTime" type="date" value-format="YYYY-MM-DD"
          class="w-[170px]!" placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否收藏" prop="collect">
        <el-input v-model="queryParams.collect" 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:WrongBook:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['learn:WrongBook:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['learn:WrongBook:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['learn:WrongBook:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="WrongBookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <el-table-column label="学生ID" align="center" prop="studentId" fixed="left" />
      <el-table-column label="题ID" align="center" prop="questionId" />
      <el-table-column label="来源" align="center" prop="source"  />
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="知识点id" align="center" prop="knownledgeId" />
      <el-table-column label="科目" align="center" prop="subjectId" />
      <el-table-column label="试卷id" align="center" prop="paperId" />
      <el-table-column label="参考答案" align="center" prop="answers" min-width="140" />
      <el-table-column label="答案1" align="center" prop="answer1" />
      <el-table-column label="答案2" align="center" prop="answer2" />
      <el-table-column label="总分" align="center" prop="scoreTotal" />
      <el-table-column label="得分" align="center" prop="score" />
      <el-table-column label="评级" align="center" prop="scoreLevel" />
      <el-table-column label="得分率" align="center" prop="scoreRate" />
      <el-table-column label="错误次数" align="center" prop="wrongCount" />
      <el-table-column label="正确次数" align="center" prop="rightCount" />
      <el-table-column label="总次数" align="center" prop="totalCount" />
      <el-table-column label="创建时间" align="center" prop="createdTime" min-width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedTime" min-width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否收藏" align="center" prop="collect" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="140" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['learn:WrongBook:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['learn:WrongBook: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="WrongBookRef" :model="form" :rules="rules" label-width="80px">
        <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="source">
          <el-input v-model="form.source" placeholder="请输入最后来源,练习,试卷，刷题" />
        </el-form-item>
        <el-form-item label="最后状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入最后状态" />
        </el-form-item>
        <el-form-item label="知识点id" prop="knownledgeId">
          <el-input v-model="form.knownledgeId" placeholder="请输入知识点id" />
        </el-form-item>
        <el-form-item label="科目" prop="subjectId">
          <el-input v-model="form.subjectId" placeholder="请输入科目" />
        </el-form-item>
        <el-form-item label="试卷id" prop="paperId">
          <el-input v-model="form.paperId" placeholder="请输入试卷id" />
        </el-form-item>
        <el-form-item label="最后参考答案" prop="answers">
          <el-input v-model="form.answers" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="答案1" prop="answer1">
          <el-input v-model="form.answer1" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="答案2" prop="answer2">
          <el-input v-model="form.answer2" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="总分" prop="scoreTotal">
          <el-input v-model="form.scoreTotal" 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="scoreLevel">
          <el-input v-model="form.scoreLevel" placeholder="请输入评级" />
        </el-form-item>
        <el-form-item label="得分率" prop="scoreRate">
          <el-input v-model="form.scoreRate" placeholder="请输入得分率" />
        </el-form-item>
        <el-form-item label="错误次数" prop="wrongCount">
          <el-input v-model="form.wrongCount" placeholder="请输入错误次数" />
        </el-form-item>
        <el-form-item label="正确次数" prop="rightCount">
          <el-input v-model="form.rightCount" placeholder="请输入正确次数" />
        </el-form-item>
        <el-form-item label="总次数" prop="totalCount">
          <el-input v-model="form.totalCount" placeholder="请输入总次数" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable v-model="form.createdTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedTime">
          <el-date-picker clearable v-model="form.updatedTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否收藏" prop="collect">
          <el-input v-model="form.collect" 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="WrongBook">
import { listWrongBook, getWrongBook, delWrongBook, addWrongBook, updateWrongBook } from "@/api/learn/WrongBook"

const { proxy } = getCurrentInstance()

const WrongBookList = 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,
    studentId: null,
    questionId: null,
    source: null,
    state: null,
    knownledgeId: null,
    subjectId: null,
    paperId: null,
    answers: null,
    answer1: null,
    answer2: null,
    scoreTotal: null,
    score: null,
    scoreLevel: null,
    scoreRate: null,
    wrongCount: null,
    rightCount: null,
    totalCount: null,
    createdTime: null,
    updatedTime: null,
    collect: null
  },
  rules: {
    questionId: [
      { required: true, message: "题ID不能为空", trigger: "blur" }
    ],
    createdTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updatedTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
  }
})

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

/** 查询学生错题本列表 */
function getList() {
  loading.value = true
  listWrongBook(queryParams.value).then(response => {
    WrongBookList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    wrongId: null,
    studentId: null,
    questionId: null,
    source: null,
    state: null,
    knownledgeId: null,
    subjectId: null,
    paperId: null,
    answers: null,
    answer1: null,
    answer2: null,
    scoreTotal: null,
    score: null,
    scoreLevel: null,
    scoreRate: null,
    wrongCount: null,
    rightCount: null,
    totalCount: null,
    createdTime: null,
    updatedTime: null,
    collect: null
  }
  proxy.resetForm("WrongBookRef")
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加学生错题本"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _wrongId = row.wrongId || ids.value
  getWrongBook(_wrongId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改学生错题本"
  })
}

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

/** 删除按钮操作 */
function handleDelete(row) {
  const _wrongIds = row.wrongId || ids.value
  proxy.$modal.confirm('是否确认删除学生错题本编号为"' + _wrongIds + '"的数据项？').then(function () {
    return delWrongBook(_wrongIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

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

getList()
</script>
