<!--阅读理解-->
<style>

</style>
<template>
  <div class="readingComprehensioQuestion">
    <template v-if="data&&data.length>0" v-for="(item,index) in data">
      <table>
        <thead>
          <tr>
            <th colspan="3" style="text-align:left;">
              第 {{index+1}} 题 ({{item.QuestionName}})
            </th>
          </tr>
          <tr>
            <th colspan="3">
              <UeEditor v-model="item.SubTitle" :config="config"></UeEditor>
            </th>
          </tr>
        </thead>
        <tbody>
          <!--选择题-->
          <template v-if="item.QuestionKey=='single' ||item.QuestionKey=='multiple'">
            <tr v-for="(subItem,subIndex) in item.SubAnwser">
              <td>
                <template v-if="item.QuestionKey=='single'">
                  <el-checkbox v-model="subItem.IsAnswer" @change="ChangeItem(item,subItem)">{{subItem.Name}}
                  </el-checkbox>
                </template>
                <template v-else-if="item.QuestionKey=='multiple'">
                  <el-checkbox v-model="subItem.IsAnswer">{{subItem.Name}}
                  </el-checkbox>
                </template>
              </td>
              <td>
                <UeEditor v-model="subItem.Content" :config="config"></UeEditor>
              </td>
              <td>
                <a style="cursor:pointer;" @click="deleteOpion(item,subIndex)">删除</a>
              </td>
            </tr>
          </template>
          <!--填空题-->
          <template v-if="item.QuestionKey=='fill-in'">
            <tr v-for="(subItem,subIndex) in item.SubAnwser">
              <td>
                第 {{subIndex+1}} 空
              </td>
              <td>
                <UeEditor v-model="subItem.Content" :config="config"></UeEditor>
              </td>
              <td>
                <a style="cursor:pointer;" @click="deleteOpion(item,subIndex)">删除</a>
              </td>
            </tr>
          </template>
          <!--判断题-->
          <template v-if="item.QuestionKey=='judge'">
            <tr v-for="(subItem,subIndex) in item.SubAnwser">
              <td>
                <el-checkbox v-model="subItem.IsAnswer" @change="ChangeItem(item,subItem)">{{subItem.Name}}
                </el-checkbox>
              </td>
              <td colspan="2" style="text-align:left;">
                {{subItem.Content}}
              </td>
            </tr>
          </template>
          <!--简答题-->
          <template v-if="item.QuestionKey=='short-answer'">
            <tr v-for="(subItem,subIndex) in item.SubAnwser">
              <td colspan="3">
                <UeEditor v-model="subItem.Content" :config="config"></UeEditor>
              </td>
            </tr>
          </template>
        </tbody>
        <tfoot>
          <tr>
            <td colspan="3">
              <!--选择题-->
              <template v-if="item.QuestionKey=='single' ||item.QuestionKey=='multiple'">
                <a style="cursor:pointer;" @click="addOption(item)">添加选项</a>
              </template>
              <!--填空题-->
              <template v-if="item.QuestionKey=='fill-in'">
                <a style="cursor:pointer;" @click="addFillIn(item)">添加更多</a>
              </template>
              &nbsp;
              <a style="cursor:pointer;" @click="deleteQuestion(index)">删除问题</a>
            </td>
          </tr>
        </tfoot>
      </table>
    </template>
    <br />
    <q-btn-dropdown color="primary" label="添加小题" style="margin-left:5px;">
      <q-list>
        <q-item v-for="(fItem,fIndex) in firstTypeList" @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>
</template>
<script>
  import {
    getOptionList, //获取选择标签【A,B,C,D....】
    optionListConfig,
    CreateQuestion
  } from '../../api/question/questionconfig'
  import {
    queryQuestionTypeList,
  } from '../../api/question/question'
  import UeEditor from '../editor/UeEditor'
  export default {
    props: {
      questionData: {
        type: Array,
      }
    },
    components: {
      UeEditor
    },
    data() {
      return {
        data: this.questionData,
        config: {
          initialFrameWidth: null,
          initialFrameHeight: 80,
        },
        optionTitleList: [],
        firstTypeList: [], //问题类型
      };
    },
    created() {
      this.initConfig();
      this.getQuestionType();
    },
    methods: {
      //获取题型列表
      getQuestionType() {
        queryQuestionTypeList({}).then(res => {
          if (res.Code == 1) {
            var tempArray = res.Data;
            if (tempArray && tempArray.length > 0) {
              this.firstTypeList = tempArray.slice(0, 5);
            }
          }
        });
      },
      initConfig() {
        this.optionTitleList = getOptionList();
      },
      //删除小题
      deleteQuestion(index) {
        this.data.splice(index, 1);
      },
      //添加小题
      onItemClick(item) {
        var qObj = {
          QuestionType: item.QId,
          QuestionKey: item.Key,
          QuestionName: item.Name,
          SubTitle: "", //题目内容
          SubAnwser: ""
        };
        if (item.Key == "short-answer") {
          qObj.SubAnwser = [];
          qObj.SubAnwser.push({
            Content: ""
          })
        } else {
          qObj.SubAnwser = CreateQuestion(item.Key);
        }
        this.data.push(qObj);
      },
      //删除选项
      deleteOpion(item, index) {
        item.SubAnwser.splice(index, 1);
        this.calcOptionTitle();
      },
      //新增选项
      addOption(item) {
        if (item.SubAnwser.length < 7) {
          item.SubAnwser.push({
            Name: "",
            Content: "",
            IsAnswer: false
          });
          this.calcOptionTitle();
        } else {
          this.$q.notify({
            type: 'warning',
            position: 'center',
            timeout: 1500,
            message: `最多只能添加7个选项`
          })
          return
        }
      },
      //添加填空
      addFillIn(item) {
        item.SubAnwser.push({
          Content: "",
        });
      },
      //添加小题
      addQuestion() {
        this.data.push(optionListConfig());
      },
      //重新计算选择Title[A,B,C,D....]
      calcOptionTitle() {
        if (this.data && this.data.length > 0) {
          this.data.forEach((item, index) => {
            if (item.QuestionKey == "single" || item.QuestionKey == "multiple")
              item.SubAnwser.forEach((subItem, subIndex) => {
                subItem.Name = this.optionTitleList[subIndex];
              })
          })
        }
      },
      //返回数据到父组件
      returnDataToParent() {
        this.$emit('getChild', this.data);
      },
      //单选
      ChangeItem(item, subItem) {
        if (item.SubAnwser && item.SubAnwser.length > 0) {
          item.SubAnwser.forEach(childItem => {
            childItem.IsAnswer = false;
          })
        }
        subItem.IsAnswer = true;
      },
    },
    mounted() {

    },
    watch: {
      data: {
        handler(newValue) {
          this.returnDataToParent();
        },
        deep: true
      },
    }
  };

</script>
