<!--连线题-->
<style>
  .matchingQuestion {
    width: 100%;
  }

  .team_tit {
    display: inline-block;
    font-size: 14px;
    color: #A8A8B3;
    padding-bottom: 20px;
    margin-left: 13px;
  }

  .line_center {
    display: inline-block;
    width: 28px;
    height: 10px;
    border-top: 1px solid #E1E1E5;
    margin: 13px 12px 0 0;
  }

  .selectBox2 {
    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;
    outline: none;
  }

</style>
<template>
  <div class="matchingQuestion">
    <span class="team_tit" style="margin-top:20px;padding-bottom:0;">第1组：</span>
    <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:10px;">
          {{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 class="iconfont icon-guanbi Tiku_DelIcon" @click="deleteOpion(0,index)"></i>
        </td>
      </tr>
      <tfoot>
        <tr>
          <td colspan="3">
            <a class="addTiMuList" style="margin-left:13px;" @click="addOption(0)">
              <i class="iconfont icon-add"></i>添加更多
            </a>
          </td>
        </tr>
      </tfoot>
    </table>
    <span class="team_tit">第2组：</span>
    <table v-if="data&&data.length>1" class="common_TiTable">
      <tr v-for="(item,index) in data[1]">
        <td style="width:40px;text-align:center;padding-top:10px;">
          {{item.Name}}
        </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 class="iconfont icon-guanbi Tiku_DelIcon" @click="deleteOpion(1,index)"></i>
        </td>
      </tr>
      <tfoot>
        <tr>
          <td colspan="3">
            <a class="addTiMuList" style="margin-left:13px;" @click="addOption(1)">
              <i class="iconfont icon-add"></i>添加更多
            </a>
          </td>
        </tr>
      </tfoot>
    </table>
    <span class="team_tit">答案</span>
    <table v-if="data&&data.length>2" style="padding-left:13px;">
      <tr v-for="(item,index) in data[0]">
        <td style="width:30px;">
          {{item.Name}}
        </td>
        <td>
          <span class="line_center"></span>
        </td>
        <td>
          <select v-model="data[2][index].Content" class="selectBox2">
            <template v-for="(cItem,cIndex) in data[1]">
              <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: -1
      };
    },
    computed: {

    },
    created() {
      this.initConfig();
    },
    methods: {
      initConfig() {
        this.optionTitleList = getOptionList();
      },
      //删除选项
      deleteOpion(index, subIndex) {
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定删除？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          //删除第一组是，也删除答案里面的
          if (index == 0) {
            this.data[2].splice(subIndex, 1);
          }
          this.data[index].splice(subIndex, 1);
          //删除的第二组
          if (index == 1) {
            var temp1 = this.data[1];
            this.data[2].forEach(item => {
              var isExists = false;
              if (temp1 && temp1.length > 0) {
                temp1.forEach(subItem => {
                  if (subItem.Name == item.Content) {
                    isExists = true;
                  }
                })
              }
              if (!isExists) {
                item.Content = "";
              }
            })
          }
          this.commonIndex = -1;
          this.commonIndex2 = -1;
          this.calcOptionTitle();
        }).onCancel(() => {

        });
      },
      //新增选项
      addOption(index) {
        if (index == 0) {
          this.data[2].push({
            Name: "",
            Content: "A",
          });
        }
        this.data[index].push({
          Name: "",
          Content: "",
        });
        this.calcOptionTitle();
      },
      //重新计算选择Title[A,B,C,D....]
      calcOptionTitle() {
        if (this.data && this.data.length > 0) {
          this.data.forEach((item, index) => {
            item.forEach((subItem, subIndex) => {
              if (index == 0 || index == 2) {
                subItem.Name = subIndex + 1;
              } else if (index == 1) {
                subItem.Name = this.optionTitleList[subIndex];
              }
            })
          })
        }
      },
      //返回数据到父组件
      returnDataToParent() {
        this.getAnswer();
        this.$emit('getChild', this.data);
      },
      //获取正确答案
      getAnswer() {
        this.setOption.Answer = "";
        if (this.data && this.data.length > 0) {
          var answer = "";
          this.data[2].forEach(item => {
            answer += "," + item.Content;
          })
          if (answer != "") {
            answer = answer.substring(1);
          }
          this.setOption.Answer = answer;
        }
      },
      //点击切换输入
      changeEdit(index) {
        this.commonIndex = index;
        this.commonIndex2 = -1;
      },
      changeEdit2(index) {
        this.commonIndex2 = index;
        this.commonIndex = -1;
      }
    },
    mounted() {},
    watch: {
      data: {
        handler(newValue) {
          this.returnDataToParent();
        },
        deep: true
      },
    }
  };

</script>
