<template>
  <div class="login-business">
    <div class="banner-login">
      <img data-not-lazy src="../assets/images/banner-login.png" alt="" class="img-bg" />
      <div class="banner-login-content">
        <img data-not-lazy src="../assets/images/quooly.png" alt="" class="txt-logo" />
        <div class="txt-jp">ログイン</div>
        <div class="txt-en">
          Real Estate Investment that Continues To Illuminate The Future.
        </div>
      </div>
    </div>
    <form id="forgot-password-form" class="form-login pt-224" @submit.prevent="submit">
      <div class="email-title">メールアドレス</div>
      <div class="form-control-group">
        <input
          v-model="model.email"
          v-validate="'required|email|email_exists'"
          type="text"
          name="email"
          placeholder="メールアドレスを入力"
        />
        <div v-if="errors.has('email')" class="error-message">
          {{ errors.first('email') }}
        </div>
      </div>
      <NuxtLink to="/login" class="forgot-password"> →ログイン画面へ </NuxtLink>
      <button type="submit" class="btn btn-primary btn-gradient">
        送信する
      </button>
    </form>
  </div>
</template>

<script>
import $ from 'jquery'
export default {
  name: 'PagesForgotPassword',
  layout: 'auth',
  auth: false,
  data() {
    return {
      model: {}
    }
  },
  created() {
    const messError = {
      custom: {
        email: {
          required: 'メールアドレスを入力してください。',
          email: 'メールアドレス形式が正しくありません。',
          email_exists: 'このメールが存在しません。'
        }
      }
    }
    this.$validator.localize('en', messError)
    const that = this
    this.$validator.extend('email_exists', {
      validate(value, args) {
        return that.$repositories.user
          .checkEmail({ email: value })
          .then((res) => {
            return !res.valid
          })
      }
    })
  },
  mounted() {},
  methods: {
    async submit() {
      const that = this
      await this.$validator.validateAll().then(async function (valid) {
        if (valid) {
          await that.$repositories.user.forgotPassword(
            $('#forgot-password-form').serialize()
          )
          that.$router.push('/login')
        } else {
          that.$el
            .querySelector(
              '[name=' + Object.keys(that.errors.collect())[0] + ']'
            )
            .focus()
          $('html, body').animate(
            {
              scrollTop:
                $(
                  '[name=' + Object.keys(that.errors.collect())[0] + ']'
                ).offset().top - 104
            },
            500
          )
        }
      })
    }
  }
}
</script>
