<template>
  <v-app>
    <v-navigation-drawer
        v-model="left"
        temporary
        app
    >
      <v-list dense>
        <v-list-item v-if="!hasUser" @click="() => { doLogin(); }">
          <v-list-item>
            <v-list-item>Login</v-list-item>
          </v-list-item>
        </v-list-item>
        <v-list-item avatar v-if="hasUser">
          <v-list-item-avatar>
            <v-img :src="user.picture" :alt="user.name"></v-img>
          </v-list-item-avatar>
          <v-list-item-content>
            <v-list-item-title v-html="user.name"></v-list-item-title>
          </v-list-item-content>
        </v-list-item>
        <v-list-item>
          <v-list-item-action>
            <v-icon>mdi-calendar-outline</v-icon>
          </v-list-item-action>
          <v-list-item-content>
            <nuxt-link to="/"><v-list-item-title>Evenemang</v-list-item-title></nuxt-link>
          </v-list-item-content>
        </v-list-item>
        <v-list-item v-if="hasUser">
          <v-list-item-action>
            <v-icon>mdi-home</v-icon>
          </v-list-item-action>
          <v-list-item-content>
            <nuxt-link to="/origins/"><v-list-item-title>Hantera startpunkter</v-list-item-title></nuxt-link>
          </v-list-item-content>
        </v-list-item>
        <v-list-item v-if="hasUser">
          <v-list-item-action>
            <v-icon>mdi-magnify</v-icon>
          </v-list-item-action>
          <v-list-item-content>
            <nuxt-link to="/filters/"><v-list-item-title>Hantera filter</v-list-item-title></nuxt-link>
          </v-list-item-content>
        </v-list-item>
        <v-list-item v-if="hasUser">
          <v-list-item-action>
            <v-icon>exit_to_app</v-icon>
          </v-list-item-action>
          <v-list-item-content>
            <nuxt-link to="/logout/"><v-list-item-title>Logga ut</v-list-item-title></nuxt-link>
          </v-list-item-content>
        </v-list-item>
      </v-list>
    </v-navigation-drawer>
    <v-app-bar app scroll-off-screen>
      <v-app-bar-nav-icon v-on:click="left = !left"></v-app-bar-nav-icon>
      <v-toolbar-title v-html="title"></v-toolbar-title>
      <v-spacer></v-spacer>
      <v-toolbar-items>
        <v-list-item avatar v-if="hasUser">
          <v-list-item-avatar>
            <v-img :src="user.picture" :alt="user.name"></v-img>
          </v-list-item-avatar>
          <v-list-item-content>
            <v-list-item-title v-html="user.name"></v-list-item-title>
          </v-list-item-content>
        </v-list-item>
      </v-toolbar-items>
    </v-app-bar>
    <v-content>
      <v-container fluid>
        <nuxt />
      </v-container>
    </v-content>
  </v-app>
</template>

<style lang="scss">
// We need this line to import all global styling
@import "assets/scss/global.scss";
</style>

<style lang="scss" scoped>
.layout {
  background-color: white;

  .log-out {
    cursor: pointer;
  }
}
</style>

<script>
import auth from "~/utils/auth";

export default {
  data() {
    return {
      user: undefined,
      left: false
    };
  },
  computed: {
    hasUser() {
      return this.user;
    },
    title() {
      return this.$store.state.title;
    }
  },
  watch: {
    "$route.path"() {
      this.fetchUser();
    }
  },
  mounted() {
    this.fetchUser();
  },
  methods: {
    fetchUser() {
      this.user = auth.getUserInfo();
    },
    doLogin() {
      auth.triggerLogin({ returnUrl: this.$route.fullPath });
    }
  }
};
</script>
