<template>
  <el-card class="rpm-sign-up">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" label-width="0">
      <el-form-item prop="name">
        <el-input v-model="registerForm.name" placeholder="Your Name" />
      </el-form-item>
      <el-form-item prop="email">
        <el-input v-model="registerForm.email" placeholder="Email Address" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="registerForm.password"
          placeholder="Set a Password"
          @keyup.native="checkCapslock"
          @blur="capsTooltip = false"
          @keyup.enter.native="handleRegister"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native.prevent="handleRegister">
          注册
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="$emit('show-form', 'signIn')">
          Back to Sign in
        </el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { register } from '@/api/auth'
import { validateUsername, validateEmail, validatePassword } from '@/utils/validate'
import md5 from '@/utils/md5';

export default {
  name: 'SignUpForm',
  data() {
    return {
      registerForm: {
        name: '',
        email: '',
        password: ''
      },
      registerRules: {
        name: [{ required: true, trigger: 'blur', validator: validateUsername }],
        email: [{ required: true, trigger: 'blur', validator: validateEmail }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  created() {
  },
  mounted() {
    if (this.registerForm.name === '') {
      this.$refs.name.focus()
    } else if (this.registerForm.email === '') {
      this.$refs.email.focus()
    } else if (this.registerForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    handleRegister() {
      let that = this
      this.$refs.registerForm.validate(async valid => {
        if (valid) {
          this.loading = true
          await register({
            name: this.registerForm.name,
            email: this.registerForm.email,
            password: md5(this.registerForm.password)
          })
          this.loading = false
          this.$notify({
            title: 'Success',
            message: 'Registered account is successful!',
            type: 'success',
            duration: 3000
          })
          setTimeout(function() { that.$emit('show-form', 'signIn') }, 3000);
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>


<style lang="scss" scoped>
.el-button {
  width: 100% !important;
}
.rpm-sign-up {
  position: absolute;
  top: 200px;
  right: 100px;
  width: 350px;
}
</style>
