<template>
  <el-form
    class="login-form"
    :model="phoneForm"
    :rules="rules"
    ref="phoneForm"
  >
    <el-form-item prop="phone">
      <el-input
        clearable
        prefix-icon="el-icon-mobile-phone"
        v-model="phoneForm.phone"
        placeholder="您的手机号"
        type="number"
      ></el-input>
    </el-form-item>
    <el-form-item prop="verificationCode">
      <el-row :gutter="15" class="verificationCode-row">
        <el-col :span="14">
          <el-input
            :disabled="!isGetVerificationCode"
            placeholder="验证码"
            clearable
            prefix-icon="el-icon-key"
            @keyup.enter.native="submitForm('phoneForm')"
            v-model="phoneForm.verificationCode"
            id="verification-code"
          >
          </el-input>
        </el-col>
        <el-col :span="10">
          <span :class="['append-style',verificationCodeTime ? 'code-btn-disabled' : '']"
                @click="getVerificationCode()">
            {{ verificationCodeTime ? `${verificationCodeTime}秒后可重新获取` : `${isGetVerificationCode ? '重新' : ''}获取验证码`
            }}
          </span>
        </el-col>
      </el-row>
    </el-form-item>
    <el-form-item
      class="btns"
    >
      <el-button
        size="medium"
        type="primary"
        @click="submitForm('phoneForm')"
        class="login-btn"
        :loading="loading"
        style="width: 100%"
      >
        登录
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script>

export default {
  name: 'PhoneNumberLogin',
  computed: {},
  data() {
    //这个方法要写在用到这个方法的地方的前面
    let checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'))
      } else {
        const reg = /^1[3-9][0-9]\d{8}$/
        // 这里定义的reg变量没有加引号，所以是一种特殊的数据类型，即正则，可以直接用test()方法
        if (reg.test(value)) {
          callback()
        } else {
          // 具体错误信息没有提示
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }


    return {
      // 表单
      phoneForm: {
        phone: '',
        verificationCode: ''
      },
      rules: {
        // 从上向下进行验证，如果把min:11写在下面，就不会用到这个判断条件了，因为checkPhone包含了不满11位的验证
        phone: [
          {min: 11, max: 11, message: '请输入11位的手机号', trigger: 'blur'},
          {validator: checkPhone, trigger: 'blur'}
        ],
        verificationCode: [
          {required: true, message: '请获取手机验证码后输入验证码', trigger: 'blur'}
        ]
      }, // 表单验证规则
      loading: false, // 登录loading
      isGetVerificationCode: false, // 是否获取手机验证码
      verificationCodeTime: 0,// 请求验证码时间限制
      codeTimer: null // 二维码定时器对象
    }
  },
  created() {

  },
  mounted() {

  },
  methods: {
    // 获取手机验证码
    getVerificationCode() {
      // 指定60秒的节流时间
      this.$refs.phoneForm.validateField('phone', valid => {
        if (this.verificationCodeTime || valid) return
        this.$message.success('验证码发送成功')
        this.isGetVerificationCode = true
        this.verificationCodeTime = 10
        this.codeTimer = setInterval(() => {
          this.verificationCodeTime -= 1
          if (!this.verificationCodeTime) clearInterval(this.codeTimer)
        }, 1000)
      })
    },

    // 登录
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return
        alert('成功')
      })
    }
  },
  beforeDestroy() {
    clearInterval(this.codeTimer)
  }

}
</script>
<style lang="scss" scoped>
@import '../css/publicStyle.scss';


::v-deep #verification-code {
  //border-right: 0;
}

.verificationCode-row {
  display: flex;
  align-items: center;

  .append-style {
    display: inline-block;
    height: 100%;
    width: 100%;
    text-align: center;
    font-family: PingFangSC-Medium;
    font-size: 14px;
    border-radius: 5px;
    border: 2px solid #777;
    color: #333;
    cursor: pointer;
    transition: all 0.2s;

    &:hover {
      color: rgb(18, 100, 189);
      border-color: rgb(18, 100, 189);
    }

    &.code-btn-disabled {
      cursor: no-drop;
      color: #999;
      border-color: #777;

      &:hover {
        color: #999;
        border-color: #777;
      }
    }

  }
}


</style>
