<template>
  <el-dialog
    custom-class="side-dialog"
    :visible.sync="modalIsShow"
    title="编辑"
    width="550px"
    :modal="true"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :key="modalForm.id"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="所属课程：">
        <el-input v-model.trim="modalForm.course" readonly></el-input>
      </el-form-item>
      <el-form-item label="创建人：">
        <el-input v-model.trim="modalForm.creator" readonly></el-input>
      </el-form-item>
      <el-form-item prop="teacherId" label="命题老师：">
        <el-select
          v-model="modalForm.teacherId"
          class="width-full"
          placeholder="请选择"
          clearable
          filterable
        >
          <el-option
            v-for="item in teachers"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { cardUpdateApi, questionTeacherQueryApi } from "../api";

const initModalForm = {
  id: null,
  course: "",
  creator: "",
  teacherId: "",
};

export default {
  name: "ModifyCard",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      teachers: [],
      rules: {
        teacherId: [
          {
            required: true,
            message: "请选择命题老师",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.getTeachers("");
  },
  methods: {
    async getTeachers(name) {
      const res = await questionTeacherQueryApi(name);
      this.teachers = res.data.content;
    },
    visibleChange() {
      this.modalForm = this.$objAssign(initModalForm, this.instance);
      const hasTeacher = this.teachers.find(
        (item) => item.id === this.instance.teacherId
      );
      if (!hasTeacher) {
        this.modalForm.teacherId = null;
        this.$nextTick(() => {
          this.$refs.modalFormComp.validateField("teacherId");
        });
      }
      this.isSubmit = false;
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      let datas = {
        cardId: this.modalForm.id,
        teacherId: this.modalForm.teacherId,
      };
      const data = await cardUpdateApi(datas).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

      this.isSubmit = false;
      this.$message.success("编辑成功！");
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
