<script setup>
import {useRole} from "~/composables/useRole.js";

const profileStore = useProfileStore()
const route = useRoute()

const role = useRole()
const links = computed(() => {
  return [
    ... profileStore.currentTenant === 21 ? [{
      label: "Lieferschein Formular",
      to: "https://loar.ma-agrarservice.de/erfassung",
      icon: "i-heroicons-rectangle-stack",
      target: "_blank",
    }] : [],
    ... profileStore.currentTenant === 5 ? [{
      label: "Support Tickets",
      to: "/support",
      icon: "i-heroicons-rectangle-stack",
    }] : [],
    {
      id: 'dashboard',
      label: "Dashboard",
      to: "/",
      icon: "i-heroicons-home"
    }, {
      id: 'historyitems',
      label: "Logbuch",
      to: "/historyitems",
      icon: "i-heroicons-book-open"
    },
    {
      label: "Organisation",
      icon: "i-heroicons-rectangle-stack",
      defaultOpen: false,
      children: [
        ... role.checkRight("tasks") ? [{
          label: "Aufgaben",
          to: "/standardEntity/tasks",
          icon: "i-heroicons-rectangle-stack"
        }] : [],
        ... profileStore.ownTenant.features.planningBoard ? [{
          label: "Plantafel",
          to: "/calendar/timeline",
          icon: "i-heroicons-calendar-days"
        }] : [],
        ... profileStore.ownTenant.features.calendar ? [{
          label: "Kalender",
          to: "/calendar/grid",
          icon: "i-heroicons-calendar-days"
        }] : [],
        ... profileStore.ownTenant.features.calendar ? [{
          label: "Termine",
          to: "/standardEntity/events",
          icon: "i-heroicons-calendar-days"
        }] : [],
        /*{
          label: "Dateien",
          to: "/files",
          icon: "i-heroicons-document"
        },*/
      ]
    },
    {
      label: "Dokumente",
      icon: "i-heroicons-rectangle-stack",
      defaultOpen: false,
      children: [
        {
          label: "Dateien",
          to: "/files",
          icon: "i-heroicons-document"
        },{
          label: "Boxen",
          to: "/standardEntity/documentboxes",
          icon: "i-heroicons-archive-box"
        },
      ]
    },
    {
      label: "Kommunikation",
      icon: "i-heroicons-megaphone",
      defaultOpen: false,
      children: [
        {
          label: "E-Mail",
          to: "/email/new",
          icon: "i-heroicons-envelope"
        }, {
          label: "Logbücher",
          to: "/communication/historyItems",
          icon: "i-heroicons-book-open"
        }, {
          label: "Chats",
          to: "/chats",
          icon: "i-heroicons-chat-bubble-left"
        }
      ]
    },
    ... (role.checkRight("customers") || role.checkRight("vendors") || role.checkRight("contacts")) ? [{
      label: "Kontakte",
      defaultOpen: false,
      icon: "i-heroicons-user-group",
      children: [
        ... role.checkRight("customers") ? [{
          label: "Kunden",
          to: "/standardEntity/customers",
          icon: "i-heroicons-user-group"
        }] : [],
        ... role.checkRight("vendors") ? [{
          label: "Lieferanten",
          to: "/standardEntity/vendors",
          icon: "i-heroicons-truck"
        }] : [],
        ... role.checkRight("contacts") ? [{
          label: "Ansprechpartner",
          to: "/standardEntity/contacts",
          icon: "i-heroicons-user-group"
        }] : [],
      ]
    },] : [],
    {
      label: "Mitarbeiter",
      defaultOpen:false,
      icon: "i-heroicons-user-group",
      children: [
          ... profileStore.ownTenant.features.timeTracking ? [{
          label: "Zeiterfassung",
          to: "/employees/timetracking",
          icon: "i-heroicons-clock"
        }] : [],
        ... profileStore.ownTenant.features.workingTimeTracking ? [{
          label: "Anwesenheiten",
          to: "/workingtimes",
          icon: "i-heroicons-clock"
        }] : [],
        ... role.checkRight("absencerequests") ? [{
          label: "Abwesenheiten",
          to: "/standardEntity/absencerequests",
          icon: "i-heroicons-document-text"
        }] : [],
        {
          label: "Fahrten",
          to: "/trackingTrips",
          icon: "i-heroicons-map"
        },
      ]
    },
    ... profileStore.ownTenant.features.accounting ? [{
      label: "Buchhaltung",
      defaultOpen: false,
      icon: "i-heroicons-chart-bar-square",
      children: [
        {
          label: "Ausgangsbelege",
          to: "/createDocument",
          icon: "i-heroicons-document-text"
        },{
          label: "Serienvorlagen",
          to: "/createDocument/serialInvoice",
          icon: "i-heroicons-document-text"
        },{
          label: "Eingangsbelege",
          to: "/incomingInvoices",
          icon: "i-heroicons-document-text"
        },{
          label: "Kostenstellen",
          to: "/standardEntity/costcentres",
          icon: "i-heroicons-document-currency-euro"
        },{
          label: "Buchungskonten",
          to: "/accounts",
          icon: "i-heroicons-document-text"
        },{
          label: "zusätzliche Buchungskonten",
          to: "/standardEntity/ownaccounts",
          icon: "i-heroicons-document-text"
        },
        {
          label: "Bank",
          to: "/banking",
          icon: "i-heroicons-document-text"
        },
      ]
    },] : [],
    ... role.checkRight("inventory") ? [{
      label: "Lager",
      icon: "i-heroicons-puzzle-piece",
      defaultOpen: false,
      children: [
        /*{
          label: "Vorgänge",
          to: "/inventory",
          icon: "i-heroicons-square-3-stack-3d"
        },{
          label: "Bestände",
          to: "/inventory/stocks",
          icon: "i-heroicons-square-3-stack-3d"
        },*/
        ... role.checkRight("spaces") ? [{
          label: "Lagerplätze",
          to: "/standardEntity/spaces",
          icon: "i-heroicons-square-3-stack-3d"
        }] : [],
      ]
    },] : [],
    {
      label: "Stammdaten",
      defaultOpen: false,
      icon: "i-heroicons-clipboard-document",
      children: [
        ... role.checkRight("products") ? [{
          label: "Artikel",
          to: "/standardEntity/products",
          icon: "i-heroicons-puzzle-piece"
        }] : [],
        ... role.checkRight("productcategories") ? [{
          label: "Artikelkategorien",
          to: "/standardEntity/productcategories",
          icon: "i-heroicons-puzzle-piece"
        }] : [],
        ... role.checkRight("services") ? [{
          label: "Leistungen",
          to: "/standardEntity/services",
          icon: "i-heroicons-wrench-screwdriver"
        }] : [],
        ... role.checkRight("servicecategories") ? [{
          label: "Leistungskategorien",
          to: "/standardEntity/servicecategories",
          icon: "i-heroicons-wrench-screwdriver"
        }] : [],
        {
          label: "Mitarbeiter",
          to: "/profiles",
          icon: "i-heroicons-user-group"
        },
        {
          label: "Stundensätze",
          to: "/standardEntity/hourrates",
          icon: "i-heroicons-user-group"
        },
        ... role.checkRight("vehicles") ? [{
          label: "Fahrzeuge",
          to: "/standardEntity/vehicles",
          icon: "i-heroicons-truck"
        }] : [],
        ... role.checkRight("inventoryitems") ? [{
          label: "Inventar",
          to: "/standardEntity/inventoryitems",
          icon: "i-heroicons-puzzle-piece"
        }] : [],
        ... role.checkRight("inventoryitems") ? [{
          label: "Inventargruppen",
          to: "/standardEntity/inventoryitemgroups",
          icon: "i-heroicons-puzzle-piece"
        }] : [],
      ]
    },
    ... role.checkRight("checks") ? [{
      label: "Überprüfungen",
      to: "/standardEntity/checks",
      icon: "i-heroicons-magnifying-glass"
    },] : [],
    ... role.checkRight("projects") ? [{
      label: "Projekte",
      to: "/standardEntity/projects",
      icon: "i-heroicons-clipboard-document-check"
    },] : [],
    ... role.checkRight("contracts") ? [{
      label: "Verträge",
      to: "/standardEntity/contracts",
      icon: "i-heroicons-clipboard-document"
    }] : [],
    ... role.checkRight("plants") ? [{
      label: "Objekte",
      to: "/standardEntity/plants",
      icon: "i-heroicons-clipboard-document"
    },] : [],

    {
      label: "Einstellungen",
      defaultOpen: false,
      icon: "i-heroicons-cog-8-tooth",
      children: [
        {
          label: "Abrechnung",
          to: "https://billing.stripe.com/p/login/cN29Eb32Vdx0gOk288",
          icon: "i-heroicons-document-currency-euro",
          target: "_blank"
        },{
          label: "Nummernkreise",
          to: "/settings/numberRanges",
          icon: "i-heroicons-clipboard-document-list"
        },{
          label: "Rollen",
          to: "/roles",
          icon: "i-heroicons-key"
        },{
          label: "E-Mail Konten",
          to: "/settings/emailAccounts",
          icon: "i-heroicons-envelope"
        },{
          label: "Bankkonten",
          to: "/settings/banking",
          icon: "i-heroicons-currency-euro"
        },{
          label: "Textvorlagen",
          to: "/settings/texttemplates",
          icon: "i-heroicons-clipboard-document-list"
        },/*{
          label: "Eigene Felder",
          to: "/settings/ownfields",
          icon: "i-heroicons-clipboard-document-list"
        },*/{
          label: "Firmeneinstellungen",
          to: "/settings/tenant",
          icon: "i-heroicons-building-office"
        },{
          label: "Projekttypen",
          to: "/projecttypes",
          icon: "i-heroicons-clipboard-document-list"
        }
      ]
    }
  ]
})
</script>

<template>

  <div
    v-for="item in links"
  >
    <UAccordion
      v-if="item.children"
      :items="[item]"
    >
      <template #default="{item,index,open}">
        <UButton
          variant="ghost"
          :color="item.children.find(i => route.path.includes(i.to)) ? 'primary' :   'gray'"
          :icon="item.icon"
        >
          {{item.label}}

          <template #trailing>
            <UIcon
                name="i-heroicons-chevron-right-20-solid"
                class="w-5 h-5 ms-auto transform transition-transform duration-200"
                :class="[open && 'rotate-90']"
            />
          </template>
        </UButton>
      </template>
      <template #item="{item, open}">
        <div class="flex flex-col">
          <UButton
              variant="ghost"
              :color="child.to === route.path ? 'primary' : 'gray'"
              :icon="child.icon"
              v-for="child in item.children"
              class="ml-4"
              :to="child.to"
              :target="child.target"
          >
            {{child.label}}
          </UButton>
        </div>
      </template>
    </UAccordion>
    <UButton
      v-else
      variant="ghost"
      :color="item.to === route.path ? 'primary' : 'gray'"
      class="w-full"
      :icon="item.icon"
      :to="item.to"
    >
      {{item.label}}
    </UButton>
  </div>
</template>