<!--共用选择题-->
<style>
  .sharingChooseQuestion {
    width: 100%;
  }

  .B-info {
    font-size: 14PX;
    color: #A8A8B3;
    padding-bottom: 10px;
    margin-left: 13px;
  }

</style>
<template>
  <div class="sharingChooseQuestion">
    <table v-if="data&&data.length>0" class="common_TiTable">
      <tr v-for="(item,index) in data[0]">
        <td style="width:40px;text-align:center;padding-top:8px;">
          {{item.Name}}
        </td>
        <td>
          <div class="InpDIV" v-html="item.Content" v-if="commonIndex!=index" @click="changeEdit(index)"></div>
          <UeEditor v-model="item.Content" :config="config" v-else></UeEditor>
        </td>
        <td style="width:40px;text-align:center;">
          <i v-if="index>0" class="iconfont icon-guanbi Tiku_DelIcon" @click="deleteOpion(index)"></i>
        </td>
      </tr>
      <tfoot>
        <tr>
          <td colspan="3">
            <a class="addTiMuList" style="margin-left:13px;" @click="addOption()">
              <i class="iconfont icon-add"></i>添加选项
            </a>
          </td>
        </tr>
      </tfoot>
    </table>
    <table v-if="data&&data.length>0" class="common_TiTable">
      <tr v-for="(item,index) in data[1]">
        <td style="width:40px;text-align:center;padding-top:8px;">
          {{index+1}}
        </td>
        <td>
          <div class="InpDIV" v-html="item.Content" v-if="commonIndex2!=index" @click="changeEdit2(index)"></div>
          <UeEditor v-model="item.Content" :config="config" v-else></UeEditor>
        </td>
        <td style="width:40px;text-align:center;">
          <i v-if="index>0" class="iconfont icon-guanbi Tiku_DelIcon" @click="deleteQuestion(index)"></i>
        </td>
      </tr>
      <tfoot>
        <tr>
          <td colspan="3">
            <a class="addTiMuList" style="margin-left:13px;" @click="addQuestion()">
              <i class="iconfont icon-add"></i>添加题干
            </a>
          </td>
        </tr>
      </tfoot>
    </table>
    <div class="B-info">答案：</div>
    <table v-if="data&&data.length>0" style="margin-bottom:20px;">
      <tr v-for="(item,index) in data[1]">
        <td style="padding:12px 0 0 13px;">
          {{index+1}}
        </td>
        <td>
          <select v-model="item.Name" class="selectBox" style="margin:10px 0 0 0;outline:none;">
            <template v-for="(cItem,cIndex) in data[0]">
              <option :key="cIndex" :label="cItem.Name" :value="cItem.Name">
              </option>
            </template>
          </select>
        </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,
        commonIndex2: -2
      };
    },
    created() {
      this.initConfig();
    },
    methods: {
      initConfig() {
        this.optionTitleList = getOptionList();
      },
      //删除选项
      deleteOpion(index) {
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定删除？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          this.data[0].splice(index, 1);
          this.commonIndex = -1;
          this.calcOptionTitle();
        }).onCancel(() => {

        });
      },
      //新增选项
      addOption() {
        if (this.data.length < 26) {
          this.data[0].push({
            Name: "",
            Content: "",
          });
          this.calcOptionTitle();
        } else {
          this.$q.notify({
            type: 'warning',
            position: 'center',
            timeout: 1500,
            message: `最多只能添加26个选项`
          })
          return
        }
      },
      //删除题干
      deleteQuestion(index) {
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定删除？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          this.data[1].splice(index, 1);
          this.commonIndex2 = -1;
          this.calcOptionTitle();
        }).onCancel(() => {

        });
      },
      //添加题干
      addQuestion() {
        this.data[1].push({
          Name: "",
          Content: "",
        });
        this.calcOptionTitle();
      },
      //重新计算选择Title[A,B,C,D....]
      calcOptionTitle() {
        if (this.data && this.data.length > 0) {
          this.data[0].forEach((item, index) => {
            item.Name = this.optionTitleList[index];
          })
        }
      },
      //返回数据到父组件
      returnDataToParent() {
        this.$emit('getChild', this.data);
      },
      getAnswer() {
        this.setOption.Answer = "";
        if (this.data && this.data.length > 1) {
          var answer = "";
          this.data[1].forEach(item => {
            answer += "," + item.Name;
          })
          if (answer != "") {
            answer = answer.substring(1);
          }
          this.setOption.Answer = answer;
        }
      },
      changeEdit(index) {
        this.commonIndex = index;
        this.commonIndex2 = -1;
      },
      changeEdit2(index) {
        this.commonIndex = -1;
        this.commonIndex2 = index;
      }
    },
    mounted() {

    },
    watch: {
      data: {
        handler(newValue) {
          this.setOption.Title = this.data[1][0].Content;
          this.returnDataToParent();
        },
        deep: true
      },
    }
  };

</script>
