<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 class="form-login pt-224" autocomplete="off" @submit.prevent="submit">
      <div class="email-title">メールアドレス</div>
      <div class="form-control-group">
        <input
          v-model="model.email"
          v-validate="'required|email'"
          autocomplete="off"
          type="text"
          name="email"
          placeholder="メールアドレスを入力"
        />
        <div v-if="errors.has('email')" class="error-message">
          {{ errors.first('email') }}
        </div>
      </div>

      <div class="password-title">パスワード</div>
      <div class="form-control-group">
        <input
          v-model="model.password"
          v-validate="'required|min:8|max:16|password_rule'"
          autocomplete="off"
          type="password"
          name="password"
          placeholder="メールアドレスを入力"
        />
        <div v-if="errors.has('password')" class="error-message">
          {{ errors.first('password') }}
        </div>
      </div>
      <NuxtLink to="/forgot-password" class="forgot-password">
        →パスワードを忘れた方
      </NuxtLink>
      <div v-if="messageError" class="error-message error-account">
        {{ messageError }}
      </div>
      <button type="submit" class="btn btn-primary btn-gradient">
        ログイン
      </button>
    </form>
  </div>
</template>

<script>
import $ from 'jquery'
export default {
  name: 'PagesLogin',
  layout: 'auth',
  auth: false,
  // middleware: ['guest'],
  data() {
    return {
      model: {},
      messageError: ''
    }
  },
  watch: {
    'model.email'(value) {
      this.messageError = ''
    },
    'model.password'(value) {
      this.messageError = ''
    }
  },
  created() {
    const messError = {
      custom: {
        email: {
          required: 'メールアドレスを入力してください。',
          email: 'メールアドレス形式が正しくありません。'
        },
        password: {
          required: 'パスワードを入力してください。',
          max: 'パスワードは８文字～１６文字入力してください',
          min: 'パスワードは８文字～１６文字入力してください',
          password_rule:
            'パスワードは半角英数字で、大文字、小文字、数字で入力してください'
        }
      }
    }
    this.$validator.localize('en', messError)
  },
  mounted() {},
  methods: {
    async submit() {
      const that = this
      await this.$validator.validateAll().then(async function (valid) {
        if (valid) {
          try {
            that.$nuxt.$loading.show()
            await that.$auth.loginWith('business', {
              data: {
                email: that.model.email,
                password: that.model.password
              }
            })
            that.$router.push('/')
          } catch (error) {
            that.messageError = 'ユーザー名とパスワードが一致しません。'
          } finally {
            that.$nuxt.$loading.hide()
          }
        } 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>
