<!--  -->
<template>
  <div class="Big-Book-ligature">
    <div class="Big-Book-Single-content">
              
      <div class="Big-Book-con">
                  <span>标题：</span>           <el-input
          style="width: 300px"
          type="textarea"
          autosize
          placeholder="请输入标题"
          v-model="curQue.title"
          @blur="curQue.title = curQue.title.trim()"
        ></el-input>
                
      </div>
      <div
        class="Big-Book-main"
        style="
          border-bottom: 1px #ccc solid;
          margin-bottom: 20px;
          display: flex;
        "
      >
        <div>
          <p>问题</p>
          <div v-for="(item, index) in curQue.con" :key="index">
            <LigatureModule
              :curQueItem="item"
              :index="index"
              :deleteOptionOne="deleteOptionOne"
              :type="'con'"
            />
          </div>
          <div class="addoption" @click="addOption('con')">添加问题</div>
        </div>
        <div>
          <p>答案</p>
          <div v-for="(item, index) in curQue.option" :key="index">
            <LigatureModule
              :curQueItem="item"
              :index="index"
              :deleteOptionOne="deleteOptionOne"
              :type="'option'"
            />
          </div>
          <div class="addoption" @click="addOption('option')">添加选项</div>
        </div>
      </div>
      <div class="Big-Book-divide">
        <el-divider content-position="center">答案配置</el-divider>
        <div class="answerList">
          <div v-for="(item, index) in curQue.con" :key="index">
            <span v-if="item.con">{{ item.con }}：</span>
            <el-radio-group
              @change="changeAnswer(index)"
              v-model="item.AnswerList"
              class="checkbox-group"
            >
              <span v-for="(op, OPindex) in curQue.option" :key="OPindex">
                <el-radio v-if="item.con && op.con" :label="OPindex">{{
                  op.con
                }}</el-radio>
              </span>
            </el-radio-group>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import LigatureModule from "../common/LigatureModule.vue";

export default {
  name: "Single",
  props: ["curQue", "fn_data"],
  components: {
    LigatureModule,
  },
  data() {
    return {
      form: {
        stem: {
          con: "",
          pinyin: "",
          english: "",
          highlight: "",
          underline: "",
          img_url: [],
          mp3_url: [],
        },
      },
      data_structure: {
        type: "ligature",
        name: "连线",
        title: "",
        optionTitle: "",
        conTitle: "",
        con: [
          {
            hanzi: "",
            pinyin: "",
            Number: "",
            mp3_list: [],
            img_list: [],
            definition_list: [],
            Answer: "",
            isAnswer: "",
            isChecked: "",
            judge: "",
            correctInput: "",
            AnswerList: [],
          },
          {
            hanzi: "",
            pinyin: "",
            Number: "",
            mp3_list: [],
            img_list: [],
            definition_list: [],
            Answer: "",
            isAnswer: "",
            isChecked: "",
            judge: "",
            correctInput: "",
            AnswerList: [],
          },
        ],
        option: [
          {
            hanzi: "",
            pinyin: "",
            mp3_list: [],
            img_list: [],
            definition_list: [],
            Answer: "",
            isAnswer: "",
            isChecked: "",
            judge: "",
            correctInput: "",
          },
          {
            hanzi: "",
            pinyin: "",
            mp3_list: [],
            img_list: [],
            definition_list: [],
            Answer: "",
            isAnswer: "",
            isChecked: "",
            judge: "",
            correctInput: "",
          },
        ],
        correct: [
          {
            input: ["", ""],
          },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  //方法集合
  methods: {
    // 修改正确选项中得某一个为正确答案
    changeAnswer(index, value) {
      this.curQue.correct[index] = this.curQue.con[index].AnswerList;
    },
    // 删除其中一个选项
    deleteOptionOne(index, type) {
      if (type == "option") {
        if (this.curQue.option.length <= 2) {
          this.$message.warning("至少要保留两个选项");
          return;
        }
        // // 多选删除选项
        // this.curQue.con.forEach((item) => {
        //   item.AnswerList.forEach((it, i) => {
        //     if (it == index) {
        //       item.AnswerList.splice(i, 1);
        //     }
        //   });
        // });
        // this.curQue.correct.forEach((item) => {
        //   item.forEach((it, i) => {
        //     if (it == index) {
        //       item.AnswerList.splice(i, 1);
        //     }
        //   });
        // });
        this.curQue.option.splice(index, 1);
      } else if (type == "con") {
        if (this.curQue.con.length <= 1) {
          this.$message.warning("至少要保留一个问题");
          return;
        }
        this.curQue.con.splice(index, 1);
      }
      console.log(this.curQue);
    },
    //添加一个选项
    addOption(CorO) {
      let leg = this.curQue.option.length;
      let last = this.curQue.option[leg - 1];
      // if (!last.hanzi && !last.pinyin) {
      //   this.$message.warning("数据不能全是空");
      //   return;
      // }
      let type = this.curQue.type;
      let cur_fn_data_arr = this.fn_data.filter((item) => item.type == type);
      let cur_fn_data = JSON.parse(JSON.stringify(cur_fn_data_arr[0]));
      if (CorO == "con") {
        let obj = cur_fn_data.data_structure.con[0];
        this.curQue.con.push(obj);
      } else {
        let obj = cur_fn_data.data_structure.option[0];
        this.curQue.option.push(obj);
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='scss' scope>
//@import url(); 引入公共css类
.Big-Book-ligature {
  &-content {
    &.m {
      display: flex;
      justify-content: flex-start;
      align-items: flex-start;
    }

    .Big-Book-title {
      font-size: 16px;
      line-height: 40px;
      color: #000;
      margin-right: 15px;
    }
  }
  .Big-Book-main {
    > div {
      margin-bottom: 10px;
      &.Big-Book-pinyin {
        display: flex;
        justify-content: flex-start;
        align-items: center;
      }
    }
    > :nth-child(2) {
      margin-left: 40px;
    }
  }
  .addoption {
    margin: 0 auto;
    width: 300px;
    height: 40px;
    left: 40px;
    top: 304px;
    background: #f3f3f3;
    border: 1px dashed rgba(0, 0, 0, 0.15);
    box-sizing: border-box;
    border-radius: 4px;
    text-align: center;
    line-height: 40px;
    cursor: pointer;
  }
  .Big-Book-divide {
    > div {
      width: 100%;
    }
    .answerList {
      > div {
        margin-top: 16px;
        display: flex;
        align-items: center;
        > :nth-child(1) {
          display: inline-block;
          width: 100px;
          margin-right: 10px;
          word-break: break-all;
        }

        .checkbox-group {
          > span {
            display: inline-block;
            margin-left: 29px;
          }
        }
      }
    }
  }
  .Big-Book-con {
    display: flex;
    align-items: center;
    margin: 10px 0;
  }
}
</style>