<!--
Header de l'application, contient entre autres le nom de l'organisation, l'accès à l'aide
et aux préférences de l'utilisateur
-->

<template>
  <v-app-bar
    clipped-left
    elevate-on-scroll
    dense
    fixed
    app
    class="ot_green ot_white--text"
  >
    <v-btn
      v-if="displayedMiniVariant"
      class="menu-btn"
      icon
      @click.stop="displayedMenu()"
    >
      <v-icon class="ot_white--text">
        mdi-menu{{ `${properties.miniVariant ? '' : '-open'}` }}
      </v-icon>
    </v-btn>

    <v-toolbar-title v-text="title" />

    <v-spacer />

    <v-btn
      elevation="2"
      color="ot_warning ot_white--text"
    >
      {{ $t('create') }}
    </v-btn>

    <v-tooltip bottom>
      <template #activator="{ on, attrs }">
        <v-btn
          icon
          class="ml-2"
          v-bind="attrs"
          v-on="on"
        >
          <a class="no-decoration" :href="properties.homeUrl + '/'"><v-icon class="ot_white--text" small>fa-home</v-icon></a>
        </v-btn>
      </template>
      <span>{{ $t('welcome') }}</span>
    </v-tooltip>

    <LayoutHeaderMenu :menu="webSiteMenu" />

    <LayoutHeaderMenu v-if="hasAccessesMenu" :menu="myAccessesMenu" />

    <LayoutHeaderMenu v-if="hasFamilyMenu" :menu="myFamilyMenu" />

    <LayoutHeaderNotification />

    <LayoutHeaderMenu v-if="hasConfigurationMenu" :menu="configurationMenu" />

    <LayoutHeaderMenu :menu="accountMenu" :avatar="true" />

    <a class="text-body pa-3 ml-2 ot_dark_grey ot_white--text text-decoration-none" href="https://support.opentalent.fr/" target="_blank">
      <span class="d-none d-sm-none d-md-flex">{{ $t('help_access') }}</span>
      <v-icon class="d-sm-flex d-md-none" color="white">fas fa-question-circle</v-icon>
    </a>
  </v-app-bar>
</template>

<script lang="ts">
import {
  defineComponent, reactive, useContext, computed, ComputedRef, Ref, UnwrapRef
} from '@nuxtjs/composition-api'
import { $useMenu } from '~/use/layout/menu'
import { AnyJson } from '~/types/interfaces'

export default defineComponent({
  setup (_props, { emit }) {
    const { store, $config } = useContext()

    const properties:UnwrapRef<AnyJson> = reactive({
      miniVariant: false,
      homeUrl: $config.baseURL_adminLegacy
    })

    const displayedMiniVariant:ComputedRef<boolean> = computed(() => store.state.profile.access.hasLateralMenu)
    const hasConfigurationMenu:ComputedRef<boolean> = computed(() => store.state.profile.access.hasConfigurationMenu)
    const hasAccessesMenu:ComputedRef<boolean> = computed(() => store.state.profile.access.hasAccessesMenu)
    const hasFamilyMenu:ComputedRef<boolean> = computed(() => store.state.profile.access.hasFamilyMenu)
    const title:ComputedRef<string> = computed(() => store.state.profile.organization.name)

    const webSiteMenu:Ref<any> = $useMenu.setupContext().useWebSiteMenuConstruct()
    const myAccessesMenu:Ref<any> = $useMenu.setupContext().useMyAccessesMenuConstruct()
    const myFamilyMenu:Ref<any> = $useMenu.setupContext().useMyFamilyMenuConstruct()
    const configurationMenu:Ref<any> = $useMenu.setupContext().useConfigurationMenuConstruct()
    const accountMenu:Ref<any> = $useMenu.setupContext().useAccountMenuConstruct()

    const displayedMenu = () => {
      properties.miniVariant = !properties.miniVariant
      emit('handle-open-menu-click', properties.miniVariant)
    }

    return {
      properties,
      displayedMiniVariant,
      hasConfigurationMenu,
      hasAccessesMenu,
      hasFamilyMenu,
      title,
      displayedMenu,
      webSiteMenu,
      myAccessesMenu,
      myFamilyMenu,
      configurationMenu,
      accountMenu
    }
  }
})
</script>

<style scoped>
  .help {
    padding: 14px 14px 13px;
    font-size: 14px;
    text-decoration: none;
  }
</style>
