<template>
  <b-container>
    <b-col sm="8" md="6" lg="4" class="mx-auto">
      <b-card class="login-card">
        <form @submit.prevent="login">
          <h1 class="title text-muted pb-3">
            KEA
          </h1>
          <b-form-group>
            <b-input-group class="input-group-seamless no-border" :class="{ 'is-invalid': form.invalid('email') }">
              <b-input-group-prepend>
                <span class="input-group-text">
                  <fa icon="envelope" :class="{ 'text-danger': form.feedback('email') }" />
                </span>
              </b-input-group-prepend>
              <b-form-input v-model="form.email"
                            type="email"
                            name="email"
                            required
                            :state="form.state('email')"
                            placeholder="Email"
              />
            </b-input-group>
            <b-form-invalid-feedback :state="form.state('email')">
              <div v-html="form.feedback('email')" />
            </b-form-invalid-feedback>
          </b-form-group>
          <b-form-group>
            <b-input-group class="input-group-seamless no-border" :class="{ 'is-invalid': form.invalid('password') }">
              <b-input-group-prepend>
                <span class="input-group-text">
                  <fa icon="lock" :class="{ 'text-danger': form.feedback('password') }" />
                </span>
              </b-input-group-prepend>
              <b-form-input v-model="form.password"
                            type="password"
                            name="password"
                            required
                            :state="form.state('password')"
                            placeholder="Password"
              />
            </b-input-group>
          </b-form-group>
          <b-button type="submit" block variant="primary" class="w-100" :disabled="form.busy">
            <b-spinner v-if="form.busy" class="spinner-center" small />
            Login
          </b-button>
        </form>
      </b-card>
    </b-col>
  </b-container>
</template>

<script>
import Form from '~/utils/form'
export default {
  name: 'Login',
  layout: 'public',
  middleware: 'guest',
  data: () => ({
      form: new Form({
          email: '',
          password: ''
      })
  }),

  methods: {
    async login () {
      let data

      // Submit the form.
      try {
          const response = await this.form.post('/login')
          data = response.data
      } catch (e) {
          return
      }

      // Save the token.
      this.$store.dispatch('auth/saveToken', {
          token: data.token,
          remember: this.remember
      })

      // Fetch the user.
      await this.$store.dispatch('auth/fetchUser')

      // Redirect home.
      this.$router.push({ name: 'panel.dashboard' })
    }
  }
}
</script>

<style lang="scss" scoped>
  .login-card {
    width: 390px;
    background: #fff;
    border-radius: 10px;
    overflow: hidden;
    padding: 10px 25px 10px 25px;
    box-shadow: 0 5px 10px 0px rgba(0, 0, 0, 0.1);
    -moz-box-shadow: 0 5px 10px 0px rgba(0, 0, 0, 0.1);
    -webkit-box-shadow: 0 5px 10px 0px rgba(0, 0, 0, 0.1);
    -o-box-shadow: 0 5px 10px 0px rgba(0, 0, 0, 0.1);
    -ms-box-shadow: 0 5px 10px 0px rgba(0, 0, 0, 0.1);
  }
  .title {
      display: block;
      line-height: 1.2;
      text-align: center;
  }
</style>
