<template>
  <div class="student-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="90px" inline>
        <el-form-item label="学院：">
          <college-select
            v-model="filter.collegeId"
            placeholder="学院"
          ></college-select>
        </el-form-item>
        <el-form-item label="专业：">
          <major-select
            v-model="filter.majorId"
            :college-id="filter.collegeId"
            cascader
            placeholder="专业"
          ></major-select>
        </el-form-item>
        <el-form-item label="班级：">
          <class-select
            v-model="filter.clazzId"
            :major-id="filter.majorId"
            cascader
            placeholder="班级"
          ></class-select>
        </el-form-item>
        <el-form-item label="姓名/学号：">
          <el-input
            style="width: 142px;"
            v-model.trim="filter.queryParams"
            placeholder="姓名/学号"
            clearable
          ></el-input>
        </el-form-item>
        <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', 'delete')"
          type="danger"
          icon="el-icon-delete"
          :disabled="!filterHasQuery"
          @click="toBatchDelete"
          >批量删除</el-button
        >
        <el-button
          type="success"
          icon="el-icon-download"
          v-if="checkPrivilege('button', 'import')"
          ><a :href="downloadUrl" :download="dfilename">模板下载</a></el-button
        >
        <upload-button
          v-if="checkPrivilege('button', 'import')"
          btn-icon="el-icon-circle-plus-outline"
          btn-content="批量导入"
          btn-type="success"
          :upload-url="uploadUrl"
          :format="['xls', 'xlsx']"
          accept=".xls,.xlsx"
          @valid-error="validError"
          @upload-success="uploadSuccess"
        >
        </upload-button>
        <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="studentName" label="姓名"></el-table-column>
        <el-table-column prop="studentCode" label="学号"></el-table-column>
        <el-table-column prop="phoneNumber" label="手机号" width="120">
          <span slot-scope="scope">{{
            scope.row.phoneNumber | defaultFieldFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="collegeName" label="学院"></el-table-column>
        <el-table-column prop="majorName" label="专业"></el-table-column>
        <el-table-column prop="clazz" 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="100"
          fixed="right"
        >
          <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', '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>

    <modify-student
      :instance="curRow"
      @modified="getList"
      ref="ModifyStudent"
    ></modify-student>
  </div>
</template>

<script>
import { studentListQuery, deleteStudent, deleteFilterStudent } from "../api";
import ModifyStudent from "../components/ModifyStudent";
import UploadButton from "../../../components/UploadButton";

export default {
  name: "student-manage",
  components: { ModifyStudent, UploadButton },
  data() {
    return {
      filter: {
        queryParams: "",
        collegeId: "",
        majorId: "",
        clazzId: ""
      },
      queriedFilter: {},
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      // import
      uploadUrl: "/api/admin/basic/student/data_import",
      downloadUrl: "/temps/studentTemplate.xlsx",
      dfilename: "学生导入模板.xlsx"
    };
  },
  computed: {
    filterHasQuery() {
      return !Object.keys(this.filter).some(
        k => this.filter[k] !== this.queriedFilter[k]
      );
    }
  },
  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 studentListQuery(datas);
      this.dataList = data.records;
      this.total = data.total;
      this.queriedFilter = { ...this.filter };
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toAdd() {
      this.curRow = {};
      this.$refs.ModifyStudent.open();
    },
    toEdit(row) {
      this.curRow = row;
      this.$refs.ModifyStudent.open();
    },
    toBatchDelete() {
      this.$confirm(`确定要根据设置的筛选条件删除所有数据吗?`, "提示", {
        type: "warning"
      })
        .then(async () => {
          await deleteFilterStudent(this.filter);
          this.$message.success("删除成功！");
          this.toPage(1);
        })
        .catch(() => {});
    },
    toDelete(row) {
      this.$confirm(`确定要删除学生【${row.studentName}】吗?`, "提示", {
        type: "warning"
      })
        .then(async () => {
          await deleteStudent([row.id]);
          this.$message.success("删除成功！");
          this.deletePageLastItem();
        })
        .catch(() => {});
    },
    // import
    validError(errorData) {
      this.$message.error(errorData.message);
    },
    uploadSuccess() {
      this.$message.success("文件上传成功，后台正在导入！");
      this.getList();
    }
  }
};
</script>
