<script setup>
import { onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { useState } from 'src/composables/useState';
import { useSession } from 'src/composables/useSession';
import UserPanel from 'components/UserPanel.vue';
import FavoriteModules from './FavoriteModules.vue';

const { t } = useI18n();
const session = useSession();
const state = useState();
const user = state.getUser();
const token = session.getToken();

onMounted(() => (state.headerMounted.value = true));

function onToggleDrawer() {
    state.drawer.value = !state.drawer.value;
}
</script>

<template>
    <q-header class="bg-dark" color="white" elevated>
        <q-toolbar class="q-py-sm q-px-md">
            <q-btn flat @click="onToggleDrawer()" round dense icon="menu">
                <q-tooltip bottom anchor="bottom right">
                    {{ t('globals.collapseMenu') }}
                </q-tooltip>
            </q-btn>
            <router-link to="/">
                <q-btn flat round class="q-ml-xs" v-if="$q.screen.gt.xs">
                    <q-avatar square size="md">
                        <q-img src="~/assets/logo_icon.svg" alt="Logo" />
                    </q-avatar>
                    <q-tooltip bottom>
                        {{ t('globals.backToDashboard') }}
                    </q-tooltip>
                </q-btn>
            </router-link>
            <q-toolbar-title shrink class="text-weight-bold">Salix</q-toolbar-title>
            <q-space></q-space>
            <div id="searchbar"></div>
            <q-space></q-space>
            <div class="q-pl-sm q-gutter-sm row items-center no-wrap">
                <div id="header-actions"></div>
                <q-btn id="favoriteModules" icon="apps" flat dense rounded>
                    <q-tooltip bottom>
                        {{ t('globals.favoriteModules') }}
                    </q-tooltip>
                    <FavoriteModules />
                </q-btn>
                <q-btn rounded dense flat no-wrap id="user">
                    <q-avatar size="lg">
                        <q-img
                            :src="`/api/Images/user/160x160/${user.id}/download?access_token=${token}`"
                            spinner-color="white"
                        >
                        </q-img>
                    </q-avatar>
                    <q-tooltip bottom>
                        {{ t('globals.userPanel') }}
                    </q-tooltip>
                    <UserPanel />
                </q-btn>
            </div>
        </q-toolbar>
    </q-header>
</template>
