<template>
  <el-dialog
    class="modify-course"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="550px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-position="top"
    >
      <el-form-item prop="basicCourseId" label="课程名称：">
        <el-select
          class="width-full"
          v-model="modalForm.basicCourseId"
          placeholder="课程"
          clearable
          filterable
          @change="courseChange"
        >
          <el-option
            v-for="item in courses"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          >
            <span>{{ `${item.name}（${item.code}）` }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="basicCourseCode" label="课程编码：">
        <el-input
          v-model.trim="modalForm.basicCourseCode"
          placeholder="课程编码"
          readonly
        ></el-input>
      </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 { updateCourseSimple, courseSimpleQuery } from "../api";

const initModalForm = {
  id: null,
  basicCourseId: "",
  basicCourseCode: ""
};

export default {
  name: "modify-course-simple",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "课程";
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      courses: [],
      rules: {
        basicCourseId: [
          {
            required: true,
            message: "请选择课程",
            trigger: "change"
          }
        ],
        basicCourseCode: [
          {
            required: true,
            message: "需要课程编码",
            trigger: "change"
          }
        ]
      }
    };
  },
  mounted() {
    this.getCourseSimple();
  },
  methods: {
    async getCourseSimple() {
      const data = await courseSimpleQuery();
      this.courses = data || [];
    },
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
      }
    },
    visibleChange() {
      this.initData(this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    courseChange(val) {
      const course = this.courses.find(item => item.id === val);
      this.modalForm.basicCourseCode = course.code;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      let datas = { basicCourseId: this.modalForm.basicCourseId };
      const data = await updateCourseSimple(datas).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

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