<template>
  <div v-if="isLoggedIn">
    <!-- 移动端布局 -->
    <div v-if="isMobile" class="mobile-layout">
      <MobileNav ref="mobileNavRef" />
      <div class="mobile-content">
        <router-view />
      </div>
    </div>
    
    <!-- 桌面端布局 -->
    <a-layout v-else style="min-height: 100vh">
      <a-layout-header class="header">
        <div class="logo">
          <a-button 
            type="text" 
            @click="settingsStore.toggleSidebar"
            style="color: white; margin-right: 8px;"
          >
            <menu-unfold-outlined v-if="sidebarCollapsed" />
            <menu-fold-outlined v-else />
          </a-button>
          宁夏智慧养殖监管平台
        </div>
        <div class="user-info">
          <a href="https://datav.ningmuyun.com/" target="_blank" style="color: white; text-decoration: none; margin-right: 16px;">大屏展示</a>
          <span>欢迎, {{ userData?.username }}</span>
          <a-button type="link" @click="handleLogout" style="color: white;">退出</a-button>
        </div>
      </a-layout-header>
      
      <a-layout>
        <a-layout-sider 
          width="200" 
          style="background: #001529"
          :collapsed="sidebarCollapsed"
          collapsible
        >
          <DynamicMenu :collapsed="sidebarCollapsed" />
        </a-layout-sider>
        
        <a-layout style="padding: 0 24px 24px">
          <a-layout-content
            :style="{ background: '#fff', padding: '24px', margin: '16px 0' }"
          >
            <router-view />
          </a-layout-content>
          
          <a-layout-footer style="text-align: center">
            宁夏智慧养殖监管平台 ©2025
          </a-layout-footer>
        </a-layout>
      </a-layout>
    </a-layout>
  </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 } 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)

// 注意：路由守卫已移至router/index.js

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

// 登出处理
const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}

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

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

<style scoped>
/* 桌面端样式 */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #001529;
  color: white;
  padding: 0 24px;
}

.logo {
  font-size: 18px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

/* 移动端布局样式 */
.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%;
}

/* 响应式调试 */
@media (max-width: 768px) {
  .debug-indicator {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: rgba(24, 144, 255, 0.9);
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    z-index: 9999;
  }
}
</style>