<template>
  <el-dialog
    class="modify-ability-dim"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="600px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="140px"
    >
      <el-form-item prop="courseCode" label="课程代码：" required>
        <el-input
          v-model.trim="modalForm.courseCode"
          placeholder="请输入课程代码"
          readonly
        ></el-input>
      </el-form-item>
      <el-form-item prop="courseName" label="课程名称：" required>
        <el-input
          v-model.trim="modalForm.courseName"
          placeholder="请输入课程名称"
          readonly
        ></el-input>
      </el-form-item>
      <el-form-item prop="namePrimary" label="一级能力维度：">
        <el-input
          v-model.trim="modalForm.namePrimary"
          placeholder="请输入一级能力维度"
          clearable
          @change="dimValidate"
        ></el-input>
      </el-form-item>
      <el-form-item prop="codePrimary" label="一级维度编号：">
        <el-input
          v-model.trim="modalForm.codePrimary"
          placeholder="请输入一级维度编号"
          clearable
          @change="dimValidate"
        ></el-input>
      </el-form-item>
      <el-form-item label="一级维度术语解释：">
        <el-input
          v-model.trim="modalForm.interpretation"
          type="textarea"
          :autosize="{ minRows: 3, maxRows: 5 }"
          placeholder="请输入一级维度术语解释"
          maxlength="100"
          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 type="danger" @click="cancel" plain>取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
const initModalForm = {
  id: null,
  courseName: "",
  courseCode: "",
  namePrimary: "",
  codePrimary: "",
  interpretation: "",
  dimensionType: "ABILITY"
};

export default {
  name: "modify-ability-dim",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    },
    dictData: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return (this.isEdit ? "编辑" : "新增") + "能力维度";
    }
  },
  data() {
    const firstNameValidator = (rule, value, callback) => {
      let existFirstDim = this.dictData.find(
        item => item.namePrimary === value
      );

      if (this.isEdit) {
        if (existFirstDim && this.instance.namePrimary !== value) {
          return callback(new Error("一级能力维度已存在"));
        }
      } else {
        if (existFirstDim) {
          return callback(new Error("一级能力维度已存在"));
        }
      }

      callback();
    };
    const firstCodeValidator = (rule, value, callback) => {
      let existFirstDim = this.dictData.find(
        item => item.codePrimary === value
      );

      if (this.isEdit) {
        if (existFirstDim && this.instance.codePrimary !== value) {
          return callback(new Error("一级维度编号已存在"));
        }
      } else {
        if (existFirstDim) {
          return callback(new Error("一级维度编号已存在"));
        }
      }

      callback();
    };

    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      rules: {
        namePrimary: [
          {
            required: true,
            message: "请输入一级能力维度",
            trigger: "change"
          },
          {
            max: 30,
            message: "一级能力维度字数不能超过30",
            trigger: "change"
          },
          {
            validator: firstNameValidator,
            trigger: "change"
          }
        ],
        codePrimary: [
          {
            required: true,
            pattern: /^[A-Z]{1}$/,
            message: "一级维度编号只能是大写字母A-Z，长度为1",
            trigger: "change"
          },
          {
            validator: firstCodeValidator,
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      this.modalForm = this.$objAssign(initModalForm, val);
    },
    visibleChange() {
      this.initData(this.instance);
      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    dimValidate() {
      this.$refs.modalFormComp.validateField([
        "namePrimary",
        "codePrimary",
        "nameSecond",
        "codeSecond"
      ]);
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      this.$emit("modified", this.modalForm);
      this.cancel();
    }
  }
};
</script>
