<template>
  <div class="register">
    <el-form
      :model="form"
      status-icon
      :rules="rules"
      ref="registerForm"
      label-width="100px"
      class="register-form"
    >
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input v-model="form.phoneNumber" autocomplete="off"></el-input>
        <!-- <el-button style="margin-left:16px">发送验证码</el-button> -->
      </el-form-item>
      <el-form-item label="验证码" prop="captcha">
        <el-input
          v-model="form.captcha"
          autocomplete="off"
          style="width: 46%; margin-right: 8px"
          clearable
        ></el-input>
        <el-button :disabled="isDisabled" @click.native="handleSend">{{
          sendButtonText
        }}</el-button>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model="form.account" autocomplete="off"></el-input>
      </el-form-item>
      <!-- <el-form-item label="手机号" prop="pass">
        <el-input v-model="ruleForm.account" autocomplete="off"></el-input>
        <el-button>发送验证码</el-button>
      </el-form-item> -->
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="form.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="form.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('registerForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('registerForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        var pattern =
          /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\\W_!@#$%^&*`~()-+=]+$)(?![0-9\\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\\W_!@#$%^&*`~()-+=]{8,30}$/;
        if (!pattern.test(value)) {
          callback(
            new Error(
              "密码长度至少8位，至少含数字,大写字母,小写字母,特殊符其中三种"
            )
          );
        }
        if (this.form.checkPass !== "") {
          this.$refs.registerForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      form: {
        account: "",
        pass: "",
        checkPass: "",
        captcha: "",
      },
      isDisabled: true,
      sendButtonText: "发送验证码",
      second: 60,
      rules: {
        account: [
          { message: "请输入账号", trigger: "blur", required: true },
          {
            pattern: /^[0-9a-zA-Z|_]{6,20}$/g,
            message: "账号长度6-20个字符，只能包括字母、数字、下划线",
            trigger: "blur",
          },
        ],
        phoneNumber: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
            message: "请输入合法手机号/电话号",
            trigger: "blur",
          },
        ],
        captcha: [{ message: "请输入验证码", required: true }],

        pass: [{ validator: validatePass, trigger: "blur", required: true }],
        checkPass: [
          { validator: validatePass2, trigger: "blur", required: true },
        ],
      },
    };
  },
  watch: {
    "form.phoneNumber": {
      handler: function (value) {
        this.$refs["registerForm"].validateField("phoneNumber", (errorMessage) => {
          let valid = errorMessage == "";
          if (valid) {
            this.isDisabled = false;
          } else {
            this.isDisabled = true;
          }
        });
      },
      deep: true,
    },
  },
  methods: {
    // 点击发送验证码
    handleSend() {
      this.isDisabled = true;
      this.second = 60;
      var timer = setInterval(() => {
        if (this.second < 1) {
          this.isDisabled = false;
          clearInterval(timer);
          timer = null;
          this.sendButtonText = "发送验证码";
        } else {
          this.second--;
          this.sendButtonText = `${this.second}秒后重新发送`;
        }
      }, 1 * 1000);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
.register {
  width: 1200px;
  margin: 50px auto;
  .register-form {
    width: 500px;
    margin: auto;
  }
}
</style>