<template>
  <div>
    <q-page padding class="fit row justify-center content-center items-center q-gutter-sm">
      <q-card class="col-12">
        <q-card-section class="fit row justify-start content-center items-center">
          <div class="col-12 text-center text-h6">Benutzereinstellungen</div>
        </q-card-section>
        <MainUserSettings :user="currentUser" @update:user="updateUser" />
      </q-card>
      <div class="col-12 text-left text-h6">Aktive Sessions:</div>
      <sessions v-for="(session, index) in sessions" :key="'session' + index" :session="session" />
    </q-page>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, onBeforeMount, ref } from 'vue';
import Sessions from '../components/settings/Sessions.vue';
import MainUserSettings from '../components/settings/MainUserSettings.vue';
import { useStore } from 'vuex';
import setLoadingBar from 'src/utils/loading';
import { UserSessionState } from '../store';

export default defineComponent({
  // name: 'PageName'
  components: { Sessions, MainUserSettings },
  setup() {
    const store = useStore<UserSessionState>();

    onBeforeMount(() => {
      store.dispatch('sessions/getSessions').catch((error) => {
        console.warn(error);
      });
    });

    const currentUser = ref(<FG.User>store.state.users.currentUser);
    const sessions = computed(() => store.state.sessions.sessions);
    const loading = computed(() => store.state.sessions.loading || store.state.users.loading > 0);
    function updateUser(value: FG.User) {
      store.dispatch('users/updateUser', value).catch((error) => {
        console.warn(error);
      });
    }

    setLoadingBar(loading);

    return {
      currentUser,
      sessions,
      updateUser,
    };
  },
});
</script>
