<template>
  <el-dialog
    title="大题信息"
    :visible.sync="modalIsShow"
    width="520px"
    :modal="false"
    append-to-body
    custom-class="side-dialog"
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="90px"
    >
      <el-form-item label="大题名称" prop="detailName">
        <el-input
          v-model="modalForm.detailName"
          class="dialog-input-width"
          placeholder="请输入题型名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="大题描述" prop="description">
        <v-editor
          v-model="modalForm.description"
          placeholder="请输入大题描述"
          :enable-formula="false"
        ></v-editor>
      </el-form-item>
      <el-form-item v-if="!onlyName" label="题型" prop="sourceDetailId">
        <source-detail-select
          v-model="modalForm"
          :course-id="modalForm.courseId"
        ></source-detail-select>
      </el-form-item>
      <el-form-item v-if="!onlyName" label="每题分值" prop="scorePerQuestion">
        <el-input-number
          v-model="modalForm.scorePerQuestion"
          placeholder="请输入每题分值"
          :step="0.1"
          :min="0.1"
          :max="999"
          :controls="false"
          :precision="1"
          step-strictly
        ></el-input-number>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" @click="confirm">保存</el-button>
      <el-button type="danger" plain @click="cancel">返回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { randomCode } from "@/plugins/utils";

const initModalForm = {
  id: null,
  detailName: "",
  courseId: "",
  description: { sections: [] },
  questionType: null,
  sourceDetailId: null,
  scorePerQuestion: 1,
};
export default {
  name: "ModifyDetailStruct",
  props: {
    detail: {
      type: Object,
      default() {
        return {};
      },
    },
    onlyName: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      modalIsShow: false,
      modalForm: { ...initModalForm },
      rules: {
        detailName: [
          {
            required: true,
            message: "请输入大题名称",
            trigger: "change",
          },
        ],
        sourceDetailId: [
          {
            required: true,
            message: "请选择题型",
            trigger: "change",
          },
        ],
        scorePerQuestion: [
          {
            required: true,
            message: "请输入每题分值",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = this.$objAssign(initModalForm, this.detail);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async confirm() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      let data = { ...this.modalForm };
      if (!data.id) data.id = randomCode();
      this.$emit("modified", data);
      this.cancel();
    },
  },
};
</script>
