<template>
  <div class="register">
    <!-- 手机验证表单 -->
    <el-form
      label-width="100px"
      :model="phoneForm"
      :rules="rulesPhoneForm"
      ref="phoneForm"
      class="phoneForm"
    >
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          placeholder="请输入手机号码"
          v-model="phoneForm.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机验证码" prop="code">
        <el-input placeholder="请填写验证码" v-model="phoneForm.code">
          <template slot="append">
            <el-button @click="getCheckcode" :disabled="isTimer"
              ><span>{{ btnTitle }}</span></el-button
            >
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          class="bntNext"
          @click="submitForm('phoneForm')"
          >登录/注册</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCheckcode, checkCode } from "@/api/login";

export default {
  name: "Register",
  data() {
    //限制输入正确的手机号码格式
    const validatePhone = (rule, value, callback) => {
      if (/^1[3-9]\d{9}$/.test(value) === false) {
        callback(new Error("请输入正确的手机号码"));
      } else {
        // 校验通过
        callback();
      }
    };
    return {
      btnTitle: "获取验证码",
      isTimer: false,
      phoneForm: {
        mobile: "", //手机号码
        code: "", //验证码
      },
      rulesPhoneForm: {
        mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { validator: validatePhone, trigger: "blur" },
        ],
        code: [
          { required: true, message: "请输入手机验证码", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  methods: {
    //倒计时
    validateBtn() {
      let time = 60;
      let timer = setInterval(() => {
        if (time === 0) {
          clearInterval(timer);
          this.isTimer = false;
          this.btnTitle = "获取验证码";
        } else {
          this.btnTitle = time + "秒后重试";
          this.isTimer = true;
          time--;
        }
      }, 1000);
    },
    //获取验证码
    getCheckcode() {
      let query = { mobile: this.phoneForm.mobile };
      getCheckcode(query).then((response) => {
        console.log("response", response);
        if (response.code == 20000) {
          this.validateBtn();
          this.$message.success(response.data);
        } else {
          this.$message.error(response.message);
        }
      });
    },
    //手机验证表单提交
    submitForm(phoneForm) {
      this.$refs[phoneForm].validate((valid) => {
        if (valid) {
          //验证验证码
          let query = this.phoneForm;
          checkCode(query).then((response) => {
            console.log("444");
            if (response.code === 20000) {
              this.$router.push("/userInformation");
              this.$message.error("提交成功");
            } else {
              this.$message.error(response.message);
            }
          });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  height: 100%;
  flex-wrap: wrap;
  justify-content: center;
  align-content: center;
  .phoneForm {
    .el-form-item__label {
      line-height: 50px;
    }
    .el-input__inner {
      height: 50px;
    }
    .bntNext {
      width: 50%;
      height: 50px;
    }
  }
  .userForm {
    .el-select {
      width: 100%;
    }
    .bntNext {
      width: 50%;
      height: 50px;
    }
  }
}
</style>
