import Vue from 'vue'
import App from './App'
import router from './router'
import BootstrapVue from 'bootstrap-vue'
import '@/assets/css/style.css'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

Vue.use(BootstrapVue)

Vue.config.productionTip = false
Vue.prototype.$hostname = Vue.config.productionTip ? 'https://hostname' : 'http://localhost:8080'
Vue.prototype.$apiUrl = 'http://localhost:8888/IMT/CDAW/frontend/public/api/api.php'

Vue.mixin({
  created: function () {
    if (localStorage.token === undefined && this.$route.name != 'Index' && this.$route.name != 'Signup') {
      window.location.href = '/#/'
    }
    else if (localStorage.token !== undefined && (this.$route.name == 'Index' || this.$route.name == 'Signup')) {
      window.location.href = '/#/home'
    }
  },
  methods: {
    isExpired() {
      fetch(this.$apiUrl + '/validatetoken', {
        method: 'GET',
        headers: {
          'Authorization' : 'Bearer ' + localStorage.token
        }
      })
        .then(response => {
          if (response.status === 200) {
            return false
          } 
          else {
            window.location.href = '/#/'
          }
        })
    }
  }
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
