<template>
  <el-dialog
    class="modify-teacher-simple"
    :visible.sync="modalIsShow"
    title="添加教师"
    top="10px"
    width="800px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <div class="mb-4 tab-btns">
      <el-button
        v-for="tab in tabs"
        :key="tab.val"
        size="medium"
        :type="curTab == tab.val ? 'primary' : 'default'"
        @click="selectMenu(tab.val)"
        >{{ tab.name }}
      </el-button>
    </div>
    <!-- input -->
    <div v-if="curTab === 'input'" class="tab-body">
      <el-form
        ref="modalFormComp"
        :model="modalForm"
        :rules="rules"
        :key="modalForm.id"
        label-width="110px"
      >
        <el-form-item prop="teacherName" label="姓名：">
          <el-input
            v-model.trim="modalForm.teacherName"
            placeholder="请输入姓名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="teacherCode" label="用户名/工号：">
          <el-input
            v-model.trim="modalForm.teacherCode"
            placeholder="请输入用户名/工号"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <!-- select -->
    <div v-if="curTab === 'select'" class="tab-body">
      <select-simple-teacher
        ref="SelectSimpleTeacher"
        v-model="selectedTeacherIds"
      ></select-simple-teacher>
    </div>
    <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 { updateTeacherSimple, batchAddTeacherSimple } from "../../api";
import SelectSimpleTeacher from "./SelectSimpleTeacher.vue";
import templateDownload from "@/mixins/templateDownload";

const initModalForm = {
  teachCourseId: null,
  teacherName: "",
  teacherCode: "",
};

export default {
  name: "modify-teacher-simple",
  components: { SelectSimpleTeacher },
  mixins: [templateDownload],
  props: {
    course: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      curTab: "select",
      tabs: [
        {
          name: "教师库添加",
          val: "select",
        },
        {
          name: "手动添加",
          val: "input",
        },
      ],
      modalForm: { ...initModalForm },
      courses: [],
      rules: {
        teacherName: [
          {
            required: true,
            // pattern: /^[0-9a-zA-Z\u4E00-\u9FA5]{1,20}$/,
            // message: "姓名只能输入汉字、数字和字母，长度不能超过20",
            message: "姓名不能超过30个字",
            max: 30,
            trigger: "change",
          },
        ],
        teacherCode: [
          {
            required: true,
            pattern: /^[0-9a-zA-Z_-]{3,30}$/,
            message: "用户名/工号只能由数字字母短横线组成，长度在3-30之间",
            trigger: "change",
          },
        ],
      },
      selectedTeacherIds: [],
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = Object.assign({}, initModalForm, {
        teachCourseId: this.course.id,
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    selectMenu(tab) {
      this.curTab = tab;
    },
    submit() {
      const submitFunc = {
        input: this.submitInput,
        select: this.submitSelect,
      };
      submitFunc[this.curTab]();
    },
    async submitInput() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

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

      if (!data) return;

      this.isSubmit = false;
      this.$message.success("添加成功！");
      this.$emit("modified");
      this.cancel();
    },
    async submitSelect() {
      if (!this.selectedTeacherIds.length) {
        this.$message.error("请选择教师");
        return;
      }

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await batchAddTeacherSimple({
        teachCourseId: this.course.id,
        userIdList: this.selectedTeacherIds.join(),
      }).catch(() => {});
      this.isSubmit = false;

      if (!data) return;

      this.$message.success("添加成功！");
      this.$emit("modified");
      this.selectedTeacherIds = [];
      this.$refs.SelectSimpleTeacher.clearSelection();
      this.cancel();
    },
  },
};
</script>
