<template>
  <div class="campus-manage">
    <div class="part-box part-box-filter part-box-flex">
      <div></div>
      <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="dataList">
        <el-table-column prop="campusName" label="校区名称"></el-table-column>
        <el-table-column prop="campusCode" label="校区编码"></el-table-column>
        <el-table-column label="操作" width="120px">
          <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>

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

<script>
import { campusQuery, deleteCampus } from "../api";
import ModifyCampus from "../components/ModifyCampus";

export default {
  name: "campus-manage",
  components: { ModifyCampus },
  data() {
    return {
      dataList: [],
      curRow: {}
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      this.dataList = await campusQuery();
    },
    toAdd() {
      this.curRow = {};
      this.$refs.ModifyCampus.open();
    },
    toEdit(row) {
      this.curRow = row;
      this.$refs.ModifyCampus.open();
    },
    toDelete(row) {
      this.$confirm(`确定要删除校区【${row.campusName}】吗?`, "提示", {
        type: "warning"
      })
        .then(async () => {
          await deleteCampus(row.id);
          this.$message.success("删除成功！");
          this.getList();
        })
        .catch(() => {});
    }
  }
};
</script>
