<!-- app/components/Sidebar.vue -->
<template>
  <UDashboardSidebar
    side="left"
    :collapsed="collapsed"
    collapsible
    :min-size="5"
    :default-size="10"
    :max-size="15"
    :ui="sidebarUI"
    class="sidebar-gradient"
    dir="rtl"
    @update:collapsed="onCollapse"
  >
    <template #header>
      <nuxt-link :to="{ name: 'DashboardBasePage' }">
        <div class="flex items-center gap-3">
          <img :src="useSystemTheme.logo.value" alt="" class="h-9 w-9" />
          <div
            v-if="useSystemTheme.currentTheme.value && !collapsed"
            class="flex flex-col"
          >
            <span class="font-bold text-gray-900 dark:text-light-primary">
              {{ useSystemTheme.currentTheme.value.title || "" }}
            </span>
            <span class="text-xs text-gray-500 dark:text-gray-400">
              {{ useSystemTheme.currentTheme.value.subTitle || "" }}
            </span>
          </div>
        </div>
      </nuxt-link>
    </template>

    <template #default>
      <div class="">
        <UNavigationMenu
          dir="rtl"
          :collapsed="collapsed"
          :items="getSideBarSchema()?.topMenu || []"
          orientation="vertical"
          :ui="navigationUI"
        />
      </div>

      <div class="mt-auto pb-2 flex flex-col gap-2">
        <UNavigationMenu
          dir="rtl"
          :collapsed="collapsed"
          :items="getSideBarSchema()?.bottomMenu || []"
          orientation="vertical"
          :ui="navigationUI"
        />
      </div>
    </template>

    <template #footer="{ collapsed }">
      <div>
        <UButton
          :avatar="{ src: '' }"
          :label="collapsed ? undefined : 'مدیرفنی سامانه'"
          color="neutral"
          variant="ghost"
          class="w-full"
          :block="collapsed"
        />
        <!-- دکمه‌های باز/بسته کردن سایدبار -->
        <div class="flex justify-center items-center gap-2 mt-2">
          <!-- دکمه باز کردن -->
          <UButton
            v-if="collapsed"
            icon="stash-chevron-double-left-solid"
            color="neutral"
            variant="ghost"
            size="sm"
            class="transition-all duration-300"
            @click="toggleSidebar"
          />
          <!-- دکمه بستن -->
          <UButton
            v-else
            icon="stash-chevron-double-right-solid"
            color="neutral"
            variant="ghost"
            size="sm"
            class="transition-all duration-300 absolute left-0 -translate-y-1/2"
            @click="toggleSidebar"
          />
        </div>
      </div>
    </template>
  </UDashboardSidebar>
</template>

<script setup>
import { ref} from "vue";
import { composSystemTheme } from "@/composables/composSystemTheme";

const useSystemTheme = composSystemTheme();

const props = defineProps({
  sidebarItems: {
    type: Object,
    required: true,
    default: () => ({
      topMenu: [],
      bottomMenu: [],
    }),
  },
});

const emit = defineEmits(["update:collapsed"]);

const collapsed = ref(false);

const onCollapse = (value) => {
  collapsed.value = value;
  emit("update:collapsed", value);
};

function getSideBarSchema(){

  const config = useRuntimeConfig();
  const IS_DEVLOP_MODE = config.public.IS_DEVLOP_MODE || 1;
  

  let result = {}

  result.topMenu = props.sidebarItems?.topMenu.filter((el) => (!el.develop || el.develop == IS_DEVLOP_MODE) )
  result.bottomMenu = props.sidebarItems?.bottomMenu.filter((el) => (!el.develop || el.develop == IS_DEVLOP_MODE) )
  
  // console.log("SideBar IS_DEVLOP_MODE ", IS_DEVLOP_MODE, result);
  
  return result 
}

// تابع برای باز/بسته کردن نرم سایدبار
const toggleSidebar = () => {
  collapsed.value = !collapsed?.value;
  emit("update:collapsed", collapsed?.value);
};

const sidebarUI = {
  width: "w-72",
  collapsed: { width: "w-16" },
  wrapper:
    "z-30 h-[calc(100vh-64px)] top-16 rounded-r-2xl border-r border-gray-200/50 dark:border-dark-primary-800/50 backdrop-blur-sm transition-all duration-300",
  base: "backdrop-blur-sm transition-all duration-300 ease-out",
  header: "border-b border-gray-200/30 dark:border-dark-primary-800/30",
  footer: "border-t border-gray-200/30 dark:border-dark-primary-800/30",
};

const navigationUI = {
  base: "space-y-1",
  wrapper: (c) => (c ? "items-center" : ""),
  inactive:
    "text-dark-primary-700 dark:text-gray-300 hover:bg-gradient-to-r hover:from-blue-50/50 hover:to-transparent dark:hover:from-blue-900/20 hover:text-blue-600 dark:hover:text-blue-400",
  active:
    "bg-gradient-to-r from-blue-100 to-blue-50/30 dark:from-blue-900/30 dark:to-blue-900/10 text-blue-600 dark:text-blue-400 font-semibold shadow-sm",
  icon: { base: "transition-transform duration-300", active: "scale-110" },
};
</script>

<style scoped>
.sidebar-gradient {
  background: linear-gradient(to bottom, #ffffff, #f9fafb);
  transition: all 0.3s ease-in-out;
}
.dark .sidebar-gradient {
  background: linear-gradient(to bottom, #111827, #1f2937);
}
</style>
