<template>
  <div>
    <div class="topbar">
      <div class="container topbar-inner">
        <div>{{ currentDateText }}</div>
        <div class="topbar-right">
          <div class="top-search">
            <input v-model="keyword" type="text" placeholder="请输入关键字" />
            <button type="button" @click="goSearch">搜索</button>
          </div>
          <div class="topbar-links">
            <NuxtLink to="/about">网站简介</NuxtLink>
            <NuxtLink to="/contact">联系我们</NuxtLink>
          </div>
        </div>
      </div>
    </div>

    <header class="hero" aria-label="广西决策咨询网横幅" />

    <nav class="main-nav">
      <div class="container nav-inner">
        <div
          v-for="item in navItems"
          :key="item.to"
          class="nav-item-group"
        >
          <NuxtLink
            :to="item.to"
            class="nav-item"
            :class="{ active: isActive(item.to) }"
          >
            {{ item.label }}
          </NuxtLink>
          <div v-if="item.children?.length" class="nav-submenu">
            <div class="nav-submenu-title">{{ item.label }}</div>
            <NuxtLink
              v-for="child in item.children"
              :key="child.to"
              :to="child.to"
              class="nav-submenu-item"
            >
              {{ child.label }}
            </NuxtLink>
          </div>
        </div>
      </div>
    </nav>
  </div>
</template>

<script setup lang="ts">
type NavChild = {
  label: string
  to: string
}

type NavItem = {
  label: string
  to: string
  children?: NavChild[]
}

const props = defineProps<{
  activePath?: string
}>()

const route = useRoute()
const keyword = ref('')

const navItems: NavItem[] = [
  { label: '首 页', to: '/' },
  {
    label: '政策要闻',
    to: '/news',
    children: [
      { label: '党中央国务院信息', to: '/news?type=central' },
      { label: '自治区党委政府信息', to: '/news?type=region' },
      { label: '其他（厅委办）信息', to: '/news?type=department' },
      { label: '最新发布', to: '/news?type=latest' }
    ]
  },
  {
    label: '决策参考',
    to: '/reference',
    children: [
      { label: '政策原文', to: '/reference?type=policy' },
      { label: '深度解读', to: '/reference?type=analysis' },
      { label: '东盟研究', to: '/reference?type=asean' },
      { label: '数据服务', to: '/reference?type=data' }
    ]
  },
  {
    label: '决策咨询',
    to: '/consultation',
    children: [
      { label: '市县决策', to: '/consultation?type=city' },
      { label: '前沿观察', to: '/consultation?type=frontier' },
      { label: '行业资讯', to: '/consultation?type=industry' },
      { label: '企业动态', to: '/consultation?type=enterprise' }
    ]
  },
  {
    label: '专家资讯',
    to: '/expert',
    children: [
      { label: '专家视点', to: '/expert?type=view' },
      { label: '专家动态', to: '/expert?type=dynamic' },
      { label: '专家申请', to: '/expert/apply' }
    ]
  },
  {
    label: '智库观察',
    to: '/think-tank',
    children: [
      { label: '智库介绍', to: '/think-tank?type=intro' },
      { label: '智库视角', to: '/think-tank?type=view' }
    ]
  },
  { label: '建言献策', to: '/suggestions' },
  {
    label: '会员服务',
    to: '/membership',
    children: [
      { label: '企业咨询', to: '/membership?type=consult' },
      { label: '专项服务', to: '/membership?type=service' }
    ]
  },
  { label: '翰墨文谈', to: '/hanmo' },
  {
    label: '关于我们',
    to: '/about',
    children: [
      { label: '学会简介', to: '/about' },
      { label: '组织机构', to: '/about/organization' },
      { label: '学会章程', to: '/about/charter' },
      { label: '咨询服务', to: '/about/consultation' }
    ]
  }
]

const currentDateText = computed(() => {
  const now = new Date()
  return `${now.getFullYear()} 年 ${String(now.getMonth() + 1).padStart(2, '0')} 月 ${String(now.getDate()).padStart(2, '0')} 日`
})

const resolvedActivePath = computed(() => props.activePath || route.path)

function isActive(path: string) {
  const currentPath = resolvedActivePath.value
  return currentPath === path || (path !== '/' && currentPath.startsWith(`${path}/`))
}

function goSearch() {
  const value = keyword.value.trim()
  navigateTo(value ? `/articles?keyword=${encodeURIComponent(value)}` : '/articles')
}
</script>

<style scoped>
.container {
  width: min(1200px, calc(100% - 32px));
  margin: 0 auto;
}

.topbar {
  height: 34px;
  border-bottom: 1px solid #e8e8e8;
  background: #fafafa;
  color: #777;
  font-size: 12px;
}

.topbar-inner {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.topbar-links {
  display: flex;
  gap: 16px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 18px;
}

.top-search {
  display: flex;
  align-items: center;
  height: 26px;
  overflow: hidden;
  border: 1px solid #d8d8d8;
  background: #fff;
}

.top-search input {
  width: 180px;
  height: 100%;
  border: 0;
  padding: 0 8px;
  color: #555;
  font-size: 12px;
  outline: none;
}

.top-search button {
  height: 100%;
  border: 0;
  border-left: 1px solid #e6e6e6;
  background: #f5f5f5;
  color: #666;
  font-size: 12px;
  padding: 0 10px;
  cursor: pointer;
}

.topbar a {
  color: #777;
}

.hero {
  position: relative;
  height: 320px;
  overflow: hidden;
  background: url('/images/banner.png') center center / cover no-repeat;
}

.main-nav {
  background: linear-gradient(180deg, #1773c2 0%, #0e63b1 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.28);
}

.nav-inner {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  position: relative;
  z-index: 5;
}

.nav-item-group {
  position: relative;
}

.nav-item {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  color: #fff;
  font-size: 15px;
  border-right: 1px solid rgba(255, 255, 255, 0.18);
}

.nav-item-group:first-child .nav-item {
  border-left: 1px solid rgba(255, 255, 255, 0.18);
}

.nav-item.active,
.nav-item:hover,
.nav-item-group:hover .nav-item {
  background: rgba(6, 38, 78, 0.22);
}

.nav-submenu {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 100%;
  padding: 0;
  background: rgba(20, 90, 150, 0.96);
  box-shadow: 0 10px 20px rgba(13, 45, 82, 0.16);
  opacity: 0;
  visibility: hidden;
  transform: translateY(6px);
  transition: all 0.18s ease;
}

.nav-item-group:hover .nav-submenu {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.nav-submenu-title {
  padding: 12px 16px;
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  background: rgba(8, 56, 97, 0.45);
}

.nav-submenu-item {
  display: block;
  padding: 11px 16px;
  color: #fff;
  font-size: 13px;
  line-height: 1.5;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  white-space: nowrap;
}

.nav-submenu-item:hover {
  background: rgba(8, 56, 97, 0.44);
}

@media (max-width: 1100px) {
  .nav-inner {
    grid-template-columns: repeat(5, 1fr);
  }

  .nav-submenu {
    min-width: 180px;
  }
}

@media (max-width: 768px) {
  .topbar {
    height: auto;
    padding: 8px 0;
  }

  .topbar-inner,
  .topbar-right {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .top-search input {
    width: 150px;
  }

  .container {
    width: min(100%, calc(100% - 24px));
  }

  .hero {
    height: 220px;
  }

  .nav-inner {
    grid-template-columns: repeat(2, 1fr);
  }

  .nav-submenu {
    display: none;
  }
}
</style>
