<style>
  .edit_stem {
    font-size: 14px;
    color: #646873;
    line-height: 34px;
    /* margin-bottom: 16px; */
    overflow: hidden;
    padding: 4px 0;
    cursor: default;
  }

  .difficulty_Degree {
    display: inline-block;
    width: 70px;
    height: 26px;
    line-height: 26px;
    color: #181E33;
    font-size: 13px;
  }

  .selectBox {
    display: inline-block;
    vertical-align: top;
    text-align: left;
    position: relative;
    cursor: pointer;
    width: 75px;
    line-height: 24px;
    height: 24px;
    background: #FFFFFF;
    border: 1px solid #E1E1E5;
    border-radius: 4px;
    font-size: 12px;
    color: #181E33;
  }

  .blue-border {
    border: 1px solid #FFFFFF;
    box-shadow: 0 0 7px 1px #75BAFF;
    outline: none;
  }

  .addKnowledge {
    display: inline-block;
    width: 98px;
    height: 28px;
    border-radius: 4px;
    border: 1px dashed #E1EAF4;
    text-align: center;
    line-height: 28px;
    color: #91A0B5;
    margin-right: 12px;
    margin-bottom: 8px;
    color: #CAD5E6;
    cursor: pointer;
    font-size: 12px;
    line-height: 24px;
  }

  .addKnowledge:hover {
    opacity: 0.7;
  }

  .knowledge_List {
    display: inline-block;
    padding: 0 14px;
    height: 28px;
    border-radius: 4px;
    border: 1px dashed #E1EAF4;
    text-align: center;
    line-height: 28px;
    color: #91A0B5;
    margin-right: 12px;
    margin-bottom: 8px;
    position: relative;
    cursor: pointer;
  }

  .knowledge_List:hover {
    color: rgba(139, 146, 166, 0.7);
  }

  .knowledge_List:hover i {
    display: block !important;
  }

  .knowledge_List i {
    position: absolute;
    font-size: 15px;
    display: none;
    right: -8px;
    top: -10px;
  }

  .question_number {
    color: #A8A8B3;
    font-size: 12px;
    margin-top: -8px;
    padding-bottom: 12px;
    display: inline-block;
  }

</style>
<template>
  <q-dialog v-model="persistent" persistent content-class="bg-grey-1" transition-show="scale" transition-hide="scale">
    <q-card style="width: 1400px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{objOption.CourseId==0?'新增问题信息':'修改问题信息'}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="row wrap">
          <div class="col-12" v-if="objOption.QuestionId==0" style="margin-bottom:20px">
            题型
            <template v-for="(fItem,fIndex) in firstTypeList">
              <q-btn :key="fIndex" color="accent" :label="fItem.Name" style="margin-left:5px;"
                @click="onItemClick(fItem)" />
            </template>
            <q-btn-dropdown color="accent" 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 class="edit_stem">
            {{questionName}}
          </div>
          <div class="col-12">
            <template v-if="questionObj.Key=='cloze'">
              <span class="question_number">选项处用##题号##替换，如##1##</span>
            </template>
            <UeEditor v-if="questionObj.Key!='sharing-choose'" v-model="objOption.Title" :config="config"
              ref="UE_Title"></UeEditor>
          </div>
          <br />
          <!--单选题-->
          <single v-if="questionObj.Key=='single'||questionObj.Key=='single-number'" :questionData="AnswerList"
            @getChild="getChildData" :setOption="objOption"></single>
          <!--多选题-->
          <multiple v-if="questionObj.Key=='multiple'" :questionData="AnswerList" @getChild="getChildData"
            :setOption="objOption"></multiple>
          <!--填空题-->
          <fill-in v-if="questionObj.Key=='fill-in'" :questionData="AnswerList" @getChild="getChildData"
            :setOption="objOption"></fill-in>
          <!--判断题-->
          <judge v-if="questionObj.Key=='judge'" :questionData="AnswerList" @getChild="getChildData"
            :setOption="objOption"></judge>
          <!--简答题、名词解释、论述题、计算题、口语题、其它-->
          <short-answer v-if="questionObj.Key=='short-answer'||questionObj.Key=='noun-explanation'||questionObj.Key=='essay-question'
            ||questionObj.Key=='calculation' || questionObj.Key=='spoken'|| questionObj.Key=='other'
            " :setOption="objOption"> </short-answer>
          <!--分录题、资料题-->
          <entry-problem v-if="questionObj.Key=='entry-problem'|| questionObj.Key=='data-question'"
            :questionData="AnswerList" :setOption="objOption" @getChild="getChildData">
          </entry-problem>
          <!--连线题-->
          <matching v-if="questionObj.Key=='matching'" :questionData="AnswerList" :setOption="objOption"
            @getChild="getChildData"></matching>
          <!--排序题-->
          <sorting-problem v-if="questionObj.Key=='sorting-problem'" :questionData="AnswerList" :setOption="objOption"
            @getChild="getChildData"></sorting-problem>
          <!--完型填空-->
          <cloze v-if="questionObj.Key=='cloze'" :questionData="AnswerList" :setOption="objOption"
            @getChild="getChildData"></cloze>
          <!--阅读理解、听力题-->
          <reading-comprehensio v-if="questionObj.Key=='reading-comprehensio'||questionObj.Key=='listening'"
            :questionData="AnswerList" :setOption="objOption" @getChild="getChildData"></reading-comprehensio>
          <!--共用选择题-->
          <sharing-choose v-if="questionObj.Key=='sharing-choose'" :questionData="AnswerList" @getChild="getChildData"
            :setOption="objOption">
          </sharing-choose>
          <br />
          <div class="col-12">
            <div style="margin:20px 0;">答案解析</div>
            <UeEditor v-model="objOption.AnswerParse" :config="config" ref="AnswerParse"></UeEditor>
          </div>
          <br />
          <div class="col-12">
            <br />
            <span class="difficulty_Degree">难度</span>
            <select v-model="objOption.DifficultyType" class="selectBox blue-border">
              <template v-for="(cItem,cIndex) in questionDifficultyTypeList">
                <option :key="cIndex" :label="cItem.Name" :value="cItem.Id">
                </option>
              </template>
            </select>
          </div>
          <div class="col-12" style="margin-top:16px;">
            <span class="difficulty_Degree">知识点: </span>
            <a class="addKnowledge" @click="isShowPoint=true">
              <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>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeCourseForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveCourseLoading"
          @click="setQuestion" />
      </q-card-actions>
    </q-card>
    <!--选择知识点-->
    <questionpoint v-if="isShowPoint" :CourseId="CourseId" :openDialog="isShowPoint" @closed="closeQuestionPoint"
      :multiple="true" @selected="getPointList">
    </questionpoint>
  </q-dialog>
</template>

<script>
  import {
    queryQuestionTypeList,
    queryDifficultyType,
    saveQuestion,
    queryQuestionInfo
  } from '../../api/question/question'
  import {
    CreateQuestion, //生成问题
  } from '../../api/question/questionconfig'
  import UeEditor from '../editor/UeEditor'
  //知识点列表
  import questionpoint from './questionpoint'

  import single from '../questiontype/single'
  import multiple from '../questiontype/multiple'
  import fillIn from '../questiontype/fill-in'
  import judge from '../questiontype/judge'
  import shortAnswer from '../questiontype/short-answer'
  import entryProblem from '../questiontype/entry-problem'
  import matching from '../questiontype/matching'
  import sortingProblem from '../questiontype/sorting-problem'
  import cloze from '../questiontype/cloze'
  import readingComprehensio from '../questiontype/reading-comprehensio'
  import sharingChoose from '../questiontype/sharing-choose'
  export default {
    components: {
      UeEditor,
      questionpoint, //知识点
      single, //单选题
      multiple, //多选题
      fillIn, //填空题
      judge, //判断题
      shortAnswer, //简答题
      entryProblem, //分录题
      matching, //连线题
      sortingProblem, //排序题
      cloze, //完型填空
      readingComprehensio, //阅读理解
      sharingChoose, //共用选择题
    },
    props: {
      setingObj: {
        type: Object,
        default: null
      },
      CourseId: {
        type: String,
        default: "0",
      }
    },
    data() {
      return {
        config: {
          initialFrameWidth: null,
          initialFrameHeight: 80,
        },
        persistent: true,
        objOption: {
          QuestionId: 0, //问题编号
          CourseId: 0, //课程编号
          Title: "", //问题名称
          DifficultyType: 1, //难易程度
          AnswerParse: "", //答案解析JSON
          QuestionTypeId: 0, //问题类型
          QuestionTypeKey: "", //问题类型Key
          Knowledge: "", //知识点编号
          IsUpdateJobExam: 0, //是否同步修改引用此题目的作业和考试(1-是)
          SortNum: 0, //排序
          Answer: "", //问题JSON
          IsMutex: 0, //填空题(答案顺序打乱也判正确)
        },
        AnswerList: [],
        optionTitle: "",
        questionObj: {}, //题目类型对象
        firstTypeList: [], //前5个数组
        secondTypeList: [], //后面的题型
        questionDifficultyTypeList: [], //问题难易程度列表
        saveCourseLoading: false,
        isShowPoint: false, //是否显示知识点弹窗
        choosePointArray: [], //知识点列表
      }
    },
    computed: {
      questionName: {
        get() {
          var str = "";
          var sumData = [];
          sumData = sumData.concat(this.firstTypeList).concat(this.secondTypeList);
          if (sumData && sumData.length > 0) {
            sumData.forEach(item => {
              if (item.Key == this.objOption.QuestionTypeKey) {
                str = item.Name;
              }
            })
          }
          return str;
        },
      },
    },
    created() {
      this.getQuestionType();
      this.getDifficultyType();
    },
    mounted() {
      this.initObj()
    },
    methods: {
      //移除知识点
      removePointTag(index) {
        this.choosePointArray.splice(index, 1);
      },
      //获取知识点列表
      getPointList(obj) {
        if (obj && obj.length > 0) {
          if (this.choosePointArray && this.choosePointArray.length > 0) {
            this.choosePointArray = this.choosePointArray.concat(obj);
          } else {
            this.choosePointArray = obj;
          }
        }
      },
      //关闭知识点弹窗
      closeQuestionPoint() {
        this.isShowPoint = false;
      },
      //获取子组件内容
      getChildData(obj) {
        if (obj) {
          this.objOption.QuestionContent = JSON.stringify(obj);
        }
      },
      //题型点击
      onItemClick(item) {
        this.AnswerList = [];
        this.questionObj = item;
        this.objOption.QuestionTypeId = item.QId;
        this.objOption.QuestionTypeKey = item.Key;
        var obj = CreateQuestion(item.Key);
        this.AnswerList = obj;
        if (obj) {
          this.objOption.QuestionContent = JSON.stringify(obj);
        }
      },
      //获取题型列表
      getQuestionType() {
        queryQuestionTypeList({}).then(res => {
          if (res.Code == 1) {
            var tempArray = res.Data;
            if (tempArray && tempArray.length > 0) {
              this.firstTypeList = tempArray.slice(0, 5);
              this.secondTypeList = tempArray.slice(5);
            }
          }
        });
      },
      //获取问题难易程度列表
      getDifficultyType() {
        queryDifficultyType().then(res => {
          if (res.Code == 1) {
            this.questionDifficultyTypeList = res.Data;
          }
        });
      },
      //初始化表单
      initObj() {
        this.objOption.CourseId = this.CourseId;
        if (this.setingObj && this.setingObj.QuestionId > 0) {
          queryQuestionInfo({
            QuestionId: this.setingObj.QuestionId
          }).then(res => {
            this.objOption.QuestionId = res.Data.QuestionId;
            this.objOption.Title = res.Data.Title;
            this.objOption.DifficultyType = res.Data.DifficultyType;
            this.objOption.AnswerParse = res.Data.AnswerParse;
            this.objOption.QuestionTypeId = res.Data.QuestionTypeId;
            this.objOption.QuestionTypeKey = res.Data.QuestionTypeKey;
            this.objOption.Knowledge = res.Data.Knowledge;
            this.objOption.IsUpdateJobExam = res.Data.IsUpdateJobExam;
            this.objOption.SortNum = res.Data.SortNum;
            this.objOption.Answer = res.Data.Answer;
            this.objOption.IsMutex = res.Data.IsMutex;
            this.questionObj.Key = res.Data.QuestionTypeKey;
            this.questionObj.QId = res.Data.QuestionTypeId;
            if (res.Data.QuestionContentObj) {
              this.AnswerList = res.Data.QuestionContentObj;
              this.objOption.QuestionContent = JSON.stringify(res.Data.QuestionContentObj);
            } else {
              this.onItemClick(this.questionObj);
            }
            if (res.Data.QuestionPointList && res.Data.QuestionPointList.length > 0) {
              this.choosePointArray = res.Data.QuestionPointList;
            }
          })
          this.optionTitle = "修改问题信息"
        } else {
          this.optionTitle = "新增问题"
          this.objOption.QuestionId = 0;
          this.objOption.Title = '';
          this.objOption.DifficultyType = 1;
          this.objOption.AnswerParse = "";
          this.objOption.QuestionTypeId = 0;
          this.objOption.QuestionTypeKey = '';
          this.objOption.Knowledge = '';
          this.objOption.IsUpdateJobExam = 0;
          this.objOption.SortNum = 0;
          this.objOption.Answer = '';
          this.objOption.IsMutex = 0;
        }
      },
      //关闭弹窗
      closeCourseForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存问题
      setQuestion() {
        //验证方法
        if (this.validateType()) {
          var pointIds = "";
          if (this.choosePointArray && this.choosePointArray.length > 0) {
            this.choosePointArray.forEach(item => {
              pointIds += ',' + item.PointId
            });
          }
          if (pointIds != '') {
            pointIds = pointIds.substring(1);
          }
          this.objOption.Knowledge = pointIds;
          this.saveCourseLoading = true;
          saveQuestion(this.objOption).then(res => {
            this.saveCourseLoading = false
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据保存成功！',
              position: 'top'
            })
            this.$emit("success")
            this.closeSaveForm()
          }).catch(() => {
            this.saveCourseLoading = false
          })
        }
      },

      //验证
      validateType() {
        //连线题
        var result = true;
        if (this.objOption.QuestionTypeKey == 'matching') {
          if (this.objOption.QuestionContent) {
            var myArr = JSON.parse(this.objOption.QuestionContent);
            for (var i = 0; i < myArr[0].length; i++) {
              if (myArr[0][i].Content == '') {
                this.$q.notify({
                  type: 'negative',
                  position: "top",
                  message: `第一组第${i+1}行数据内容为空！`
                })
                result = false;
                return;
              }
            }
            if (this.isRepeat(myArr[0], 1)) {
              result = this.isRepeat(myArr[0], 1);
            } else {
              return;
            }
            for (var i = 0; i < myArr[1].length; i++) {
              if (myArr[1][i].Content == '') {
                this.$q.notify({
                  type: 'negative',
                  position: "top",
                  message: `第二组第${i+1}行数据内容为空！`
                })
                result = false;
                return;
              }
            }
            if (this.isRepeat(myArr[1], 2)) {
              result = this.isRepeat(myArr[1], 2);
            } else {
              return;
            }
          }
        }
        //公用选择题
        if (this.objOption.QuestionTypeKey == 'sharing-choose') {
          if (this.objOption.QuestionContent) {
            var myArr = JSON.parse(this.objOption.QuestionContent);
            for (var i = 0; i < myArr[0].length; i++) {
              if (myArr[0][i].Content == '') {
                this.$q.notify({
                  type: 'negative',
                  position: "top",
                  message: `选项内容不能为空！`
                })
                result = false;
                return;
              }
            }
            if (this.isRepeat(myArr[0], 3)) {
              result = this.isRepeat(myArr[0], 3);
            } else {
              return;
            }
            for (var i = 0; i < myArr[1].length; i++) {
              if (myArr[1][i].Content == '') {
                this.$q.notify({
                  type: 'negative',
                  position: "top",
                  message: `题干内容不能为空！`
                })
                result = false;
                return;
              }
            }
            if (this.isRepeat(myArr[1], 4)) {
              result = this.isRepeat(myArr[1], 4);
            } else {
              return;
            }
          }
        }
        //其他何口语题
        if (this.objOption.QuestionTypeKey == 'other' || this.objOption.QuestionTypeKey == 'spoken') {
          if (this.objOption.Title == '') {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: `请输入试题的题干`
            })
            result = false;
            return;
          }
        }
        //听力题 阅读理解
        if (this.objOption.QuestionTypeKey == 'listening' || this.objOption.QuestionTypeKey == 'reading-comprehensio') {
          if (this.objOption.QuestionContent) {
            var myArr = JSON.parse(this.objOption.QuestionContent);
            for (var i = 0; i < myArr.length; i++) {
              if (myArr[i].SubTitle == '') {
                this.$q.notify({
                  type: 'negative',
                  position: "top",
                  message: `请输入第${i+1}小题题干!`
                })
                result = false;
                return;
              }
              for (var j = 0; j < myArr[i].SubAnwser.length; j++) {
                if (myArr[i].SubAnwser[j].Content === '') {
                  this.$q.notify({
                    type: 'negative',
                    position: "top",
                    message: `请输入第${i+1}小题选项`
                  })
                  result = false;
                  return;
                }
              }
              if (this.isRepeat(myArr[i].SubAnwser, 3)) {
                result = this.isRepeat(myArr[i].SubAnwser, 3);
              } else {
                return;
              }
            }
          }
        }
        //口语题
        if (this.objOption.QuestionTypeKey == 'spoken') {
          if (this.objOption.Title == '') {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: `请输入试题的题干`
            })
            result = false;
            return;
          }
        }
        //阅读理解
        if (this.objOption.QuestionTypeKey == 'cloze') {
          if (this.objOption.Title == '') {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: `请输入试题的题干`
            })
            result = false;
            return;
          }
          if (this.objOption.QuestionContent) {
            var myArr = JSON.parse(this.objOption.QuestionContent);
            for (var i = 0; i < myArr.length; i++) {
              for (var j = 0; j < myArr[i].length; j++)
                if (myArr[i][j].Content === '') {
                  this.$q.notify({
                    type: 'negative',
                    position: "top",
                    message: `请输入第${i+1}小题选项`
                  })
                  result = false;
                  return;
                }
              if (this.isRepeat(myArr[i], 3)) {
                result = this.isRepeat(myArr[i], 3);
              } else {
                return;
              }
            }
          }
        }
        //排序题
        if (this.objOption.QuestionTypeKey == 'sorting-problem') {
          if (this.objOption.Title == '') {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: `请输入试题的题干`
            })
            result = false;
            return;
          }
          if (this.objOption.QuestionContent) {
            var myArr = JSON.parse(this.objOption.QuestionContent);
            for (var i = 0; i < myArr[0].length; i++) {
              if (myArr[0].Content == '') {
                this.$q.notify({
                  type: 'negative',
                  position: "top",
                  message: `请输入试题选项`
                })
                result = false;
                return;
              }
              if (this.isRepeat(myArr[0], 3)) {
                result = this.isRepeat(myArr[0], 3);
              } else {
                return;
              }
            }
          }
        }
        //资料提 分录题 多选题
        if (this.objOption.QuestionTypeKey == 'data-question' || this.objOption.QuestionTypeKey == 'entry-problem' ||
          this.objOption.QuestionTypeKey == 'multiple' ||
          this.objOption.QuestionTypeKey == 'single') {
          if (this.objOption.Title == '') {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: `请输入题干！`
            })
            result = false;
            return;
          }
          if (this.objOption.QuestionContent) {
            var myArr = JSON.parse(this.objOption.QuestionContent);
            for (var i = 0; i < myArr.length; i++) {
              if (myArr[i].Content == '') {
                this.$q.notify({
                  type: 'negative',
                  position: "top",
                  message: `请输入第${i+1}的选项！`
                })
                result = false;
                return;
              }
              if (this.isRepeat(myArr, 3)) {
                result = this.isRepeat(myArr, 3);
              } else {
                return;
              }
            }
          }
        }
        //计算题
        if (this.objOption.QuestionTypeKey == 'calculation' || this.objOption.QuestionTypeKey == 'essay-question' ||
          this.objOption.QuestionTypeKey == 'noun-explanation') {
          if (this.objOption.Title == '') {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: `请输入题干！`
            })
            result = false;
            return;
          }
          if (this.objOption.Answer == '') {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: `请输入回答！`
            })
            result = false;
            return;
          }
        }
        //判断题
        if (this.objOption.QuestionTypeKey == 'judge') {
          if (this.objOption.Title == '') {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: `请输入题干！`
            })
            result = false;
            return;
          }
        }
        return result;
      },

      //js 判断重复
      isRepeat(Arr, type) {
        var result = true;
        var msg = '';
        switch (type) {
          case 1:
            msg = '第1组数据选项不能相同'
            break;
          case 2:
            msg = '第2组数据选项不能相同'
            break;
          case 3:
            msg = '选项内容不能相同'
            break;
          case 4:
            msg = '题干内容不能相同!';
            break;
          default:
        }
        let num = 0;
        for (var i = 0; i < Arr.length - 1; i++) {
          for (var j = i + 1; j < Arr.length; j++) {
            if (Arr[i].Content === Arr[j].Content) {
              this.$q.notify({
                type: 'negative',
                position: "top",
                message: msg
              })
              num++;
            }
          }
        }
        if (num > 0) {
          result = false;
        } else {
          result = true;
        }
        return result;
      }
    },
  }

</script>
