<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')">
          <secp-select
            v-model="filter"
            defaultSelectExam
            @exam-default="search"
          ></secp-select>
          <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', 'AddCourse')"
          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="college" label="开课学院"></el-table-column>
        <el-table-column prop="createTime" label="创建人" width="170">
          <template slot-scope="scope">
            {{ scope.row.userName | defaultFieldFilter }}（{{
              scope.row.userLoginName | defaultFieldFilter
            }}）
          </template>
        </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="120">
          <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') && userId === 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>

    <!-- ModifyCourseSimple -->
    <modify-course-simple
      v-if="checkPrivilege('button', 'AddCourse')"
      ref="ModifyCourseSimple"
      :course="curCourse"
      @modified="getList"
    ></modify-course-simple>
    <!-- CourseSimpleDetail -->
    <course-simple-detail
      v-if="checkPrivilege('link', 'Link')"
      ref="CourseSimpleDetail"
      :course="curCourse"
    ></course-simple-detail>
  </div>
</template>

<script>
import { courseSimpleListPage, deleteCourseSimple } from "../api";
import ModifyCourseSimple from "../components/course-simple/ModifyCourseSimple.vue";
import CourseSimpleDetail from "../components/course-simple/CourseSimpleDetail.vue";

export default {
  name: "course-simple-manage",
  components: { ModifyCourseSimple, CourseSimpleDetail },
  data() {
    return {
      filter: { semesterId: "", examId: "", courseName: "" },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      courses: [],
      curCourse: {},
      userId: this.$ls.get("user", { id: "" }).id,
    };
  },
  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();
    },
    search() {
      this.toPage(1);
    },
    toAdd() {
      this.curCourse = { ...this.filter };
      this.$refs.ModifyCourseSimple.open();
    },
    toEditClazz(row) {
      this.curCourse = { ...row, examId: this.filter.examId };
      this.$refs.CourseSimpleDetail.open();
    },
    toDelete(row) {
      this.$confirm(`确定要删除课程【${row.courseName}】吗?`, "提示", {
        type: "warning",
      })
        .then(async () => {
          await deleteCourseSimple([row.id]);
          this.$message.success("删除成功！");
          this.deletePageLastItem();
        })
        .catch(() => {});
    },
  },
};
</script>
