<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="qtpye">
        <el-input
          v-model="queryParams.qtpye"
          placeholder="请输入类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学科Id" prop="subjectId">
        <el-input
          v-model="queryParams.subjectId"
          placeholder="请输入学科Id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="paperId">
        <el-input
          v-model="queryParams.paperId"
          placeholder="请输入${comment}"
          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="${comment}" prop="diff">
        <el-input
          v-model="queryParams.diff"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试题在题库中的相似度，相似度越高，质量越低" prop="similarity">
        <el-input
          v-model="queryParams.similarity"
          placeholder="请输入试题在题库中的相似度，相似度越高，质量越低"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="knowId">
        <el-input
          v-model="queryParams.knowId"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级ID" prop="gradeId">
        <el-input
          v-model="queryParams.gradeId"
          placeholder="请输入年级ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="knowledges">
        <el-input
          v-model="queryParams.knowledges"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试题区域" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入试题区域"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试题年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入试题年份"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试题类型：1，月考；2，模拟考；3，中考；4，高考；5，学业考试；6，其他" prop="paperTpye">
        <el-input
          v-model="queryParams.paperTpye"
          placeholder="请输入试题类型：1，月考；2，模拟考；3，中考；4，高考；5，学业考试；6，其他"
          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="fromSite">
        <el-input
          v-model="queryParams.fromSite"
          placeholder="请输入试题来源"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否存在图片水印" prop="isSub">
        <el-input
          v-model="queryParams.isSub"
          placeholder="请输入是否存在图片水印"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否常规题，如果选择题无法正常提取标准答案或者选项，有小题的答题无法正常提取小题，则isNormal为0，否则为1" prop="isNormal">
        <el-input
          v-model="queryParams.isNormal"
          placeholder="请输入是否常规题，如果选择题无法正常提取标准答案或者选项，有小题的答题无法正常提取小题，则isNormal为0，否则为1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否匹配章节知识点，1匹配，0不匹配" prop="isKonw">
        <el-input
          v-model="queryParams.isKonw"
          placeholder="请输入是否匹配章节知识点，1匹配，0不匹配"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="tiid">
        <el-input
          v-model="queryParams.tiid"
          placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试题题干的md5值" prop="md5">
        <el-input
          v-model="queryParams.md5"
          placeholder="请输入试题题干的md5值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="isunique">
        <el-input
          v-model="queryParams.isunique"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="md52">
        <el-input
          v-model="queryParams.md52"
          placeholder="请输入${comment}"
          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="${comment}" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="paperTypeTitle">
        <el-input
          v-model="queryParams.paperTypeTitle"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否更新" prop="isUpdate">
        <el-input
          v-model="queryParams.isUpdate"
          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:questions:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['learn:questions:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['learn:questions:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['learn:questions:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questionsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="试题-题干" align="center" prop="title" />
      <el-table-column label="选项A" align="center" prop="optionA" />
      <el-table-column label="选项B" align="center" prop="optionB" />
      <el-table-column label="选项C" align="center" prop="optionC" />
      <el-table-column label="选项D" align="center" prop="optionD" />
      <el-table-column label="选项E" align="center" prop="optionE" />
      <el-table-column label="选项D" align="center" prop="optionF" />
      <el-table-column label="选项E" align="center" prop="optionG" />
      <el-table-column label="${comment}" align="center" prop="answer1" />
      <el-table-column label="非标准格式答案或含部分过程说明的答案" align="center" prop="answer2" />
      <el-table-column label="类型" align="center" prop="qtpye" />
      <el-table-column label="学科Id" align="center" prop="subjectId" />
      <el-table-column label="${comment}" align="center" prop="paperId" />
      <el-table-column label="知识点" align="center" prop="knowledgeId" />
      <el-table-column label="${comment}" align="center" prop="diff" />
      <el-table-column label="试题在题库中的相似度，相似度越高，质量越低" align="center" prop="similarity" />
      <el-table-column label="试题解析" align="center" prop="parse" />
      <el-table-column label="${comment}" align="center" prop="knowId" />
      <el-table-column label="年级ID" align="center" prop="gradeId" />
      <el-table-column label="${comment}" align="center" prop="knowledges" />
      <el-table-column label="试题区域" align="center" prop="area" />
      <el-table-column label="试题年份" align="center" prop="year" />
      <el-table-column label="试题类型：1，月考；2，模拟考；3，中考；4，高考；5，学业考试；6，其他" align="center" prop="paperTpye" />
      <el-table-column label="来源" align="center" prop="source" />
      <el-table-column label="试题来源" align="center" prop="fromSite" />
      <el-table-column label="是否存在图片水印" align="center" prop="isSub" />
      <el-table-column label="是否常规题，如果选择题无法正常提取标准答案或者选项，有小题的答题无法正常提取小题，则isNormal为0，否则为1" align="center" prop="isNormal" />
      <el-table-column label="是否匹配章节知识点，1匹配，0不匹配" align="center" prop="isKonw" />
      <el-table-column label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" align="center" prop="tiid" />
      <el-table-column label="试题题干的md5值" align="center" prop="md5" />
      <el-table-column label="${comment}" align="center" prop="isunique" />
      <el-table-column label="${comment}" align="center" prop="md52" />
      <el-table-column label="得分" align="center" prop="score" />
      <el-table-column label="选项" align="center" prop="options" />
      <el-table-column label="${comment}" align="center" prop="number" />
      <el-table-column label="${comment}" align="center" prop="paperTypeTitle" />
      <el-table-column label="选项" align="center" prop="options0" />
      <el-table-column label="试题-材料题题干" align="center" prop="title0" />
      <el-table-column label="试题-材料题题干" align="center" prop="title1" />
      <el-table-column label="试题解析" align="center" prop="parse0" />
      <el-table-column label="${comment}" align="center" prop="answer0" />
      <el-table-column label="是否更新" align="center" prop="isUpdate" />
      <el-table-column label="子题类型" align="center" prop="isSubType" />
      <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:questions:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['learn:questions: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="questionsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="试题-题干" prop="title">
          <el-input v-model="form.title" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选项A" prop="optionA">
          <el-input v-model="form.optionA" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选项B" prop="optionB">
          <el-input v-model="form.optionB" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选项C" prop="optionC">
          <el-input v-model="form.optionC" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选项D" prop="optionD">
          <el-input v-model="form.optionD" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选项E" prop="optionE">
          <el-input v-model="form.optionE" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选项D" prop="optionF">
          <el-input v-model="form.optionF" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选项E" prop="optionG">
          <el-input v-model="form.optionG" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="${comment}" prop="answer1">
          <el-input v-model="form.answer1" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="非标准格式答案或含部分过程说明的答案" prop="answer2">
          <el-input v-model="form.answer2" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="类型" prop="qtpye">
          <el-input v-model="form.qtpye" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item label="学科Id" prop="subjectId">
          <el-input v-model="form.subjectId" placeholder="请输入学科Id" />
        </el-form-item>
        <el-form-item label="${comment}" prop="paperId">
          <el-input v-model="form.paperId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="知识点" prop="knowledgeId">
          <el-input v-model="form.knowledgeId" placeholder="请输入知识点" />
        </el-form-item>
        <el-form-item label="${comment}" prop="diff">
          <el-input v-model="form.diff" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="试题在题库中的相似度，相似度越高，质量越低" prop="similarity">
          <el-input v-model="form.similarity" placeholder="请输入试题在题库中的相似度，相似度越高，质量越低" />
        </el-form-item>
        <el-form-item label="试题解析" prop="parse">
          <el-input v-model="form.parse" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="${comment}" prop="knowId">
          <el-input v-model="form.knowId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="年级ID" prop="gradeId">
          <el-input v-model="form.gradeId" placeholder="请输入年级ID" />
        </el-form-item>
        <el-form-item label="${comment}" prop="knowledges">
          <el-input v-model="form.knowledges" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="试题区域" prop="area">
          <el-input v-model="form.area" placeholder="请输入试题区域" />
        </el-form-item>
        <el-form-item label="试题年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入试题年份" />
        </el-form-item>
        <el-form-item label="试题类型：1，月考；2，模拟考；3，中考；4，高考；5，学业考试；6，其他" prop="paperTpye">
          <el-input v-model="form.paperTpye" placeholder="请输入试题类型：1，月考；2，模拟考；3，中考；4，高考；5，学业考试；6，其他" />
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="试题来源" prop="fromSite">
          <el-input v-model="form.fromSite" placeholder="请输入试题来源" />
        </el-form-item>
        <el-form-item label="是否存在图片水印" prop="isSub">
          <el-input v-model="form.isSub" placeholder="请输入是否存在图片水印" />
        </el-form-item>
        <el-form-item label="是否常规题，如果选择题无法正常提取标准答案或者选项，有小题的答题无法正常提取小题，则isNormal为0，否则为1" prop="isNormal">
          <el-input v-model="form.isNormal" placeholder="请输入是否常规题，如果选择题无法正常提取标准答案或者选项，有小题的答题无法正常提取小题，则isNormal为0，否则为1" />
        </el-form-item>
        <el-form-item label="是否匹配章节知识点，1匹配，0不匹配" prop="isKonw">
          <el-input v-model="form.isKonw" placeholder="请输入是否匹配章节知识点，1匹配，0不匹配" />
        </el-form-item>
        <el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="tiid">
          <el-input v-model="form.tiid" placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" />
        </el-form-item>
        <el-form-item label="试题题干的md5值" prop="md5">
          <el-input v-model="form.md5" placeholder="请输入试题题干的md5值" />
        </el-form-item>
        <el-form-item label="${comment}" prop="isunique">
          <el-input v-model="form.isunique" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="md52">
          <el-input v-model="form.md52" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="得分" prop="score">
          <el-input v-model="form.score" placeholder="请输入得分" />
        </el-form-item>
        <el-form-item label="选项" prop="options">
          <el-input v-model="form.options" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="${comment}" prop="number">
          <el-input v-model="form.number" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="paperTypeTitle">
          <el-input v-model="form.paperTypeTitle" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="选项" prop="options0">
          <el-input v-model="form.options0" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="试题-材料题题干" prop="title0">
          <el-input v-model="form.title0" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="试题-材料题题干" prop="title1">
          <el-input v-model="form.title1" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="试题解析" prop="parse0">
          <el-input v-model="form.parse0" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="${comment}" prop="answer0">
          <el-input v-model="form.answer0" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否更新" prop="isUpdate">
          <el-input v-model="form.isUpdate" 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="Questions">
import { listQuestions, getQuestions, delQuestions, addQuestions, updateQuestions } from "@/api/learn/questions"

const { proxy } = getCurrentInstance()

const questionsList = 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,
    title: null,
    optionA: null,
    optionB: null,
    optionC: null,
    optionD: null,
    optionE: null,
    optionF: null,
    optionG: null,
    answer1: null,
    answer2: null,
    qtpye: null,
    subjectId: null,
    paperId: null,
    knowledgeId: null,
    diff: null,
    similarity: null,
    parse: null,
    knowId: null,
    gradeId: null,
    knowledges: null,
    area: null,
    year: null,
    paperTpye: null,
    source: null,
    fromSite: null,
    isSub: null,
    isNormal: null,
    isKonw: null,
    tiid: null,
    md5: null,
    isunique: null,
    md52: null,
    score: null,
    options: null,
    number: null,
    paperTypeTitle: null,
    options0: null,
    title0: null,
    title1: null,
    parse0: null,
    answer0: null,
    isUpdate: null,
    isSubType: null
  },
  rules: {
  }
})

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

/** 查询试题列表 */
function getList() {
  loading.value = true
  listQuestions(queryParams.value).then(response => {
    questionsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    optionA: null,
    optionB: null,
    optionC: null,
    optionD: null,
    optionE: null,
    optionF: null,
    optionG: null,
    answer1: null,
    answer2: null,
    qtpye: null,
    subjectId: null,
    paperId: null,
    knowledgeId: null,
    diff: null,
    similarity: null,
    parse: null,
    knowId: null,
    gradeId: null,
    knowledges: null,
    area: null,
    year: null,
    paperTpye: null,
    source: null,
    fromSite: null,
    isSub: null,
    isNormal: null,
    isKonw: null,
    tiid: null,
    md5: null,
    isunique: null,
    md52: null,
    createTime: null,
    score: null,
    options: null,
    number: null,
    paperTypeTitle: null,
    options0: null,
    title0: null,
    title1: null,
    parse0: null,
    answer0: null,
    isUpdate: null,
    isSubType: null
  }
  proxy.resetForm("questionsRef")
}

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

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

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

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

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getQuestions(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改试题"
  })
}

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

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

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

getList()
</script>
