<template>
  <div class="main" v-if="loaded">
    <div class="position-relative" v-if="$route.query.type==6">
      <i
        class="iconfont icon-fenye-shangyiye position-absolute left-arrow cursor-pointer"
        @click="leftClick"
        v-if="clickindex >= 1"
      ></i>
      <div class="practice-daily white-bg mb20" ref="daily">
        <ul
          class="clearfix practice-daily-ul"
          :style="{width:ulWidth+'px',marginLeft:-clickindex*1200+'px'}"
        >
          <li
            class="fl daily-li align-center line-height22 gray777-font pb10 pt10 cursor-pointer"
            :class="currentData==i?'red-bg':''"
            v-for="(item,i) in dateList"
            :key="i"
            @click="setIndex(i,item.day)" :style="'width:'+liwidth+'px'"
          >
            <h3 class="weight-normal" :class="currentData==i?'white-font':''">{{item.week}}</h3>
            <p :class="currentData==i?'white-font':''">{{item.day}}</p>
          </li>
        </ul>
      </div>
      <i
        class="iconfont icon-fenye-shangyiye1 position-absolute right-arrow cursor-pointer"
        @click="rightClick"
        v-if="clickindex < Math.ceil(dateList.length/7)-1"
      ></i>
    </div>
    <div class="path-box white-bg line-height26 f14" v-if="test_num">
      <span class="fr">进度：{{index+1}}/{{test_num}}</span>
      练习内容：
      <font v-for="(item,i) in category_list" :key="i">
        {{item.categoryname}}
        <i
          v-if="i+1<category_list.length"
          class="iconfont icon-jiantou f20 vertical-align"
        ></i>
      </font>
    </div>
    <div class="test-con">
      <div v-if="$route.query.type==6 && dateList && dateList[currentData] && dateList[currentData].is_test ">
        <ul class="examination-result clearfix white-bg mt17">
          <li class="text-box1 fl f16">
            <p>本次练习共{{dateList[currentData].testdata.tk_num}}道题目</p>
            <p>答错{{dateList[currentData].testdata.error_num}}道题</p>
            <p>交卷时间：{{dateList[currentData].testdata.adddate}}</p>
          </li>
          <li class="text-box2 fl">
            <ks-circle-progress
              ref="$circle"
              class="progress"
              :isAnimation="true"
              :isRound="true"
              width="240"
              radius="10"
              :progress="dateList[currentData].testdata.zhunquelv.toString()"
              barColor="#f78075"
              duration="1000"
              delay="20"
              timeFunction="cubic-bezier(0.99, 0.01, 0.22, 0.94)"
              backgroundColor="#e6e6e6"
              text="正确率"
            ></ks-circle-progress>
          </li>
          <li class="text-box3 fl f16">
            <p>正确率：{{dateList[currentData].testdata.zhunquelv}}%</p>
            <p>
              考试时长：
              <font
                v-if="Math.floor(dateList[currentData].testdata.exam_time/60)"
              >{{Math.floor(dateList[currentData].testdata.exam_time/60)}}分钟</font>
              {{Math.floor(dateList[currentData].testdata.exam_time%60)||0}}秒
            </p>
          </li>
        </ul>
      </div>
      <div class="clearfix" v-else>
        <div class="test-con-left fl">
          <div v-for="(item,n) in testList" :key="n" class="test-type-box">
            <ul class="test-list mt17">
              <li
                v-for="(citem,i) in item.tklist"
                :key="citem.tkid"
                class="white-bg"
                v-show="i==index"
                :ref="'djump'+citem.tkid"
              >
                <ks-radio
                  :item="citem"
                  :index="i"
                  :isshow_collect="isshow_collect"
                  v-if="(citem.tktype==2||citem.tktype==4)&&showClass"
                  @flagStatus="flagStatus"
                  @collectStatus="collectStatus"
                  @redressStatus="redressStatus"
                  @ifDoexam="ifDoexam"
                />
                <ks-check
                  :item="citem"
                  :index="i"
                  :isshow_collect="isshow_collect"
                  v-else-if="citem.tktype==3&&showClass"
                  @flagStatus="flagStatus"
                  @collectStatus="collectStatus"
                  @redressStatus="redressStatus"
                  @ifDoexam="ifDoexamCheck"
                />
                <ks-subjective-questions
                  :item="citem"
                  :index="i"
                  :isshow_collect="isshow_collect"
                  type = '2'
                  v-else-if="citem.tktype==1&&showClass"
                  @flagStatus="flagStatus"
                  @collectStatus="collectStatus"
                  @redressStatus="redressStatus"
                  @ifDoexam="ifDoexamStuff"
                />
                <ks-stuff
                  :data="citem"
                  :index="i"
                  :isshow_collect="isshow_collect"
                  type = '2'
                  v-else-if="citem.tktype==5&&showClass"
                  @flagStatus="flagStatus"
                  @collectStatus="collectStatus"
                  @redressStatus="redressStatus"
                  @ifDoexam="ifDoexamStuff"
                />
                <div v-else-if="citem.tktype==6">
                  <ks-subjective-questions
                    :item="citem"
                    :isshow_collect="isshow_collect"
                    :realtest="paperInfo.realtest"
                    :is_tjda="paperInfo.is_tjda"
                    :paperid="getParameters.paperid"
                    :schoolid="getParameters.schoolid"
                    :index="i"
                    type = '2'
                    @flagStatus="flagStatus"
                    @collectStatus="collectStatus"
                    @redressStatus="redressStatus"
                    @ifDoexam="ifDoexamStuff"
                    @getResult="getResult"
                  />
                </div>
                <div v-if="citem.tktype!=6">
                  <div v-if="citem.lookanswer" class="analysis-box mb20">
                    <p class="darkgray-font">
                      【参考答案】
                      <span
                        class="green-font"
                        v-if="citem.tktype==4"
                      >{{citem.tkinfo.answer.toString()==0?'错误':'正确'}}</span>
                      <span class="green-font" v-else>
                        <span v-if="citem.tktype==1" v-html="citem.tkinfo.answer.toString()"></span>
                        <span v-else>{{citem.tkinfo.answer.toString()}}</span>
                      </span>
                    </p>
                    <p class="darkgray-font">
                      【答案解析】
                      <span v-html="citem.tkinfo.analysis"></span>
                    </p>
                  </div>
                </div>
                <div v-else>
                  <div v-if="citem.lookanswer" class="analysis-box mb20">
                    <div v-for="(ccitem,c) in citem.tkinfo.childtk" :key="c" class="mb20">
                      <p>第【{{c+1}}】小题</p>
                      <p class="darkgray-font">
                        【参考答案】
                        <span
                          class="green-font"
                          v-if="citem.tktype==4"
                        >{{ccitem.tkinfo.answer.toString()==0?'错误':'正确'}}</span>
                        <span class="green-font" v-else>
                          <span v-if="ccitem.tktype==1" v-html="ccitem.tkinfo.answer.toString()"></span>
                          <span v-else>{{ccitem.tkinfo.answer.toString()}}</span>
                        </span>
                      </p>
                      <p class="darkgray-font">
                        【答案解析】
                        <span v-html="ccitem.tkinfo.analysis"></span>
                      </p>
                    </div>
                  </div>
                </div>
              </li>
            </ul>
          </div>
          <div class="bottom-operating white-bg position-fixed" :style="{bottom:fixedBottom+'px'}">
            <ul class="clearfix align-center f15">
              <li class="prev-question fl">
                <span @click="prevQuestion" class="cursor-pointer">
                  <i class="iconfont icon-fenye-shangyiye ml10"></i>上一题
                </span>
              </li>
              <li class="fl">
                <span class="cursor-pointer" @click="lookAnswer(index)">
                  <i class="iconfont icon-xinde f18 mr10"></i>查看答案
                </span>
              </li>
              <li
                class="next-question fl"
                v-if="testList[0] && index+1 == testList[0].tklist.length && $route.query.type!=6 && continuedo"
              >
                <ks-button type="danger" @click="continueDo">继续做题</ks-button>
              </li>
              <li class="next-question fl" v-else-if="(testList[0] && (index+1) < testList[0].tklist.length) || continuedo">
                <span @click="nextQuestion" class="cursor-pointer">
                  下一题
                  <i class="iconfont icon-fenye-shangyiye1 mr10"></i>
                </span>
              </li>
            </ul>
          </div>
        </div>
        <div class="test-con-right fr">
          <div
            :class="isFixed==true?'test-right-inner isFixed':'test-right-inner'"
            ref="test-right-inner"
          >
            <div class="top-application white-bg align-center line-height26">
              <p class="f20 yellow-font">{{countdown}}</p>
              <p class="darkgray-font f14">做题用时</p>
            </div>
            <div class="answer-sheet-box white-bg">
              <h2 class="align-center f16 weight-normal">答题卡</h2>
              <div class="answer-tips mt20 mb20">
                <span class="mr20">
                  <i class="red-bg vertical-align"></i>
                  <font class="vertical-align">已做</font>
                </span>
                <span class="mr20">
                  <i class="green-bg vertical-align"></i>
                  <font class="vertical-align">标记</font>
                </span>
                <span class="mr20">
                  <i class="yellow-bg vertical-align"></i>
                  <font class="vertical-align">收藏</font>
                </span>
                <span class="mr20">
                  <i class="lightgreen-bg vertical-align"></i>
                  <font class="vertical-align">标记且收藏</font>
                </span>
              </div>
              <div class="answer-sheet-inner" :style="'max-height:'+maxHeight+'px'">
                <div class="answer-sheet" v-for="(item,n) in testList" :key="n">
                  <ul class="clearfix">
                    <div v-for="(citem,i) in item.tklist" :key="i">
                      <li
                        v-if="citem.ifdoexam==1"
                        class="fl align-center red-bg white-font red-border"
                        @click="jump(citem.tkid,i)"
                      >{{i+1}}</li>
                      <div v-else>
                        <li
                          v-if="citem.flag==0 && citem.collect==0"
                          class="fl align-center"
                          @click="jump(citem.tkid,i)"
                        >{{i+1}}</li>
                        <li
                          v-else-if="citem.flag==1 && citem.collect==0"
                          class="fl align-center green-bg white-font green-border"
                          @click="jump(citem.tkid,i)"
                        >{{i+1}}</li>
                        <li
                          v-else-if="citem.flag==0 && citem.collect==1"
                          class="fl align-center yellow-bg white-font yellow-border"
                          @click="jump(citem.tkid,i)"
                        >{{i+1}}</li>
                        <li
                          v-else-if="citem.flag==1 && citem.collect==1"
                          class="fl align-center lightgreen-bg white-font lightgreen-border"
                          @click="jump(citem.tkid,i)"
                        >{{i+1}}</li>
                      </div>
                    </div>
                  </ul>
                </div>
              </div>
              <ks-button type="danger" btnstyle="width:100%;" @click="save">结束练习</ks-button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <ks-dialog
      :visible.sync="isShowOpera"
      title="提示"
      width="300px"
      align="center"
      top="30%"
      isshowclose="1"
    >
      <div class="tips-con">
        <i
          class="f50 iconfont vertical-align"
          :class="operatstatus==0 && 'red-font icon-shibai'||operatstatus==1 && 'green-font icon-chenggong'||operatstatus==3 && 'red-font icon-jinggao'"
        ></i>
        <font class="f16 vertical-align ml10">{{operaTips}}</font>
      </div>
    </ks-dialog>
    <ks-dialog :visible.sync="isShowRedress" title="题目纠错" width="600px" align="center" top="20%" v-if="showClass2">
      <div class="redress-con">
        <p class="line-height26 f14 align-left">一直在完善，从未停歇过，但有些题目可能仍然存在瑕疵，对您造成的不便我们深表歉意！</p>
        <p class="line-height28 darkgray-font align-left">您纠错的题目ID:{{redressid}}</p>
        <div class="line-height28 align-left redress-option">
          <p class="darkgray-font">为方便我们排查错误，请您详细描述本题错误，例如：</p>
          <div class="line-height28">
            <label v-for="item in radioOption" :key="item.id" class="mr20">
              <input type="radio" :value="item.id" v-model="redressType" name="radioOption">
              {{item.name}}
            </label>
          </div>
        </div>
        <div class="redress-con_con align-left">
          <textarea rows="10" v-model="redresscon" class="redress-textarea"></textarea>
        </div>
        <div class="dialog-btn">
          <ks-button type="danger" class="ml10" @click="submitError">提交纠错</ks-button>
        </div>
      </div>
    </ks-dialog>
  </div>
</template>
<script>
import {
  GetTestTk,
  GetPaperErrortype,
  SetPaperCollect,
  SetTkCorrection,
  EndTest,
  GetDailyTestDateList,
  GetDailyTestPaper,
  EndDailyTest
} from "@/api/exam";
export default {
  name: "Exercise",
  data() {
    return {
      index: 0,
      loaded: false,
      getParameters: {
        type: this.$route.query.type,
        courseid:this.$route.query.courseid,
        typeid:this.$route.query.typeid,
        categoryid: this.$route.query.categoryid,
        knowledgeid: this.$route.query.knowledgeid || 0,
        isrand: this.$route.query.isrand || 0,
        sectionid: this.$route.query.sectionid || 0,
        tktype: this.$route.query.tktype || 0,
        tkids: []
      },
      category_list: [],
      option: [],
      testList: [],
      paperTime: 0,
      paperInfo: {},
      finishPaper: {},
      isFixed: false,
      isShowOpera: false,
      isShowRedress: false,
      operatstatus: 0,
      operaTips: "",
      unfinished: 0,
      subjectNum: 0,
      categoryid: 0,
      radioOption: [],
      redressid: 0,
      redressType: 19,
      redresscon: "",
      cloneTestList: [],
      redress_s: 0, //纠错类型请求接口的开关
      isshow_collect: 0, //收藏的开关
      h: 0, //定义时，分，秒，毫秒并初始化为0；
      m: 0,
      s: 0,
      ms: 0,
      time: 0,
      fixedBottom:0,
      timeStop: false,
      countdown: "00:00:00",
      look_tkanalysis_isfree: 0,
      look_tkanalysis_isfree_tips: "",
      ulWidth: 0,
      currentData: 0,
      day: "",
      dateList: [],
      clickindex: 0,
      set_dailytime: 0,
      dailyid: 0,
      showClass: true,
      showClass2: true,
      liwidth:0,
      test_num:0,
      continuedo:true,
      maxHeight:0
    };
  },
  watch: {
    day() {
      this.showClass = false;
      this.$nextTick(() => {
        this.showClass = true;
      });
    },
    'redressid'(){
        this.showClass2 = false;
        this.$nextTick(()=>{
            this.showClass2 = true
            this.redresscon = ''
        })
    },
    "$route.query": function() {
      if (this.$route.name == "Practicedaily") {
          
        this.init()
      }
    }

  },
  methods: {
    /**把阿拉伯数字全部转换为大写（1，2，3.... = 一、二、三、） */
    num(n) {
      if (!/(^[1-9]\d*$)/) {
        return "非法数字";
      }
      var uppercase = "千百亿千百十万千百十个";
      var nLength = n.length;
      var newStr = "";
      if (uppercase.length - nLength < 0) {
        return "数字过长";
      }
      uppercase = uppercase.substr(uppercase.length - nLength);
      for (var i = 0; i < nLength; i++) {
        newStr += "零一二三四五六七八九".charAt(n[i]) + uppercase.charAt(i);
      }
      newStr = newStr.substr(0, newStr.length - 1);
      return newStr;
    },
    /**答题卡切换试题 */
    jump(tkid, index) {
      this.index = index;
    },
    /**右侧答题卡悬浮 */
    handleScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      let offsetTop = this.$refs["test-right-inner"].offsetTop;
      scrollTop > offsetTop ? (this.isFixed = true) : (this.isFixed = false);
      if(typeof CONFIG === 'object' && CONFIG.fixed_bottom){
        let scrollHeight = document.body.scrollHeight;
        let a = window.innerHeight + scrollTop;
        let b = scrollHeight - CONFIG.fixed_bottom;
         if(a > b){
           let c = a-b;
           this.fixedBottom = c;
         }else{
            this.fixedBottom = 0;
         }
      }
    },
    /**定义计时函数 */
    timer() {
      if (this.timeStop) {
        return;
      }
      this.ms = this.ms + 50; //毫秒
      if (this.ms >= 1000) {
        this.ms = 0;
        this.s = this.s + 1; //秒
      }
      if (this.s >= 60) {
        this.s = 0;
        this.m = this.m + 1; //分钟
      }
      if (this.m >= 60) {
        this.m = 0;
        this.h = this.h + 1; //小时
      }
      this.countdown =
        this.toDub(this.h) +
        ":" +
        this.toDub(this.m) +
        ":" +
        this.toDub(this.s) +
        "" /*+this.toDubms(this.ms)+"毫秒"*/;
      if (this.$route.query.type == 6 && this.set_dailytime) {
        let usertime = this.h * 60 * 60 + this.m * 60 + this.s;
        if (usertime == this.set_dailytime) {
          this.save();
        }
      }
    },
    //补0操作
    toDub(n) {
      if (n < 10) {
        return "0" + n;
      } else {
        return "" + n;
      }
    },
    //给毫秒补0操作
    toDubms(n) {
      if (n < 10) {
        return "00" + n;
      } else {
        return "0" + n;
      }
    },

    /**获取试卷 */
    loadPage(action) {
      if (this.$route.query.type == 6) {
        this.test_num = 0
        GetDailyTestPaper({
          categoryid: this.getParameters.categoryid,
          day: this.day
        }).then(res => {
          if (res.result) {
            this.dailyid = res.dailyid;
            this.set_dailytime = res.set_dailytime * 60;
            this.look_tkanalysis_isfree_tips = res.data.look_analysis_free;
            this.isshow_collect = res.data.isshow_collect;
            this.category_list = res.category_list;
            let tknum = 0;
            res.data.tmlist.map((item, i) => {
              let n = (i + 1).toString();
              item.order = this.num(n);
              if (item.tklist) {
                item.tklist.map((citem, j) => {
                  citem.flag = 0;
                  citem.ifdoexam = 0;
                });
                tknum = 1;
              } else {
                tknum = 0;
              }
            });
            if (tknum == 0) {
              this.isShowOpera = true;
              this.operatstatus = 1;
              this.operaTips = "暂无小题";
              setTimeout(() => {
                this.isShowOpera = false;
                history.back();
              }, 1500);
            }
            this.paperInfo = res.data;

            if (action == 1) {
              let newArr = [];
              this.testList[0].tklist.map(item => {
                newArr.push(item);
              });
              res.data.tmlist[0].tklist.map(item => {
                newArr.push(item);
              });
              this.testList[0].tklist = newArr;
              // console.log(this.testList)
            } else {
              this.testList = res.data.tmlist;
            }

            let n = 0;
            this.testList.map(item => {
              this.test_num = this.test_num+item.tklist.length
              
              item.tklist.map(citem => {
                if (citem.tktype == 1) {
                  n++;
                } else {
                  return n;
                }
              });
            });
            this.subjectNum = n;
            this.categoryid = res.data.categoryid;

            /**这个是标记的缓存 */
            let arr = localStorage["userid"+(this.$store.state.userid||0)+"paper" + this.getParameters.paperid];
            if (arr) {
              arr.split(",").map(tkid => {
                if (tkid) {
                  tkid = JSON.parse(tkid);
                }
                this.testList.map(item => {
                  item.tklist.map(citem => {
                    if (citem.tkid == tkid) {
                      citem.flag = 1;
                    }
                  });
                });
              });
            }
            
          } else {
            this.testList = [];
            this.isShowOpera = true;
            this.operatstatus = 1;
            this.operaTips = res.msg;
            setTimeout(() => {
              this.isShowOpera = false;
              history.back();
            }, 1500);
          }
        });
        
        this.resetTime();
      } else {
        GetTestTk(this.getParameters).then(res => {
          if (res.result) {
            // this.look_tkanalysis_isfree = res.data.look_tkanalysis_isfree
            this.look_tkanalysis_isfree_tips = res.data.look_analysis_free;
            this.isshow_collect = res.data.isshow_collect;
            this.category_list = res.category_list;
            res.data.tmlist.map((item, i) => {
              let n = (i + 1).toString();
              item.order = this.num(n);
              item.tklist.map((citem, j) => {
                citem.flag = 0;
                citem.ifdoexam = 0;
              });
            });
            this.paperInfo = res.data;

            if (action == 1) {
              let newArr = [];
              this.testList[0].tklist.map(item => {
                newArr.push(item);
              });
              res.data.tmlist[0].tklist.map(item => {
                newArr.push(item);
              });
              this.testList[0].tklist = newArr;
              // console.log(this.testList)
            } else {
              this.testList = res.data.tmlist;
            }

            let n = 0;
            this.testList.map(item => {
              item.tklist.map(citem => {
                if (citem.tktype == 1) {
                  n++;
                } else {
                  return n;
                }
              });
            });
            this.subjectNum = n;
            this.categoryid = res.data.categoryid;

            /**这个是标记的缓存 */
            let arr = localStorage["userid"+(this.$store.state.userid||0)+"paper" + this.getParameters.paperid];
            if (arr) {
              arr.split(",").map(tkid => {
                if (tkid) {
                  tkid = JSON.parse(tkid);
                }
                this.testList.map(item => {
                  item.tklist.map(citem => {
                    if (citem.tkid == tkid) {
                      citem.flag = 1;
                    }
                  });
                });
              });
            }
          } else {
            if (action == 1) {
              this.isShowOpera = true;
              this.operatstatus = 3;
              this.operaTips = res.msg;
              setTimeout(() => {
                this.isShowOpera = false;
                this.continuedo = false
              }, 1500);
            } else {
              this.testList = [];
              this.isShowOpera = true;
              this.operatstatus = 3;
              this.operaTips = res.msg;
              setTimeout(() => {
                if(history.length==1){
                  this.isShowOpera = false;
                  window.close();
                }else{
                  this.isShowOpera = false;
                  history.back();
                }
                
              }, 1500);
            }
          }
        });
      }
      this.loaded = true;
    },
    resetTime() {
      this.h = 0; //定义时，分，秒，毫秒并初始化为0；
      this.m = 0;
      this.s = 0;
      this.ms = 0;
      this.time = 0;
      this.timeStop = false;
      this.countdown = "00:00:00";
    },

    /**纠错 */
    redressStatus(tkid) {
      this.redressid = tkid;
      this.isShowRedress = true;
      if (this.redress_s == 0) {
        GetPaperErrortype().then(res => {
          if (res.result) {
            this.radioOption = res.data;
          } else {
            this.radioOption = [];
          }
        });
        this.redress_s = 1;
      }
    },
    /**提交纠错 */
    submitError() {
      SetTkCorrection({
        tkid: this.redressid,
        categoryid: this.getParameters.categoryid,
        content: this.redresscon,
        errortype: this.redressType,
        schoolid: this.getParameters.schoolid,
        apptoken: this.$store.state.apptoken
      }).then(res => {
        if (res.result) {
          this.isShowRedress = false;
          this.isShowOpera = true;
          this.operatstatus = 1;
          this.operaTips = "提交成功";
          setTimeout(() => {
            this.isShowOpera = false;
          }, 1500);
        } else {
          this.isShowRedress = false;
          this.isShowOpera = true;
          this.operatstatus = 0;
          this.operaTips = res.msg;
          setTimeout(() => {
            this.isShowOpera = false;
          }, 1500);
        }
      });
    },
    /**标记 */
    flagStatus(val, tkid) {
      let cookiename = "userid"+(this.$store.state.userid||0)+"paper" + this.getParameters.paperid;
      let cookieArr = [];
      let cookieString = "";

      this.testList.map((item, i) => {
        item.tklist.map((citem, j) => {
          if (citem.tkid == tkid) {
            citem.flag = val;
          }
          if (citem.flag == 1) {
            cookieArr.push(citem.tkid);
          }
        });
      });

      cookieString = cookieArr.toString();
      localStorage[cookiename] = cookieString;
    },
    /**收藏 */
    collectStatus(val, tkid) {
      let tipstext = "";
      if (val == 1) {
        tipstext = "收藏成功";
      } else {
        tipstext = "取消收藏";
      }
      SetPaperCollect({
        type: 2, //收藏类型，为1表示试卷收藏，为2表示题目收藏
        infoid: tkid, //题库id
        categoryid: this.getParameters.categoryid,
        schoolid: this.getParameters.schoolid,
        apptoken: this.$store.state.apptoken,
        collect: val
      }).then(res => {
        if (res.result) {
          this.testList.map((item, i) => {
            item.tklist.map((citem, j) => {
              if (citem.tkid == tkid) {
                citem.collect = val;
              }
            });
          });
          this.isShowOpera = true;
          this.operatstatus = 1;
          this.operaTips = tipstext;
          setTimeout(() => {
            this.isShowOpera = false;
          }, 1500);
        } else {
          this.isShowOpera = true;
          this.operatstatus = 0;
          this.operaTips = res.msg;
          setTimeout(() => {
            this.isShowOpera = false;
          }, 1500);
        }
      });
    },
    /**---------------判断是否已做题------------------ */
    /**判断单选是否有做题 */
    ifDoexam(order, tkid) {
      this.testList.map((item, i) => {
        item.tklist.map((citem, j) => {
          if (citem.tkid == tkid) {
            if (order) {
              citem.useranswer = order;
            } else {
              citem.useranswer = [];
            }

            citem.ifdoexam = 1;
          }
        });
      });
    },
    /**判断多选是否有做题 */
    ifDoexamCheck(tkid, n, orderArr) {
      this.testList.map((item, i) => {
        item.tklist.map((citem, j) => {
          if (citem.tkid == tkid) {
            if (n) {
              citem.useranswer = orderArr;
              citem.ifdoexam = 1;
            } else {
              citem.useranswer = [];
              citem.ifdoexam = 0;
            }
          }
        });
      });
    },
    /**判断填空是否有做题 */
    ifDoexamStuff(tkid, val) {
      this.testList.map((item, i) => {
        item.tklist.map((citem, j) => {
          if (citem.tkid == tkid) {
            if (val) {
              citem.useranswer = val;
              citem.ifdoexam = 1;
            } else {
              citem.useranswer = [];
              citem.ifdoexam = 0;
            }
          }
        });
      });
    },
    /**获取阅读题的结果 */
    getResult(val) {
      if (val.isrdoexam == 0 && val.iscdoexam == 0 && val.issdoexam == 0) {
        val.ifdoexam = 0;
      } else {
        val.ifdoexam = 1;
      }
      this.testList.map((item, i) => {
        item.tklist.map((citem, j) => {
          if (citem.tkid == val.tkid) {
            citem = val;
          }
        });
      });
    },
    /**交卷时候构造新格式 */
    getPaperInfo() {
      this.examtime = this.h * 60 * 60 + this.m * 60 + this.s;

      let newarr = [];
      let arr = {};
      this.testList.map((item, i) => {
        let tklist = [];
        item.tklist.map((citem, j) => {
          let useranswer = [];
          if (citem.tktype == 1 || citem.tktype == 2 || citem.tktype == 4) {
            if (citem.useranswer) {
              if (citem.tktype == 4) {
                if (citem.useranswer == "A") {
                  useranswer.push(0);
                } else {
                  useranswer.push(1);
                }
              } else {
                useranswer.push(citem.useranswer);
              }
            } else {
              useranswer = [];
            }
          } else if (citem.tktype == 3 || citem.tktype == 5) {
            if (citem.useranswer) {
              useranswer = citem.useranswer;
            } else {
              useranswer = [];
            }
          } else {
            let arr = [];
            citem.tkinfo.childtk.map(ccitem => {
              if (ccitem.useranswer) {
                if (ccitem.tktype == 4) {
                  if (ccitem.useranswer == "A") {
                    arr.push({
                      tktype: ccitem.tktype,
                      useranswer: [0]
                    });
                  } else {
                    arr.push({
                      tktype: ccitem.tktype,
                      useranswer: [1]
                    });
                  }
                } else {
                  arr.push({
                    tktype: ccitem.tktype,
                    useranswer: ccitem.useranswer
                  });
                }
              } else {
                arr.push({
                  tktype: ccitem.tktype,
                  useranswer: []
                });
              }
            });
            if (citem.score) {
              citem.score.map((sitem, s) => {
                arr[s].score = sitem;
              });
            }

            useranswer = arr;
          }
          tklist.push({
            tkid: citem.tkid,
            score: citem.score,
            tktype: citem.tktype,
            useranswer: useranswer
          });
        });
        newarr.push({
          title: item.title,
          tklist: tklist
        });
      });
      arr = {
        schoolid: this.getParameters.schoolid,
        userid: this.getParameters.userid,
        knowledgeid: this.getParameters.knowledgeid,
        isrand: this.getParameters.isrand,
        sectionid: this.getParameters.sectionid,
        type: this.getParameters.type,
        courseid:this.getParameters.courseid,
        typeid:this.getParameters.typeid,
        categoryid: this.getParameters.categoryid,
        examtime: this.examtime,
        answer: newarr
      };
      this.finishPaper = arr;
    },
    /**上一题 */
    prevQuestion() {
      if (this.index > 0) {
        this.index--;
      } else {
        this.isShowOpera = true;
        this.operatstatus = 3;
        this.operaTips = "已经是第一题";
        setTimeout(() => {
          this.isShowOpera = false;
        }, 1500);
      }
    },
    /**下一题 */
    nextQuestion() {
      let tkLength = this.testList[0].tklist.length;
      if (this.index < tkLength - 1) {
        this.index++;
      } else {
        this.isShowOpera = true;
        this.operatstatus = 3;
        this.operaTips = "已经是最后一题";
        setTimeout(() => {
          this.isShowOpera = false;
        }, 1500);
      }
    },
    /**继续做题 */
    continueDo() {
      let tkids = [];
      this.testList[0].tklist.map(item => {
        tkids.push(item.tkid);
      });
      this.getParameters.tkids = tkids;
      this.loadPage(1);
    },
    /**查看解析 */
    lookAnswer(index) {
      if (this.look_tkanalysis_isfree_tips.result) {
        this.testList[0].tklist.map((item, i) => {
          if (!item.lookanswer) {
            if (i == index) {
              item.lookanswer = true;
            }
          }
        });
      } else {
        this.isShowOpera = true;
        this.operatstatus = 3;
        this.operaTips = this.look_tkanalysis_isfree_tips.msg;
        setTimeout(() => {
          this.isShowOpera = false;
        }, 1500);
      }
    },
    /**结束练习 */
    save() {
      this.getPaperInfo();
      this.finishPaper.answer.map(fitem => {
        fitem.tklist.map(ftitem => {
          if (ftitem.tktype == 6) {
            let allScore = 0;
            ftitem.score.map(sitem => {
              allScore += Number(sitem);
            });
            ftitem.score = allScore;
          }
        });
      });
      if (this.$route.query.type == 6) {
        this.finishPaper.day = this.day;
        this.finishPaper.dailyid = this.dailyid;
        EndDailyTest(this.finishPaper).then(res => {
          if (res.result) {
            this.isShowOpera = true;
            this.operatstatus = 1;
            this.operaTips = "结束练习";
            setTimeout(() => {
              this.isShowOpera = false;
              this.$router.push({
                name: "ExaminationResult",
                query: {
                  answerid: res.id,
                  type: 2,
                  paperid: this.getParameters.paperid,
                  categoryid: this.getParameters.categoryid,
                  dtnum: this.testList.length,
                  tknum: this.paperInfo.tk_num,
                  answertime: this.set_dailytime,
                  sumscore: this.paperInfo.sumscore,
                  schoolname: this.paperInfo.copyfrom,
                  typeid:this.$route.query.typeid
                }
              });
            }, 1500);
          } else {
            this.isShow = false;
            this.isShowOpera = true;
            this.operatstatus = 0;
            this.operaTips = res.msg;
            setTimeout(() => {
              this.isShowOpera = false;
            }, 1500);
          }
        });
      } else {
        EndTest(this.finishPaper).then(res => {
          if (res.result) {
            this.isShowOpera = true;
            this.operatstatus = 1;
            this.operaTips = "结束练习";
            setTimeout(() => {
              this.isShowOpera = false;
              this.$router.push({
                name: "ExaminationResult",
                query: {
                  answerid: res.id,
                  type: 2,
                  paperid: this.getParameters.paperid,
                  categoryid: this.getParameters.categoryid,
                  dtnum: this.testList.length,
                  tknum: this.paperInfo.tk_num,
                  answertime: this.paperInfo.exam_time,
                  sumscore: this.paperInfo.sumscore,
                  schoolname: this.paperInfo.copyfrom,
                  typeid:this.$route.query.typeid
                }
              });
            }, 1500);
          } else {
            this.isShow = false;
            this.isShowOpera = true;
            this.operatstatus = 0;
            this.operaTips = res.msg;
            setTimeout(() => {
              this.isShowOpera = false;
            }, 1500);
          }
        });
      }

      clearInterval(this.time);
    },
    /**获取第几天 */
    setIndex(i, day) {
      this.currentData = i;
      this.day = day;
      if (this.dateList[i].is_test) {
        this.loaded = true;
      } else {
        clearInterval(this.time);
        this.loadPage();
      }
    },
    leftClick() {
      this.clickindex--;
    },
    rightClick() {
      this.clickindex++;
    },
    init(){
  
      if(!this.$store.state.isallowpractise){
        this.$store.ready((state)=>{
            if(state.userid!=0){
              //document.body.style.height = '3000px';
              let paperInfoTitle = ''
              if(this.$route.query.type==1){
                paperInfoTitle='题库练习'
              }else if(this.$route.query.type==2){
                paperInfoTitle='错题练习'
              }else if(this.$route.query.type==3){
                paperInfoTitle='收藏练习'
              }else if(this.$route.query.type==4){
                paperInfoTitle = '自定义题库练习'
              }else if(this.$route.query.type==5){
                paperInfoTitle = '知识点练习'
              }else if(this.$route.query.type==6){
                paperInfoTitle = '每日一练'
              }else if(this.$route.query.type==7){
                paperInfoTitle = '课程章节练习'
              }
              
              

              setInterval(this.timer, 50);
              if (this.$route.query.type == 6) {
                GetDailyTestDateList({
                  categoryid: this.getParameters.categoryid
                }).then(res => {
                  if(res.result){
                    this.dateList = res.data;
                    this.dateList.map((item, i) => {
                      if (item.checked == true) {
                        this.currentData = i;
                        this.day = item.day;
                        if (item.is_test) {
                          this.loaded = true;
                          this.test_num = 0
                        } else {
                          this.loadPage();
                        }
                      }
                    });
                    this.clickindex = Math.ceil((this.currentData+1)/7)-1
                    this.$nextTick(() => {
                      this.liwidth = this.$refs.daily.clientWidth/7
                      this.ulWidth = this.liwidth * this.dateList.length;
                    });
                  }
                  
                  
                });
              } else {
                this.loadPage();
              }
              
              window.addEventListener("scroll", this.handleScroll);
            }else{
              this.loaded = true
              this.isShowOpera = true;
              this.operatstatus = 3;
              this.operaTips = '您还未登录，请登录后做题！';
              setTimeout(() => {
                this.isShowOpera = false;
                history.back();
              }, 1500);
            }
        })
      }else{
        //document.body.style.height = '3000px';
        let paperInfoTitle = ''
        if(this.$route.query.type==1){
          paperInfoTitle='题库练习'
        }else if(this.$route.query.type==2){
          paperInfoTitle='错题练习'
        }else if(this.$route.query.type==3){
          paperInfoTitle='收藏练习'
        }else if(this.$route.query.type==4){
          paperInfoTitle = '自定义题库练习'
        }else if(this.$route.query.type==5){
          paperInfoTitle = '知识点练习'
        }else if(this.$route.query.type==6){
          paperInfoTitle = '每日一练'
        }else if(this.$route.query.type==7){
          paperInfoTitle = '课程章节练习'
        }
        
        

        setInterval(this.timer, 50);
        if (this.$route.query.type == 6) {
          GetDailyTestDateList({
            categoryid: this.getParameters.categoryid
          }).then(res => {
            if(res.result){
              this.dateList = res.data;
              this.dateList.map((item, i) => {
                if (item.checked == true) {
                  this.currentData = i;
                  this.day = item.day;
                  if (item.is_test) {
                    this.loaded = true;
                    this.test_num = 0
                  } else {
                    this.loadPage();
                  }
                }
              });
              this.clickindex = Math.ceil((this.currentData+1)/7)-1
              this.$nextTick(() => {
                this.liwidth = this.$refs.daily.clientWidth/7
                this.ulWidth = this.liwidth * this.dateList.length;
              });
            }
            
            
          });
        } else {
          this.loadPage();
        }
        
        window.addEventListener("scroll", this.handleScroll);
      }
      
    
    }
  },
  mounted() {
    this.init()
    this.maxHeight = window.innerHeight - 126
    // if(typeof CONFIG === 'object' && CONFIG.fixed_bottom && CONFIG.head_height){
    //     this.maxHeight = window.innerHeight - (CONFIG.fixed_bottom+CONFIG.head_height) - 126
    // }else{
    //     this.maxHeight = 300
    // }
            
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
    if(this.timer){
      clearInterval(this.timer)
    }
    
  }
};
</script>
<style scoped>
.path-box {
  padding: 12px 44px;
}
.test-tips {
  margin-bottom: 35px;
}
.test-con-left,
.bottom-operating {
  width: 920px;
}
.test-con-left {
  padding-bottom: 60px;
}
.bottom-operating {
  line-height: 50px;
  height: 50px;
  bottom: 0;
  box-shadow: 0px 0px 15px 5px rgba(0, 0, 0, 0.05);
  border-radius: 5px 5px 0 0;
}
.bottom-operating li {
  width: 50%;
}
.bottom-operating li.prev-question {
  width: 25%;
}
.bottom-operating li.next-question {
  width: 25%;
}
.test-type-tips {
  margin: 17px 0;
  padding: 20px;
}
.test-list li {
  padding: 28px 44px;
  margin-bottom: 26px;
  min-height: 740px;
}
.test-option {
  margin: 14px 7px;
}
.test-con-right,
.test-right-inner {
  width: 256px;
}
.top-application {
  margin: 17px 0;
  padding: 20px;
}
.top-application li {
  width: 33.33%;
}
.top-application li p {
  height: 26px;
}
.answer-sheet-box {
  padding: 20px 25px;
}
.answer-sheet-box h2 {
  margin-bottom: 20px;
}
.answer-sheet-box h3 {
  margin-bottom: 16px;
}
.answer-sheet ul li {
  min-width: 20px;
  height: 20px;
  border: 1px solid #bbb;
  line-height: 20px;
  margin-right: 10px;
  margin-bottom: 10px;
  box-sizing: border-box;
  cursor: pointer;
}
.answer-sheet {
  margin-bottom: 28px;
}
.isFixed {
  position: fixed;
  top: 0;
  z-index: 999;
}
.tips-con p {
  margin-bottom: 20px;
  line-height: 50px;
  letter-spacing: 1px;
}
.dialog-btn {
  margin-bottom: 30px;
}
.f50 {
  font-size: 50px;
}
.redress-option {
  margin-bottom: 15px;
  padding-top: 15px;
  border-top: 1px solid #eee;
  margin-top: 15px;
}
.redress-con_con {
  padding-top: 20px;
  border-top: 1px solid #eee;
  margin-bottom: 20px;
}
.redress-con_con textarea {
  border: none;
  width: 100%;
  background: #f8f8f8;
  -webkit-box-shadow: inset 0px 0px 25px #efefef;
  box-shadow: inset 0px 0px 25px #efefef;
  border-radius: 4px;
  padding: 10px;
  box-sizing: border-box;
}
.analysis-box {
  margin: 20px 0;
  padding: 15px;
  background: #f9f9f9;
  line-height: 30px;
}
.answer-sheet-inner{
  max-height: 300px;
  overflow-y: auto;
  margin-left: -25px;
  margin-right: -25px;
  padding: 0 25px;
  margin-bottom: 20px;
}
.answer-tips {
  margin-left: -25px;
  background: #f9f9f9;
  margin-right: -25px;
  padding: 10px 25px;
}
.answer-tips span {
  line-height: 25px;
}
.answer-tips span i {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  margin-right: 5px;
}
.practice-daily {
  overflow: hidden;
}
.left-arrow {
  left: -20px;
  top: 50%;
  margin-top: -9px;
  color: #888;
}
.right-arrow {
  right: -20px;
  top: 50%;
  margin-top: -9px;
  color: #888;
}
.examination-result {
  padding: 30px 162px;
}
.examination-result li {
  float: left;
}
.examination-result .text-box1 {
  width: 317px;
}
.examination-result .text-box2 {
  width: 240px;
}
.examination-result .text-box3 {
  width: 316px;
  padding-left: 110px;
  box-sizing: border-box;
}
.examination-result li p {
  line-height: 42px;
}
</style>
