<template>
  <el-dialog
    custom-class="side-dialog"
    :visible.sync="modalIsShow"
    :title="title"
    width="500px"
    :modal="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item prop="name" label="题型名称">
        <el-input
          v-model="modalForm.name"
          placeholder="请输入题型名称"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item label="题型类型" prop="questionType">
        <el-radio-group
          v-model="modalForm.questionType"
          size="medium"
          style="line-height: 46px"
        >
          <el-radio
            v-for="item in QUESTION_TYPES"
            :key="item.code"
            :label="item.code"
            >{{ item.name }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button type="primary" :disabled="loading" @click="confirm"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateSourceDetailApi } from "../api";
import { QUESTION_TYPES } from "@/constants/constants";

const initModalForm = {
  id: "",
  name: "",
  courseId: "",
  questionType: "",
  rootOrgId: "",
};

export default {
  name: "ModifySourceDetail",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      QUESTION_TYPES,
      modalForm: {
        ...initModalForm,
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入题型名称",
            trigger: "change",
          },
          {
            max: 100,
            message: "题型名称不能超过100",
            trigger: "change",
          },
        ],
        questionType: [
          {
            required: true,
            message: "请输入试题类型",
            trigger: "change",
          },
        ],
      },
      loading: false,
    };
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "题型";
    },
  },
  methods: {
    visibleChange() {
      this.modalForm = this.$objAssign(initModalForm, this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async confirm() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.loading) return;
      this.loading = true;

      const datas = { ...this.modalForm };
      const res = await updateSourceDetailApi(datas).catch(() => {});
      this.loading = false;
      if (!res) return;

      this.$message.success("修改成功！");
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
