<template>
  <div class="input">
    <div class="left">
      <h3>填空题</h3>
      <div
        class="header"
        v-html="Data.content"
      >
      </div>
      <div class="body">
        <p>正确答案：</p>
        <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>
        <p>选中答案：</p>
        <div
          v-for="(item,index) in Data.values"
          :key="index+'a'"
          class="item"
        >
          <label>{{index+1}}、</label> <input
            @input="Success"
            :value="values[index]"
            :data-index='index'
            type="text"
          >
        </div>
      </div>
    </div>
    <div
      v-if="State"
      class="right"
    >
      <div class="header">{{Data.header}}</div>
      <div class="body"><span style="font-weight:bold;">解析：</span>
        <div class="desc">{{Data.desc}}</div>
      </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: [],
      values: [],
    }
  },
  created () {
    this.ValueFunc();
  },
  methods: {
    ValueFunc () {
      let { value } = this.Data, arr = [];
      value.forEach(item => arr.push(item.label));
      this.value = arr;
      let { values } = this.Data, brr = [];
      values.forEach(item => brr.push(item.label));
      this.values = brr;
    },
    // 获取选择的值
    Success (e) {
      let { value, dataset } = e.target, { index } = dataset;
      this.value[index] = value;
      let data = JSON.parse(JSON.stringify(this.value));
      this.$emit("Func", data);
    }
  },
  watch: {
    Data (val, oldval) {
      this.value = [];
      this.values = [];
      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;
    .header {
      padding: 10px;
      line-height: 20px;
      border-bottom: 1px solid #fff;
    }
    .body {
      padding: 10px;
      .desc {
        margin-top: 10px;
        width: 100%;
        height: 100%;
        line-height: 24px;
      }
    }
  }
}
</style>