<template>
  <div v-if="isLoggedIn">
    <!-- 移动端布局 -->
    <div v-if="isMobile" class="mobile-layout">
      <MobileNav ref="mobileNavRef" />
      <div class="mobile-content">
        <router-view />
      </div>
    </div>
    
    <!-- 桌面端布局 -->
    <div v-else class="desktop-layout">
      <!-- 固定头部 -->
      <header class="header">
        <div class="header-content">
          <div class="logo">
            <a-button 
              type="text" 
              @click="settingsStore.toggleSidebar"
              class="menu-toggle"
            >
              <menu-unfold-outlined v-if="sidebarCollapsed" />
              <menu-fold-outlined v-else />
            </a-button>
            <span class="logo-text">银行管理后台系统</span>
          </div>
          <div class="user-info">
            <a-dropdown>
              <a-button type="text" class="user-button">
                <user-outlined />
                {{ userData?.real_name || userData?.username }}
                <down-outlined />
              </a-button>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="profile" @click="goToProfile">
                    <user-outlined />
                    个人中心
                  </a-menu-item>
                  <a-menu-item key="settings" @click="goToSettings">
                    <setting-outlined />
                    系统设置
                  </a-menu-item>
                  <a-menu-divider />
                  <a-menu-item key="logout" @click="handleLogout">
                    <logout-outlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
        </div>
      </header>
      
      <!-- 侧边栏 -->
      <aside class="sidebar" :class="{ collapsed: sidebarCollapsed }">
        <DynamicMenu :collapsed="sidebarCollapsed" />
      </aside>
      
      <!-- 主内容区域 -->
      <main class="main-content">
        <div class="content-wrapper">
          <router-view />
        </div>
      </main>
      
      <!-- 页脚 -->
      <footer class="footer">
        银行管理后台系统 ©2025
      </footer>
    </div>
  </div>
  <div v-else>
    <router-view />
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import DynamicMenu from './components/DynamicMenu.vue'
import MobileNav from './components/MobileNav.vue'
import { useUserStore, useSettingsStore } from './stores'
import { 
  MenuFoldOutlined, 
  MenuUnfoldOutlined,
  UserOutlined,
  DownOutlined,
  SettingOutlined,
  LogoutOutlined
} from '@ant-design/icons-vue'

// 使用Pinia状态管理
const userStore = useUserStore()
const settingsStore = useSettingsStore()
const router = useRouter()

// 移动端导航引用
const mobileNavRef = ref()

// 响应式检测
const isMobile = ref(false)

// 检测屏幕尺寸
const checkScreenSize = () => {
  isMobile.value = window.innerWidth <= 768
}

// 计算属性
const isLoggedIn = computed(() => userStore.isLoggedIn)
const userData = computed(() => userStore.userData)
const sidebarCollapsed = computed(() => settingsStore.sidebarCollapsed)

// 监听多标签页登录状态同步
const handleStorageChange = (event) => {
  if (event.key === 'bank_token' || event.key === 'bank_user') {
    userStore.checkLoginStatus()
  }
}

// 登出处理
const handleLogout = async () => {
  try {
    await userStore.logout()
    router.push('/login')
  } catch (error) {
    console.error('登出失败:', error)
  }
}

// 跳转到个人中心
const goToProfile = () => {
  router.push('/profile')
}

// 跳转到系统设置
const goToSettings = () => {
  router.push('/settings')
}

onMounted(() => {
  userStore.checkLoginStatus()
  checkScreenSize()
  window.addEventListener('storage', handleStorageChange)
  window.addEventListener('resize', checkScreenSize)
})

onUnmounted(() => {
  window.removeEventListener('storage', handleStorageChange)
  window.removeEventListener('resize', checkScreenSize)
})
</script>

<style scoped>
/* 全局重置 */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
}

#app {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

/* 桌面端布局样式 */
.desktop-layout {
  display: grid;
  grid-template-areas: 
    "header header"
    "sidebar main";
  grid-template-columns: 200px 1fr;
  grid-template-rows: 64px 1fr;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.desktop-layout.collapsed {
  grid-template-columns: 80px 1fr;
}

/* 头部样式 - 完全修复 */
.header {
  grid-area: header;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1001;
  background: #001529;
  color: white;
  height: 64px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  width: 100vw;
  margin: 0;
  padding: 0;
  border: none;
  display: flex;
  align-items: center;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 100%;
  padding: 0 24px;
  margin: 0;
}

.logo {
  display: flex;
  align-items: center;
  height: 100%;
  margin: 0;
  padding: 0;
}

.menu-toggle {
  color: white !important;
  margin-right: 8px;
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0 8px;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  line-height: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  margin: 0;
  padding: 0;
}

.user-info {
  display: flex;
  align-items: center;
  height: 100%;
  margin: 0;
  padding: 0;
}

.user-button {
  color: white !important;
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0 8px;
}

/* 侧边栏样式 - 完全修复 */
.sidebar {
  grid-area: sidebar;
  position: fixed;
  top: 64px;
  left: 0;
  width: 200px;
  height: calc(100vh - 64px);
  background: #001529;
  z-index: 1000;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0;
  margin: 0;
  border: none;
  transition: width 0.2s;
}

.sidebar.collapsed {
  width: 80px;
}

.sidebar::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-track {
  background: #001529;
}

.sidebar::-webkit-scrollbar-thumb {
  background: #1890ff;
  border-radius: 3px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
  background: #40a9ff;
}

/* 主内容区域样式 - 完全修复 */
.main-content {
  grid-area: main;
  margin-left: 200px;
  height: calc(100vh - 64px);
  overflow-y: auto;
  overflow-x: hidden;
  background: #f0f2f5;
  padding: 0;
  margin: 0;
  transition: margin-left 0.2s;
}

.desktop-layout.collapsed .main-content {
  margin-left: 80px;
}

.content-wrapper {
  padding: 24px;
  min-height: calc(100vh - 64px - 70px);
  background: #f0f2f5;
  margin: 0;
}

/* 页脚样式 - 完全修复 */
.footer {
  position: fixed;
  bottom: 0;
  left: 200px;
  right: 0;
  height: 70px;
  line-height: 70px;
  text-align: center;
  background: #fff;
  border-top: 1px solid #f0f0f0;
  color: #666;
  z-index: 999;
  transition: left 0.2s;
}

.desktop-layout.collapsed .footer {
  left: 80px;
}

/* 侧边栏折叠时的样式 */
.desktop-layout :deep(.ant-layout-sider-collapsed) {
  width: 80px !important;
  min-width: 80px !important;
  max-width: 80px !important;
  flex: 0 0 80px !important;
}

.desktop-layout :deep(.ant-layout-sider-collapsed) + .content-layout {
  margin-left: 80px;
  transition: margin-left 0.2s;
}

/* 响应式支持 */
@media (max-width: 768px) {
  .content-layout {
    margin-left: 0 !important;
  }
  
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s;
  }
  
  .sidebar.ant-layout-sider-collapsed {
    transform: translateX(0);
  }
}

/* 内容区域滚动条样式 */
.main-content::-webkit-scrollbar {
  width: 8px;
}

.main-content::-webkit-scrollbar-track {
  background: #f0f0f0;
  border-radius: 4px;
}

.main-content::-webkit-scrollbar-thumb {
  background: #d9d9d9;
  border-radius: 4px;
}

.main-content::-webkit-scrollbar-thumb:hover {
  background: #bfbfbf;
}

/* 移动端布局样式 */
.mobile-layout {
  min-height: 100vh;
  background: #f0f2f5;
}

.mobile-content {
  padding: 12px;
  padding-top: 68px; /* 为固定头部留出空间 */
  min-height: calc(100vh - 56px);
}

/* 移动端页面内容优化 */
.mobile-layout :deep(.page-header) {
  flex-direction: column;
  gap: 12px;
  align-items: stretch;
}

.mobile-layout :deep(.search-area) {
  flex-direction: column;
  gap: 8px;
}

.mobile-layout :deep(.search-input) {
  width: 100%;
}

.mobile-layout :deep(.search-buttons) {
  display: flex;
  gap: 8px;
}

.mobile-layout :deep(.search-buttons .ant-btn) {
  flex: 1;
  height: 40px;
}

/* 移动端表格优化 */
.mobile-layout :deep(.ant-table-wrapper) {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.mobile-layout :deep(.ant-table) {
  min-width: 600px;
}

.mobile-layout :deep(.ant-table-thead > tr > th) {
  padding: 8px 4px;
  font-size: 12px;
  white-space: nowrap;
}

.mobile-layout :deep(.ant-table-tbody > tr > td) {
  padding: 8px 4px;
  font-size: 12px;
}

/* 移动端模态框优化 */
.mobile-layout :deep(.ant-modal) {
  margin: 0 !important;
  width: 100vw !important;
  max-width: 100vw !important;
  top: 0 !important;
}

.mobile-layout :deep(.ant-modal-content) {
  border-radius: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.mobile-layout :deep(.ant-modal-body) {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.mobile-layout :deep(.ant-modal-footer) {
  border-top: 1px solid #f0f0f0;
  padding: 12px 16px;
}

/* 移动端卡片优化 */
.mobile-layout :deep(.ant-card) {
  margin-bottom: 12px;
  border-radius: 8px;
}

.mobile-layout :deep(.ant-card-body) {
  padding: 12px;
}

/* 移动端按钮优化 */
.mobile-layout :deep(.ant-btn) {
  min-height: 40px;
  border-radius: 6px;
}

.mobile-layout :deep(.ant-space) {
  width: 100%;
}

.mobile-layout :deep(.ant-space-item) {
  flex: 1;
}

.mobile-layout :deep(.ant-space-item .ant-btn) {
  width: 100%;
}
</style>