<script setup>



const user = useSupabaseUser()
const router = useRouter()
const route = useRoute()
const supabase = useSupabaseClient()
const tenants = (await supabase.from("tenants").select()).data




const dataStore = useDataStore()
const {loaded} = storeToRefs(useDataStore())
const {fetchData} = dataStore
fetchData()

const navLinks = [
  {
    label: "Home",
    to: "/",
    icon: 'i-heroicons-home'
  },
  {
    label: "Aufgaben",
    to: "/tasks",
    icon: "i-heroicons-rectangle-stack"
  },
  {
    label: "Plantafel",
    to: "/planningBoard",
    icon: "i-heroicons-calendar-days"
  },
  {
    label: "Kunden",
    to: "/customers",
    icon: "i-heroicons-user-group"
  },
  {
    label: "Projekte",
    to: "/projects",
    icon: "i-heroicons-clipboard-document-check"
  },
  {
    label: "Zeiterfassung",
    to: "/timetracking",
    icon: "i-heroicons-clock"
  },
  {
    label: "Artikel",
    to: "/products"
  },
  {
    label: "Dokumente",
    to: "/documents",
    icon: "i-heroicons-document"
  },
  {
    label: "Inventar",
    to: "/inventory"
  }
]

const linksForBreadcrumbs = ref([])

const generateLinks = () => {
  let pathSteps = route.fullPath.split("/")
  let returnArr = []

  pathSteps.forEach((step,index) => {

    let stepLink = navLinks.find(link => link.link == step)

    if(stepLink) {
      returnArr[index] = {
        label: stepLink.label,
        icon: stepLink.icon,
        to:  '/' + stepLink.link
      }
    }

    linksForBreadcrumbs.value = returnArr

  })



}

watch(
    () => route.path,
    () => {
      generateLinks()
    }
)
generateLinks()




const userTenant = ref({})
if(user) userTenant.value = tenants.find(tenant => tenant.id === user.value.app_metadata.tenant)
const userDropdownItems = [
    [
      {
        label: user.value ? user.value.email : "Profil",

      },/*{
        label: user ? userTenant.value.name : "",

      }*/
    ],
    [
      {
        label: "Logout",
        click: () => {

          router.push("/login")
        }
      }
    ]
]


useHead({
  meta: [
    { name: 'viewport', content: 'width=device-width, initial-scale=1' }
  ],
  link: [
    { rel: 'icon', href: '/favicon.ico' }
  ],
  htmlAttrs: {
    lang: 'de'
  }
})

useSeoMeta({
  ogSiteName: 'spaces.software',
  twitterCard: 'summary_large_image'
})


</script>

<template>
  <UHeader :links="navLinks">
    <template #logo>
      spaces.software
    </template>
    <template #right>
      <UColorModeButton/>
    </template>
  </UHeader>
  <div class="m-3" id="contentContainer">
    <NuxtPage
        v-if="loaded"
    />
    <div
        v-else
    >
      <UProgress animation="carousel" />
    </div>
  </div>
<!--  <UFooter>
    <template #left>
      <p class="text-gray-500 dark:text-gray-400 text-sm">
        Copyright © 2023-{{ new Date().getFullYear() }} <NuxtLink class="hover:underline" to="https://federspiel.tech" target="_blank">
        Federspiel Technolog UG haftungsbeschränkt
      </NuxtLink>
      </p>
    </template>

    <template #right>

    </template>
  </UFooter>-->


  <UNotifications/>

<!--    <UCard id="page">
      <template #header>
        <div id="menu">
          <router-link
              v-for="link in navLinks"
              :to="link.to"
              class="mr-2"
          >
            <UButton>{{link.label}}</UButton>
          </router-link>
          &lt;!&ndash;<router-link to="/customers" class="mr-2"><UButton>Kunden</UButton></router-link>
          <router-link to="/projects" class="mr-2"><UButton>Projekte</UButton></router-link>
  -
          <router-link to="/vendorinvoices" class="mr-2"><UButton>Eingangsrechnungen</UButton></router-link>

          <router-link to="/timetracking" class="mr-2"><UButton>Zeiterfassung</UButton></router-link>
          <router-link to="/products" class="mr-2"><UButton>Artikel</UButton></router-link>
          <router-link to="/documents" class="mr-2"><UButton>Dokumente</UButton></router-link>
          <router-link to="/inventory" class="mr-2"><UButton>Inventar</UButton></router-link>&ndash;&gt;
          <UDropdown :items="userDropdownItems" :popper="{placement: 'bottom-start'}">
            <UButton color="white" label="Benutzer" trailing-icon="i-heroicons-chevron-down-20-solid" />
          </UDropdown>
        </div>
        <UBreadcrumb
            class="my-3"
            :links="linksForBreadcrumbs"
        />
      </template>
      <NuxtPage
          v-if="loaded"
      />
      <div
          v-else
      >
        <UProgress animation="carousel" />
      </div>
    </UCard>-->



</template>

<style>

#contentContainer {
  width: 95vw;
  height: 85vh;
}

.scrollList {
  overflow-y: scroll;
  height: 85vh;
  margin-top: 1em;
  -ms-overflow-style: none;  /* IE and Edge */
  scrollbar-width: none;  /* Firefox */
}

.scrollList::-webkit-scrollbar {
  display: none;
}


  #page {
    width: 98vw;
    height: 95vh;
    margin:1em;
  }

  .listItem {
    padding: .1em;
    border: 1px solid grey;
    border-radius: 15px;
    margin-top: 1em;
  }
  .listItem:hover {
    border: 1px solid #69c350;
  }

</style>