<template>
  <div style="width: 100%">
    <el-main>
      <div class="tou">组织管理</div>
      <div>
        <el-button
          size="small"
          type="primary"
          class="dialog_diy"
          style="position: absolute; right: 20px; top: 90px"
          @click="dialogVisible1 = true"
          >添加组织</el-button
        >
        <!-- <div>Banner管理</div>      -->
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :header-cell-style="{ background: '#f1f1f1', 'text-align': 'center' }"
          :cell-style="{ 'text-align': 'center' }"
          style="width: 100%; margin-top: 70px"
          v-loading="isLoading"
          :height="tableHeight"
        >
          <el-table-column prop="name" label="学校" min-width="80">
          </el-table-column>
          <el-table-column prop="do" label="操作" min-width="20">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                @click="deleteOrg(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="student_page">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="10"
          :total="total"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </el-main>

    <el-dialog
      title="添加"
      :visible.sync="dialogVisible1"
      width="500px"
      :before-close="handleClose"
      class="dialog_diy"
      center
    >
      <div class="addBox">
        <span class="tian1">组织名称</span>
        <el-input
          v-model="orgName"
          style="width: 250px; margin: 15px 0px"
          placeholder="请输入学校名称"
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="addOrg">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      dialogVisible1: false,
      tableData: [],
      orgName: "",
      page: 1,
      total: 0,
      isLoading: false,
      tableHeight: 500,
    };
  },
  methods: {
    handleClose(done) {
      done();
    },
    time() {
      if (!this.now) {
        this.now = new Date().getTime();
        return true;
      } else {
        let time = new Date().getTime();
        if (time - this.now > 3000) {
          this.now = time;
          return true;
        } else {
          return false;
        }
      }
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val;
      this.getOrg();
    },
    getOrg() {
      this.isLoading = true;
      let params = { page: this.page };
      this.ajax
        .get(this.$store.state.api + "getSchoolAdmin", params)
        .then((res) => {
          this.isLoading = false;
          this.total = res.data[0].length > 0 ? res.data[0][0].num : 0;
          this.tableData = res.data[0];
        })
        .catch((err) => {
          this.isLoading = false;
          console.error(err);
        });
    },

    addOrg() {
      if (this.time()) {
        let params = [{ n: this.orgName,cuid:this.$store.state.userInfo.uid }];
        this.ajax
          .post(this.$store.state.api + "addSchoolAdmin", params)
          .then((res) => {
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.dialogVisible1 = false;
            this.orgName = "";
            this.getOrg();
          })
          .catch((err) => {
            this.$message.error("添加失败");
            console.error(err);
          });
      }
    },
    deleteOrg(id) {
      let params = [{ id: id }];
      var _this = this;
      _this
        .$confirm("确定删除此学校吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          _this.ajax
            .post(_this.$store.state.api + "deleteSchoolAdmin", params)
            .then((res) => {
              _this.$message({
                message: "删除成功",
                type: "success",
              });
              if (_this.page != 1 && _this.tableData.length == 1) {
                _this.page--;
              }
              _this.getOrg();
            })
            .catch((err) => {
              _this.$message.error("删除失败");
              console.error(err);
            });
        })
        .catch(() => {});
    },
    changeHeight() {
      this.tableHeight =
        window.innerHeight - this.$refs.table.$el.offsetTop - 200;
      if (this.tableHeight <= 530) {
        this.tableHeight = 530;
      }
      // 监听窗口大小变化
      let self = this;
      window.onresize = function () {
        self.tableHeight =
          window.innerHeight - self.$refs.table.$el.offsetTop - 200;
        if (self.tableHeight <= 530) {
          self.tableHeight = 530;
        }
      };
      // this.$refs.table.$el.offsetTop：表格距离浏览器的高度 //200表示你想要调整的表格距离底部的高度（你可以自己随意调整），因为我们一般都有放分页组件的，所以需要给它留一个高度
    },
  },
  created() {
    this.$nextTick(function () {
      this.page = 1;
      this.changeHeight();
      this.getOrg();
    });
  },
};
</script>

<style scoped>
.tou {
  border-bottom: 1px solid #c9c9c9;
  height: 50px;
  font-size: 30px;
}
.addBox {
  display: flex;
  align-items: center;
  justify-content: center;
}
.tian1 {
  margin-right: 10px;
}
.dialog_diy >>> .el-dialog__header {
  background: #3d67bc !important;
  padding: 15px 20px;
}
.dialog_diy >>> .el-dialog__title {
  color: #fff;
}
.dialog_diy >>> .el-dialog__headerbtn {
  top: 19px;
}
.dialog_diy >>> .el-dialog__headerbtn .el-dialog__close {
  color: #fff;
}
.dialog_diy >>> .el-dialog__headerbtn .el-dialog__close:hover {
  color: #fff;
}
.student_page {
  margin-top: 15px;
}
</style>