<template>
  <div class="input">
    <div class="left">
      <h3>填空题 第{{Data.id}}题</h3>
      <div
        class="header"
        v-html="Data.content"
      >
      </div>
      <div class="body">
        <div
          v-for="(item,index) in Data.value"
          :key="index"
          class="item"
        >
          <label>{{index+1}}、</label> <input
            @input="Success"
            :value="value[index]"
            :data-index='index'
            type="text"
          >
        </div>
      </div>
    </div>
    <div
      v-if="State"
      class="right"
    >
      <div class="header">共错误3次，最后一次出错：2021-04-26 15:23，XXXXXX模拟考试</div>
      <div class="body">解析：
        XXXXXX</div>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    //状态  答题 查看答案
    State: {
      type: Boolean,
      default: false
    },
    // 题
    //  {
    //     id: 3,
    //     type: "input",
    //     content: "<p>结合xxx特征<p>",
    //     value: ['', ''],
    //     result: {
    //       yes: ["正确答案", "正确答案"],
    //       no: ["错误答案", "错误答案"],
    //     }
    //   }
    Data: {
      type: Object,
      default: {}
    }
  },
  data () {
    return {
      value: []
    }
  },
  created () {
    this.ValueFunc();
  },
  methods: {
    ValueFunc () {
      return new Promise(resolve => {
        let { value } = this.Data, arr = [];
        value.forEach(item => arr.push(item.label));
        this.value = arr;
        let data = JSON.parse(JSON.stringify(this.value));
        this.$emit("Func", data);
        resolve();
      })
    },
    // 获取选择的值
    Success (e) {
      let { value, dataset } = e.target, { index } = dataset;
      this.value[index] = value.trim();
      let data = JSON.parse(JSON.stringify(this.value));
      this.$emit("Func", data);
    }
  },
  watch: {
    Data (val, oldval) {
      this.value = [];
      this.ValueFunc()
    }
  }
}
</script>
<style lang="less" scoped>
.input {
  display: flex;
  .left {
    width: 100%;
    h3 {
      margin-top: 0;
    }
    .body {
      margin: 5px 0;
      .item {
        margin-bottom: 15px;
        display: flex;
        align-items: flex-end;
        label {
          width: 30px;
        }
        input {
          width: 100%;
          border: 0;
          color: #777;
          font-size: 16px;
          padding-bottom: 2px;
          border-bottom: 1px solid #eee;
          &:focus {
            outline: none;
          }
        }
      }
    }
  }
  .right {
    width: 400px;
    background-color: #f4f7ed;
  }
}
</style>