<script>
import { post } from '../../utils/request'
export default {
  data() {
    const validateConfirmEmail = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('Comfirm email is required'))
      } else if (value !== this.ruleForm.email) {
        callback(new Error('The New Email and Confirm Email do not match'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        email: '',
        confirmation: ''
      },
      rules: {
        email: [
          {
            required: true,
            message: 'Email Address is required',
            trigger: ['']
          },
          {
            type: 'email',
            message: 'Please enter a valid Email Address',
            trigger: ['']
          }
        ],
        confirmation: [
          { required: true, validator: validateConfirmEmail, trigger: [''] }
        ]
      },
      click: true
    }
  },
  methods: {
    sendEmail() {
      if(this.click){
        this.click = false
        setTimeout(() => {
          this.click = true
        }, 3000)
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            post('/pdf-tech/vppMember/resetPassword', {
              email: this.ruleForm.email
            }).then((res) => {
              if (
                res.data.code === 200 &&
                res.data.msg ===
                  'Email sent successful, please confirm your new password in email'
              ) {
                this.$message({
                  duration: 5000,
                  message:
                    'Email was successfully sent, please confirm your new password in email',
                  type: 'success'
                })
                this.$router.push("/login")
              } else if (
                res.data.code === 700 &&
                res.data.msg === 'Please create account first'
              ) {
                this.$message.error({
                  duration: 5000,
                  message: 'Please create account first'
                })
              }
            })
          }
        })
      }
    }
  }
}
</script>

<template>
  <div class="bg w-full min-h-100vh flex justify-center items-center m-auto">
    <div class="w-404px h-auto rounded-8px p-24px bg-[#fff] loginBox">
      <h1 class="text-24px font-bold mb-24px">Forgot Password</h1>
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        :rules="rules"
        label-position="top"
        label-width="100px"
        inline-message
      >
        <el-form-item prop="email">
          <div style="display: flex">
            <el-input
              v-model.trim="ruleForm.email"
              clearable
              placeholder="Email Address"
            >
            </el-input>
          </div>
        </el-form-item>
        <el-form-item prop="confirmation">
          <div style="display: flex">
            <el-input
              v-model.trim="ruleForm.confirmation"
              clearable
              placeholder="Email Address Confirmation"
            >
            </el-input>
          </div>
        </el-form-item>
        <el-form-item>
          <button
            type="button"
            @click="sendEmail()"
            class="
              w-full
              h-48px
              bg-[#1460F3]
              text-[#fff]
              rounded-8px
              hover:opacity-80
            "
          >
            Send Email
          </button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.bg {
  background: #e7eaee url('@/assets/images/background.png') center center/100% 100% no-repeat;
}
.loginBox {
  box-shadow: 0px 0px 12px rgba(35, 97, 169, 0.05);
}
.el-input::v-deep input {
  border: 0px !important;
  border-bottom: 1px solid #d9d9d9 !important;
  border-radius: 0 !important;
}
</style>
