<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="wrongId">
        <el-input v-model="queryParams.wrongId" placeholder="请输入错题本id" 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="source">
        <el-input v-model="queryParams.source" placeholder="请输入来源" 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="科目" prop="subjectId">
        <el-input v-model="queryParams.subjectId" 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="答卷id" prop="examineeId">
        <el-input v-model="queryParams.examineeId" placeholder="请输入答卷id" 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>
        <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:WrongDetail:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['learn:WrongDetail:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['learn:WrongDetail:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['learn:WrongDetail:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="WrongDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <!-- <el-table-column label="wrong_detail_id" align="center" prop="wrongDetailId" fixed="left" /> -->
      <el-table-column label="错题本id" align="center" prop="wrongId" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="source" />
      <el-table-column label="学生id" align="center" prop="studentId" />
      <el-table-column label="科目" align="center" prop="subjectId" />
      <el-table-column label="知识点id" align="center" prop="knownledgeId" />
      <el-table-column label="答卷id" align="center" prop="examineeId" />
      <el-table-column label="试卷id" align="center" prop="paperId" />
      <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" 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:WrongDetail:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['learn:WrongDetail: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="WrongDetailRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="错题本id" prop="wrongId">
          <el-input v-model="form.wrongId" placeholder="请输入错题本id" />
        </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="source">
          <el-input v-model="form.source" placeholder="请输入来源" />
        </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="科目" prop="subjectId">
          <el-input v-model="form.subjectId" 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="答卷id" prop="examineeId">
          <el-input v-model="form.examineeId" placeholder="请输入答卷id" />
        </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="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>
      <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="WrongDetail">
import { listWrongDetail, getWrongDetail, delWrongDetail, addWrongDetail, updateWrongDetail } from "@/api/learn/WrongDetail"

const { proxy } = getCurrentInstance()

const WrongDetailList = 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,
    wrongId: null,
    createdTime: null,
    source: null,
    studentId: null,
    subjectId: null,
    knownledgeId: null,
    examineeId: null,
    paperId: null,
    scoreTotal: null,
    score: null,
    scoreLevel: null,
    scoreRate: null
  },
  rules: {
    wrongId: [
      { required: true, message: "错题本id不能为空", trigger: "blur" }
    ],
    createdTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
  }
})

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

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

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

// 表单重置
function reset() {
  form.value = {
    wrongDetailId: null,
    wrongId: null,
    createdTime: null,
    source: null,
    studentId: null,
    subjectId: null,
    knownledgeId: null,
    examineeId: null,
    paperId: null,
    scoreTotal: null,
    score: null,
    scoreLevel: null,
    scoreRate: null
  }
  proxy.resetForm("WrongDetailRef")
}

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

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

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

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

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _wrongDetailId = row.wrongDetailId || ids.value
  getWrongDetail(_wrongDetailId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改学生错题流水"
  })
}

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

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

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

getList()
</script>
