<template>
  <a-layout style="min-height: 100vh; display: flex;">
    <!-- 侧边栏 - 固定 -->
    <a-layout-sider
      v-model:collapsed="collapsed"
      collapsible
      :style="{ position: 'fixed', left: 0, top: 0, bottom: 0, height: '100vh', overflow: 'auto', zIndex: 10 }"
    >
      <div class="logo">
        <h2 v-if="!collapsed">政府管理系统</h2>
        <h2 v-else>政府</h2>
      </div>
      <Sidebar />
    </a-layout-sider>

    <!-- 主内容区 - 可滚动 -->
    <a-layout
      :style="{ marginLeft: collapsed ? '80px' : '200px', width: 'calc(100% - ' + (collapsed ? '80px' : '200px') + ')' }"
      class="main-content-wrapper"
    >
      <!-- 头部 -->
      <a-layout-header
        style="background: #fff; padding: 0 16px; display: flex; justify-content: space-between; align-items: center; position: sticky; top: 0; zIndex: 5; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1)"
      >
        <div>
          <MenuUnfoldOutlined
            v-if="collapsed"
            class="trigger"
            @click="() => (collapsed = !collapsed)"
          />
          <MenuFoldOutlined
            v-else
            class="trigger"
            @click="() => (collapsed = !collapsed)"
          />
        </div>
        <div>
          <a-dropdown>
            <a class="ant-dropdown-link" @click.prevent>
              <user-outlined />
              {{ userStore.userInfo.real_name || '管理员' }}
              <down-outlined />
            </a>
            <template #overlay>
              <a-menu>
                <a-menu-item key="profile">
                  <user-outlined />
                  个人中心
                </a-menu-item>
                <a-menu-item key="logout" @click="handleLogout">
                  <logout-outlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </a-layout-header>

      <!-- 内容区 - 可滚动 -->
      <a-layout-content style="margin: 16px; padding-bottom: 24px; overflow-y: auto; max-height: calc(100vh - 136px)">
        <div :style="{ padding: '24px', background: '#fff', minHeight: '360px' }">
          <router-view />
        </div>
      </a-layout-content>

      <!-- 底部 -->
      <a-layout-footer style="text-align: center; position: sticky; bottom: 0; background: #fff">
        政府端后台管理系统 ©2024
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import {
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  UserOutlined,
  DownOutlined,
  LogoutOutlined
} from '@ant-design/icons-vue'
import { useUserStore } from '@/stores/user'
import Sidebar from '@/layout/Sidebar.vue'

const router = useRouter()
const userStore = useUserStore()
const collapsed = ref(false)

// 退出登录
const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}
</script>

<style scoped>
.logo {
  height: 64px;
  line-height: 64px;
  background: #002140;
  text-align: center;
  overflow: hidden;
}

.logo h2 {
  color: white;
  margin: 0;
  font-size: 18px;
}

.trigger {
  font-size: 18px;
  cursor: pointer;
  transition: color 0.3s;
}

.trigger:hover {
  color: #1890ff;
}

.ant-dropdown-link {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.main-content-wrapper {
  display: flex;
  flex-direction: column;
  height: 100vh;
  transition: all 0.3s ease;
}

/* 隐藏滚动条但保持滚动功能 */
.ant-layout-content::-webkit-scrollbar {
  width: 0;
  height: 0;
}

.ant-layout-content::-webkit-scrollbar-track {
  background: transparent;
}

.ant-layout-content::-webkit-scrollbar-thumb {
  background: transparent;
}

/* 兼容Firefox */
.ant-layout-content {
  scrollbar-width: none;
  -ms-overflow-style: none;
}
</style>