<style>
  .examPaper_Top {
    width: 100%;
    height: 83px;
    border-bottom: 1px solid #d9d9d9;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .Exam-main {
    width: 100%;
    min-width: 1000px;
    height: auto !important;
    overflow: hidden;
    margin-top: 20px;
  }

  .Exam-left {
    width: 33%;
    border-right: 1px solid #d9d9d9;
    border-bottom: 1px solid #d9d9d9;
    border-radius: 3px;
    border-top: 1px solid #d9d9d9;
    background: #f0f0f0;
    padding-top: 20px;
    padding-bottom: 0px;
  }

  .f1 {
    float: left;
  }

  .Exam-right {
    width: 65%;
    min-height: 795px;
    background: #fff;
    border-radius: 3px;
    overflow-y: auto;
    border: 1px solid #d9d9d9;
    margin-left: 20px;
  }

  .Left-ptitel {
    font-size: 14px;
    color: #777;
    padding-left: 18px;
  }

  .Left-ptitel span {
    font-size: 14px;
    color: #333;
  }

  .Left-ptitel i {
    color: #c90000;
    font-style: normal;
  }

  .Left-list {
    width: 100%;
    border-top: 1px solid #d9d9d9;
  }

  .List-ul {
    margin: 0;
    padding: 0;
  }

  .List-ul li {
    height: 33px;
    border-bottom: 1px solid #f3f3f3;
    list-style-type: none;
  }

  .List-ul-li-checked {
    background: #a6a6a6;
    color: #333;
    width: 328px;
  }

  .List-ul li {
    display: block;
    padding-left: 25px;
    line-height: 33px;
    font-size: 12px;
    height: 33px;
    border-bottom: 1px solid #f3f3f3;
    color: #555;
    text-decoration: none;
    display: flex;
  }

  .List-ul-li-checked b {
    color: #000;
  }

  .Showbtn {
    display: block;
    width: 90px;
    float: right;
    padding-top: 8px;
    z-index: 1000;
  }

  .Left-btn {
    height: 58px;
    padding: 20px 0 20px 20px;
    width: 100%;
    border-bottom: 1px solid #d9d9d9;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .List-bt {
    height: 27px;
    margin-left: 1px;
    background: #fdfdfd;
    font-size: 14px;
    line-height: 27px;
    padding-left: 10px;
  }

  .List-bt i {
    position: relative;
    top: 1px;
    left: 5px;
  }

  .List-bt i:hover {
    color: #2961fe;
    cursor: pointer;
  }

  .List-ul li:hover {
    background: #a6a6a6;
    color: #333;
  }

  .checkUl {
    background: #a6a6a6;
    color: #333;
  }

  .List-ul li:hover .exam_ListSHow {
    display: block;
  }

  .left-btn-list {
    padding: 5px 10px;
    display: inline-block;
    color: #555;
    border-radius: 3px;
    font-size: 14px;
    border: 1px solid #999;
    margin-right: 10px;
    cursor: pointer;
  }

  #btn1 {
    background: #26a69a !important;
  }

  .difficulty_Degree {
    display: inline-block;
    width: 80px;
    text-align: right;
    line-height: 56px;
  }

  .Left-list::-webkit-scrollbar {
    width: 3px;
    height: 3px;
    background-color: #F5F5F5;
  }

  /*!*定义滚动条轨道 内阴影+圆角*!*/
  .Left-list::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
    border-radius: 10px;
    background-color: #F5F5F5;
  }

  /*!*定义滑块 内阴影+圆角*!*/
  .Left-list::-webkit-scrollbar-thumb {
    border-radius: 10px;
    -webkit-box-shadow: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.5, transparent), to(transparent));
    background-color: #0ae;
  }

  .commonDalei {
    display: flex;
    align-content: center;
    margin-bottom: 20px;
  }

  .examPaper .edui-editor {
    z-index: 99 !important;
  }

  .List-bt input {
    outline: none;
    height: 25px;
  }

  .exam_DetailList {
    width: 80%;
    display: inline-block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    margin-left: 5px;
  }

  .exam_ListSHow {
    display: none;
  }

  .exam_ListSHow i:hover {
    color: #2961fe;
  }

</style>
<template>
  <div class="examPaper page-body">
    <div class="examPaper_Top">
      <div style="width:80%;display:flex;">
        <div style="width:36%;margin-right:20px;">
          <q-input filled label="标题" v-model="DataObj.PaperName" />
        </div>
        <div style="width:24%;">
          <q-select filled option-value="Id" option-label="Name" :options="questionDifficultyTypeList" emit-value
            map-options label="难度" use-input clearable v-model="DataObj.DifficultyType" />
        </div>
        <div style="margin:8px 0 0 20px">
          <q-radio v-model="DataObj.IsOpen" :val="1" label="私有" />
          <q-radio v-model="DataObj.IsOpen" :val="2" label="公开" />
        </div>
      </div>
      <div style="width:20%;text-align:right;">
        <q-btn color="accent" size="sm" class="q-mr-md" label="预览" style="display:none;" />
        <q-btn color="accent" size="sm" class="q-mr-md" label="保存并返回" :loading="savePaperLoading"
          @click="SavePaper()" />
      </div>
    </div>
    <div class="Exam-main">
      <div class="Exam-left f1">
        <p class="Left-ptitel">
          <span>题量：<i>{{examNum}}</i></span>
          <span>  总分：<i>{{examScore.toFixed(2)}}</i></span>
        </p>
        <div class="Left-list" style="min-height:740px;overflow:auto;">
          <template v-if="DataObj&&DataObj.GroupList&&DataObj.GroupList.length>0">
            <div class="List-con1" v-for="(gItem,gIndex) in DataObj.GroupList" :key="gIndex">
              <div class="List-bt">
                <strong class="f1">
                  <template v-if="!gItem.isShowEdit">{{gIndex+1}}&nbsp;{{gItem.GroupName}}
                    （共{{ gItem.DetailsList?gItem.DetailsList.length:0 }}题，{{gItem.GScore}}分）</template>
                  <template v-else>
                    <input style="width:160px;margin-right:10px;" v-model="gItem.GroupName" />
                    <input style="width:40px;" filled v-model="gItem.GScore" @change="changeGroupScore(gItem)" />
                  </template>
                </strong>
                <i class="iconfont icon-edit" title="编辑" style="font-size:18px;margin-right:5px;"
                  @click="getNameEdit(DataObj.GroupList,gIndex)"></i>
                <i class="iconfont icon-ico_commodity_defaul" v-if="gItem.isShowEdit" title="保存"
                  style="top:0;margin-right:5px;" @click="saveExamName(DataObj.GroupList)"></i>
                <i class="iconfont icon-shangyi2" style="margin-right:5px;" @click.stop="MoveFatherItem(gIndex,0)"
                  v-if="gIndex!=0"></i>
                <i class="iconfont icon-xiayi" @click.stop="MoveFatherItem(gIndex,1)"
                  v-if="gIndex!=DataObj.GroupList.length-1"></i>
                <i class="iconfont icon-shanchu2" style="margin-left:5px;" @click.stop="removeFather(gItem,gIndex)"></i>
              </div>
              <ul class="List-ul" v-if="gItem.DetailsList&&gItem.DetailsList.length>0">
                <li class="" style="cursor:pointer;" :class="{'checkUl':dIndex==ComCheckIndex&&gIndex==ComOneIndex}"
                  v-for="(dItem,dIndex) in gItem.DetailsList" :key="dIndex" @click="ClickItem(dItem,gIndex,dIndex)">
                  ({{dIndex+1}}) <span class="exam_DetailList" v-html="dItem.ShowTitle"></span>
                  <span class="exam_ListSHow">
                    <i class="iconfont icon-shangyi2" @click.stop="MoveChildItem(gIndex,dIndex,0)"
                      style="margin-right:5px;" v-if="dIndex!=0"></i>
                    <i class="iconfont icon-xiayi" @click.stop="MoveChildItem(gIndex,dIndex,1)"
                      v-if="dIndex!=gItem.DetailsList.length-1"></i>
                    <i class="iconfont icon-shanchu2" @click.stop="delExam(gItem.DetailsList,dItem,dIndex)"
                      style="margin-left:5px;font-size:17px;"></i>
                  </span>
                </li>
              </ul>
            </div>
          </template>
        </div>
      </div>
      <div class="Exam-right f1">
        <div class="Left-btn">
           <div>
            <template v-for="(fItem,fIndex) in firstTypeList">
              <q-btn :key="fIndex" color="accent" :id="commonIndex==fIndex?'btn1':''" size="sm" :label="fItem.Name"
                style="margin-left:5px;" @click="onItemClick(fItem,fIndex)" />
            </template>
            <q-btn-dropdown color="accent" size="sm" label="更多" style="margin-left:5px;">
              <q-list>
                <q-item v-for="(fItem,fIndex) in secondTypeList" @click="onItemClick(fItem)" clickable v-close-popup
                  :key="fIndex">
                  <q-item-section>
                    <q-item-label>{{fItem.Name}}</q-item-label>
                  </q-item-section>
                </q-item>
              </q-list>
            </q-btn-dropdown>
          </div>
           <div>
            <q-btn color="accent" size="sm" @click="isShowQuestion=true" class="q-mr-md" label="题库选题"  />
          </div>
        </div>
        <div class="row wrap" v-if="ChooseItem" style="padding:20px;">
          <div class="edit_stem">
            <q-input filled stack-label maxlength="5" :dense="false" @keyup.native="checkPrice(ChooseItem,'Score')"
              v-model="ChooseItem.Score" class="col-12 q-pb-lg" label="分数" />
          </div>
          <div class="col-12">
            <template v-if="ChooseItem.QuestionTypeKey=='cloze'">
              <span class="question_number">选项处用##题号##替换，如##1##</span>
            </template>
            <UeEditor v-if="ChooseItem.QuestionTypeKey!='sharing-choose'" v-model="ChooseItem.Title" :config="config"
              ref="UE_Title"></UeEditor>
          </div>
          <br />
          <!--单选题-->
          <single v-if="ChooseItem.QuestionTypeKey=='single'||ChooseItem.QuestionTypeKey=='single-number'"
            :questionData="ChooseItem.QuestionContentObj" @getChild="getChildData" :setOption="ChooseItem"></single>
          <!--多选题-->
          <multiple v-if="ChooseItem.QuestionTypeKey=='multiple'" :questionData="ChooseItem.QuestionContentObj"
            @getChild="getChildData" :setOption="ChooseItem"></multiple>
          <!--填空题-->
          <fill-in v-if="ChooseItem.QuestionTypeKey=='fill-in'" :questionData="ChooseItem.QuestionContentObj"
            @getChild="getChildData" :setOption="ChooseItem"></fill-in>
          <!--判断题-->
          <judge v-if="ChooseItem.QuestionTypeKey=='judge'" :questionData="ChooseItem.QuestionContentObj"
            @getChild="getChildData" :setOption="ChooseItem"></judge>
          <!--简答题、名词解释、论述题、计算题、口语题、其它-->
          <short-answer v-if="ChooseItem.QuestionTypeKey=='short-answer'||ChooseItem.QuestionTypeKey=='noun-explanation'||ChooseItem.QuestionTypeKey=='essay-question'
            ||ChooseItem.QuestionTypeKey=='calculation' || ChooseItem.QuestionTypeKey=='spoken'|| ChooseItem.QuestionTypeKey=='other'
            " :setOption="ChooseItem"> </short-answer>
          <!--分录题、资料题-->
          <entry-problem
            v-if="ChooseItem.QuestionTypeKey=='entry-problem'|| ChooseItem.QuestionTypeKey=='data-question'"
            :questionData="ChooseItem.QuestionContentObj" :setOption="ChooseItem" @getChild="getChildData">
          </entry-problem>
          <!--连线题-->
          <matching v-if="ChooseItem.QuestionTypeKey=='matching'" :questionData="ChooseItem.QuestionContentObj"
            :setOption="ChooseItem" @getChild="getChildData"></matching>
          <!--排序题-->
          <sorting-problem v-if="ChooseItem.QuestionTypeKey=='sorting-problem'"
            :questionData="ChooseItem.QuestionContentObj" :setOption="ChooseItem" @getChild="getChildData">
          </sorting-problem>
          <!--完型填空-->
          <cloze v-if="ChooseItem.QuestionTypeKey=='cloze'" :questionData="ChooseItem.QuestionContentObj"
            :setOption="ChooseItem" @getChild="getChildData"></cloze>
          <!--阅读理解、听力题-->
          <reading-comprehensio
            v-if="ChooseItem.QuestionTypeKey=='reading-comprehensio'||ChooseItem.QuestionTypeKey=='listening'"
            :questionData="ChooseItem.QuestionContentObj" :setOption="ChooseItem" @getChild="getChildData">
          </reading-comprehensio>
          <!--共用选择题-->
          <sharing-choose v-if="ChooseItem.QuestionTypeKey=='sharing-choose'"
            :questionData="ChooseItem.QuestionContentObj" @getChild="getChildData" :setOption="ChooseItem">
          </sharing-choose>
          <br />
          <div class="col-12">
            <div style="margin:20px 0;">答案解析</div>
            <UeEditor v-model="ChooseItem.AnswerParse" :config="config" ref="UE_AnswerParse"></UeEditor>
          </div>
          <br />
          <div class="col-12 commonDalei" style="margin-top:20px;">
            <span class="difficulty_Degree">难度：</span>
            <q-select filled option-value="Id" size="sm" style="display:inline-block;width:300px;" option-label="Name"
              v-model="ChooseItem.DifficultyType" :options="questionDifficultyTypeList" emit-value map-options />
          </div>
          <div class="col-12 commonDalei">
            <br />
            <span class="difficulty_Degree">所属大类：</span>
            <q-select filled option-value="Id" size="sm" style="display:inline-block;width:300px;" option-label="Name"
              v-model="ChooseItem.Category" :options="questionCategoryList" emit-value map-options />
          </div>
          <div class="col-12 commonDalei">
            <br />
            <span class="difficulty_Degree">考级程度：</span>
            <q-select filled option-value="Id" size="sm" style="display:inline-block;width:300px;" option-label="Name"
              v-model="ChooseItem.LevelType" :options="questionLevalTypeList" emit-value map-options />
          </div>
          <div class="col-12">
            <span class="difficulty_Degree">知识点：</span>
            <a class="addKnowledge" @click="isShowPoint=true" style="cursor:pointer;">
              <i class="iconfont icon-add" style="position:relative;top:1px;"></i>选择知识点
            </a>
            <span class="knowledge_List" v-for="(x, i) in choosePointArray" :key="i">
              {{x.PointName}}
              <i class="iconfont icon-shanchu1" @click="removePointTag(i)"></i>
            </span>
          </div>
        </div>
      </div>
      <questionForm v-if="isShowQuestion" @close="closeQuestForm" @success="getQuestionList"></questionForm>
      <!--选择知识点-->
      <questionpoint v-if="isShowPoint" CourseId="0" :openDialog="isShowPoint" @closed="closeQuestionPoint"
        :multiple="true" @selected="getPointList">
      </questionpoint>
    </div>
  </div>
</template>
<script>
  import UeEditor from '../../components/editor/UeEditor'
  //知识点列表
  import questionpoint from '../../components/question/questionpoint'

  import single from '../../components/questiontype/single'
  import multiple from '../../components/questiontype/multiple'
  import fillIn from '../../components/questiontype/fill-in'
  import judge from '../../components/questiontype/judge'
  import shortAnswer from '../../components/questiontype/short-answer'
  import entryProblem from '../../components/questiontype/entry-problem'
  import matching from '../../components/questiontype/matching'
  import sortingProblem from '../../components/questiontype/sorting-problem'
  import cloze from '../../components/questiontype/cloze'
  import readingComprehensio from '../../components/questiontype/reading-comprehensio'
  import sharingChoose from '../../components/questiontype/sharing-choose'
  import questionForm from '../../components/exam/question-form'
  import {
    GetPaper,
    savePaperInfo,
  } from '../../api/teacher/index';
  import {
    CreateQuestion, //生成问题
  } from '../../api/question/questionconfig'
  import {
    queryQuestionTypeList,
    queryDifficultyType,
    queryQuestionCategory,
    queryQuestionLevelType,
    DeletePaperGroup,
    DeletePaperDetails
  } from '../../api/question/question'
  export default {
    components: {
      UeEditor,
      questionpoint, //知识点
      single, //单选题
      multiple, //多选题
      fillIn, //填空题
      judge, //判断题
      shortAnswer, //简答题
      entryProblem, //分录题
      matching, //连线题
      sortingProblem, //排序题
      cloze, //完型填空
      readingComprehensio, //阅读理解
      sharingChoose, //共用选择题、
      questionForm
    },
    meta: {
      title: "考试"
    },
    watch: {
      'ChooseItem.Title': {
        handler(newValue) {
          if (this.ChooseItem) {
            this.ChooseItem.ShowTitle = this.getRegItem(newValue);
          }
        },
        deep: true
      },
      'ChooseItem.Score': {
        handler(newValue) {
          if (this.ChooseItem) {
            this.ChooseItem.Score = newValue;
            this.calcPaper();
          }
        },
        deep: true
      },
      'ChooseItem.QuestionContentObj': {
        handler(newValue) {
          if (this.ChooseItem) {
            this.ChooseItem.QuestionContentObj = newValue;
          }
        },
        deep: true
      }
    },
    data() {
      return {
        config: {
          initialFrameWidth: null,
          initialFrameHeight: 80,
        },
        msg: {
          PaperId: 0
        },
        //试卷对象
        DataObj: {
          PaperId: 0, //试卷编号
          PaperName: '', //试卷名称
          QuestionBandIds: "", //关联题库编号
          DifficultyType: 1, //试卷难易程度
          GenerateType: 2, //组卷类型(1-智能组卷，2-手动组卷，3-智能导入)
          CreateNum: 1, //生成试卷套数
          PaperType: 2, //试卷类型(1-文件夹，2-试卷)
          ParentId: 0, //父节点编号,
          GroupList: [], //试卷答题分类
          IsOpen: 1 //(1-私有,2-开放)
        },
        //点击问题对象
        ChooseItem: null,
        questionDifficultyTypeList: [], //难易程度列表
        questionLevalTypeList: [],
        questionCategoryList: [], //分类
        choosePointArray: [], //选择知识点
        firstTypeList: [], //第一类题型
        secondTypeList: [], //第二类题型
        allTypeList: [], //所有题型
        isShowQuestion: false,
        commonIndex: -1,
        examNum: 0, //题量
        examScore: 0, //总分
        ComOneIndex: -1,
        ComCheckIndex: -1,
        savePaperLoading: false,
        isShowPoint: false, //选择知识点
      }
    },
    created() {
      if (this.$route.query && this.$route.query.Id) {
        this.msg.PaperId = decodeURI(this.$route.query.Id);
      }
      if (this.$route.query && this.$route.query.ToParentId) {
        if (parseInt(this.$route.query.ToParentId) > 0) {
          this.DataObj.ParentId = parseInt(this.$route.query.ToParentId);
        }
      }
      this.getQuestionType();
      this.getDifficultyType();
      this.getQuestionCategory();
      this.getQuestionLevelType();
    },
    mounted() {
      if (this.msg.PaperId && this.msg.PaperId > 0) {
        this.GetPaperInfo()
      }
    },
    methods: {
      //关闭知识点弹窗
      closeQuestionPoint() {
        this.isShowPoint = false;
      },
      //获取知识点列表
      getPointList(obj) {
        if (obj && obj.length > 0) {
          if (this.choosePointArray && this.choosePointArray.length > 0) {
            this.choosePointArray = this.choosePointArray.concat(obj);
          } else {
            this.choosePointArray = obj;
          }
        }
        var pointIds = "";
        if (this.choosePointArray && this.choosePointArray.length > 0) {
          this.choosePointArray.forEach(item => {
            pointIds += ',' + item.PointId
          });
        }
        if (pointIds != '') {
          this.ChooseItem.Knowledge = pointIds.substring(1);
        }
      },
      //大类改变分数
      changeGroupScore(gItem) {
        var subScore = 0;
        if (gItem.GScore && gItem.GScore > 0) {
          subScore = gItem.GScore;
        }
        if (gItem.DetailsList && gItem.DetailsList.length > 0) {
          var avgScore = Number(subScore / gItem.DetailsList.length).toFixed(2);
          gItem.DetailsList.forEach(sItem => {
            sItem.Score = avgScore;
          })
        }
        // this.calcPaper();
      },
      //上移下移(IsUp:0上移，1下移)
      MoveFatherItem(subIndex, IsUp) {
        var currentItem = this.DataObj.GroupList[subIndex];
        //上移
        if (IsUp == 0) {
          if (subIndex > 0) {
            var upItem = this.DataObj.GroupList[subIndex - 1];
            this.$set(this.DataObj.GroupList, subIndex - 1, currentItem);
            this.$set(this.DataObj.GroupList, subIndex, upItem);
            this.$forceUpdate();
          }
        } else {
          //下移
          if (subIndex != this.DataObj.GroupList.length - 1) {
            var downItem = this.DataObj.GroupList[subIndex + 1];
            this.$set(this.DataObj.GroupList, subIndex + 1, currentItem);
            this.$set(this.DataObj.GroupList, subIndex, downItem);
            this.$forceUpdate();
          }
        }
      },
      //移动子项
      MoveChildItem(Index, subIndex, IsUp) {
        var currentItem = this.DataObj.GroupList[Index].DetailsList[subIndex];
        //上移
        if (IsUp == 0) {
          if (subIndex > 0) {
            var upItem = this.DataObj.GroupList[Index].DetailsList[subIndex - 1];
            this.$set(this.DataObj.GroupList[Index].DetailsList, subIndex - 1, currentItem);
            this.$set(this.DataObj.GroupList[Index].DetailsList, subIndex, upItem);
            this.$forceUpdate();
          }
        } else {
          //下移
          if (subIndex != this.DataObj.GroupList[Index].DetailsList.length - 1) {
            var downItem = this.DataObj.GroupList[Index].DetailsList[subIndex + 1];
            this.$set(this.DataObj.GroupList[Index].DetailsList, subIndex + 1, currentItem);
            this.$set(this.DataObj.GroupList[Index].DetailsList, subIndex, downItem);
            this.$forceUpdate();
          }
        }
      },
      //删除题目
      delExam(gitem, item, index) {
        var that = this;
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定删除',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          if (item.Id == 0) {
            gitem.splice(index, 1);
            that.calcPaper();
          } else {
            let msg = {
              Id: item.Id,
            }
            DeletePaperDetails(msg).then(res => {
              if (res.Code == 1) {
                gitem.splice(index, 1);
                that.calcPaper();
              }
            })
          }
        })
      },
      //删除分组及问题
      removeFather(item, index) {
        var that = this;
        this.$q.dialog({
          title: '提示信息',
          message: '是否删除此分组?',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          if (item.GId == 0) {
            that.DataObj.GroupList.splice(index, 1);
            that.calcPaper();
          } else {
            let msg = {
              GId: item.GId,
              QuestionTypeId: item.QuestionTypeId,
              PaperId: this.msg.PaperId,
            }
            DeletePaperGroup(msg).then(res => {
              if (res.Code == 1) {
                that.DataObj.GroupList.splice(index, 1);
                that.calcPaper();
              }
            })
          }
        })
      },
      //点击修改标题名字和分数
      getNameEdit(item, index) {
        item.forEach(x => {
          x.isShowEdit = false;
        })
        item[index].isShowEdit = true;
        this.$forceUpdate();
      },
      //鼠标移开输入框 保存数据 还有隐藏输入框
      saveExamName(item) {
        item.forEach(x => {
          x.isShowEdit = false;
        })
        this.$forceUpdate();
      },
      //获取子组件内容
      getChildData(obj) {
        if (obj) {
          this.ChooseItem.QuestionContent = JSON.stringify(obj);
        }
      },
      //获取题型列表
      getQuestionType() {
        queryQuestionTypeList({}).then(res => {
          if (res.Code == 1) {
            var tempArray = res.Data;
            this.allTypeList = tempArray;
            if (tempArray && tempArray.length > 0) {
              this.firstTypeList = tempArray.slice(0, 5);
              this.secondTypeList = tempArray.slice(5);
            }
          }
        });
      },
      //题型点击【添加问题】
      onItemClick(item, index) {
        var questionObj = this.getDetailsObj();
        questionObj.QuestionTypeId = item.QId;
        questionObj.QuestionTypeKey = item.Key;
        questionObj.GroupName = item.Name;
        var obj = CreateQuestion(item.Key);
        questionObj.QuestionContentObj = obj;
        if (obj) {
          questionObj.QuestionContent = JSON.stringify(obj);
        }
        var tempArray = [];
        tempArray.push(questionObj);
        this.AddQuestion(tempArray);
      },
      //添加问题
      AddQuestion(qArray) {
        if (this.DataObj) {
          if (!this.DataObj.GroupList) {
            this.DataObj.GroupList = [];
          }
          if (qArray && qArray.length > 0) {
            qArray.forEach(qItem => {
              if (!this.checkGroupExists(qItem.QuestionTypeId)) {
                this.DataObj.GroupList.push({
                  GId: 0, //大题分组编号
                  PaperId: 0, //试卷编号
                  GroupName: qItem.GroupName,
                  QuestionTypeId: qItem.QuestionTypeId,
                  QuestionTypeKey: qItem.QuestionTypeKey,
                  GSortNum: 0,
                  GScore: 0, //分数
                  DetailsList: [], //问题列表
                });
              }
              this.DataObj.GroupList.forEach((gItem, gIndex) => {
                if (gItem.QuestionTypeId == qItem.QuestionTypeId) {
                  if (!gItem.DetailsList) {
                    gItem.DetailsList = [];
                  }
                  gItem.DetailsList.push(qItem);
                }
              })
            })
          }
        }
        this.calcPaper();
      },
      //判断题库是否存在
      checkGroupExists(QuestionTypeId) {
        var isExistsGroup = false;
        if (this.DataObj && this.DataObj.GroupList && this.DataObj.GroupList.length > 0) {
          this.DataObj.GroupList.forEach(item => {
            if (!isExistsGroup) {
              if (item.QuestionTypeId == QuestionTypeId) {
                isExistsGroup = true;
              }
            }
          })
        }
        return isExistsGroup;
      },
      //获取试卷问题详细Obj
      getDetailsObj() {
        var objOption = {
          Id: 0, //试卷详情编号
          PaperId: 0, //试卷编号
          BankId: 0, //题库编号
          QuestionId: 0, //问题编号
          Title: "", //问题标题
          QuestionContent: "", //问题内容
          DifficultyType: 1, //问题难易程度
          AnswerParse: "", //答案解析
          QuestionTypeId: 0, //问题类型编号
          QuestionTypeKey: "", //问题类型Key
          Knowledge: "", //知识点
          IsUpdateJobExam: 0, //是否同步修改引用此题目的作业和考试(1-是)
          SortNum: 0, //排序
          Answer: '', //问题答案【JSON字符串】
          QuestionContentObj: [], //问题选项
          IsMutex: 1, //填空题(答案顺序打乱也判正确)
          Category: 0, //问题大类，
          LevelType: 1, //考级程度
          Score: 0, //分数
          GroupName: "", //分组名称
          ShowTitle: ''
        };
        return objOption;
      },
      //获取问题大类
      getQuestionCategory() {
        queryQuestionCategory().then(res => {
          this.questionCategoryList = res.Data;
        })
      },
      //获取问题考级程度
      getQuestionLevelType() {
        queryQuestionLevelType().then(res => {
          this.questionLevalTypeList = res.Data;
        })
      },
      //获取问题难易程度列表
      getDifficultyType() {
        queryDifficultyType().then(res => {
          if (res.Code == 1) {
            this.questionDifficultyTypeList = res.Data;
          }
        });
      },
      //获取题目
      GetPaperInfo() {
        GetPaper(this.msg).then(res => {
          var tempData = res.Data;
          if (res.Code == 1) {
            this.DataObj.PaperId = tempData.PaperId;
            this.DataObj.PaperName = tempData.PaperName;
            this.DataObj.QuestionBandIds = tempData.QuestionBandIds;
            this.DataObj.DifficultyType = tempData.DifficultyType;
            this.DataObj.GenerateType = tempData.GenerateType;
            this.DataObj.CreateNum = tempData.CreateNum;
            this.DataObj.PaperType = tempData.PaperType;
            this.DataObj.ParentId = tempData.ParentId;
            if (tempData.GroupList && tempData.GroupList.length > 0) {
              this.DataObj.GroupList = tempData.GroupList;
            }
            this.calcPaper();
          }
        })
      },
      //计算试卷相关
      calcPaper() {
        this.examScore = 0;
        this.examNum = 0;
        if (this.DataObj && this.DataObj.GroupList) {
          this.DataObj.GroupList.forEach(x => {
            var tempGScore = 0;
            if (x.DetailsList && x.DetailsList.length > 0) {
              this.examNum += x.DetailsList.length;
              x.DetailsList.forEach(y => {
                if (y.Score) {
                  tempGScore += Number(y.Score);
                  this.examScore += Number(y.Score);
                }
              })
            }
            x.GScore = tempGScore.toFixed(2);
          })
        }
      },
      //点击问题
      ClickItem(questionItem, index, index2) {
        this.ChooseItem = null;
        this.ComOneIndex = index;
        this.ComCheckIndex = index2;
        this.ChooseItem = questionItem;
        this.ChooseItem.QuestionContentObj = questionItem.QuestionContentObj;
        if (questionItem.QuestionTypeKey != 'sharing-choose' && this.$refs.UE_Title) {
          this.$refs.UE_Title.reloadNewValue();
        }
        if (this.$refs.UE_AnswerParse) {
          this.$refs.UE_AnswerParse.reloadNewValue();
        }
        this.ChooseItem.AnswerParse = questionItem.AnswerParse;
        if (questionItem.QuestionPointList) {
          this.choosePointArray = questionItem.QuestionPointList;
        } else {
          this.choosePointArray = []
        }
        this.$forceUpdate();
      },
      //正则匹配替换
      getRegItem(val) {
        let Item = val.replace(/<iframe(([\s\S])*?)<\/iframe>/ig, '[音频]').replace(/<img.*?(?:>|\/>)/gi, '[图片]');
        return Item
      },
      //保存题目
      SavePaper() {
        if (this.validateType()) {
          this.savePaperLoading = true;
          savePaperInfo(this.DataObj).then(res => {
            this.savePaperLoading = false;
            if (res.Code == 1) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功!',
                position: 'top'
              })
              this.$router.push({
                path: '/exam/examPaper',
                query: {}
              });
              // this.ChooseItem = null;
              // this.GetPaperInfo();
            } else {
              this.$q.notify({
                type: 'negative',
                position: "top",
                message: `操作失败!`
              })
            }
          })
        }
      },
      //验证
      validateType() {
        var result = true;
        if (this.DataObj && this.DataObj.GroupList && this.DataObj.GroupList.length > 0) {
          for (let k = 0; k < this.DataObj.GroupList.length; k++) {
            var gIndex = k;
            var newKey = this.DataObj.GroupList[k].QuestionTypeKey;
            var gName = this.DataObj.GroupList[k].GroupName;
            var detailsList = this.DataObj.GroupList[k].DetailsList;
            var retMessage = "";
            switch (newKey) {
              //单选题
              case "single":
                retMessage = this.chooseValidate(gName, detailsList);
                break;
                //多选题
              case 'multiple':
                retMessage = this.chooseValidate(gName, detailsList);
                break;
                //填空题
              case "fill-in":
                retMessage = this.fillInValidate(gName, detailsList);
                break;
                //判断题
              case "judge":
                retMessage = this.judgeValidate(gName, detailsList);
                break;
                //简答题
              case "short-answer":
                retMessage = this.shortanswerValidate(gName, detailsList);
                break;
                //名词解释
              case "noun-explanation":
                retMessage = this.shortanswerValidate(gName, detailsList);
                break;
                //论述题
              case "essay-question":
                retMessage = this.shortanswerValidate(gName, detailsList);
                break;
                //计算题
              case "calculation":
                retMessage = this.shortanswerValidate(gName, detailsList);
                break;
                //分录题
              case "entry-problem":
                retMessage = this.fillInValidate(gName, detailsList);
                break;
                //资料题
              case "data-question":
                retMessage = this.fillInValidate(gName, detailsList);
                break;
                //连线题
              case "matching":
                retMessage = this.matchingValidate(gName, detailsList);
                break;
                //排序题
              case "sorting-problem":
                retMessage = this.sortingproblemValidate(gName, detailsList);
                break;
                //完型填空
              case "cloze":
                retMessage = this.clozeValidate(gName, detailsList);
                break;
                //阅读理解
              case "reading-comprehensio":
                retMessage = this.readingcomprehensioValidate(gName, detailsList);
                break;
                //口语题
              case "spoken":
                retMessage = this.shortanswerValidate(gName, detailsList);
                break;
                //听力题
              case "listening":
                retMessage = this.readingcomprehensioValidate(gName, detailsList);
                break;
                //公用选项题
              case "sharing-choose":
                retMessage = this.sharingchooseValidate(gName, detailsList);
                break;
                //其它题
              case "other":
                retMessage = this.shortanswerValidate(gName, detailsList);
                break;
                //单选题-数字
              case "single-number":
                retMessage = this.chooseValidate(gName, detailsList);
                break;
            }
            if (retMessage != '') {
              this.$q.notify({
                type: 'negative',
                position: "top",
                message: retMessage
              });
              result = false;
              return;
            }
          }
        }
        return result;
      },
      //【单选题、多选题、数字选择题】验证
      chooseValidate(gName, detailsList) {
        var message = "";
        //单选题 多选题
        if (detailsList && detailsList.length > 0) {
          for (let i = 0; i < detailsList.length; i++) {
            var Num = 0;
            if (detailsList[i].Title == '') {
              message = `请填写【${gName}】第 ${i+1} 题 问题内容！`;
              return message;
            }
            for (let j = 0; j < detailsList[i].QuestionContentObj.length; j++) {
              if (detailsList[i].QuestionContentObj[j].Content == '') {
                message = `请填写【${gName}】第 ${i+1} 题，选项 ${detailsList[i].QuestionContentObj[j].Name} 内容！`;
                return message;
              }
              if (detailsList[i].QuestionContentObj[j].IsAnswer) {
                Num++
              }
            }
            if (Num == 0) {
              message = `请设置【${gName}】第 ${i+1} 题 正确答案！`;
              return message;
            }
          }
        }
        return message;
      },
      //【填空题、分录题、资料题】验证
      fillInValidate(gName, detailsList) {
        var message = "";
        for (let i = 0; i < detailsList.length; i++) {
          if (detailsList[i].Title == '') {
            message = `请填写【${gName}】第 ${i+1} 题 问题内容！`;
            return message;
          }
          for (let j = 0; j < detailsList[i].QuestionContentObj.length; j++) {
            if (detailsList[i].QuestionContentObj[j].Content == '') {
              message = `请填写【${gName}】第 ${i+1} 题，第 ${j+1} 空 答案！`;
              return message;
            }
          }
        }
        return message;
      },
      //【判断题】验证
      judgeValidate(gName, detailsList) {
        var message = "";
        for (let i = 0; i < detailsList.length; i++) {
          if (detailsList[i].Title == '') {
            message = `请填写【${gName}】第 ${i+1} 题 问题内容！`;
            return message;
          }
        }
        return message;
      },
      //【简答题、名词解释、论述题、计算题、其它题】验证
      shortanswerValidate(gName, detailsList) {
        var message = "";
        for (let i = 0; i < detailsList.length; i++) {
          if (detailsList[i].Title == '') {
            message = `请填写【${gName}】第 ${i+1} 题 问题内容！`;
            return message;
          }
          if (detailsList[i].Answer == '') {
            message = `请填写【${gName}】第 ${i+1} 题 问题答案！`;
            return message;
          }
        }
        return message;
      },
      //【连线题】验证
      matchingValidate(gName, detailsList) {
        var message = "";
        for (let i = 0; i < detailsList.length; i++) {
          if (detailsList[i].Title == '') {
            message = `请填写【${gName}】第 ${i+1} 题 问题内容！`;
            return message;
          }
          if (detailsList[i].QuestionContentObj.length > 0) {
            for (let p = 0; p < detailsList[i].QuestionContentObj[0].length; p++) {
              if (detailsList[i].QuestionContentObj[0][p].Content == '') {
                message = `请填写【${gName}】第 ${i+1} 题 第一组 选项 ${detailsList[i].QuestionContentObj[0][p].Name} 内容！`
                return message;
              }
            }
          }
          if (detailsList[i].QuestionContentObj.length > 0) {
            for (let p = 0; p < detailsList[i].QuestionContentObj[1].length; p++) {
              if (detailsList[i].QuestionContentObj[1][p].Content == '') {
                message = `请填写【${gName}】第 ${i+1} 题 第二组 选项 ${detailsList[i].QuestionContentObj[1][p].Name} 内容！`
                return message;
              }
            }
          }
        }
        return message;
      },
      //【排序题】验证
      sortingproblemValidate(gName, detailsList) {
        var message = "";
        if (detailsList && detailsList.length > 0) {
          for (let i = 0; i < detailsList.length; i++) {
            if (detailsList[i].Title == '') {
              message = `请填写【${gName}】第 ${i+1} 题 问题内容！`;
              return message;
            }
            for (let j = 0; j < detailsList[i].QuestionContentObj[0].length; j++) {
              if (detailsList[i].QuestionContentObj[0][j].Content == '') {
                message = `请填写【${gName}】第 ${i+1} 题 选项 ${detailsList[i].QuestionContentObj[0][j].Name} 内容！`
                return message;
              }
            }
          }
        }
        return message;
      },
      //【完型填空】验证
      clozeValidate(gName, detailsList) {
        var message = "";
        if (detailsList && detailsList.length > 0) {
          for (let i = 0; i < detailsList.length; i++) {
            if (detailsList[i].Title == '') {
              message = `请填写【${gName}】第 ${i+1} 题 问题内容！`;
              return message;
            }
            for (let j = 0; j < detailsList[i].QuestionContentObj.length; j++) {
              var Num = 0;
              for (var k = 0; k < detailsList[i].QuestionContentObj[j].length; k++) {
                if (detailsList[i].QuestionContentObj[j][k].Content == '') {
                  message =
                    `请填写【${gName}】第 ${i+1} 题，第 ${j+1} 小题 选项 ${detailsList[i].QuestionContentObj[j][k].Name} 内容！`;
                  return message;
                }
                if (detailsList[i].QuestionContentObj[j][k].IsAnswer) {
                  Num++
                }
              }
              if (Num == 0) {
                message = `请设置【${gName}】第 ${i+1} 题，第 ${j+1} 小题 正确答案！`;
                return message;
              }
            }
          }
        }
        return message;
      },
      //【阅读理解、听力题】验证
      readingcomprehensioValidate(gName, detailsList) {
        var message = "";
        if (detailsList && detailsList.length > 0) {
          for (let i = 0; i < detailsList.length; i++) {
            if (detailsList[i].Title == '') {
              message = `请填写【${gName}】第 ${i+1} 题 问题内容！`;
              return message;
            }
            for (let j = 0; j < detailsList[i].QuestionContentObj.length; j++) {
              var subQuestion = detailsList[i].QuestionContentObj[j];
              if (subQuestion) {
                if (subQuestion.SubTitle == '') {
                  message = `请填写【${gName}】第 ${i+1} 题，第 ${j+1} 小题 问题内容！`;
                  return message;
                }
                //单选题、多选题
                if (subQuestion.QuestionKey == 'single' || subQuestion.QuestionKey == "multiple") {
                  var singleNum = 0;
                  for (var k = 0; k < subQuestion.SubAnwser.length; k++) {
                    if (subQuestion.SubAnwser[k].Content == '') {
                      message = `请填写【${gName}】第 ${i+1} 题，第 ${j+1} 小题 选项 ${subQuestion.SubAnwser[k].Name} 内容！`;
                      return message;
                    }
                    if (subQuestion.SubAnwser[k].IsAnswer) {
                      singleNum++
                    }
                  }
                  if (singleNum == 0) {
                    message = `请设置【${gName}】第 ${i+1} 题，第 ${j+1} 小题 正确答案！`;
                    return message;
                  }
                }
                //填空题
                if (subQuestion.QuestionKey == "fill-in") {
                  for (var k = 0; k < subQuestion.SubAnwser.length; k++) {
                    if (subQuestion.SubAnwser[k].Content == '') {
                      message = `请填写【${gName}】第 ${i+1} 题，第 ${j+1} 小题 第 ${k+1} 空答案！`;
                      return message;
                    }
                  }
                }
                //简答题
                if (subQuestion.QuestionKey == "short-answer") {
                  for (var k = 0; k < subQuestion.SubAnwser.length; k++) {
                    if (subQuestion.SubAnwser[k].Content == '') {
                      message = `请填写【${gName}】第 ${i+1} 题，第 ${j+1} 小题答案！`;
                      return message;
                    }
                  }
                }
              }
            }
          }
        }
        return message;
      },
      //【公用选项题】验证
      sharingchooseValidate(gName, detailsList) {
        var message = "";
        if (detailsList && detailsList.length > 0) {
          for (var i = 0; i < detailsList.length; i++) {
            for (var j = 0; j < detailsList[i].QuestionContentObj[0].length; j++) {
              if (detailsList[i].QuestionContentObj[0][j].Content == '') {
                message = `请填写【${gName}】第 ${i+1} 题 第一组 选项 ${detailsList[i].QuestionContentObj[0][j].Name} 内容！`;
                return message;
              }
            }
            for (var j = 0; j < detailsList[i].QuestionContentObj[1].length; j++) {
              if (detailsList[i].QuestionContentObj[1][j].Content == '') {
                message = `请填写【${gName}】第 ${i+1} 题 第二组 题干 ${j+1} 内容！`;
                return message;
              }
              if (detailsList[i].QuestionContentObj[1][j].Name == '') {
                message = `请选择【${gName}】第 ${i+1} 题 第二组 题干 ${j+1} 答案！`;
                return message;
              }
            }
          }
        }
        return message;
      },
      //关闭弹窗
      closeQuestForm() {
        this.isShowQuestion = false;
      },
      //获取选择的问题列表
      getQuestionList(qArray) {
        var tempArray = [];
        if (qArray && qArray.length > 0) {
          qArray.forEach(eItem => {
            var qObj = this.getDetailsObj();
            qObj.QuestionId = eItem.QuestionId;
            qObj.Title = eItem.SourceTitle;
            qObj.ShowTitle = eItem.Title;
            qObj.QuestionContent = eItem.QuestionContent;
            qObj.DifficultyType = eItem.DifficultyType;
            qObj.AnswerParse = eItem.AnswerParse;
            qObj.QuestionTypeId = eItem.QuestionTypeId;
            qObj.QuestionTypeKey = eItem.QuestionTypeKey;
            qObj.Knowledge = eItem.Knowledge;
            qObj.IsUpdateJobExam = eItem.IsUpdateJobExam;
            qObj.SortNum = eItem.SortNum;
            qObj.Answer = eItem.Answer;
            qObj.IsMutex = eItem.IsMutex;
            qObj.Category = eItem.Category;
            qObj.LevelType = eItem.LevelType;
            var tempType = this.allTypeList.find(x => x.QId == qObj.QuestionTypeId);
            if (tempType) {
              qObj.GroupName = tempType.Name;
            }
            //问题选项
            if (eItem.QuestionContent) {
              qObj.QuestionContentObj = JSON.parse(eItem.QuestionContent);
            }
            tempArray.push(qObj);
          });
        }
        this.AddQuestion(tempArray);
        this.isShowQuestion = false;
      },

    }
  }

</script>
