<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <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="paperId">
        <el-input
          v-model="queryParams.paperId"
          placeholder="请输入试卷id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime">
        <el-date-picker clearable
          v-model="queryParams.beginTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束时间">
        </el-date-picker>
      </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="分级" 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 label="名次" prop="ranking">
        <el-input
          v-model="queryParams.ranking"
          placeholder="请输入名次"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年排名次" prop="gradeRanking">
        <el-input
          v-model="queryParams.gradeRanking"
          placeholder="请输入年排名次"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选择题正确数" prop="chooseCount">
        <el-input
          v-model="queryParams.chooseCount"
          placeholder="请输入选择题正确数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选择题总数" prop="chooseTotal">
        <el-input
          v-model="queryParams.chooseTotal"
          placeholder="请输入选择题总数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主观题正确数" prop="subjectiveCount">
        <el-input
          v-model="queryParams.subjectiveCount"
          placeholder="请输入主观题正确数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主观题总数" prop="subjectiveTotal">
        <el-input
          v-model="queryParams.subjectiveTotal"
          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:examinee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['learn:examinee:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['learn:examinee:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['learn:examinee:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="examineeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="答卷id" align="center" prop="examineeId" />
      <el-table-column label="学生id" align="center" prop="studentId" />
      <el-table-column label="试卷类型" align="center" prop="paperType" />
      <el-table-column label="试卷id" align="center" prop="paperId" />
      <el-table-column label="开始时间" align="center" prop="beginTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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" prop="ranking" />
      <el-table-column label="年排名次" align="center" prop="gradeRanking" />
      <el-table-column label="选择题正确数" align="center" prop="chooseCount" />
      <el-table-column label="选择题总数" align="center" prop="chooseTotal" />
      <el-table-column label="主观题正确数" align="center" prop="subjectiveCount" />
      <el-table-column label="主观题总数" align="center" prop="subjectiveTotal" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['learn:examinee:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['learn:examinee: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="examineeRef" :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="paperId">
          <el-input v-model="form.paperId" placeholder="请输入试卷id" />
        </el-form-item>
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker clearable
            v-model="form.beginTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="未答题, 已答题, 通过, 未通过" prop="state">
          <el-input v-model="form.state" 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="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-item label="名次" prop="ranking">
          <el-input v-model="form.ranking" placeholder="请输入名次" />
        </el-form-item>
        <el-form-item label="年排名次" prop="gradeRanking">
          <el-input v-model="form.gradeRanking" placeholder="请输入年排名次" />
        </el-form-item>
        <el-form-item label="选择题正确数" prop="chooseCount">
          <el-input v-model="form.chooseCount" placeholder="请输入选择题正确数" />
        </el-form-item>
        <el-form-item label="选择题总数" prop="chooseTotal">
          <el-input v-model="form.chooseTotal" placeholder="请输入选择题总数" />
        </el-form-item>
        <el-form-item label="主观题正确数" prop="subjectiveCount">
          <el-input v-model="form.subjectiveCount" placeholder="请输入主观题正确数" />
        </el-form-item>
        <el-form-item label="主观题总数" prop="subjectiveTotal">
          <el-input v-model="form.subjectiveTotal" 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="Examinee">
import { listExaminee, getExaminee, delExaminee, addExaminee, updateExaminee } from "@/api/learn/examinee"

const { proxy } = getCurrentInstance()

const examineeList = 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,
    paperType: null,
    paperId: null,
    beginTime: null,
    endTime: null,
    state: null,
    scoreLevel: null,
    score: null,
    scoreRate: null,
    ranking: null,
    gradeRanking: null,
    chooseCount: null,
    chooseTotal: null,
    subjectiveCount: null,
    subjectiveTotal: null,
  },
  rules: {
  }
})

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

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

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

// 表单重置
function reset() {
  form.value = {
    examineeId: null,
    studentId: null,
    paperType: null,
    paperId: null,
    beginTime: null,
    endTime: null,
    state: null,
    scoreLevel: null,
    score: null,
    scoreRate: null,
    ranking: null,
    gradeRanking: null,
    chooseCount: null,
    chooseTotal: null,
    subjectiveCount: null,
    subjectiveTotal: null,
    createTime: null,
    updateTime: null
  }
  proxy.resetForm("examineeRef")
}

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

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

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

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

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

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

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

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

getList()
</script>
