<template>
  <el-dialog
    class="modify-course-simple"
    :visible.sync="modalIsShow"
    title="添加教学班"
    top="10px"
    width="800px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form ref="FilterForm" label-position="left" inline label-width="0px">
      <el-form-item>
        <course-common-select
          v-model="filter.courseId"
          placeholder="请选择课程"
          clearable
        ></course-common-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model.trim="filter.clazzName"
          placeholder="请输入教学班名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="TableList"
      :data="dataList"
      border
      max-height="440px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        fixed="left"
        width="55"
        align="center"
      ></el-table-column>
      <el-table-column
        type="index"
        label="序号"
        width="70"
        :index="indexMethod"
      ></el-table-column>
      <el-table-column
        prop="clazzName"
        label="教学班名称"
        min-width="120"
      ></el-table-column>
      <el-table-column prop="name" label="课程名称（代码）" min-width="200">
        <span slot-scope="scope"
          >{{ scope.row.courseName }}（{{ scope.row.courseCode }}）</span
        >
      </el-table-column>
    </el-table>

    <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 { updateClazzSimple, basicTeachClazzListPage } from "../../api";

export default {
  name: "add-clazz-simple-dialog",
  props: {
    course: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      dataList: [],
      filter: {
        clazzName: "",
        courseId: "",
      },
      multipleSelection: [],
    };
  },
  methods: {
    visibleChange() {
      this.multipleSelection = [];
      this.getList();
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async getList() {
      const datas = {
        examId: this.course.examId,
        ...this.filter,
      };

      const data = await basicTeachClazzListPage(datas);
      this.dataList = data || [];
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => item.id);
    },
    async submit() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择班级");
        return;
      }

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await updateClazzSimple({
        idList: this.multipleSelection,
        teachCourseId: this.course.id,
      }).catch(() => {});
      this.isSubmit = false;

      if (!data) return;

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