<!-- 历史错题 -->
<template>
  <div class="page-container historyOfIncorrectQuestions-addPage">
    <div class="page-container-top-max-big-box">
      <p class="top-1-p">历史错题</p>
      <p class="top-2-p" @click="backPage()">返回</p>
    </div>
    <div class="page-form-title-box">
      <el-form :model="queryParams" class="form-box" ref="queryForm"
               :inline="true" style="width:100%;">
        <el-form-item label="" prop="knowledgePointId" v-if="historyOfIncorrectQuestionsPropsData.type!=3">
          <el-cascader
            v-model="queryParams.knowledgePointId"
            :options="optionListA"
            :props="{
                emitPath:false,
                checkStrictly: true,
                value: 'id',
                label: 'knowledgePointName',
                children: 'children',
              }"
            style="width: 180px"
            :show-all-levels="false"
            filterable
            placeholder="知识点"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="" prop="questionType">
          <el-select v-model="queryParams.questionType" placeholder="题型" style="width: 200px">
            <el-option
              v-for="dict in optionListB"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="difficulty">
          <el-select v-model="queryParams.difficulty" placeholder="难度" style="width: 200px">
            <el-option
              v-for="dict in optionListC"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="searchValue">
          <el-input
            maxLength="30"
            v-model="queryParams.searchValue"
            placeholder="搜索名称"
            style="width: 200px"
          />
        </el-form-item>
        <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
        <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
        <p class="page-submit-common-style-button"
           v-if="dataList[0]"
           style="float: right;"
           @click="tableButton(1)"
        >开始训练</p>
      </el-form>
    </div>
    <div class="page-content-box">
      <el-table class="table-box"  border :data="dataList">
        <el-table-column label="题干" prop="questionContent"  show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ cleanRichText(scope.row.questionContent) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="题型" prop="content" width="200" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.questionType == 1?'单选题':(scope.row.questionType == 2?'多选题':(scope.row.questionType == 3?'判断题':'')) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="难度" prop="content" width="200" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.difficulty == 1?'简单':(scope.row.difficulty == 2?'一般':(scope.row.difficulty == 3?'较难':(scope.row.difficulty == 4?'极难':''))) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="做对次数" prop="correctCount" width="200" show-overflow-tooltip/>
        <el-table-column label="做错次数" prop="errorCount" width="200" show-overflow-tooltip/>
        <el-table-column label="操作" width="200" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="table-button-box">
              <p class="table-button-null"></p>
              <p class="table-button-p"
                 @click="tableButton(2,scope.row)"
              >查看答案</p>
              <p class="table-button-null"></p>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page-sizes="[20, 30, 40, 50]"
                  v-show="total>0"
                  :total="total"
                  :page.sync="queryParams.page"
                  :limit.sync="queryParams.pageSize"
                  @pagination="getList"
      />
    </div>
    <el-drawer
      size="40%"
      custom-class="historyOfIncorrectQuestions-addPage-drawer"
      :append-to-body="true"
      :title="drawerTitle"
      ref="drawer"
      :visible.sync="drawer"
      direction="rtl">
      <answerKey v-if="drawer" :answerKeyPropsData="answerKeyPropsData"></answerKey>
    </el-drawer>
  </div>
</template>

<script>
  //import { getDicts } from "@/api/commonality/noPermission";
  //import { systemUserSelect } from "@/api/commonality/permission";
  import answerKey from "@/views/safetyEducationExaminationNew/components/answerKey.vue";
  import {
    examElKnowledgePointTreeList,examElExamAttemptAnswerExamAnswer,
    examElExamAttemptAnswerAttemptAnswer,examUserPracticeWrongQuestions,
  } from "@/api/safetyEducationExaminationNew/index";
  export default {
    name: 'addPage',
    components: {
      answerKey,
    },
    props: {
      historyOfIncorrectQuestionsPropsData: {}
    },
    data() {
      return {
        //下拉列表数据
        optionListA: [],
        optionListB: [{value:'1',label:'单选'},{value:'2',label:'多选'},{value:'3',label:'判断'}],
        optionListC: [{value:"1",label:'简单'},{value:"2",label:'一般'},{value:"3",label:'较难'},{value:"4",label:'极难'}],
        queryParams: {
          page: 1,
          pageSize: 20,
          knowledgePointId: null,
          questionType: null,
          difficulty: null,
          searchValue: '',
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //弹窗相关
        drawerTitle: '',
        drawerType: '',
        drawer: false,
        answerKeyPropsData:{},
      }
    },
    created() {

    },
    mounted() {
      this.getList();
      this.examElKnowledgePointTreeList();
    },
    methods: {
      // 返回按钮
      backPage() {
        this.$parent.tableButton(6)
      },
      // 提交按钮
      submitButton() {

      },
      tableButton(type,row){
        if(type == '1'){
          //开始训练
          let obj = {};
          if(this.historyOfIncorrectQuestionsPropsData.type == 1){
            obj.source = 2;
            obj.examId = this.historyOfIncorrectQuestionsPropsData.examId;
          }else if(this.historyOfIncorrectQuestionsPropsData.type == 2){
            obj.source = 3;
            obj.attemptId = this.historyOfIncorrectQuestionsPropsData.attemptId;
          }else if(this.historyOfIncorrectQuestionsPropsData.type == 3){
            obj.source = 1;
            obj.knowledgePointId = this.historyOfIncorrectQuestionsPropsData.knowledgePointId;
          }
          this.$router.push({
            path: '/startTraining',
            query: obj
          })
        }else if(type == 2){
          //查看答案
          this.$set(this, 'drawerTitle', '查看试题答案')
          this.$set(this, 'drawerType', 1)
          this.$set(this, 'drawer', true)
          this.$set(this,'answerKeyPropsData',{
            id:this.historyOfIncorrectQuestionsPropsData.type == 3?row.questionId:row.sourceQuestionId,
          });
        }
      },
      //弹窗返回
      drawerReturnButton(){
        this.$refs.drawer.closeDrawer();
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          knowledgePointId: null,
          questionType: null,
          difficulty: null,
          searchValue: '',
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        if(this.historyOfIncorrectQuestionsPropsData.type == 1){
          obj.examId = this.historyOfIncorrectQuestionsPropsData.examId;
          examElExamAttemptAnswerExamAnswer(obj).then(response => {
            this.$set(this,'dataList',response.data.records);
            this.$set(this,'total',response.data.total);
          });
        }else if(this.historyOfIncorrectQuestionsPropsData.type == 2){
          obj.attemptId = this.historyOfIncorrectQuestionsPropsData.attemptId;
          examElExamAttemptAnswerAttemptAnswer(obj).then(response => {
            this.$set(this,'dataList',response.data.records);
            this.$set(this,'total',response.data.total);
          });
        }else if(this.historyOfIncorrectQuestionsPropsData.type == 3){
          obj.knowledgePointId = this.historyOfIncorrectQuestionsPropsData.knowledgePointId;
          examUserPracticeWrongQuestions(obj).then(response => {
            this.$set(this,'dataList',response.data.records);
            this.$set(this,'total',response.data.total);
          });
        }
      },
      examElKnowledgePointTreeList(){
        examElKnowledgePointTreeList({}).then(response => {
          const list = response.data || [];
          this.formatTreeData(list);
          this.$set(this, 'optionListA', list);
        });
      },
      //清洗富文本
      cleanRichText(htmlStr, maxLength = 50) {
        if (!htmlStr) return '';
        const tempDiv = document.createElement('div');
        tempDiv.innerHTML = htmlStr;
        let text = tempDiv.textContent || tempDiv.innerText || '';
        text = text.replace(/\s+/g, ' ').trim();
        if (maxLength && text.length > maxLength) {
          return text.substring(0, maxLength) + '...';
        }
        return text;
      },
    }
  }
</script>

<style scoped lang="scss">
  .historyOfIncorrectQuestions-addPage {
    .content-box {
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      padding: 20px;
    }
  }
</style>
