<template>
  <div class="border">
    <div class="flex-end mb-5">
      <el-button type="primary" @click="addItem">新增</el-button>
    </div>
    <div
      v-for="(item, index) in options"
      :key="index"
      class="flex-align-start"
    >
      <div class="flex-1">
        <label class="label" style="width: 40px;">Q:</label>
        <div style="margin-left: 40px;margin-bottom: 10px;">
          <el-input v-model="item.question" type="textarea" placeholder="请输入问题" :autosize="{ minRows: 2, maxRows: 8}" />
        </div>
      </div>
      <div class="flex-1">
        <label class="label" style="width: 40px;">A:</label>
        <div style="margin-left: 40px;margin-bottom: 10px;">
          <el-input v-model="item.answer" type="textarea" placeholder="请输入回答" :autosize="{ minRows: 2, maxRows: 8}" />
        </div>
      </div>

      <el-popconfirm title="确定删除吗？" style="margin: 5px 0px 5px 10px; margin-left: 10px;" @onConfirm="deleteItem(index)">
        <el-button slot="reference" type="danger">删除</el-button>
      </el-popconfirm>
    </div>
  </div>
</template>

<script>

export default {
  name: 'QAText',

  props: {
    value: {
      type: Array,
      default: () => []
    }
  },

  data() {
    return {
      options: this.value
    }
  },

  watch: {
    value: {
      handler(val) {
        this.options = val
      },
      immediate: true
    },
    options: {
      handler(val) {
        this.$emit('input', val)
      },
      deep: true,
      immediate: true
    }
  },

  mounted() {
  },

  methods: {
    addItem() {
      this.options.push({
        question: '',
        answer: ''
      })
    },
    deleteItem(index) {
      this.options.splice(index, 1)
    }
  }
}
</script>

<style scoped>
.flex-end {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.flex-align-start {
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
}
.flex-1 {
  flex: 1;
}
.mb-5 {
  margin-bottom: 5px;
}

.label {
  text-align: right;
  vertical-align: top;
  float: left;
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 0;
  box-sizing: border-box;
}
.border {
  width: 100%;
  min-height: 300px;
  border-radius: 10px;
  border: 1px solid #EEEEEE;
  padding: 20px;
}
</style>
