<template>
  <a-layout>
    <a-layout-header class="header">
      <div class="logo">童飞玩具购物网站</div>
      <a-menu theme="dark" mode="horizontal" :selectedKeys="[active]">
        <a-menu-item key="home" @click="go('/')">首页</a-menu-item>
        <a-menu-item key="products" @click="go('/products')">商品</a-menu-item>
        <a-menu-item key="cart" @click="go('/cart')">购物车</a-menu-item>
        <a-menu-item key="orders" @click="go('/orders')">我的订单</a-menu-item>
        <a-menu-item key="profile" @click="go('/profile')">个人中心</a-menu-item>
      </a-menu>
      <div class="actions">
        <a-button type="link" v-if="!token" @click="go('/login')">登录</a-button>
        <a-button type="link" v-if="!token" @click="go('/register')">注册</a-button>
        <a-button type="link" v-if="token" @click="logout">退出</a-button>
      </div>
    </a-layout-header>
    <a-layout-content class="content">
      <router-view />
    </a-layout-content>
    <a-layout-footer class="footer">© 2026 童飞玩具购物网站</a-layout-footer>
  </a-layout>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const token = localStorage.getItem('token')
const active = computed(() => {
  if (route.path.startsWith('/products')) return 'products'
  if (route.path.startsWith('/cart')) return 'cart'
  if (route.path.startsWith('/orders')) return 'orders'
  if (route.path.startsWith('/profile')) return 'profile'
  return 'home'
})

const go = (path) => router.push(path)
const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('role')
  router.push('/')
}
</script>

<style scoped>
.header {
  display: flex;
  align-items: center;
  gap: 24px;
}
.logo {
  color: #fff;
  font-weight: 600;
}
.actions {
  margin-left: auto;
}
.content {
  min-height: 80vh;
  padding: 24px;
  background: #f5f5f5;
}
.footer {
  text-align: center;
}
</style>
