<template>
  <div class="verificationCode">
    <el-form
      :model="dynamicValidateForm"
      ref="dynamicValidateForm"
      label-width="100px"
      class="demo-dynamic"
      :rules="rules"
      ><el-form-item prop="buyerPhone"
        ><el-input
          v-model="dynamicValidateForm.phone"
          placeholder="请输入手机号码"
        ></el-input>
      </el-form-item>
      <el-form-item
        ><el-input
          v-model="dynamicValidateForm.verificationCode"
          placeholder="请输入验证码"
        ></el-input
        ><el-button @click="getVerificationCode">{{ btnInnerText }}</el-button>
      </el-form-item>
      <el-button type="primary" @click="submitForm()">登录</el-button>
      <p>点击【登录】即可表示同意<span>《启服通用户协议》</span></p>
    </el-form>
    <p>未注册用户，系统默认为您创建账号</p>
  </div>
</template>

<script>
export default {
  data() {
    var checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[3|4|5|7|8][0-9]{9}$/;
      if (!this.dynamicValidateForm.phone) {
        return callback(new Error("手机号码不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(+this.dynamicValidateForm.phone)) {
          callback(new Error("请输入数字值"));
        } else {
          if (phoneReg.test(this.dynamicValidateForm.phone)) {
            callback();
          } else {
            callback(new Error("手机号码格式不正确"));
          }
        }
      }, 100);
    };
    //
    return {
      dynamicValidateForm: {
        phone: "",
        verificationCode: ""
      },
      btnInnerText: "获取验证码",
      flag: true,
      timeNum: 10,
      timer: null,
      rules: {
        buyerPhone: [{ validator: checkPhone, trigger: "blur" }]
      }
    };
  },
  methods: {
    //获取验证码
    getVerificationCode() {
      if (this.flag) {
        this.flag = false;
        this.btnInnerText = this.timeNum + "秒之后重新获取";
        this.timer = setInterval(() => {
          this.timeNum--;
          this.btnInnerText = this.timeNum + "秒之后重新获取";
          if (this.timeNum <= 0) {
            clearInterval(this.timer);
            this.btnInnerText = "重新获取验证码";
            this.timeNum = 10;
            this.flag = true;
          }
        }, 1000);
      }
    },

    //提交表单
    submitForm() {
      if (
        /^1[3|4|5|7|8][0-9]{9}$/.test(this.dynamicValidateForm.phone) &&
        this.dynamicValidateForm.verificationCode
      ) {
        console.log(123);
      }
    }
  }
};
</script>

<style lang="scss">
.verificationCode {
  .el-form {
    .el-form-item {
      .el-form-item__content {
        margin-left: 0 !important;
        display: flex;
      }
    }
  }
  p {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
    line-height: 20px;
    margin-top: 60px;
    text-align: center;
  }
  .el-input {
    input {
      width: 100%;
      height: 42px;
      border-radius: 0;
    }
  }
  .el-form {
    .el-form-item {
      margin-top: 30px;
      button {
        border-radius: 0;
        border: 1px solid #108ee9;
        color: #108ee9;
        background: #fff;
        margin-left: 10px;
        width: 150px;
        height: 41px;
        margin-top: 0;
      }
    }
    button {
      width: 100%;
      height: 42px;
      margin-top: 20px;
      border-radius: 0;
      background: #3071fe;
      border: 1px solid #3071fe;
    }
    p {
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: rgba(102, 102, 102, 1);
      line-height: 20px;
      text-align: center;
      margin-top: 10px;
      span {
        color: #108ee9;
        cursor: pointer;
      }
    }
  }
}
</style>
