<!-- 刷题练习 -->
<template>
  <div class="app-container practiceQuestions">
    <div class="page-container practiceQuestionsPage" v-if="pageType === 1">
      <p class="pageTitle">刷题练习</p>
      <div class="max-big-box">
        <div class="left-box scrollbar-box">
          <p @click="leftCheckButton(index)"
             v-for="(item,index) in letDataList" :key="index"
             class="left-for-p" :class="index==leftDataIndex?'leftCheck':''">
            {{item.knowledgePointName}}
          </p>
        </div>
        <div class="right-box">
          <div class="page-form-title-box">
            <el-form :model="queryParams" class="form-box" ref="queryForm"
                     :inline="true" style="width:100%;">
              <div class="table-top-button-box">
                <div>
                  <p :class="practiceStatus === ''?'check-table-button':''" @click="tableTypeButton('')">全部</p>
                  <p :class="practiceStatus === 0?'check-table-button':''" @click="tableTypeButton(0)">未开始</p>
                  <p :class="practiceStatus === 1?'check-table-button':''" @click="tableTypeButton(1)">练习中</p>
                  <p :class="practiceStatus === 2?'check-table-button':''" @click="tableTypeButton(2)">已学完</p>
                </div>
              </div>
              <el-form-item label="" prop="knowledgePointId">
                <el-cascader
                  v-model="queryParams.knowledgePointId"
                  :options="optionListB"
                  :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="disciplineTypeId">
                <el-select v-model="queryParams.disciplineTypeId" placeholder="题目分类" style="width: 200px">
                  <el-option
                    v-for="dict in disciplineOptionList"
                    :key="dict.id"
                    :label="dict.disciplineName"
                    :value="dict.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="" prop="searchName">
                <el-input
                  maxLength="30"
                  v-model="queryParams.searchName"
                  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>
            </el-form>
          </div>
          <div class="page-content-box">
            <div class="for-max-big-box scrollbar-box">
              <div class="for-big-box" v-for="(item,index) in dataList" :key="index" @click="tableButton(1,item)">
                <p class="title-p">{{item.knowledgePointName}}</p>
                <div class="text-box">
                  <div class="text-min-box">
                    <p class="el-icon-time"></p>
                    <p :style="!item.lastAnswerTime?'font-size:14px;':''">{{item.lastAnswerTime?parseTime(item.lastAnswerTime,"{y}-{m}-{d} {h}:{i}"):'未练习'}}</p>
                  </div>
                  <p class="text-p">练习进度：{{item.answeredCount}} / {{item.totalCount}}</p>
                  <p class="text-p">正确数：{{item.correctCount}} 丨 错题数：{{item.wrongCount}} 丨 正确率：{{item.correctRate}}%</p>
                </div>
                <p class="position-p" v-if="item.hasWrongQuestion" @click.stop="tableButton(2,item)">历史错题</p>
                <el-progress
                  class="num-p" type="circle"
                  :stroke-width="4"
                  :width="60" :percentage="item.progress">
                </el-progress>
              </div>
            </div>
            <pagination :page-sizes="[20, 30, 40, 50]"
                        style="margin-right:20px;"
                        v-show="total>0"
                        :total="total"
                        :page.sync="queryParams.page"
                        :limit.sync="queryParams.pageSize"
                        @pagination="getList"
            />
          </div>
        </div>
      </div>
    </div>
    <historyOfIncorrectQuestions :historyOfIncorrectQuestionsPropsData="historyOfIncorrectQuestionsPropsData" v-if="pageType === 2"></historyOfIncorrectQuestions>
  </div>
</template>
<script>
  //import { getDicts } from "@/api/commonality/noPermission";
  //import { systemUserSelect } from "@/api/commonality/permission";
  //import { getInfo } from "@/api/basicsModules/index";
  //import addPage from "./addPage.vue";
  import { examUserPracticeList,examElKnowledgePointTreeList,examElDisciplineTypeList, } from "@/api/safetyEducationExaminationNew/index";
  import historyOfIncorrectQuestions from "@/views/safetyEducationExaminationNew/components/historyOfIncorrectQuestions.vue";
  export default {
    name: 'index',
    components: {
      historyOfIncorrectQuestions,
    },
    data() {
      return {
        //页面状态
        pageType: 1,
        letDataList:[],
        leftDataIndex:0,
        //下拉列表数据
        optionListB: [],
        disciplineOptionList:[],
        //查询条件
        practiceStatus:'',
        queryParams: {
          page: 1,
          pageSize: 20,
          knowledgePointId: null,
          disciplineTypeId: null,
          searchName: ''
        },
        //列表数据
        dataList: [],
        //数据数量
        total: 0,
        historyOfIncorrectQuestionsPropsData:{},
      }
    },
    created() {

    },
    mounted() {
      if(this.$route.query.disciplineId){
        this.$set(this.queryParams,'disciplineTypeId',this.$route.query.disciplineId);
      }
      if(sessionStorage.getItem('skipQueryParamsData')){
        let skipQueryParamsData = JSON.parse(sessionStorage.getItem('skipQueryParamsData'))
        this.$set(this,'queryParams',skipQueryParamsData.queryParams);
        this.$set(this,'leftDataIndex',skipQueryParamsData.leftDataIndex);
        this.$set(this,'practiceStatus',skipQueryParamsData.practiceStatus);
        sessionStorage.removeItem('skipQueryParamsData');
      }
      this.examElKnowledgePointTreeList();
      this.examElDisciplineTypeList();
    },
    methods: {
      //左侧列表切换
      leftCheckButton(index){
        if(this.leftDataIndex != index){
          this.$set(this,'leftDataIndex',index);
          this.$set(this.queryParams, 'knowledgePointId', null);
          this.$set(this, 'optionListB', this.letDataList[index].children?this.letDataList[index].children:[]);
          this.handleQuery();
        }
      },
      //切换按钮
      tableTypeButton(val){
        if(this.practiceStatus !== val){
          this.$set(this,'practiceStatus',val);
          this.handleQuery();
        }
      },
      //查询按钮
      handleQuery() {
        this.$set(this.queryParams, 'page', 1)
        this.getList()
      },
      //重置按钮
      resetQuery() {
        this.$set(this, 'queryParams', {
          page: 1,
          pageSize: 20,
          knowledgePointId: null,
          disciplineTypeId: null,
          searchName: ''
        })
        this.getList()
      },
      //获取数据列表
      getList() {
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        if(!this.queryParams.knowledgePointId){
          obj.knowledgePointId = this.letDataList[this.leftDataIndex].id
        }
        obj.practiceStatus = this.practiceStatus;
        examUserPracticeList(obj).then(response => {
          this.$set(this, 'dataList', response.data.records)
          this.$set(this, 'total', response.data.total)
        })
      },
      //操作按钮
      tableButton(type, item) {
        let self = this
        if (type == 1) {
          //刷题练习
          this.$router.push({
            path: '/startPracticing',
            query: {
              knowledgePointId: item.knowledgePointId,
              type: '1'
            }
          })
        } else if (type == 2) {
          //历史错题
          this.$set(this,'historyOfIncorrectQuestionsPropsData',{
            type:3,
            knowledgePointId:item.knowledgePointId,
          });
          this.$set(this, 'pageType', 2)
        } else if (type == 6) {
          //返回并刷新
          this.$set(this, 'pageType', 1)
          this.getList()
        }
      },
      examElKnowledgePointTreeList(){
        let self = this;
        examElKnowledgePointTreeList({ dataScope:2 }).then(response => {
          const list = response.data || [];
          this.formatTreeData(list);
          this.$set(this, 'letDataList', [{knowledgePointName:'全部',id:''},...list]);
          if(this.$route.query.knowledgePointId){
            for(let i=0;i<self.letDataList.length;i++){
              if(self.letDataList[i].id == this.$route.query.knowledgePointId){
                self.$set(self,'leftDataIndex',i);
              }
            }
          }
          // this.$set(this, 'optionListB', list[0].children?list[0].children:[]);
          this.getList();
        });
      },
      //查询全部专业类别
      examElDisciplineTypeList(){
        examElDisciplineTypeList({}).then(response => {
          this.$set(this,'disciplineOptionList',response.data);
        });
      },
    },
    beforeRouteLeave(to, from, next) {
      if (to.name === 'startPracticing'||to.name === 'startTraining') {
        const skipQueryParamsData = {
          queryParams: this.queryParams,
          leftDataIndex: this.leftDataIndex,
          practiceStatus: this.practiceStatus,
        };
        sessionStorage.setItem('skipQueryParamsData', JSON.stringify(skipQueryParamsData));
      } else {
        sessionStorage.removeItem('skipQueryParamsData');
      }
      next(); // 必须调用 next() 放行路由
    },
  }
</script>
<style scoped lang="scss">
  .practiceQuestions {
    .practiceQuestionsPage {
      .pageTitle{
        height:60px;
        line-height:60px;
        padding:0 20px;
        border-bottom:1px solid #dedede;
      }
      .max-big-box{
        flex: 1;
        display: flex;
        overflow: hidden;
        .left-box{
          width:200px;
          border-right:1px solid #dedede;
          padding:20px 0 ;
          .left-for-p{
            margin:5px 0;
            padding:0 20px;
            text-align: center;
            border-left:6px solid #ffffff;
            cursor: pointer;
            font-weight:700;
            font-size:16px;
            line-height:40px;
            /*单行省略号*/
            display:block;
            overflow:hidden;
            text-overflow:ellipsis;
            white-space:nowrap;
          }
          .left-for-p:hover{
            background-color: #0183fa;
            color: #ffffff;
          }
          .leftCheck{
            color: #fff;
            background-color: #0183fa;
            /*border-right:4px solid #0183fa;*/
          }
        }
        .right-box{
          flex:1;
          display: flex;
          flex-direction: column;
          overflow: hidden;
          .top-max-button-box{
            display: flex;
            margin:20px 0 0 20px;
            p{
              width:120px;
              line-height:40px;
              border-radius:4px;
              color:#333;
              border:1px solid #333;
              text-align: center;
              margin-right:20px;
              cursor: pointer;
            }
            .top-check-button{
              border:1px solid #0183fa;
              background-color: #0183fa;
              color:#fff;
            }
          }
          .table-top-button-box{
            display: inline-block;
            vertical-align: top;
            margin-right:10px;
            div{
              display: flex;
              p{
                border:1px solid #DCDFE6;
                width:80px;
                text-align: center;
                height:40px;
                line-height:40px;
                cursor: pointer;
                font-size:14px;
              }
              p:nth-child(1){
                border-top-left-radius:4px;
                border-bottom-left-radius:4px;
              }
              p:nth-child(2){
                border-left:none;
              }
              p:nth-child(3){
                border-left:none;
                border-right:none;
              }
              p:nth-child(4){
                border-top-right-radius:4px;
                border-bottom-right-radius:4px;
              }
              .check-table-button{
                border:1px solid #0183fa;
                background-color: #0183fa;
                color:#fff;
              }
            }
          }
          .page-content-box{
            padding:20px 0;
          }
          .for-max-big-box{
            flex:1;
            .for-big-box{
              cursor: pointer;
              display: inline-block;
              width:660px;
              margin:0 0 20px 20px;
              border-radius:10px;
              overflow: hidden;
              box-shadow: 0 2px 8px rgba(37, 50, 74, 0.12);
              position: relative;
              border:1px solid rgba(222,222,222,0.4);
              background-color: rgba(222,222,222,0.2);
              .title-p{
                margin:20px;
                font-size:16px;
                font-weight:700;
              }
              .text-box{
                margin:0 20px 20px 20px;
                background-color: rgba(1,131,250,0.1);
                overflow: hidden;
                border-radius:10px;
                padding:10px 20px;
                .text-min-box{
                  height:40px;
                  line-height: 40px;
                  color:#0183fa;
                  display: flex;
                  p:nth-child(1){
                    font-size:24px;
                    line-height: 40px;
                    margin-right:6px;
                  }
                  p:nth-child(2){
                    font-size:16px;
                    font-weight:700;
                  }
                }
                .text-p{
                  font-size:14px;
                  color:#666;
                  line-height:30px;
                  font-weight:700;
                }
              }
              .position-p{
                position: absolute;
                right:20px;
                top:20px;
                color:#0183fa;
                font-weight:700;
              }
              .position-time{
                position: absolute;
                right:40px;
                bottom:29px;
                font-size:14px;
                color:#666;
                line-height:30px;
                font-weight:700;
              }
              .num-p{
                position: absolute;
                right:40px;
                bottom:65px;
              }
              .num-p ::v-deep .el-progress__text {
                margin-left:0!important;
                font-size:12px!important;
              }
              .num-p ::v-deep svg path:first-child {
                stroke: #cccccc !important; /* 修改为红色 */
              }
            }
          }
        }
      }
    }
  }
</style>
