<template>
  <el-dialog
    class="modify-semester"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="600px"
    :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="evaluation" label="评价方式：">
        <el-input
          v-model.trim="modalForm.evaluation"
          placeholder="请输入评价方式"
          clearable
          :disabled="isEdit"
        ></el-input>
      </el-form-item>
      <el-form-item prop="evaluationDesc" label="评价方式描述：">
        <el-input
          v-model="modalForm.evaluationDesc"
          placeholder="请输入评价方式描述"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 6 }"
          :maxlength="999"
          resize="none"
          show-word-limit
        ></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 { updateCourseEvaluation } from "../../api";
import { mapState } from "vuex";

const initModalForm = {
  evaluationId: null,
  teachCourseId: "",
  evaluation: "",
  evaluationDesc: "",
};

export default {
  name: "modify-course-evaluation",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    ...mapState("base", ["cwStatus"]),
    isEdit() {
      return !!this.instance.evaluationId;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "评价方式";
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      rules: {
        evaluation: [
          {
            required: true,
            message: "请输入评价方式",
            trigger: "change",
          },
          {
            message: "评价方式不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        evaluationDesc: [
          {
            required: true,
            max: 999,
            message: "评价方式描述不能超过999个字",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    initData(val) {
      this.modalForm = this.$objAssign(initModalForm, val);
    },
    visibleChange() {
      this.initData(this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.cwStatus.everSettingStatus) {
        const confirm = await this.$confirm(
          `${this.title}会影响权重设置，确定要${this.title}吗?`,
          "提示",
          {
            type: "warning",
          }
        ).catch(() => {});
        if (confirm !== "confirm") return;
      }

      if (this.isSubmit) return;
      this.isSubmit = true;
      let datas = { ...this.modalForm, id: this.modalForm.evaluationId };
      const res = await updateCourseEvaluation(datas).catch(() => {});
      this.isSubmit = false;

      if (!res) return;

      this.$message.success(this.title + "成功！");
      if (res.warning) {
        this.$notify({
          title: "警告",
          message: "评价方式有变化，请重新设置权重设置！",
          type: "warning",
          duration: 5000,
        });
      }
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
