<template>
  <header
    class="w-full bg-white shadow flex items-center justify-center px-4 h-24 relative"
  >
    <nav class="flex justify-center space-x-4">
      <!-- 권한 기반 메뉴 -->
      <button
        v-for="menu in availableMenus"
        :key="menu.code"
        class="menu-btn"
        :class="{ active: modelValue === menu.code }"
        @click="onMenuClick(menu.code)"
      >
        {{ menu.name }}
      </button>
    </nav>

    <!-- 사용자 정보 및 드롭다운 -->
    <div class="user-menu-wrapper">
      <div class="user-info" @click="toggleDropdown">
        <span class="user-name">{{ userStore.user?.name }}</span>
        <div class="user-icon">
          <svg
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="#222"
            stroke-width="1.5"
            stroke-linecap="round"
            stroke-linejoin="round"
          >
            <circle cx="12" cy="8" r="4" />
            <path d="M4 20c0-2.5 3.5-4 8-4s8 1.5 8 4" />
          </svg>
        </div>
      </div>
      <div v-show="showDropdown" class="user-dropdown">
        <div class="dropdown-divider"></div>
        <button class="logout-btn" @click="logout">
          <svg
            width="16"
            height="16"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
          >
            <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path>
            <polyline points="16,17 21,12 16,7"></polyline>
            <line x1="21" y1="12" x2="9" y2="12"></line>
          </svg>
          로그아웃
        </button>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
const modelValue = defineModel({ type: String, required: true });

const showDropdown = ref(false);
const userStore = useUserStore();
const permissionStore = usePermissionsStore();

// 권한이 있고 메뉴에 표시할 페이지그룹들만 필터링
const availableMenus = computed(() => {
  return permissionStore.permissions.resources.pageGroups.filter(pageGroup => {
    return (
      permissionStore.hasPageGroupPermission(pageGroup.code) &&
      pageGroup.menuYn === "Y"
    );
  });
});

// 메뉴 클릭 핸들러
function onMenuClick(menu: string) {
  modelValue.value = menu;
}

function toggleDropdown() {
  showDropdown.value = !showDropdown.value;
}

function handleClickOutside(event: MouseEvent) {
  const menu = document.querySelector(".user-menu-wrapper");
  if (menu && !menu.contains(event.target as Node)) {
    showDropdown.value = false;
  }
}

async function logout() {
  showDropdown.value = false;
  userStore.logout();
}

onMounted(() => {
  window.addEventListener("click", handleClickOutside);
});
onBeforeUnmount(() => {
  window.removeEventListener("click", handleClickOutside);
});
</script>

<style scoped>
.menu-btn {
  font-size: 1.08rem;
  font-weight: 500;
  color: #222;
  background: none;
  border: none;
  padding: 0.5rem 1.5rem;
  border-radius: 6px;
  transition:
    background 0.15s,
    color 0.15s;
  cursor: pointer;
}
.menu-btn.active {
  background: none;
  color: #1976d2;
}
.menu-btn:hover {
  background: #e6f0fa;
  color: #1976d2;
}
.group:hover .group-hover\:opacity-100 {
  opacity: 1 !important;
  pointer-events: auto !important;
}
.group-hover\:pointer-events-auto {
  pointer-events: auto;
}
.user-menu-wrapper {
  position: absolute;
  right: 2rem;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
}
.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: background 0.15s;
}
.user-info:hover {
  background: #f5f5f5;
}
.user-name {
  font-size: 0.9rem;
  font-weight: 500;
  color: #333;
}
.user-icon {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.user-dropdown {
  position: absolute;
  top: 48px;
  right: 0;
  min-width: 200px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  padding: 1rem;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
.user-details {
  width: 100%;
  margin-bottom: 8px;
}
.user-email {
  font-size: 0.85rem;
  color: #666;
  margin: 0 0 4px 0;
}
.user-role {
  font-size: 0.8rem;
  color: #888;
  margin: 0;
  font-weight: 500;
}
.dropdown-divider {
  width: 100%;
  height: 1px;
  background: #e0e0e0;
  margin: 8px 0;
}
.logout-btn {
  background: none;
  border: none;
  color: #d32f2f;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  padding: 8px 0;
  width: 100%;
  text-align: left;
  border-radius: 6px;
  transition: background 0.15s;
  display: flex;
  align-items: center;
  gap: 8px;
}
.logout-btn:hover {
  background: #fbe9e7;
}
</style>
