<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 { defineComponent, onBeforeMount, ref } from 'vue';
import Sessions from '../components/settings/Sessions.vue';
import MainUserSettings from '../components/settings/MainUserSettings.vue';
import { useMainStore } from 'src/store';
import { useSessionStore } from '../store';
import { useUserStore } from '../store';

export default defineComponent({
  // name: 'PageName'
  components: { Sessions, MainUserSettings },
  setup() {
    const mainStore = useMainStore();
    const sessionStore = useSessionStore();
    const userStore = useUserStore();

    onBeforeMount(() => sessionStore.getSessions().then((s) => (sessions.value = s)));
    const currentUser = ref(mainStore.currentUser);
    const sessions = ref([] as FG.Session[]);

    async function updateUser(value: FG.User) {
      await userStore.updateUser(value);
    }

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