<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.username }}</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 == 6
                    ? "区级评审员"
                    : scope.row.type == 5
                    ? "区县评委"
                    : "暂无"
                }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="schoolName"
            label="学校"
            min-width="10"
            align="center"
            ><template slot-scope="scope">
              <div>
                {{ scope.row.schoolName ? scope.row.schoolName : "-" }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="20">
            <template slot-scope="scope">
              <div class="tableButton">
                <el-button v-if="scope.row.type == 5 || scope.row.type == 6" @click="assCase(scope.row.userid)"
                  >分配案例</el-button
                >
                <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="6">区级评审员</el-option>
          </el-select>
        </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>

    <el-dialog
      title="分配案例"
      :visible.sync="dialogVisibleMember"
      :append-to-body="true"
      width="25%"
      height="80%"
      :before-close="handleClose"
      class="dialog_diy1 customWidth"
    >
      <div class="people">
        <div class="people_top">
          <div class="people_top_right">
            <div class="people_search">
              <div>选择评委</div>
              <el-select v-model="reviewer" placeholder="请选择评委">
                <el-option
                  v-for="item in reviewerBox"
                  :key="item.userid"
                  :label="item.name"
                  :value="item.userid"
                ></el-option>
              </el-select>
            </div>
          </div>
        </div>
        <el-checkbox
          :indeterminate="isIndeterminate"
          v-model="checkAll"
          @change="handleCheckAllChange"
          style="padding: 15px 0 0 15px"
          >全选</el-checkbox
        >
        <el-checkbox-group
          v-model="checkboxList"
          class="people_name"
          v-if="anliBox.length"
          @change="handleCheckedAnliChange"
        >
          <el-checkbox v-for="item in anliBox" :key="item.id" :label="item.id">
            <div class="t_j_box">
              <div>案例名称：</div>
              <el-tooltip
                placement="top"
                :content="item.info.title ? item.info.title : '暂无姓名'"
              >
                <span>{{
                  item.info.title ? item.info.title : "暂无姓名"
                }}</span>
              </el-tooltip>
            </div>
          </el-checkbox>
        </el-checkbox-group>
        <div style="text-align: center; margin-top: 10px" v-else>暂无数据</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleMember = false">取 消</el-button>
        <el-button type="primary" @click="addCase">确定</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,
      dialogVisibleMember: false,
      sName: "",
      sPhone: "",
      sType: "",
      upId: "",
      reviewer: "",
      reviewerBox: [],
      anliBox: [],
      checkboxList: [],
      checkboxIdList: [],
      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: {
    clearSchoolList() {
      this.sSchool = "";
    },
    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;
      }
    },
    getSchool() {
      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;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    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;
      } 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: 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.sName = "";
          this.sPhone = "";
          this.sType = "";
          this.sSchool = "";
          this.dialogVisible = false;
          this.getAdmin();
        })
        .catch((err) => {
          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) {
      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(() => {});
    },
    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);
        });
    },
    assCase(id) {
      this.reviewer = id;
      this.dialogVisibleMember = true;
    },
    addCase() {
      if (this.reviewer == "") {
        this.$message.error("请选择评审员");
        return;
      } else if (this.checkboxList.length == 0) {
        this.$message.error("请选择案例");
        return;
      }
      let params = [
        {
          uid: this.reviewer,
          box: this.checkboxList.join(","),
        },
      ];
      this.ajax
        .post(this.$store.state.api + "insertCase", params)
        .then((res) => {
          this.$message({
            message: "分配成功",
            type: "success",
          });
          this.reviewer = "";
          this.checkboxList = [];
          this.isIndeterminate = false;
          this.checkAll = false;
          this.dialogVisibleMember = false;
        })
        .catch((err) => {
          this.$message.error("网络不佳");
          console.error(err);
        });
    },
  },
  created() {
    this.getAdmin();
    this.getAnliBox();
    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;
}
.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>