<!--排序题-->
<style>

</style>
<template>
  <div class="sortingProblemQuestion">
    <table v-if="data&&data.length>0">
      <tr v-for="(item,index) in data[0]">
        <td>
          {{item.Name}}
        </td>
        <td>
          <UeEditor v-model="item.Content" :config="config"></UeEditor>
        </td>
        <td>
          <a style="cursor:pointer;" @click="deleteOpion(index)">删除</a>
        </td>
      </tr>
      <tfoot>
        <tr>
          <td colspan="3">
            <a style="cursor:pointer;" @click="addOption()">添加选项</a>
          </td>
        </tr>
      </tfoot>
    </table>
    答案：请在下方下拉框中对选项进行排序（顺序从左到右）
    <br />
    <table v-if="data&&data.length>0">
      <tr>
        <td>
          <template v-for="(item,index) in data[1]">
            <select v-model="item.Name">
              <template v-for="(cItem,cIndex) in data[0]">
                <option :key="cIndex" :label="cItem.Name" :value="cItem.Name">
                </option>
              </template>
            </select>
          </template>
        </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,
      },
    },
    components: {
      UeEditor
    },
    data() {
      return {
        data: this.questionData,
        config: {
          initialFrameWidth: null,
          initialFrameHeight: 80,
        },
        optionTitleList: [],
      };
    },
    created() {
      this.initConfig();
    },
    methods: {
      initConfig() {
        this.optionTitleList = getOptionList();
      },
      //删除选项
      deleteOpion(index) {
        this.data[0].splice(index, 1);
        this.calcOptionTitle();
      },
      //新增选项
      addOption() {
        if (this.data.length < 26) {
          this.data[0].push({
            Name: "",
            Content: "",
          });
          this.data[1].push({
            Name: "A",
            Content: "",
          });
          this.calcOptionTitle();
        } else {
          this.$q.notify({
            type: 'warning',
            position: 'center',
            timeout: 1500,
            message: `最多只能添加26个选项`
          })
          return
        }
      },
      //重新计算选择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);
      },
    },
    mounted() {

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

</script>
