<!--
Menu principal de l'application
Prend en paramètre une liste de ItemMenu et les met en forme
-->

<template>
  <v-navigation-drawer
    :mini-variant.sync="miniVariant"
    v-model="open"
    fixed
    clipped
    class="ot_dark_grey ot_menu_color--text"
    app
  >
    <v-list class="left-menu">
      <div v-for="(item, i) in menu" :key="i">
        <v-list-item
          v-if="!item.children"
          :href="item.isExternalLink ? item.to : undefined"
          :to="!item.isExternalLink ? item.to : undefined"
          router
          exact
        >
          <v-list-item-action>
            <v-icon class="ot_menu_color--text" small>
              {{ item.icon }}
            </v-icon>
          </v-list-item-action>
          <v-list-item-content>
            <v-list-item-title class="ot_menu_color--text" v-text="$t(item.title)" />
          </v-list-item-content>
        </v-list-item>

        <v-list-group
          v-else
          v-model="item.active"
          no-action
        >
          <template #activator>
            <v-list-item-action>
              <v-icon class="ot_menu_color--text" small>
                {{ item.icon }}
              </v-icon>
            </v-list-item-action>
            <v-list-item-content>
              <v-list-item-title class="ot_menu_color--text" v-text="$t(item.title)" />
            </v-list-item-content>
          </template>

          <v-list-item
            v-for="child in item.children"
            :key="child.title"
            :href="child.isExternalLink ? child.to : undefined"
            :to="!child.isExternalLink ? child.to : undefined"
            router
            exact
          >
            <v-list-item-action>
              <v-icon class="ot_white--text" small>
                {{ child.icon }}
              </v-icon>
            </v-list-item-action>
            <v-list-item-content>
              <v-list-item-title class="ot_white--text" v-text="$t(child.title)" />
            </v-list-item-content>
          </v-list-item>

          <template #appendIcon>
            <v-icon class="ot_menu_color--text" small>mdi-chevron-down</v-icon>
          </template>
        </v-list-group>
      </div>
    </v-list>
  </v-navigation-drawer>
</template>

<script lang="ts">
import {computed, defineComponent, onUnmounted, ref, toRefs, watch} from '@nuxtjs/composition-api'
import { ItemsMenu } from '~/types/interfaces'
import {WatchStopHandle} from "@vue/composition-api";

export default defineComponent({
  props: {
    menu: {
      type: Array as () => ItemsMenu,
      required: true
    },
    miniVariant: {
      type: Boolean,
      required: true
    },
    openMenu: {
      type: Boolean,
      required: true
    }
  },
  setup(props){
    const {openMenu} = toRefs(props)
    const open = ref(false)
    const unwatch: WatchStopHandle = watch(openMenu, (newValue, oldValue) => {
      open.value = true
    })

    onUnmounted(() => {
      unwatch()
    })

    return {
      open
    }
  }
})
</script>

<style scoped>
  .v-list-item__action, .v-list-group__header__prepend-icon {
    margin-right: 10px !important;
  }

  .v-application--is-ltr .v-list-group--no-action > .v-list-group__header{
    margin-left: 0px;
    padding-left: 0px;
  }
  .v-application--is-ltr .v-list-group--no-action > .v-list-group__items > .v-list-item {
    padding-left: 30px;
  }

  .v-list-item__title {
    font-size: 14px;
  }

  .v-list-item {
    min-height: 10px !important;
  }

  .v-list-item__action {
    margin: 10px 0;
  }

  .v-list-item__content {
    padding: 8px 0;
  }

  .home_menu {
    font-size: 23px;
  }
</style>
