<template>
  <div id="app" v-if="model">
    <TopMenu v-bind:user="model.user" />
    <div class="container">
      <Main />
    </div>
    <div id="loading" v-if="loading">
      <div id="spinner-wrapper">
        <b-spinner variant="primary" style="width: 3rem; height: 3rem;" label="Loading"></b-spinner>
      </div>
    </div>
  </div>
</template>

<script>
import TopMenu from './components/TopMenu.vue';
import Main from './components/Main.vue';
import { mapState } from 'vuex';
import client from 'api-client';

export default {
  name: 'app',
  components: {
    TopMenu,
    Main
  },
  computed: mapState({
    model: state => state.model,
    input: state => state.input,
    loading: state => state.loading
  }),
  mounted: function() {
    var self = this;
    document.addEventListener('apiError', function (event) {
      self.$bvToast.toast(event.message, {
        title: "Error",
        variant: 'danger',
        solid: true
      });
    });
    document.addEventListener('loading', function (event) {
      self.$store.commit('setLoading', event.value);
    });

    // retrieve the initial model
    client.fetchHomePageModel(this.input)
      .then(model => {
        this.$store.commit('updateHomePageModel', model);
      });
  }
}
</script>

<style scoped>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#loading {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background-color: rgba(255, 255, 255, 0.7);
  z-index: 1000;
}

#spinner-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
}
</style>
