<template>
  <div class="page-login">
    <div class="main-content">
      <img data-not-lazy class="pc img" src="~assets/images/title-page.png" alt="Title" />
      <div class="background-img pc">
        <img data-not-lazy src="~assets/images/login.png" alt="Login" />
        <h4>パスワード忘れた</h4>
        <p>Real Estate Investment that Continues To Illuminate The Future.</p>
      </div>
      <form id="forgot-password-form" class="form-login" @submit.prevent="submit">
        <div class="form-login-header sp">パスワード忘れた</div>
        <div class="email-title">メールアドレス</div>
        <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>
        <NuxtLink to="/login">
          <div class="forgot-password">→ログイン画面へ</div>
        </NuxtLink>
        <button type="submit" class="btn-gradient btn btn-common btn-login">
          送信する
        </button>
      </form>
    </div>
    <div class="foo">
      <div class="box-1">
        <div class="el">
          まだ<span>会員登録</span>が<br class="pc" />お済みでないお客様へ
        </div>
        <p class="p">＊すべて無料</p>
      </div>
      <div class="foo-el">
        <div class="num">01</div>
        <div class="foo-el__wrap">
          <div class="foo-el__title">プレミアム物件閲覧</div>
          <div class="desc">
            会員様限定のプレミアム物件を<br class="pc"/>閲覧いただけます
          </div>
        </div>
      </div>
      <div class="foo-el">
        <div class="num">02</div>
        <div class="foo-el__wrap">
          <div class="foo-el__title">不動産とのマッチ度</div>
          <div class="desc">お客様の好みの物件が<br class="pc"/>一目でわかります</div>
        </div>
      </div>
      <div class="foo-el">
        <div class="num">03</div>
        <div class="foo-el__wrap">
          <div class="foo-el__title">お得な最新情報をお届け</div>
          <div class="desc">新着情報をいち早くお届けします</div>
        </div>
      </div>
      <div class="foo-el br-none">
        <NuxtLink to="/register">
          <div class="btn-outline">会員登録</div>
        </NuxtLink>
      </div>
    </div>
    <CommonSearchNews class="sp login-search-footer" />
  </div>
</template>

<script>
import $ from 'jquery'
export default {
  name: 'PagesForgotPassword',
  data() {
    return {
      title: 'パスワード忘れた',
      model: {}
    }
  },
  head() {
    return {
      title: this.title,
      meta: [
        {
          hid: 'description',
          name: 'description',
          content: this.title + ' | Quooly不動産投資と収益物件の総合情報'
        }
      ]
    }
  },
  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>
