<template>
  <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
    <div class="container-fluid">
      <NuxtLink class="navbar-brand" to="/">
        <i class="bi bi-clipboard-check me-2"></i>
        주간업무보고
      </NuxtLink>

      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav me-auto">
          <!-- 동적 메뉴 렌더링 -->
          <template v-for="menu in userMenus" :key="menu.menuId">
            <!-- 자식이 없는 메뉴 -->
            <li class="nav-item" v-if="!menu.children || menu.children.length === 0">
              <NuxtLink class="nav-link" :to="menu.menuPath || '/'" active-class="active">
                <i :class="['bi', menu.menuIcon, 'me-1']" v-if="menu.menuIcon"></i>
                {{ menu.menuName }}
              </NuxtLink>
            </li>
            <!-- 자식이 있는 메뉴 (드롭다운) -->
            <li class="nav-item dropdown" v-else>
              <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">
                <i :class="['bi', menu.menuIcon, 'me-1']" v-if="menu.menuIcon"></i>
                {{ menu.menuName }}
              </a>
              <ul class="dropdown-menu">
                <li v-for="child in menu.children" :key="child.menuId">
                  <NuxtLink class="dropdown-item" :to="child.menuPath || '/'">
                    <i :class="['bi', child.menuIcon, 'me-2']" v-if="child.menuIcon"></i>
                    {{ child.menuName }}
                  </NuxtLink>
                </li>
              </ul>
            </li>
          </template>
        </ul>

        <!-- 사용자 정보 -->
        <div class="d-flex align-items-center" v-if="currentUser">
          <NuxtLink to="/mypage" class="text-white text-decoration-none me-3 user-link">
            <i class="bi bi-person-circle me-1"></i>
            {{ currentUser.employeeName }}
          </NuxtLink>
          <button class="btn btn-outline-light btn-sm" @click="handleLogout">
            <i class="bi bi-box-arrow-right"></i> 로그아웃
          </button>
        </div>
      </div>
    </div>
  </nav>
</template>

<script setup lang="ts">
const { currentUser, userMenus, logout } = useAuth()
const router = useRouter()

async function handleLogout() {
  await logout()
  router.push('/login')
}
</script>

<style scoped>
.navbar-brand {
  font-weight: 600;
}
.nav-link {
  padding: 0.5rem 1rem;
}
.nav-link.active {
  font-weight: 500;
}
.user-link {
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 0.25rem;
  transition: background-color 0.2s;
}
.user-link:hover {
  background-color: rgba(255, 255, 255, 0.1);
}
</style>
