<template>
  <div class="edit-composition">
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="100px"
    >
      <!-- <el-form-item prop="topicNo" label="大题序号：">
        <el-input-number
          v-model="modalForm.topicNo"
          :min="1"
          :max="99"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item> -->
      <el-form-item prop="topicName" label="题目名称：">
        <el-input
          v-model="modalForm.topicName"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 5 }"
          resize="none"
          placeholder="请输入题目名称"
          clearable
          maxlength="100"
          show-word-limit
        ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

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

export default {
  name: "edit-composition",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalForm: { ...initModalForm },
      rules: {
        topicNo: [
          {
            required: true,
            message: "请输入大题序号",
            trigger: "change",
          },
        ],
        topicName: [
          {
            required: true,
            message: "请输入题目名称",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      const valInfo = val.parent || val;
      this.modalForm = { ...valInfo };
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;
      this.modalForm.topicName = this.modalForm.topicName.trim();
      this.$emit("modified", this.modalForm);
    },
  },
};
</script>
