<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"
            :disabled="!filter.basicCourseId"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div 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
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="teachClazzName" label="姓名"></el-table-column>
        <el-table-column
          prop="teachStudentCount"
          label="工号"
        ></el-table-column>
        <el-table-column
          prop="teachStudentCount"
          label="角色"
        ></el-table-column>
        <el-table-column
          prop="teachStudentCount"
          label="所在机构"
        ></el-table-column>
        <el-table-column class-name="action-column" label="操作" width="170px">
          <template slot-scope="scope">
            <el-button
              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
      ref="ModifyTeacherSimple"
      :course="course"
      @modified="getList"
    ></modify-teacher-simple>
  </div>
</template>

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

export default {
  name: "teacher-simple-manage",
  components: { ModifyTeacherSimple },
  props: {
    course: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      filter: {
        basicCourseId: "",
        userInfo: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      multipleSelection: [],
      dataList: [],
      curRow: {}
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      this.filter.basicCourseId = 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();
    },
    async toDelete(row) {
      const confirm = await this.$confirm(
        `确定要删除教师【${row.teachClazzName}】吗?`,
        "提示",
        {
          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>
