<!--  -->
<template>
  <div class="Big-Book-ligature">
    <div class="Big-Book-Single-content">
      <div class="adult-book-input-item">
          <span class="adult-book-lable">标题：</span>   <el-input
          class="adult-book-input"
          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"
      >
        <div>
          <p>问题</p>
          <div class="adult-book-input-item">
              <span class="adult-book-lable">问题标题：</span>   <el-input
              class="adult-book-input"
              type="textarea"
              autosize
              placeholder="请输入问题标题"
              v-model="curQue.conTitle"
              @blur="curQue.conTitle = curQue.conTitle.trim()"
            ></el-input>
          </div>
          <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 class="adult-book-input-item">
              <span class="adult-book-lable">答案标题：</span>   <el-input
              class="adult-book-input"
              type="textarea"
              autosize
              placeholder="请输入答案标题"
              v-model="curQue.optionTitle"
              @blur="curQue.optionTitle = curQue.optionTitle.trim()"
            ></el-input>
          </div>
          <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.detail.sentence"
              >{{ item.detail.sentence }}：</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.detail.sentence && op.detail.sentence"
                  :label="OPindex"
                  >{{ op.detail.sentence }}</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", "changeCurQue"],
  components: {
    LigatureModule,
  },
  data() {
    return {
      form: {
        stem: {
          con: "",
          pinyin: "",
          english: "",
          highlight: "",
          underline: "",
          img_url: [],
          mp3_url: [],
        },
      },
      data_structure: {
        type: "ligature_chs",
        name: "连线",
        title: "",
        conTitle: "",
        optionTitle: "",
        con: [
          {
            mp3_list: [],
            img_list: [],
            AnswerList: [],
            detail: {
              pyPosition: "top", //top 拼音在上面；bottom 拼音在下面
              sentence: "", //句子
              segList: [], //分词结果
              seg_words: "",
              wordsList: [],
            },
            number: "",
          },
          {
            mp3_list: [],
            img_list: [],
            AnswerList: [],
            number: "",

            detail: {
              pyPosition: "top", //top 拼音在上面；bottom 拼音在下面
              sentence: "", //句子
              segList: [], //分词结果
              seg_words: "",
              wordsList: [],
            },
          },
        ],
        option: [
          {
            con: "",
            mp3_list: [],
            img_list: [],
            number: "",
            AnswerList: [],
            detail: {
              pyPosition: "top", //top 拼音在上面；bottom 拼音在下面
              sentence: "", //句子
              segList: [], //分词结果
              seg_words: "",
              wordsList: [],
            },
          },
          {
            con: "",
            mp3_list: [],
            img_list: [],
            number: "",
            AnswerList: [],
            detail: {
              pyPosition: "top", //top 拼音在上面；bottom 拼音在下面
              sentence: "", //句子
              segList: [], //分词结果
              seg_words: "",
              wordsList: [],
            },
          },
        ],
        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;
      // }
      if (CorO == "con") {
        let obj = this.data_structure.con[0];
        this.curQue.con.push(obj);
      } else {
        let obj = this.data_structure.option[0];
        this.curQue.option.push(obj);
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    if (!this.curQue) {
      let res_data = JSON.parse(JSON.stringify(this.data_structure));
      this.changeCurQue(res_data);
    }
  },
  //生命周期 - 挂载完成（可以访问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 {
    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>