<template>
  <el-card class="rpm-forgot">
    <el-form
      ref="forgotPwdForm"
      :model="forgotPwdForm"
      :rules="forgotPwdRules"
      label-width="0"
    >
      <el-form-item prop="email">
        <el-input ref="email" v-model="forgotPwdForm.email" placeholder="Email Address" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native.prevent="handleForgotPwd">
          Send me a verify code
        </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 { forgetPassword } from '@/api/auth'
import { validateEmail } from '@/utils/validate'

export default {
  name: 'ForgotPwdForm',
  data() {
    return {
      forgotPwdForm: {
        email: ''
      },
      forgotPwdRules: {
        email: [{ required: true, trigger: 'blur', validator: validateEmail }]
      },
      loading: false
    }
  },

  mounted() {
    if (this.forgotPwdForm.email === '') {
      this.$refs.email.focus()
    }
  },
  methods: {
    handleForgotPwd() {
      this.$refs.forgotPwdForm.validate(async valid => {
        if (valid) {
          this.loading = true
          await forgetPassword({ mail: this.forgotPwdForm.email })
          this.loading = false
          this.$notify({
            title: 'Success',
            message: 'Email sent successfully.',
            type: 'success',
            duration: 3000
          })
          this.$emit('show-form', 'resetPwd')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>


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