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

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

  .sharingChooseQuestion .sharingChoose_td {
    width: 50px;
    text-align: center;
  }

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

</style>
<template>
  <div class="sharingChooseQuestion">
    <table v-if="data&&data.length>0" class="common_TiTable">
      <!--选项部分-->
      <tr v-for="(item,index) in data[0]" :key="index">
        <td class="sharingChoose_td" style="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 class="sharingChoose_td">
          <i v-if="data[0].length>1" class="iconfont icon-guanbi Tiku_DelIcon" @click="deleteOpion(index)"></i>
        </td>
      </tr>
      <tr>
        <td>
        </td>
        <td colspan="2">
          <a class="addTiMuList" @click="addOption()">
            <i class="iconfont icon-add"></i>添加选项
          </a>
        </td>
      </tr>
      <!--题干部分-->
      <tr v-for="(item,index) in data[1]" :key="index+1000">
        <td class="sharingChoose_td" style="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 class="sharingChoose_td">
          <i v-if="data[1].length>1" class="iconfont icon-guanbi Tiku_DelIcon" @click="deleteQuestion(index)"></i>
        </td>
      </tr>
      <tr>
        <td>
        </td>
        <td colspan="2">
          <a class="addTiMuList" @click="addQuestion()">
            <i class="iconfont icon-add"></i>添加题干
          </a>
        </td>
      </tr>
      <tr>
        <td colspan="3" class="B-info">
          答案：
        </td>
      </tr>
      <tr v-for="(item,index) in data[1]" :key="index+2000">
        <td class="sharingChoose_td">
        第 {{index+1}} 题
        </td>
        <td colspan="2">
          <select v-model="item.Name" class="selectBox" style="outline:none;margin-left:10px;">
            <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
      },
      questionData: {
        handler(newValue) {
          this.data = this.questionData;
        },
        deep: true
      },
    }
  };

</script>
