<template>
  <div class="teacher-simple-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item>
          <el-input
            v-model.trim="filter.userInfo"
            placeholder="请输入姓名或用户名/工号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toPage(1)">查询</el-button>
        </el-form-item>
      </el-form>
      <div v-if="editable" class="part-box-action">
        <el-button type="danger" @click="toBatchDelete">批量删除</el-button>
        <el-button type="primary" @click="toAdd">新增教师</el-button>
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-table
        ref="TableList"
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          v-if="editable"
          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="userName" label="姓名"></el-table-column>
        <el-table-column prop="userCode" label="用户名/工号"></el-table-column>
        <el-table-column prop="roleName" label="角色"></el-table-column>
        <el-table-column prop="orgName" label="所在机构"></el-table-column>
        <el-table-column
          v-if="editable"
          class-name="action-column"
          label="操作"
          width="140px"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toAssignClazz(scope.row)"
              >分配班级</el-button
            >
            <el-button
              v-if="course.createId !== scope.row.userId"
              class="btn-danger"
              type="text"
              @click="toDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>

    <!-- ModifyTeacherSimple -->
    <modify-teacher-simple
      v-if="editable"
      ref="ModifyTeacherSimple"
      :course="course"
      @modified="getList"
    ></modify-teacher-simple>
    <!-- TeacherSimpleAssignCourseDialog -->
    <teacher-simple-assign-course-dialog
      v-if="editable"
      ref="TeacherSimpleAssignCourseDialog"
      :teacher="curRow"
    ></teacher-simple-assign-course-dialog>
  </div>
</template>

<script>
import { teacherSimpleListQuery, deleteTeacherSimple } from "../../api";
import ModifyTeacherSimple from "./ModifyTeacherSimple.vue";
import TeacherSimpleAssignCourseDialog from "./TeacherSimpleAssignCourseDialog.vue";

export default {
  name: "teacher-simple-manage",
  components: { ModifyTeacherSimple, TeacherSimpleAssignCourseDialog },
  props: {
    course: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      filter: {
        teachCourseId: "",
        userInfo: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      multipleSelection: [],
      dataList: [],
      curRow: {},
      userId: this.$ls.get("user", { id: "" }).id,
    };
  },
  computed: {
    editable() {
      return this.userId === this.course.createId;
    },
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      this.filter.teachCourseId = this.course.id;
      await this.getList();
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await teacherSimpleListQuery(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => item.id);
    },
    toAdd() {
      this.$refs.ModifyTeacherSimple.open();
    },
    toAssignClazz(row) {
      this.curRow = {
        ...row,
        teachCourseId: this.course.id,
      };
      this.$refs.TeacherSimpleAssignCourseDialog.open();
    },
    async toDelete(row) {
      const confirm = await this.$confirm(
        `确定要删除教师【${row.userName}】吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      await deleteTeacherSimple([row.id]);
      this.$message.success("删除成功！");
      this.deletePageLastItem();
    },
    async toBatchDelete() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择要删除的数据");
        return;
      }

      const confirm = await this.$confirm(
        `确定要删除选中的这些数据吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      await deleteTeacherSimple(this.multipleSelection);
      this.$message.success("删除成功！");
      this.deletePageLastItem(this.multipleSelection.length);
      this.multipleSelection = [];
    },
  },
};
</script>
