<template>
  <nav class="navbar has-shadow sticky px-3" style="top: 0" role="navigation">
    <div class="navbar-brand mr-5">
      <span class="navbar-item is-gap-1">
      <div style="width: 16px; height: 16px" class="has-background-primary rounded-full"></div>
      <span class="fs-17 font-semibold has-text-primary">{{ $dayjs().format('DD/MM') }}</span>
    </span>
      <a
        class="navbar-item p-0 has-text-primary"
        @click="changeTab(leftmenu[0])"
      >
      <svg style="max-height: none; width: 44px" width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M40.5 6C59.0015 6 74 20.9985 74 39.5C74 58.0015 59.0015 73 40.5 73C21.9985 73 7 58.0015 7 39.5C7 20.9985 21.9985 6 40.5 6ZM17.9834 29.0654V48.2373H30.9395V44.8955H22.0371V40.3174H30.2373V36.9756H22.0371V32.4072H30.9014V29.0654H17.9834ZM33.8604 48.2373H37.9141V41.4404H40.873L44.5039 48.2373H48.9785L44.9092 40.7852C44.9677 40.7598 45.0277 40.7378 45.085 40.7109C46.1269 40.2242 46.9225 39.5252 47.4717 38.6143C48.0209 37.6969 48.2959 36.6012 48.2959 35.3281C48.2959 34.0613 48.0244 32.9595 47.4814 32.0234C46.9448 31.0812 46.1613 30.3545 45.1318 29.8428C44.1085 29.3248 42.8725 29.0655 41.4248 29.0654H33.8604V48.2373ZM50.8965 48.2373H54.9492V42.0215H58.3574C59.83 42.0214 61.0843 41.7499 62.1201 41.207C63.1623 40.6641 63.9577 39.9052 64.5068 38.9316C65.0559 37.9582 65.331 36.8354 65.3311 35.5625C65.3311 34.2895 65.0595 33.1659 64.5166 32.1924C63.9799 31.2127 63.2001 30.4476 62.1768 29.8984C61.1533 29.343 59.914 29.0654 58.46 29.0654H50.8965V48.2373ZM57.6826 32.3789C58.4689 32.3789 59.1182 32.5139 59.6299 32.7822C60.1416 33.0443 60.5228 33.4151 60.7725 33.8955C61.0283 34.3698 61.1562 34.9259 61.1562 35.5625C61.1562 36.1925 61.0281 36.7507 60.7725 37.2373C60.5228 37.7178 60.1416 38.0955 59.6299 38.3701C59.1245 38.6384 58.482 38.7734 57.7021 38.7734H54.9492V32.3789H57.6826ZM40.6475 32.3789C41.4274 32.3789 42.0733 32.4948 42.585 32.7256C43.1028 32.9502 43.4867 33.2811 43.7363 33.7178C43.9922 34.1546 44.1201 34.6916 44.1201 35.3281C44.1201 35.9584 43.9922 36.4858 43.7363 36.9102C43.4867 37.3344 43.1064 37.6531 42.5947 37.8652C42.083 38.0774 41.4399 38.1836 40.666 38.1836H37.9141V32.3789H40.6475Z" fill="currentColor"/>
      </svg>
      </a>
      <a
        role="button"
        class="navbar-burger"
        id="burger"
        aria-label="menu"
        aria-expanded="false"
        data-target="navMenu"
        @click="handleClick()"
      >
        <span aria-hidden="true"></span>
        <span aria-hidden="true"></span>
        <span aria-hidden="true"></span>
        <span aria-hidden="true"></span>
      </a>
    </div>
    <div class="navbar-menu" id="navMenu">
      <div class="navbar-start is-gap-1 is-align-items-center" >
        <template v-for="(v, i) in leftmenu" :key="i" :id="v.code">
          <a class="navbar-item rounded-lg is-clipped p-0" v-if="!v.submenu" @click="changeTab(v)">
            <span :class="[
              'px-3 py-2 fs-14 font-medium',
              currentTab.code === v.code ? 'has-text-primary-50 has-background-primary-95' : 'has-text-grey-30'
            ]">
              {{ v[lang] }}
            </span>
          </a>
          <div class="navbar-item has-dropdown is-hoverable" v-else>
            <a class="navbar-item px-2" @click="changeTab(v)">
              <span :class="[
                'px-3 py-1 font-medium',
                currentTab.code === v.code ? 'has-text-primary-bold has-background-primary-soft' : 'has-text-grey-30'
              ]">
                <span>{{ v[lang] }}</span>
                <SvgIcon
                  style="padding-top: 5px"
                  v-bind="{ name: 'down2.svg', type: currentTab.code === v.code ? 'white' : 'dark', size: 15 }"
                >
                </SvgIcon>
              </span>
            </a>
            <div class="navbar-dropdown has-background-light">
              <a
                class="navbar-item has-background-light py-1 border-bottom"
                v-for="x in v.submenu"
                @click="changeTab(v, x)"
              >
                {{ x[lang] }}
              </a>
            </div>
          </div>
        </template>
      </div>
      <!-- <div class="navbar-end">
        <a class="navbar-item" @click="changeTab(tabConfig)" v-if="tabConfig">
          <SvgIcon v-bind="{ name: 'configuration.svg', type: 'findata', size: 24 }"></SvgIcon>
        </a>
        <a class="navbar-item" @click="openProfile()" v-if="avatar">
          <Avatarbox v-bind="avatar"></Avatarbox>
        </a>
      </div> -->
      <div class="navbar-end">
        <a class="navbar-item">
          <div>
            <p class="fs-13">Xin chào,</p>
            <p class="fs-14 font-bold">Quản lý</p>
          </div>
          <Avatarbox text="Q" type="findata" size="two" />
        </a>
      </div>
    </div>
  </nav>
</template>
<script setup>
import Avatarbox from '@/components/common/Avatarbox.vue';
import { watch } from "vue";
const router = useRouter();
const route = useRoute();
const emit = defineEmits(["changetab", "langChanged"]);
const { $find, $filter, $findIndex, $store } = useNuxtApp();
const lang = ref($store.lang);
// var menu = $filter($store.common, { category: "topmenu" });
const menu = [
  {
    id: 1,
    category: 'topmenu',
    classify: 'left',
    code: 'dashboard',
    vi: 'Dashboard',
    link: null,
    detail: {
      base: 'Dashboard',
      component: 'DashboardMaster',
    },
    index: 0,
  },
  {
    id: 2,
    category: 'topmenu',
    classify: 'left',
    code: 'orders',
    vi: 'Đơn hàng',
    link: null,
    detail: {
      base: 'Orders',
      component: 'OrdersMaster',
    },
    index: 0,
  },
  {
    id: 1,
    category: 'topmenu',
    classify: 'left',
    code: 'inventory',
    vi: 'Tồn kho',
    link: null,
    detail: {
      base: 'Inventory',
      component: 'InventoryMaster',
    },
    index: 0,
  },
]
// if($store.rights.length>0) {
//   menu = menu.filter(v=>$findIndex($store.rights, {setting: v.id})>=0)
// }
if(menu.length===0) {
  $snackbar($store.lang==='vi'? 'Bạn không có quyền truy cập' : 'You do not have permission to access.')
}
// menu.map(v=>{
//   let arr = $filter($store.common, {category: 'submenu', classify: v.code})
//   if($store.rights.length>0) {
//     arr = arr.filter(x=>$findIndex($store.rights, {setting: x.id})>=0)
//   }
//   v.submenu = arr.length>0? arr : null
// })
var leftmenu = $filter(menu, {category: 'topmenu', classify: 'left'})
var currentTab = ref(leftmenu.length>0? leftmenu[0] : undefined)
var subTab = ref();
var tabConfig = $find(menu, { code: "configuration" });
var avatar = ref();
var isAdmin = ref();
const handleClick = function () {
  const target = document.getElementById("burger");
  target.classList.toggle("is-active");
  const target1 = document.getElementById("navMenu");
  target1.classList.toggle("is-active");
};
const closeMenu = function () {
  if (!document) return;
  const target = document.getElementById("burger");
  const target1 = document.getElementById("navMenu");
  if (!target) return;
  if (target.classList.contains("is-active")) {
    target.classList.remove("is-active");
    target1.classList.remove("is-active");
  }
};
function changeTab(tab, subtab) {
  if (tab.submenu && tab.submenu.length > 0 && !subtab && !tab.detail) {
    subtab = tab.submenu[0];
  }
  currentTab.value = tab;
  subTab.value = subtab;
  emit("changetab", tab, subtab);
  closeMenu();
  let query = subtab ? { tab: tab.code, subtab: subtab.code } : { tab: tab.code };
  router.push({ query: query });
}
function openProfile() {
  let modal = { component: "user/Profile", width: "1100px", height: "360px", title: $store.lang==='vi'? 'Thông tin cá nhân' : '"User profile"' };
  $store.commit("showmodal", modal);
}
let found = route.query.tab? $find(menu, {code: route.query.tab}) : undefined
if(found || currentTab.value) changeTab(found || currentTab.value)
onMounted(() => {
  if (!$store.login) return;
  avatar.value = {
    image: null,
    text: $store.login.fullname.substring(0, 1).toUpperCase(),
    size: "two",
    type: "findata",
  };
  isAdmin.value = $store.login.type__code === "admin";
});
watch(
  () => $store.login,
  (newVal, oldVal) => {
    if (!newVal) return;
    avatar.value = {
      image: null,
      text: $store.login.fullname.substring(0, 1).toUpperCase(),
      size: "two",
      type: "findata",
    };
    isAdmin.value = $store.login.type__code === "admin";
    lang.value = $store.lang;
  }
);
</script>
<style scoped>
.navbar-dropdown {
  padding-block: 0.375rem;
  overflow: hidden;
}
.navbar-dropdown > .navbar-item {
  &:hover {
    background-color: hsl(30, 48%, 82%) !important;
  }

  &:last-child {
    border-bottom: none;
  }
}
</style>