<template>
  <div class="clazz-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 label="班级名称：">
          <el-input
            v-model.trim="filter.teachClazzName"
            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="teachClazzName"
          label="班级名称"
        ></el-table-column>
        <el-table-column
          prop="teachStudentCount"
          label="学生人数"
        ></el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="170">
          <span slot-scope="scope">{{
            scope.row.createTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column class-name="action-column" label="操作" width="140">
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toManageStudent(scope.row)"
              >管理学生</el-button
            >
            <el-button
              v-if="editable"
              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>

    <!-- AddClazzSimpleDialog -->
    <add-clazz-simple-dialog
      v-if="editable"
      ref="AddClazzSimpleDialog"
      :course="course"
      @modified="getList"
    ></add-clazz-simple-dialog>
    <!-- StudentSimpleManage -->
    <student-simple-manage
      ref="StudentSimpleManage"
      :clazz="curRow"
      @modified="getList"
    ></student-simple-manage>
  </div>
</template>

<script>
import { clazzSimpleListPage, deleteClazzSimple } from "../../api";
import AddClazzSimpleDialog from "./AddClazzSimpleDialog.vue";
import StudentSimpleManage from "./StudentSimpleManage.vue";

export default {
  name: "clazz-simple-manage",
  components: { StudentSimpleManage, AddClazzSimpleDialog },
  props: {
    course: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      filter: {
        teachCourseId: "",
        teachClazzName: "",
      },
      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() {
      Object.assign(this.filter, {
        teachCourseId: this.course.id,
      });
      await this.getList();
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await clazzSimpleListPage(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.AddClazzSimpleDialog.open();
    },
    toManageStudent(row) {
      const course = { ...this.course };
      delete course.id;
      this.curRow = { ...row, ...course, teachCourseId: this.course.id };
      this.$refs.StudentSimpleManage.open();
    },
    async toDelete(row) {
      const confirm = await this.$confirm(
        `确定要删除班级【${row.teachClazzName}】吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      await deleteClazzSimple([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 deleteClazzSimple(this.multipleSelection);
      this.$message.success("删除成功！");
      this.deletePageLastItem(this.multipleSelection.length);
      this.multipleSelection = [];
    },
  },
};
</script>
