<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.type == 4
                    ? "评审员"
                    : scope.row.type == 1
                    ? "教师"
                    : scope.row.type == 5
                    ? "县级评委"
                    : "暂无"
                }}
              </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="sType" placeholder="请选择权限">
            <el-option label="教师" value="1">教师</el-option>
            <el-option label="评审员" value="4">评审员</el-option>
            <el-option label="区县评委" value="5">区县评委</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: "",
      reviewer: "",
      reviewerBox: [],
      anliBox: [],
      checkboxList: [],
      checkboxIdList: [],
      isIndeterminate: false,
      checkAll: 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 "";
      }
    },
    handleCheckAllChange(val) {
      if (this.checkboxIdList.length == 0) {
        for (var i = 0; i < this.anliBox.length; i++) {
          this.checkboxIdList.push(this.anliBox[i].id);
        }
      }
      this.checkboxList = val ? this.checkboxIdList : [];
      this.isIndeterminate = false;
    },
    handleCheckedAnliChange(value) {
      let checkedCount = this.checkboxList.length;
      this.checkAll = checkedCount === this.anliBox.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.anliBox.length;
    },
    handleClose(done) {
      done();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getAdmin();
    },
    search() {
      this.page = 1;
      this.getAdmin();
    },
    addUser() {
      this.sName = "";
      this.sPhone = "";
      this.sType = "";
      this.isAddOrUp = false;
      this.dialogVisible = true;
    },
    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);
        });
    },
    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.sType == "") {
        this.$message.error("请选择权限");
        return;
      }
      this.isloading = true;
      let params = [
        {
          reg: "",
          res: "",
          sch: this.$store.state.userInfo.school,
          n: this.sName,
          p: this.sPhone,
          t: this.sType,
          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.sType = r.type;
      this.upId = r.userid;
      this.isAddOrUp = true;
      this.dialogVisible = true;
    },
    updateRaceUser() {
      let params = {
        n: this.sName,
        p: this.sPhone,
        t: this.sType,
        s: this.$store.state.userInfo.school,
        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) {
      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);
        });
    },
    getAnliBox() {
      this.ajax
        .get(this.$store.state.api + "selecAnliBox")
        .then((res) => {
          this.anliBox = res.data[0];
          for (var i = 0; i < this.anliBox.length; i++) {
            this.anliBox[i].info = JSON.parse(this.anliBox[i].info);
            this.anliBox[i].overview = JSON.parse(this.anliBox[i].overview);
            this.anliBox[i].process = JSON.parse(this.anliBox[i].process);
            this.anliBox[i].proact = JSON.parse(this.anliBox[i].proact);
            this.anliBox[i].proexc = JSON.parse(this.anliBox[i].proexc);
            this.anliBox[i].results = JSON.parse(this.anliBox[i].results);
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },
  },
  created() {
    this.getAdmin();
    this.getAnliBox();
  },
};
</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;
}
.people {
  border: 1px solid rgb(229 229 229);
  height: 495px;
  border-radius: 5px;
  width: 100%;
  overflow: auto;
  background: #fff;
  box-shadow: 0px 0px 10px 8px #ededed;
}
.people::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 6px;
  /*高宽分别对应横竖滚动条的尺寸*/
  height: 6px;
}

/*定义滚动条轨道 内阴影+圆角*/
.people::-webkit-scrollbar-track {
  border-radius: 10px;
  background-color: #eee;
}

/*定义滑块 内阴影+圆角*/
.people::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
  background-color: rgba(0, 0, 0, 0.1);
}
.people_top {
  display: flex;
  width: 100%;
  flex-direction: column;
  padding: 10px 25px 10px;
  box-sizing: border-box;
  border-bottom: 1px solid #f4f4f7;
}

.people_top_right {
  height: 40px;
  margin-bottom: 10px;
}

.people_search {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
}
.people_search > div:nth-child(1) {
  font-size: 20px;
  margin-right: 10px;
}
.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>