<template>
  <v-container>
    <Header />

    <p>user logged in: {{ user.login }}</p>

    <ul>
      <li v-for="deck in decks" :key="deck.id">
        {{ deck.id }}: {{ deck.content.name }}
      </li>
    </ul>

    <v-btn @click.native="logout">Logout</v-btn>
  </v-container>
</template>

<script>
import * as Cookies from 'js-cookie'
import axios from '@/plugins/axios'

import Header from '@/components/Header.vue'

export default {
  name: 'UserCP',

  components: {
    Header
  },

  data: () => ({
    user: '',
    decks: ''
  }),

  methods: {
    goHome() {
      Cookies.remove('session')
      this.$router.push({ name: 'home' })
    },

    logout() {
      axios
        .post('/user/logout', {
          session: Cookies.get('session')
        })
        .then(response => {
          if (response.data.success) {
            this.goHome()
          }
        })
    }
  },

  mounted() {
    axios
      .post('/user/info', {
        session: Cookies.get('session')
      })
      .then(response => {
        if (response.data.success) {
          this.user = response.data.user
        } else {
          this.goHome()
        }
      })
    axios
      .post('/decks/list', {
        session: Cookies.get('session')
      })
      .then(response => {
        if (response.data.success) {
          this.decks = response.data.decks
        }
      })
  }
}
</script>
