<template>
  <div class="data-manage">
    <div class="part-box">
      <el-form ref="FilterForm" label-position="left" label-width="80px" inline>
        <el-form-item label="年级名称">
          <el-input
            v-model.trim="filter.name"
            placeholder="年级名称模糊查询"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="年级状态">
          <el-select v-model="filter.status" style="width: 150px;" clearable>
            <el-option
              v-for="(val, key) in ABLE_TYPE"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" icon="ios-search" @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box">
      <div class="part-title">
        <div class="part-title-infos">
          <el-button type="primary" icon="md-add" @click="toAdd"
            >新增</el-button
          >
        </div>
      </div>
      <el-table ref="TableList" :data="grades" border>
        <el-table-column
          type="index"
          label="序号"
          width="70"
          align="center"
          :index="indexMethod"
        >
        </el-table-column>
        <el-table-column prop="name" label="年级名称" min-width="200">
        </el-table-column>
        <el-table-column prop="status" label="状态" min-width="200">
          <template slot-scope="scope">
            <span>{{ ABLE_TYPE[scope.row.status] }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="toDelete(scope.row)"
              >删除</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-data -->
    <modify-data
      :instance="curCourse"
      @modified="getList"
      ref="ModifyData"
    ></modify-data>
  </div>
</template>

<script>
import { ABLE_TYPE } from "@/constants/enumerate";
import { courseList, deleteCourse } from "../api";
import ModifyData from "../components/ModifyData";

export default {
  name: "data-manage",
  components: { ModifyData },
  data() {
    return {
      filter: {
        name: "",
        status: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      visible: false,
      grades: [
        {
          id: "11",
          name: "名称1",
          status: "ENABLE"
        },
        {
          id: "22",
          name: "名称2",
          status: "ENABLE"
        },
        {
          id: "33",
          name: "名称3",
          status: "ENABLE"
        }
      ],
      curCourse: {},
      ABLE_TYPE
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    indexMethod(index) {
      return (this.current - 1) * this.size + index + 1;
    },
    async getList() {
      const datas = {
        ...this.filter,
        current: this.current,
        size: this.size
      };
      const data = await courseList(datas);
      this.grades = data.list.map(item => {
        return {
          id: item.id,
          name: item.name,
          status: item.status,
          createTime: item.createTime
        };
      });
      this.total = data.totalCount;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toAdd() {
      this.curCourse = {};
      this.$refs.ModifyData.open();
    },
    toEdit(row) {
      this.curCourse = row;
      this.$refs.ModifyData.open();
    },
    toDelete(row) {
      this.$confirm("确定要删除当前学校吗？", "删除警告", {
        cancelButtonClass: "el-button--primary",
        confirmButtonClass: "el-button--default-act",
        type: "warning"
      })
        .then(async () => {
          await deleteCourse(row.id);
          this.$message.success("删除成功！");
          // 解决最后一项删除后的问题
          this.deletePageLastItem();
        })
        .catch(() => {});
    }
  }
};
</script>
