<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>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="课程：">
            <el-select
              v-model="filter.teachCourseId"
              placeholder="课程"
              filterable
            >
              <el-option
                v-for="item in courses"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              >
                <span>{{ `${item.name}（${item.code}）` }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="班级名称：">
            <el-input
              v-model.trim="filter.teachClazzName"
              placeholder="班级名称"
              clearable
            ></el-input>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            :disabled="!filter.teachCourseId"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'add')"
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="toAdd"
          >新增班级</el-button
        >
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <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="170px">
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'edit')"
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >重命名</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'Window')"
              class="btn-primary"
              type="text"
              @click="toEditStudent(scope.row)"
              >人员管理</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'delete')"
              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,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <!-- ModifyClazzSimple -->
    <modify-clazz-simple
      :instance="curRow"
      :courses="courses"
      @modified="getList"
      ref="ModifyClazzSimple"
    ></modify-clazz-simple>
    <!-- ModifyClazzSimpleStudent -->
    <modify-clazz-simple-student
      ref="ModifyClazzSimpleStudent"
      :clazz="curRow"
      @modified="getList"
    ></modify-clazz-simple-student>
  </div>
</template>

<script>
import {
  clazzSimpleListPage,
  clazzTeachCourseQuery,
  deleteClazzSimple
} from "../api";
import ModifyClazzSimple from "../components/ModifyClazzSimple";
import ModifyClazzSimpleStudent from "../components/ModifyClazzSimpleStudent";

export default {
  name: "clazz-simple-manage",
  components: { ModifyClazzSimple, ModifyClazzSimpleStudent },
  data() {
    return {
      filter: {
        teachCourseId: "",
        teachClazzName: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      courses: [],
      dataList: [],
      curRow: {}
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      if (this.$route.params.teachCourseId) {
        this.filter.teachCourseId = this.$route.params.teachCourseId;
        this.getCourseSimple();
        this.getList();
      } else {
        await this.getCourseSimple();
        this.filter.teachCourseId = this.courses[0] && this.courses[0].id;
        this.getList();
      }
    },
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      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();
    },
    async getCourseSimple() {
      const data = await clazzTeachCourseQuery();
      this.courses = data || [];
    },
    toAdd() {
      this.curRow = { teachCourseId: this.filter.teachCourseId };
      this.$refs.ModifyClazzSimple.open();
    },
    toEdit(row) {
      this.curRow = row;
      this.$refs.ModifyClazzSimple.open();
    },
    toEditStudent(row) {
      this.curRow = row;
      this.$refs.ModifyClazzSimpleStudent.open();
    },
    toDelete(row) {
      this.$confirm(`确定要删除班级【${row.teachClazzName}】吗?`, "提示", {
        type: "warning"
      })
        .then(async () => {
          await deleteClazzSimple([row.id]);
          this.$message.success("删除成功！");
          this.deletePageLastItem();
        })
        .catch(() => {});
    }
  }
};
</script>
