<!--多选题-->
<style>
  .multipleQuestion {
    width: 100%;
  }

</style>
<template>
  <div class="multipleQuestion">
    <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="num_option_dx2" :class="{'Is_Answer':item.IsAnswer}">
            {{item.Name}}
          </div>
        </td>
        <td>
          <div class="InpDIV" style="border:0;" v-html="item.Content"></div>
        </td>
        <td style="width:40px;text-align:center;">
        </td>
      </tr>
    </table>
  </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: 80,
        },
        optionTitleList: [],
        commonIndex: -1,
      };
    },
    created() {
      this.initConfig();
    },
    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) => {
            item.Name = this.optionTitleList[index];
          })
        }
      },
      //返回数据到父组件
      returnDataToParent() {
        if (this.data && this.data.length > 0) {
          var answer = "";
          this.data.forEach(item => {
            if (item.IsAnswer) {
              answer += "," + item.Name;
            }
          })
          if (answer != "") {
            answer = answer.substring(1);
          }
          this.setOption.Answer = answer;
        }
        this.$emit('getChild', this.data);
      },
    },
    mounted() {

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

</script>
