<template>
  <div class="pb_content">
    <div class="pb_head">
      <div>
        <span>学校管理</span>
      </div>
    </div>
    <div class="touTop">
      <div class="touLeft">
        <div>
          <el-input v-model="suser" placeholder="请输入手机号或用户名">
          </el-input>
          <el-button @click="search">查询</el-button>
        </div>
      </div>
      <div class="touRight">
        <div></div>
        <div>
          <el-button @click="addUser">添加账号</el-button>
        </div>
      </div>
    </div>
    <div class="pb_content_body" style="margin: 0 auto">
      <div class="student_table">
        <el-table
          ref="table"
          :data="tableData"
          border
          :height="tableHeight"
          :fit="true"
          v-loading="isLoading"
          style="width: 100%"
          :header-cell-style="{ background: '#f1f1f1', fontSize: '17px' }"
          :row-class-name="tableRowClassName"
        >
          <el-table-column label="姓名" min-width="10" align="center">
            <template slot-scope="scope">
              <div class="userImg">
                <div>
                  {{ scope.row.name }}
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="电话" min-width="10" align="center">
            <template slot-scope="scope">
              <div>{{ scope.row.phone ? scope.row.phone : "-" }}</div>
            </template>
          </el-table-column>
          <el-table-column
            prop="level"
            label="权限等级"
            min-width="10"
            align="center"
            ><template slot-scope="scope">
              <div>{{ scope.row.schoolName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="20">
            <template slot-scope="scope">
              <div class="tableButton">
                <el-button @click="upUser(scope.row)">修改</el-button>
                <el-button @click="deUser(scope.row.userid)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="student_page">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="10"
          :total="total"
          v-if="page"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
    <el-dialog
      title="添加管理员"
      :visible.sync="dialogVisible"
      :append-to-body="true"
      width="500px"
      :before-close="handleClose"
      class="dialog_diy"
    >
      <el-form class="inputClass">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <span>
            <el-input
              placeholder="请输入名称"
              clearable
              v-model="sName"
              class="add_input"
            ></el-input>
          </span>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <span>
            <el-input
              placeholder="请输入手机号"
              clearable
              v-model="sPhone"
              class="add_input"
            ></el-input>
          </span>
        </el-form-item>
        <el-form-item label="学校选择" :label-width="formLabelWidth">
          <el-select
            v-model="sSchool"
            filterable
            clearable
            @clear="clearSchoolList"
            :allow-create="false"
            @focus="remoteMethodclick"
            remote
            placeholder="请输入学校"
            :remote-method="remoteMethod"
            :loading="loading"
            no-data-text="暂未查询到此学校"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div style="text-align: center; color: #adb3b7">
          注：添加账号账号密码为123456
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer flex">
        <el-button class="left" @click="dialogVisible = false">取消</el-button>
        <el-button class="right" @click="addRaceUser" v-if="isAddOrUp == false"
          >确认</el-button
        >
        <el-button
          class="right"
          @click="updateRaceUser"
          v-if="isAddOrUp == true"
          >修改</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      suser: "",
      tableData: [],
      tableHeight: "500px",
      isLoading: false,
      formLabelWidth: "100px",
      page: 1,
      total: 0,
      dialogVisible: false,
      isAddOrUp: false,
      sName: "",
      sPhone: "",
      sType: "",
      upId: "",
      sSchool: "",
      checkboxList: [],
      checkboxIdList: [],
      schoolList: [],
      isIndeterminate: false,
      checkAll: false,
      options: [],
      list: [],
      loading: false,
    };
  },
  mounted() {
    this.$nextTick(function () {
      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;
        }
      };
    });
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if ((rowIndex + 1) % 2 === 0) {
        return "even_row";
      } else {
        return "";
      }
    },
    remoteMethodclick() {
      if (!this.options.length) {
        var _option = [];
        for (var i = 0; i < 10; i++) {
          var _a = this.randomNum(0, 10);
          _option.push(this.list[_a]);
        }
        this.options = _option;
      }
    },
    randomNum(minNum, maxNum) {
      switch (arguments.length) {
        case 1:
          return parseInt(Math.random() * minNum + 1, 10);
          break;
        case 2:
          return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
          break;
        default:
          return 0;
          break;
      }
    },
    remoteMethod(query) {
      if (query !== "") {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          this.options = this.list.filter((item) => {
            return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
          });
        }, 200);
      } else {
        // this.options = [];
        var _option = [];
        for (var i = 0; i < 10; i++) {
          var _a = this.randomNum(0, 10);
          _option.push(this.list[_a]);
        }
        this.options = _option;
      }
    },
    handleClose(done) {
      done();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getAdmin();
    },
    clearSchoolList() {
      this.sSchool = "";
    },
    search() {
      this.page = 1;
      this.getAdmin();
    },
    addUser() {
      this.sName = "";
      this.sPhone = "";
      this.sSchool = "";
      this.isAddOrUp = false;
      this.dialogVisible = true;
    },
    addRaceUser() {
      if (this.sName == "") {
        this.$message.error("请输入名称");
        return;
      } else if (this.sPhone == "") {
        this.$message.error("请输入手机号");
        return;
      } else if (
        this.sId != "" &&
        !/^[1][3,4,5,7,8][0-9]{9}$/.test(this.sPhone)
      ) {
        this.$message.error("手机号格式不正确");
        return;
      } else if (this.sSchool == "") {
        this.$message.error("请选择学校");
        return;
      }
      this.isloading = true;
      let params = [
        {
          reg: "",
          res: "",
          sch: this.sSchool,
          n: this.sName,
          p: this.sPhone,
          t: 4,
          parent: this.$store.state.userInfo.userid,
        },
      ];
      this.ajax
        .post(this.$store.state.api + "addRaceUser", params)
        .then((res) => {
          this.$message({
            message: "创建成功",
            type: "success",
          });
          this.isloading = false;
          this.sName = "";
          this.sPhone = "";
          this.sType = "";
          this.dialogVisible = false;
          this.getAdmin();
        })
        .catch((err) => {
          this.isloading = false;
          this.$message.error("创建失败");
          console.error(err);
        });
    },
    upUser(r) {
      this.sName = r.name;
      this.sPhone = r.phone;
      this.sSchool = r.schoolName;
      this.upId = r.userid;
      this.isAddOrUp = true;
      this.dialogVisible = true;
    },
    updateRaceUser() {
      let params = {
        n: this.sName,
        p: this.sPhone,
        t: 4,
        s: this.sSchool,
        uid: this.upId,
      };
      this.ajax
        .get(this.$store.state.api + "updateReviewer", params)
        .then((res) => {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.sName = "";
          this.sPhone = "";
          this.sType = "";
          this.upId = "";
          this.isAddOrUp = false;
          this.dialogVisible = false;
          this.getAdmin();
        })
        .catch((err) => {
          console.error(err);
        });
    },
    deUser(id) {
      this.$confirm("确定删除该账号吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            uid: id,
          };
          this.ajax
            .get(this.$store.state.api + "deleteReviewer", params)
            .then((res) => {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.dialogVisible = false;
              this.getAdmin();
            })
            .catch((err) => {
              console.error(err);
            });
        })
        .catch(() => {});
    },
    getAdmin() {
      let params = {
        uid: this.$store.state.userInfo.userid,
        cn: this.suser,
        page: this.page,
      };
      this.ajax
        .get(this.$store.state.api + "selectReviewer", params)
        .then((res) => {
          this.total = res.data[0].length > 0 ? res.data[0][0].num : 0;
          this.tableData = res.data[0];
          this.reviewerBox = res.data[0];
        })
        .catch((err) => {
          console.error(err);
        });
    },
    getSchool() {
      this.isloading = true;
      let params = {
        n: "",
      };
      this.ajax
        .get(this.$store.state.api + "getSchool", params)
        .then((res) => {
          this.schoolList = res.data[0];
          this.list = this.schoolList.map((item) => {
            return { value: item.id, label: item.name };
          });
          var _option = [];
          for (var i = 0; i < 10; i++) {
            var _a = this.randomNum(0, this.list.length);
            _option.push(this.list[_a]);
          }
          this.options = _option;
          this.isloading = false;
        })
        .catch((err) => {
          this.isloading = false;
          console.error(err);
        });
    },
  },
  created() {
    this.getAdmin();
    this.getSchool();
  },
};
</script>

<style scoped>
.dialog_diy >>> .el-dialog__header,
.dialog_diy1 >>> .el-dialog__header {
  background: #3d67bd !important;
  padding: 15px 20px;
}
.dialog_diy1 >>> .el-dialog__header {
  text-align: center;
}
.dialog_diy >>> .el-dialog__title,
.dialog_diy1 >>> .el-dialog__title {
  color: #fff;
}

.dialog_diy >>> .el-dialog__headerbtn,
.dialog_diy1 >>> .el-dialog__headerbtn {
  top: 19px;
}

.dialog_diy >>> .el-dialog__headerbtn .el-dialog__close,
.dialog_diy1 >>> .el-dialog__headerbtn .el-dialog__close {
  color: #fff;
}

.dialog_diy >>> .el-dialog__headerbtn .el-dialog__close:hover,
.dialog_diy1 >>> .el-dialog__headerbtn .el-dialog__close:hover {
  color: #fff;
}

.dialog_diy >>> .el-dialog__body,
.dialog_diy >>> .el-dialog__footer,
.dialog_diy1 >>> .el-dialog__body,
.dialog_diy1 >>> .el-dialog__footer {
  background: #fafafa;
}
/* .inputClass.is-active >>> .el-input__inner,
.inputClass >>> .el-input__inner:focus {
  border-color: #5c549f;
}

.inputClass >>> .el-select .el-input.is-focus .el-input__inner {
  border-color: #5c549f;
}
.inputClass >>> .el-select .el-input__inner:focus {
  border-color: #5c549f;
} */
.left {
  background: #fff !important;
  color: #000 !important;
}
.right,
.left {
  width: 80px;
  color: #fff;
  background: #409efe;
  margin-bottom: 20px;
}
.touTop {
  margin: 15px auto;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: space-between;
  width: 95%;
}

.touLeft {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
}

.touLeft > div {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
}

.touLeft > div > .el-button {
  background: #2268bd;
  color: #fff;
  margin-left: 10px;
}

.touRight > div:nth-child(2) > .el-button {
  background: #2268bd;
  color: #fff;
}
.student_table >>> .el-table--border td {
  border-right: 0px !important;
}
.student_table >>> .el-table,
.student_table >>> .el-table__body-wrapper {
  height: auto !important;
}
.student_page {
  margin-top: 10px;
}
.tableButton {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
}
.tableButton > .el-button {
  background: #409efe;
  color: #fff;
  cursor: pointer;
}
.customWidth >>> .el-dialog {
  min-width: 500px !important;
}
.t_j_box {
  display: flex;
}
.t_j_box span:nth-child(1) {
  width: 15%;
  overflow: hidden;
  margin-right: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.t_j_box span:nth-child(2) {
  width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 10px;
}
.t_j_box span:nth-child(3) {
  width: calc(55% - 20px);
  overflow: hidden;
  text-overflow: ellipsis;
}
.people_name {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-items: flex-start;
  padding: 15px 0 0 15px;
}
.people_name >>> .el-checkbox {
  margin-bottom: 10px;
}
</style>