<i18n>
{
  "en": {
    "username": "Username",
    "password": "Password",
    "login": "Login",
    "usernameError": "User not found.",
    "passwordError": "Wrong password.",
    "usernameEmptyError": "Username can not be empty.",
    "passwordEmptyError": "Passwort can not be empty."
  },
  "de": {
    "username": "Benutzername",
    "password": "Passwort",
    "login": "Anmelden",
    "usernameError": "Benutzer nicht gefunden.",
    "passwordError": "Passwort falsch.",
    "usernameEmptyError": "Benutzername kann nicht leer sein.",
    "passwordEmptyError": "Passwort kann nicht leer sein."
  }
}
</i18n>

<template>
  <v-app dark class="grey darken-4 non-selectable">
    <div class="login-page">
      <img class="logo non-draggable" src="@/assets/logo.svg" />
      <v-form class="login-form" ref="form" v-model="valid" lazy-validation @submit.prevent="login">
        <v-text-field
          v-model="username"
          :rules="usernameRules"
          :label="$t('username')"
          autocomplete="off"
          @input="clearErrors"
        ></v-text-field>
        <v-text-field
          type="password"
          v-model="password"
          :rules="passwordRules"
          :label="$t('password')"
          autocomplete="off"
          @input="passwordError = false"
        ></v-text-field>
        <v-btn type="submit" class="login-button primary" raised>{{ $t('login') }}</v-btn>
      </v-form>
    </div>
  </v-app>
</template>

<script>
export default {
  name: 'LoginPage',
  data () {
    return {
      valid: true,
      username: '',
      usernameError: false,
      usernameRules: [
        v => !!v || this.$t('usernameEmptyError'),
        v => !this.usernameError || this.$t('usernameError')
      ],
      password: '',
      passwordError: false,
      passwordRules: [
        v => !!v || this.$t('passwordEmptyError'),
        v => !this.passwordError || this.$t('passwordError')
      ]
    }
  },
  methods: {
    clearErrors () {
      if (this.usernameError) this.usernameError = false
      if (this.passwordError) {
        this.passwordError = false
        this.$refs.form.validate()
      }
    },
    login () {
      if (this.$refs.form.validate()) {
        this.$http.post('/api/login', { username: this.username, password: this.password })
          .then(response => {
            this.$router.push('/dashboard')
          })
          .catch(error => {
            if (error.response.data.status === 'USER_NOTFOUND') {
              this.usernameError = true
            } else if (error.response.data.status === 'PASSWORD_INVALID') {
              this.passwordError = true
            }
            this.$refs.form.validate()
          })
      }
    }
  }
}
</script>

<style scoped>

.login-page {
  height: 100%;
  width: 100%;
  min-height: 500px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  height: 180px;
  margin-bottom: 60px;
}

.login-form {
  width: 300px;
}

.login-button {
  margin-top: 20px;
  float: right;
}

</style>
