<template>
  <section>
    <!-- 添加员工模块 -->
    <div class="block">
      <el-button round @click="clearValidate()">添加员工</el-button>
    </div>
    <!-- 添加员工弹窗 -->
    <el-dialog title="员工信息" :visible.sync="addPeoVisible" width="30%">
      <el-form
        status-icon
        label-position="left"
        :model="registerForm"
        :rules="rules"
        ref="registerForm"
        label-width="100px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="registerForm.workName"
            placeholder="请输入真实姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="tel">
          <el-input
            v-model="registerForm.workPhone"
            placeholder="请输入正确手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input
            v-model="registerForm.workPassword"
            placeholder="密码为手机号末尾4位加1位大写字母"
          ></el-input>
        </el-form-item>
        <el-form-item label="校区" prop="region">
          <el-select v-model="registerForm.workCampus" placeholder="请选择校区">
            <el-option label="墨轩湖校区" value="1"></el-option>
            <el-option label="黄家湖校区" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addPeoVisible = false">取 消</el-button>
        <el-button type="primary" @click="registerWork()">添 加</el-button>
      </span>
    </el-dialog>
    <!-- 员工列表模块 -->
    <el-table :data="tableData" style="width: 100%" height="150">
      <el-table-column prop="workName" label="姓名"> </el-table-column>
      <el-table-column prop="workPhone" label="手机号"> </el-table-column>
      <el-table-column prop="workPassword" label="密码"> </el-table-column>
      <el-table-column label="校区">
        <template slot-scope="scope">
          {{ Campus[scope.row.workCampus] }}</template
        >
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="
              deleteRow(scope.$index, tableData, scope.row.workId)
            "
            type="text"
            size="small"
          >
            移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      :current-page="curpage"
      :pager-count="7"
      @current-change="handleCurrentChange"
      background
      layout="prev, pager, next"
      :total="100"
    >
    </el-pagination>
  </section>
</template>

<script>
export default {
  data() {
    //自定义验证方法
    var telPass = (rule, value, callback) => {
      if (this.registerForm.workPhone == "") {
        this.registerForm_submit = 1;
        callback(new Error("手机号不能为空"));
      } else {
        let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/g;
        let boo = phoneCodeVerification.test(this.registerForm.workPhone);
        // console.log(boo);
        if (boo) {
          callback();
        } else {
          this.registerForm_submit = 1;
          callback(new Error("请输入11位手机号"));
        }
      }
    };
    var pwdPass = (rule, value, callback) => {
      if (this.registerForm.workPassword == "") {
        this.registerForm_submit = 1;
        callback(new Error("密码不能为空"));
      } else {
        let phoneCodeVerification = /^\d{4}[A-Z]$/g;
        let boo = phoneCodeVerification.test(this.registerForm.workPassword);

        if (boo) {
          callback();
        } else {
          this.registerForm_submit = 1;
          callback(new Error("请输入手机号尾号4位与一位字母"));
        }
      }
    };
    var namePass = (rule, value, callback) => {
      if (this.registerForm.workName == "") {
        this.registerForm_submit = 1;
        callback(new Error("姓名不能为空"));
      } else {
        let phoneCodeVerification = /[\u4e00-\u9fa5]/;
        let boo = phoneCodeVerification.test(this.registerForm.workName);
        if (boo) {
          callback();
        } else {
          this.registerForm_submit = 1;
          callback(new Error("请输入中文名字"));
        }
      }
    };
    return {
      tableData: [], //员工列表信息
      addPeoVisible: false, //添加员工弹窗
      registerForm_submit: 0,
      registerForm: {
        workPhone: "", //号码
        workCampus: "", //校区
        workPassword: "", //密码
        workName: "" //姓名
      },
      rules: {
        name: [
          {
            validator: namePass,
            trigger: "blur"
          }
        ],
        tel: [
          {
            validator: telPass,
            trigger: "blur"
          }
        ],
        pwd: [
          {
            validator: pwdPass,
            trigger: "blur"
          }
        ]
      },
      curpage: 1, //页码
      pageSize: 15, //一页条数
      Campus: ["黄家湖校区", "墨轩湖校区"],
      token: null
    };
  },
  created() {
    this.token = sessionStorage.getItem("token");
    if (this.token) {
      this.getWorker();
    } else {
      this.$message({
        message: "授权失效，请重新登录",
        type: "warning"
      });
      this.$router.push({ path: "/" });
    }
  },
  methods: {
    //当改变当前页数的时候触发的事件
    handleCurrentChange(currentPage) {
      // console.log(this.sear_data);
      this.curpage = currentPage;
      this.getWorker();
    },
    //获取维修人员列表
    getWorker() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      this.$axios({
        method: "post",
        url: `/baoxiu/repairApi/work/queryAllWorkStatus?pageNum=${this.curpage}&pageSize=${this.pageSize}`,
        headers: {
          token: this.token
        }
      })
        .then(({ data }) => {
          if (data.status == 200) {
            this.tableData = [];
            this.tableData = data.data.list;
          } else {
            this.$message({
              message: data.message,
              type: "warning"
            });
          }
          loading.close();
        })
        .catch(err => {
          loading.close();
          this.$message({
            message: err,
            type: "warning"
          });
        });
    },
    //清楚表单校验
    clearValidate() {
      this.addPeoVisible = true;
      //清空
      this.registerForm.workPhone = "";
      this.registerForm.workName = "";
      this.registerForm.workCampus = "";
      this.registerForm.workPassword = "";
      this.$nextTick(() => {
        this.$refs["registerForm"].clearValidate();
      });
    },
    //维修人员注册
    registerWork() {
      if (this.registerForm_submit != 0) {
        this.$message({
          message: "无法注册，请规范注册",
          type: "warning"
        });
      } else {
        this.$axios({
          method: "post",
          url: `/baoxiu/repairApi/work/insertWork?workPhone=${this.registerForm.workPhone}&workCampus=${this.registerForm.workCampus}&workPassword=${this.registerForm.workPassword}&workName=${this.registerForm.workName}`,
          headers: {
            token: this.token
          }
        })
          .then(({ data }) => {
            // console.log(data);
            if (data.status != 200) {
              this.$message({
                message: data.message,
                type: "warning"
              });
            } else {
              this.addPeoVisible = false;
              this.$message({
                message: data.message,
                type: "success"
              });
              //清空
              this.clearValidate();
            }
          })
          .catch(err => {
            this.$message({
              message: err,
              type: "warning"
            });
          });
      }
    },
    //移除人员
    deleteRow(index, rows, work_id) {
      this.$axios({
        method: "post",
        url: `/baoxiu/repairApi/work/removeWorkStatus?workId=${work_id}`,
        headers: {
          token: this.token
        }
      })
        .then(({ data }) => {
          if (data.status == 200) {
            this.$confirm("此操作将永久移除该员工, 是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            })
              .then(() => {
                this.$message({
                  type: "success",
                  message: "移除成功!"
                });
                rows.splice(index, 1); // 删除人员
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消移除"
                });
              });
          } else {
            this.$message({
              message: err,
              type: "warning"
            });
          }
        })
        .catch(err => {
          loading.close();
          this.$message({
            message: err,
            type: "warning"
          });
        });
    }
  }
};
</script>

<style scoped>
@import url("RepairPeo.css");
</style>