<style>
  .singleQuestion {
    width: 100%;
  }

</style>
<!--单选题-->
<template>
  <div class="singleQuestion">
    <table v-if="data&&data.length>0" class="common_TiTable">
      <tr v-for="(item,index) in data">
        <td style="width:40px;text-align:center;">
          <div class="Answer_List" @click="ChangeItem(item)" :class="{'Is_Answer':item.IsAnswer}">
            {{item.Name}}
          </div>
        </td>
        <td>
          <div class="InpDIV" v-html="item.Content" v-if="commonIndex!=index" @click="changeEdit(index)"></div>
          <UeEditor v-model="item.Content" v-else :config="config"></UeEditor>
        </td>
        <td style="width:40px;text-align:center;">
          <i class="iconfont icon-guanbi Tiku_DelIcon" @click="deleteOpion(index)"></i>
        </td>
      </tr>
    </table>
    <a class="addTiMuList" @click="addOption()">
      <i class="iconfont icon-add"></i>添加选项
    </a>
  </div>
</template>
<script>
  import {
    getOptionList, //获取选择标签【A,B,C,D....】
  } from '../../api/question/questionconfig'
  import UeEditor from '../editor/UeEditor'
  export default {
    props: {
      questionData: {
        type: Array,
      },
      setOption: {
        type: Object,
      }
    },
    components: {
      UeEditor
    },
    data() {
      return {
        data: this.questionData,
        config: {
          initialFrameWidth: null,
          initialFrameHeight: 90,
        },
        optionTitleList: [],
        commonIndex: -1,
      };
    },
    created() {
      this.initConfig();
    },
    mounted() {},
    methods: {
      initConfig() {
        this.optionTitleList = getOptionList();
      },
      //删除选项
      deleteOpion(index) {
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定删除？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          this.data.splice(index, 1);
          this.commonIndex = -1;
          this.calcOptionTitle();
        }).onCancel(() => {

        });
      },
      //新增选项
      addOption() {
        if (this.data.length < 7) {
          this.data.push({
            Name: "",
            Content: "",
            IsAnswer: false
          });
          this.calcOptionTitle()
        } else {
          this.$q.notify({
            type: 'warning',
            position: 'center',
            timeout: 1500,
            message: `最多只能添加7个选项`
          })
          return
        }
      },
      //重新计算选择Title[A,B,C,D....]
      calcOptionTitle() {
        if (this.data && this.data.length > 0) {
          this.data.forEach((item, index) => {
            if (this.setOption.QuestionTypeKey == "single-number") {
              item.Name = index + 1;
            } else {
              item.Name = this.optionTitleList[index];
            }
          })
        }
      },
      //返回数据到父组件
      returnDataToParent() {
        this.getAnswer();
        this.$emit('getChild', this.data);
      },
      ChangeItem(item) {
        if (this.data && this.data.length > 0) {
          this.data.forEach(item => {
            item.IsAnswer = false;
          })
        }
        item.IsAnswer = true;
      },
      //获取正确答案
      getAnswer() {
        this.setOption.Answer = "";
        if (this.data && this.data.length > 0) {
          this.data.forEach(item => {
            if (item.IsAnswer) {
              this.setOption.Answer = item.Name;
            }
          })
        }
      },
      //点击切换输入
      changeEdit(index) {
        this.commonIndex = index;
      }
    },
    mounted() {

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

</script>
