<template>
  <div class="customer-layout">
    <!-- 顶部导航 -->
    <header class="customer-header">
      <div class="header-container">
        <div class="brand" @click="goHome">
          <div class="logo">🐾</div>
          <span class="brand-text">爱维宠物医院</span>
        </div>
        
        <nav class="nav-menu">
          <router-link to="/welcome" class="nav-item" :class="{ active: route.path === '/welcome' }">
            首页
          </router-link>
          <router-link to="/pets" class="nav-item" :class="{ active: route.path === '/pets' }">
            我的宠物
          </router-link>
          <router-link to="/appointments" class="nav-item" :class="{ active: route.path === '/appointments' }">
            预约挂号
          </router-link>
          <router-link to="/reports" class="nav-item" :class="{ active: route.path === '/reports' }">
            检查报告
          </router-link>
          <router-link to="/orders" class="nav-item" :class="{ active: route.path === '/orders' }">
            我的订单
          </router-link>
        </nav>
        
        <div class="user-section">
          <div class="user-avatar" @click="toggleUserMenu">
            <img v-if="auth.user?.avatar" :src="auth.user.avatar" />
            <span v-else>{{ auth.user?.username?.[0] || 'U' }}</span>
          </div>
          <div v-if="showUserMenu" class="user-dropdown">
            <div class="dropdown-item user-info">
              <div class="username">{{ auth.user?.username }}</div>
              <div class="role">客户</div>
            </div>
            <div class="dropdown-divider"></div>
            <div class="dropdown-item" @click="logout">
              <t-icon name="logout" />
              <span>退出登录</span>
            </div>
          </div>
        </div>
      </div>
    </header>
    
    <!-- 主体内容 -->
    <main class="customer-main">
      <RouterView />
    </main>
    
    <!-- 底部 -->
    <footer class="customer-footer">
      <div class="footer-container">
        <div class="footer-section">
          <h4>联系我们</h4>
          <p>📞 400-123-4567</p>
          <p>📍 XX市XX区XX路XX号</p>
          <p>🕒 09:00 - 21:00</p>
        </div>
        <div class="footer-section">
          <h4>服务项目</h4>
          <p>宠物诊疗</p>
          <p>疫苗接种</p>
          <p>宠物美容</p>
        </div>
        <div class="footer-section">
          <h4>关于我们</h4>
          <p>专业团队</p>
          <p>先进设备</p>
          <p>贴心服务</p>
        </div>
      </div>
      <div class="footer-bottom">
        <p>© 2026 爱维宠物医院 版权所有</p>
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useAuthStore } from '../store/auth';

const route = useRoute();
const router = useRouter();
const auth = useAuthStore();
const showUserMenu = ref(false);

const goHome = () => {
  router.push('/welcome');
};

const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value;
};

const logout = () => {
  auth.clear();
  router.push('/login');
};

// 点击外部关闭下拉菜单
const closeUserMenu = (e: MouseEvent) => {
  const target = e.target as HTMLElement;
  if (!target.closest('.user-section')) {
    showUserMenu.value = false;
  }
};

document.addEventListener('click', closeUserMenu);
</script>

<style scoped>
.customer-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f8fafc;
}

/* 头部导航 */
.customer-header {
  background: linear-gradient(135deg, #0d9488 0%, #14b8a6 100%);
  color: white;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  transition: opacity 0.2s;
}

.brand:hover {
  opacity: 0.9;
}

.logo {
  font-size: 28px;
}

.brand-text {
  font-size: 20px;
  font-weight: 700;
}

/* 导航菜单 */
.nav-menu {
  display: flex;
  gap: 8px;
}

.nav-item {
  padding: 8px 16px;
  border-radius: 20px;
  color: rgba(255, 255, 255, 0.9);
  text-decoration: none;
  font-size: 15px;
  font-weight: 500;
  transition: all 0.2s;
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.15);
  color: white;
}

.nav-item.active {
  background: rgba(255, 255, 255, 0.25);
  color: white;
}

/* 用户区域 */
.user-section {
  position: relative;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s;
  overflow: hidden;
}

.user-avatar:hover {
  background: rgba(255, 255, 255, 0.3);
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-avatar span {
  font-size: 16px;
  font-weight: 600;
}

/* 用户下拉菜单 */
.user-dropdown {
  position: absolute;
  top: 50px;
  right: 0;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  padding: 8px 0;
  min-width: 180px;
  z-index: 101;
}

.dropdown-item {
  padding: 12px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: background 0.2s;
}

.dropdown-item:hover {
  background: #f3f4f6;
}

.dropdown-item.user-info {
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  cursor: default;
}

.dropdown-item.user-info:hover {
  background: transparent;
}

.dropdown-item .username {
  font-weight: 600;
  color: #1f2937;
  font-size: 15px;
}

.dropdown-item .role {
  font-size: 13px;
  color: #6b7280;
}

.dropdown-divider {
  height: 1px;
  background: #e5e7eb;
  margin: 8px 0;
}

.dropdown-item:not(.user-info) {
  color: #4b5563;
  font-size: 14px;
}

/* 主体内容 */
.customer-main {
  flex: 1;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: 24px;
}

/* 底部 */
.customer-footer {
  background: #1f2937;
  color: #9ca3af;
  padding: 40px 24px 20px;
}

.footer-container {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 40px;
}

.footer-section h4 {
  color: white;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 16px;
}

.footer-section p {
  font-size: 14px;
  margin-bottom: 8px;
  line-height: 1.6;
}

.footer-bottom {
  max-width: 1200px;
  margin: 40px auto 0;
  padding-top: 20px;
  border-top: 1px solid #374151;
  text-align: center;
  font-size: 14px;
}

/* 响应式 */
@media (max-width: 768px) {
  .nav-menu {
    display: none;
  }
  
  .header-container {
    padding: 0 16px;
  }
  
  .customer-main {
    padding: 16px;
  }
  
  .footer-container {
    grid-template-columns: 1fr;
    gap: 24px;
  }
}
</style>
