<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useCartStore } from '@/stores/cart'
import {
  Search, ShoppingCart, User, List, Phone, HomeFilled, Shop,
  Ticket, Bell, ArrowRight, Grid, Timer, Star, Location, Document
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const cartStore = useCartStore()

const searchKeyword = ref('')
const showCategoryMenu = ref(false)
const mobileMenuOpen = ref(false)

onMounted(() => {
  cartStore.fetchCart()
})

function onSearch() {
  if (searchKeyword.value.trim()) {
    router.push({ name: 'search', query: { q: searchKeyword.value } })
  }
}

function goLogin() {
  router.push('/login')
}

function goUser() {
  if (!userStore.isLoggedIn) {
    goLogin()
    return
  }
  router.push('/user')
}

function logout() {
  userStore.clearAuth()
  router.push('/')
}

const bottomNavItems = [
  { label: '首页', icon: HomeFilled, path: '/' },
  { label: '分类', icon: Grid, path: '/category/1' },
  { label: '秒杀', icon: Timer, path: '/seckill' },
  { label: '购物车', icon: ShoppingCart, path: '/cart' },
  { label: '我的', icon: User, path: '/user' },
]

function isActive(path: string) {
  if (path === '/') return route.path === '/'
  return route.path.startsWith(path)
}
</script>

<template>
  <div class="main-layout">
    <!-- Top bar -->
    <div class="top-bar">
      <div class="container top-bar-inner">
        <div class="top-left">
          <span class="welcome">欢迎光临零食商城 🍿</span>
        </div>
        <div class="top-right">
          <template v-if="!userStore.isLoggedIn">
            <a @click="goLogin" class="link">登录</a>
            <span class="divider">|</span>
            <a @click="router.push('/register')" class="link">注册</a>
          </template>
          <template v-else>
            <span class="nickname">你好，{{ userStore.user?.nickname || userStore.user?.username }}</span>
            <a @click="goUser" class="link">个人中心</a>
            <span class="divider">|</span>
            <a @click="logout" class="link">退出</a>
          </template>
          <span class="divider">|</span>
          <a @click="router.push('/notice')" class="link">
            <el-icon size="14"><Bell /></el-icon> 公告
          </a>
          <span class="hotline">
            <el-icon size="14"><Phone /></el-icon> 400-888-8888
          </span>
        </div>
      </div>
    </div>

    <!-- Sticky Header -->
    <header class="site-header">
      <div class="container header-inner">
        <div class="logo" @click="router.push('/')">
          <div class="logo-icon">
            <el-icon :size="28"><Shop /></el-icon>
          </div>
          <span class="logo-text">零食商城</span>
        </div>

        <div class="search-box">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索美味零食..."
            size="large"
            class="search-input"
            @keyup.enter="onSearch"
          >
            <template #append>
              <el-button :icon="Search" class="search-btn" @click="onSearch" />
            </template>
          </el-input>
        </div>

        <div class="header-actions">
          <el-badge
            :value="cartStore.totalCount"
            :hidden="cartStore.totalCount === 0"
            class="action-badge"
            type="danger"
          >
            <div class="action-btn" @click="router.push('/cart')">
              <el-icon :size="20"><ShoppingCart /></el-icon>
              <span>购物车</span>
            </div>
          </el-badge>
          <div class="action-btn" @click="goUser">
            <el-icon :size="20"><User /></el-icon>
            <span>我的</span>
          </div>
        </div>

        <!-- Mobile hamburger -->
        <div class="mobile-toggle" @click="mobileMenuOpen = !mobileMenuOpen">
          <el-icon :size="24"><List /></el-icon>
        </div>
      </div>

      <!-- Mobile menu -->
      <transition name="slide-down">
        <div v-if="mobileMenuOpen" class="mobile-menu">
          <a v-for="item in bottomNavItems" :key="item.path" class="mobile-link" @click="router.push(item.path); mobileMenuOpen = false">
            <el-icon><component :is="item.icon" /></el-icon>
            {{ item.label }}
          </a>
        </div>
      </transition>
    </header>

    <!-- Nav -->
    <nav class="main-nav">
      <div class="container nav-inner">
        <div
          class="category-trigger"
          @mouseenter="showCategoryMenu = true"
          @mouseleave="showCategoryMenu = false"
        >
          <el-icon><List /></el-icon>
          <span>全部商品分类</span>
          <el-icon><ArrowRight /></el-icon>
          <div v-show="showCategoryMenu" class="category-dropdown">
            <div class="category-list">
              <div
                v-for="c in 6" :key="c"
                class="category-item"
                @click="router.push({ name: 'category', params: { id: c } })"
              >
                <span>分类 {{ c }}</span>
                <el-icon><ArrowRight /></el-icon>
              </div>
            </div>
          </div>
        </div>
        <div class="nav-links">
          <router-link to="/" exact-active-class="active">
            <el-icon><HomeFilled /></el-icon> 首页
          </router-link>
          <router-link to="/seckill" active-class="active">
            <el-icon><Ticket /></el-icon> 限时秒杀
          </router-link>
          <router-link to="/category/1" active-class="active">坚果炒货</router-link>
          <router-link to="/category/2" active-class="active">肉干肉脯</router-link>
          <router-link to="/category/3" active-class="active">糖果巧克力</router-link>
          <router-link to="/category/4" active-class="active">饼干糕点</router-link>
        </div>
      </div>
    </nav>

    <!-- Main content -->
    <main class="main-content">
      <router-view />
    </main>

    <!-- Footer -->
    <footer class="site-footer">
      <div class="container footer-inner">
        <div class="footer-brand">
          <div class="logo" @click="router.push('/')">
            <div class="logo-icon">
              <el-icon :size="24"><Shop /></el-icon>
            </div>
            <span class="logo-text">零食商城</span>
          </div>
          <p class="brand-desc">精选全球美味零食，让每一口都充满幸福感。</p>
          <div class="social-links">
            <div class="social-icon"><el-icon><Phone /></el-icon></div>
            <div class="social-icon"><el-icon><Bell /></el-icon></div>
            <div class="social-icon"><el-icon><Star /></el-icon></div>
          </div>
        </div>
        <div class="footer-links">
          <div class="link-group">
            <h4>购物指南</h4>
            <a>购物流程</a>
            <a>会员介绍</a>
            <a>常见问题</a>
          </div>
          <div class="link-group">
            <h4>配送方式</h4>
            <a>上门自提</a>
            <a>配送服务</a>
            <a>配送费标准</a>
          </div>
          <div class="link-group">
            <h4>支付方式</h4>
            <a>货到付款</a>
            <a>在线支付</a>
            <a>分期付款</a>
          </div>
          <div class="link-group">
            <h4>售后服务</h4>
            <a>售后政策</a>
            <a>退款说明</a>
            <a>取消订单</a>
          </div>
        </div>
        <div class="footer-bottom">
          <p>零食商城 版权所有 © 2026</p>
        </div>
      </div>
    </footer>

    <!-- Mobile Bottom Nav -->
    <nav class="mobile-bottom-nav">
      <a
        v-for="item in bottomNavItems"
        :key="item.path"
        class="bottom-nav-item"
        :class="{ active: isActive(item.path) }"
        @click="router.push(item.path)"
      >
        <el-icon :size="22"><component :is="item.icon" /></el-icon>
        <span>{{ item.label }}</span>
      </a>
    </nav>
  </div>
</template>

<style scoped>
.main-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-bottom: 0;
}

/* ===== Top Bar ===== */
.top-bar {
  background: linear-gradient(90deg, #FF6B35 0%, #FF8F5D 100%);
  color: rgba(255, 255, 255, 0.95);
  font-size: 13px;
}
.top-bar-inner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
}
.welcome {
  font-weight: 500;
  letter-spacing: 0.5px;
}
.top-right {
  display: flex;
  align-items: center;
  gap: 4px;
}
.top-right .link {
  color: rgba(255, 255, 255, 0.95);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background var(--transition-fast);
  display: flex;
  align-items: center;
  gap: 4px;
}
.top-right .link:hover {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
}
.top-right .nickname {
  padding: 0 8px;
  font-weight: 500;
}
.divider {
  opacity: 0.4;
  padding: 0 2px;
}
.hotline {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: 8px;
  font-weight: 600;
}

/* ===== Header ===== */
.site-header {
  position: sticky;
  top: 0;
  z-index: 200;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--color-border-light);
}
.header-inner {
  display: flex;
  align-items: center;
  gap: 32px;
  padding-top: 14px;
  padding-bottom: 14px;
}
.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  flex-shrink: 0;
}
.logo-icon {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  background: var(--color-primary-gradient);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  box-shadow: var(--shadow-md);
}
.logo-text {
  font-family: 'Fredoka', sans-serif;
  font-size: 26px;
  font-weight: 700;
  background: var(--color-primary-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.search-box {
  flex: 1;
  max-width: 520px;
}
.search-input :deep(.el-input__wrapper) {
  border-radius: var(--radius-full) 0 0 var(--radius-full);
  box-shadow: 0 0 0 1px var(--color-border) inset;
  padding-left: 18px;
}
.search-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--color-primary) inset, var(--shadow-glow) !important;
}
.search-input :deep(.el-input-group__append) {
  border-radius: 0 var(--radius-full) var(--radius-full) 0;
  background: var(--color-primary-gradient);
  border-color: var(--color-primary);
  color: #fff;
  padding: 0 20px;
}
.search-btn {
  color: #fff !important;
  background: transparent !important;
  border: none !important;
}
.header-actions {
  display: flex;
  gap: 12px;
}
.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 14px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  color: var(--color-text-secondary);
  font-size: 12px;
}
.action-btn:hover {
  background: var(--color-primary-light);
  color: var(--color-primary);
}
.action-badge :deep(.el-badge__content) {
  border: none !important;
  font-size: 10px;
  padding: 0 5px;
  height: 18px;
  line-height: 18px;
}
.mobile-toggle {
  display: none;
  padding: 8px;
  cursor: pointer;
  color: var(--color-text-secondary);
}
.mobile-menu {
  display: none;
  padding: 12px var(--space-md);
  background: #fff;
  border-top: 1px solid var(--color-border-light);
  gap: 4px;
}
.mobile-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  color: var(--color-text);
  cursor: pointer;
  transition: background var(--transition-fast);
}
.mobile-link:hover {
  background: var(--color-primary-light);
  color: var(--color-primary);
}

/* ===== Nav ===== */
.main-nav {
  background: #fff;
  border-bottom: 1px solid var(--color-border-light);
}
.nav-inner {
  display: flex;
  align-items: center;
  height: 52px;
  gap: 8px;
}
.category-trigger {
  position: relative;
  background: var(--color-primary-gradient);
  color: #fff;
  padding: 0 24px;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  cursor: pointer;
  width: 220px;
  border-radius: 0;
  transition: filter var(--transition-fast);
}
.category-trigger:hover {
  filter: brightness(1.05);
}
.category-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  width: 220px;
  background: #fff;
  box-shadow: var(--shadow-xl);
  border-radius: 0 0 var(--radius-md) var(--radius-md);
  z-index: 100;
  overflow: hidden;
}
.category-list {
  padding: 8px 0;
}
.category-item {
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--color-text);
  cursor: pointer;
  transition: all var(--transition-fast);
  font-size: 14px;
}
.category-item:hover {
  background: var(--color-primary-light);
  color: var(--color-primary);
  font-weight: 600;
}
.nav-links {
  display: flex;
  gap: 6px;
}
.nav-links a {
  color: var(--color-text-secondary);
  padding: 0 18px;
  height: 36px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 500;
  border-radius: var(--radius-full);
  transition: all var(--transition-fast);
}
.nav-links a:hover {
  color: var(--color-primary);
  background: var(--color-primary-light);
}
.nav-links a.active {
  color: #fff;
  background: var(--color-primary-gradient);
  box-shadow: var(--shadow-sm);
}

/* ===== Main ===== */
.main-content {
  flex: 1;
  background: var(--color-background);
}

/* ===== Footer ===== */
.site-footer {
  background: #1F2937;
  color: #9CA3AF;
  padding: 60px 0 24px;
}
.footer-inner {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 48px;
  align-items: start;
}
.footer-brand .logo {
  margin-bottom: 16px;
}
.footer-brand .logo-icon {
  background: var(--color-primary-gradient);
}
.footer-brand .logo-text {
  color: #fff;
  -webkit-text-fill-color: #fff;
}
.brand-desc {
  line-height: 1.7;
  max-width: 280px;
  margin-bottom: 20px;
  color: #9CA3AF;
}
.social-links {
  display: flex;
  gap: 12px;
}
.social-icon {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  background: rgba(255, 255, 255, 0.08);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #D1D5DB;
  cursor: pointer;
  transition: all var(--transition-fast);
}
.social-icon:hover {
  background: var(--color-primary);
  color: #fff;
  transform: translateY(-2px);
}
.footer-links {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 32px;
}
.link-group h4 {
  color: #fff;
  margin-bottom: 16px;
  font-size: 15px;
  font-weight: 600;
}
.link-group a {
  display: block;
  color: #9CA3AF;
  margin-bottom: 10px;
  font-size: 14px;
  cursor: pointer;
  transition: color var(--transition-fast);
}
.link-group a:hover {
  color: var(--color-primary);
}
.footer-bottom {
  grid-column: 1 / -1;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  padding-top: 24px;
  text-align: center;
  font-size: 13px;
  color: #6B7280;
}

/* ===== Mobile Bottom Nav ===== */
.mobile-bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 64px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--color-border-light);
  z-index: 300;
  justify-content: space-around;
  align-items: center;
}
.bottom-nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  color: var(--color-text-muted);
  font-size: 11px;
  padding: 6px 12px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
}
.bottom-nav-item.active {
  color: var(--color-primary);
}
.bottom-nav-item:active {
  transform: scale(0.95);
}

/* ===== Transitions ===== */
.slide-down-enter-active,
.slide-down-leave-active {
  transition: all 250ms ease;
}
.slide-down-enter-from,
.slide-down-leave-to {
  opacity: 0;
  transform: translateY(-8px);
}

/* ===== Responsive ===== */
@media (max-width: 1024px) {
  .footer-inner {
    grid-template-columns: 1fr;
    gap: 36px;
  }
  .footer-links {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .top-bar {
    display: none;
  }
  .header-inner {
    flex-wrap: wrap;
    gap: 10px;
    padding-top: 10px;
    padding-bottom: 10px;
  }
  .search-box {
    order: 3;
    max-width: 100%;
    width: 100%;
  }
  .header-actions {
    display: none;
  }
  .mobile-toggle {
    display: block;
    margin-left: auto;
  }
  .mobile-menu {
    display: flex;
    flex-direction: column;
  }
  .main-nav {
    display: none;
  }
  .site-footer {
    padding-bottom: 88px;
  }
  .footer-links {
    grid-template-columns: repeat(2, 1fr);
    gap: 24px;
  }
  .mobile-bottom-nav {
    display: flex;
  }
  .main-layout {
    padding-bottom: 64px;
  }
}
</style>
