<template>
  <div class="edit-explain">
    <el-form
      ref="modalFormComp"
      :key="modalForm.id"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item prop="topicName" label="题目名称：">
        <el-input
          v-model="modalForm.topicName"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }"
          resize="none"
          placeholder="请输入题目名称"
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
const initModalForm = {
  id: "",
  topicName: "",
};

export default {
  name: "EditExplain",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalForm: { ...initModalForm },
      rules: {
        topicName: [
          {
            required: true,
            message: "请输入题目名称",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      const valInfo = val.parent || val;
      this.modalForm = this.$objAssign(initModalForm, valInfo);
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      this.modalForm.topicName = this.modalForm.topicName.trim();
      let data = {};
      if (this.instance.parent) {
        data = this.$objAssign(this.instance.parent, this.modalForm);
      } else {
        data = this.$objAssign(this.instance, this.modalForm);
      }
      this.$emit("modified", data);
    },
  },
};
</script>
