<template>
  <div class="course-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-input
              v-model.trim="filter.courseName"
              placeholder="课程名称"
              clearable
            ></el-input>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @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="courses">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="courseName" label="课程名称"></el-table-column>
        <el-table-column prop="courseCode" 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="160">
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'Link')"
              class="btn-primary"
              type="text"
              @click="toEditClazz(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>

    <!-- ModifyCourseSimple -->
    <modify-course-simple
      v-if="checkPrivilege('button', 'add')"
      ref="ModifyCourseSimple"
      :instance="curCourse"
      @modified="getList"
    ></modify-course-simple>
  </div>
</template>

<script>
import { courseSimpleListPage, deleteCourseSimple } from "../api";
import ModifyCourseSimple from "../components/ModifyCourseSimple";

export default {
  name: "course-simple-manage",
  components: { ModifyCourseSimple },
  data() {
    return {
      filter: {
        courseName: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      courses: [],
      curCourse: {}
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };

      const data = await courseSimpleListPage(datas);
      this.courses = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toAdd() {
      this.curCourse = {};
      this.$refs.ModifyCourseSimple.open();
    },
    toEdit(row) {
      this.curCourse = row;
      this.$refs.ModifyCourseSimple.open();
    },
    toEditClazz(row) {
      this.$router.push({
        name: "ClazzSimpleManage",
        params: {
          teachCourseId: row.id
        }
      });
    },
    toDelete(row) {
      this.$confirm(`确定要删除课程【${row.courseName}】吗?`, "提示", {
        type: "warning"
      })
        .then(async () => {
          await deleteCourseSimple([row.id]);
          this.$message.success("删除成功！");
          this.deletePageLastItem();
        })
        .catch(() => {});
    }
  }
};
</script>
