<template>
  <div class="course-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
            style="width: 142px;"
            v-model.trim="filter.courseName"
            placeholder="请输入内容"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="课程编码：">
          <el-input
            style="width: 142px;"
            v-model.trim="filter.courseCode"
            placeholder="请输入内容"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="icon icon-search" @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button type="warning" icon="icon icon-plus" @click="toAdd"
          >新增课程</el-button
        >
      </div>
    </div>
    <div class="part-box">
      <el-table ref="TableList" :data="courses" border stripe>
        <el-table-column prop="courseName" label="课程名称"></el-table-column>
        <el-table-column prop="courseCode" label="课程编码"></el-table-column>
        <el-table-column label="操作" align="center" width="120px">
          <template slot-scope="scope">
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-edit"
              @click="toEdit(scope.row)"
              title="编辑"
            ></el-button>
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-delete"
              @click="toDelete(scope.row)"
              title="删除"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <modify-course
      :instance="curCourse"
      @modified="getList"
      ref="ModifyCourse"
    ></modify-course>
  </div>
</template>

<script>
import { courseListPage, deleteCourse } from "../api";
import ModifyCourse from "../components/ModifyCourse";

export default {
  name: "course-manage",
  components: { ModifyCourse },
  data() {
    return {
      filter: {
        courseName: "",
        courseCode: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      courses: [],
      curCourse: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await courseListPage(datas);
      this.courses = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toAdd() {
      this.curCourse = {};
      this.$refs.ModifyCourse.open();
    },
    toEdit(row) {
      this.curCourse = row;
      this.$refs.ModifyCourse.open();
    },
    toDelete(row) {
      this.$confirm("确定要删除当前课程吗?", "提示", {
        cancelButtonClass: "el-button--danger is-plain",
        confirmButtonClass: "el-button--primary",
        type: "warning"
      }).then(async () => {
        await deleteCourse(row.id);
        this.$message.success("删除成功！");
        this.deletePageLastItem();
      });
    }
  }
};
</script>
