<!-- eslint-disable @intlify/vue-i18n/no-raw-text -->
<template>
  <q-drawer
    v-bind="$attrs"
    v-model="leftDrawerOpen"
    :width="214"
    :breakpoint="500"
    behavior="mobile"
    class="detached-container"
  >
    <div class="column full-height no-wrap">
      <div class="overflow-hidden" style="border-radius: 8px 8px 0px 0px">
        <div class="flex flex-center full-width q-pa-sm" style="height: 50px">
          <q-img :src="Logo" style="max-width: 92px" />
        </div>
      </div>

      <div class="column full-height no-wrap">
        <q-scroll-area class="menu-scroll col" bar-color="secondary">
          <q-list class="column no-wrap">
            <template v-for="item in navigation_store.navigationItems">
              <template v-if="item.permission">
                <!-- Single Menu -->
                <q-item
                  v-if="item.type === 'single'"
                  :key="item.name"
                  v-ripple
                  clickable
                  exact-active-class="menu-selected"
                  active-class="menu-selected"
                  :exact="item.name == 'HomePage'"
                  :to="{ name: item.name }"
                >
                  <q-item-section avatar>
                    <q-icon :name="item.icon" style="font-size: 20px" />
                  </q-item-section>
                  <q-item-section>{{ $t(item.title) }}</q-item-section>
                </q-item>
                <!-- Expansive Menu with children -->
                <q-expansion-item
                  v-else
                  :key="item.icon"
                  v-model="isExpasionItemExpanded"
                  :class="{
                    'menu-selected':
                      childrenAreActive(item.children) &&
                      !isExpasionItemExpanded,
                  }"
                >
                  <template #header>
                    <q-item-section avatar>
                      <q-icon :name="item.icon" style="font-size: 20px" />
                    </q-item-section>
                    <q-item-section>{{ $t(item.title) }}</q-item-section>
                  </template>
                  <div
                    v-for="child in item.childrens"
                    :key="child.name"
                    :class="{
                      'financial-submenu': item.title === 'Financeiro',
                    }"
                  >
                    <q-item
                      v-ripple
                      clickable
                      :to="{ name: child.name }"
                      exact
                      exact-active-class="menu-selected"
                      class="q-pl-lg"
                    >
                      <q-item-section avatar>
                        <q-icon :name="child.icon" style="font-size: 20px" />
                      </q-item-section>
                      <q-item-section>{{ $t(child.title) }}</q-item-section>
                    </q-item>
                  </div>
                </q-expansion-item>
              </template>
            </template>
          </q-list>
        </q-scroll-area>

        <q-list class="q-mt-auto">
          <q-item v-ripple clickable @click="openUrl('https://softpar.inf.br')">
            <div class="flex full-width justify-center">
              <q-img
                :src="$q.dark.isActive ? LogoSoftparLight : LogoSoftparDark"
                style="width: 100%; height: 30px; max-width: 114px"
              />
            </div>
          </q-item>
        </q-list>
        <div
          class="full-width text-center text-subtitle3 cursor-pointer"
          @click="gotoVersionPage()"
        >
          <span class="text-caption text-weight-light">{{
            $t("common.terms.version") + " " + version
          }}</span>
        </div>
      </div>
    </div>
  </q-drawer>
</template>

<script setup>
import { navigationStore } from "src/stores/navigation";
import { onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { version } from "src/../package.json";

import Logo from "src/assets/logo.png";
import LogoSoftparLight from "src/assets/softpar_logo_light.svg";
import LogoSoftparDark from "src/assets/softpar_logo_dark.svg";

const router = useRouter();
const route = useRoute();
const navigation_store = navigationStore();

const leftDrawerOpen = defineModel({ type: Boolean });
const isExpasionItemExpanded = ref([]);

const childrenAreActive = (children) => {
  if (!Array.isArray(children) || children.length === 0) {
    return false;
  }
  return children.some((child) => route?.name === child?.name);
};

const openUrl = (url) => {
  window.open(url, "_blank");
};

const gotoVersionPage = () => {
  router.push({ name: "SystemVersionsPage" });
};

onMounted(() => {
  navigation_store.navigationItems.forEach((item, index) => {
    if (childrenAreActive(item.childrens)) {
      isExpasionItemExpanded.value[index] = true;
    }
  });
});
</script>

<style lang="scss" scoped>
@import "/src/css/quasar.variables.scss";
.text-subtitle3 {
  font-size: 1.1rem !important;
  font-weight: 400 !important;
}

.menu-selected {
  background-color: rgba($primary, 0.1);
  color: $primary;
}

.financial-submenu :deep(.q-item) {
  color: $secondary;
}

.financial-submenu :deep(.q-item.menu-selected) {
  background-color: rgba($secondary, 0.14);
  color: $secondary;
}

.menu-scroll {
  min-height: 0;
}

.menu-scroll :deep(.q-scrollarea__bar--v) {
  width: 6px;
  right: 2px;
  border-radius: 6px;
}
</style>
