<template>
  <a-layout class="layout">
    <a-layout-sider v-model:collapsed="collapsed" :trigger="null" collapsible>
      <div class="logo" />
      <a-menu
        v-model:selectedKeys="selectedKeys"
        v-model:openKeys="openKeys"
        theme="dark"
        mode="inline"
      >
        <a-sub-menu key="user">
          <template #icon>
            <user-outlined />
          </template>
          <template #title>用户管理</template>
          <a-menu-item key="user-list">
            <router-link to="/user/list">用户列表</router-link>
          </a-menu-item>
        </a-sub-menu>
        
        <a-sub-menu key="community">
          <template #icon>
            <home-outlined />
          </template>
          <template #title>小区管理</template>
          <a-menu-item key="community-list">
            <router-link to="/community/list">小区列表</router-link>
          </a-menu-item>
          <a-menu-item key="building-list">
            <router-link to="/community/building">楼栋列表</router-link>
          </a-menu-item>
          <a-menu-item key="station-list">
            <router-link to="/community/station">驿站列表</router-link>
          </a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <div class="header-right">
          <a-dropdown>
            <a class="ant-dropdown-link" @click.prevent>
              {{ userInfo.username }}
              <down-outlined />
            </a>
            <template #overlay>
              <a-menu>
                <a-menu-item @click="handleLogout">
                  <logout-outlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
        <menu-unfold-outlined
          v-if="collapsed"
          class="trigger"
          @click="() => (collapsed = !collapsed)"
        />
        <menu-fold-outlined
          v-else
          class="trigger"
          @click="() => (collapsed = !collapsed)"
        />
      </a-layout-header>
      <a-layout-content
        :style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: '280px' }"
      >
        <router-view></router-view>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script>
import { ref, defineComponent } from 'vue'
import {
  UserOutlined,
  HomeOutlined,
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  DownOutlined,
  LogoutOutlined,
} from '@ant-design/icons-vue'
import { useRouter } from 'vue-router'

export default defineComponent({
  name: 'BasicLayout',
  components: {
    UserOutlined,
    HomeOutlined,
    MenuUnfoldOutlined,
    MenuFoldOutlined,
    DownOutlined,
    LogoutOutlined,
  },
  setup() {
    const router = useRouter()
    const collapsed = ref(false)
    const selectedKeys = ref(['user-list'])
    const openKeys = ref(['user', 'community'])
    
    const userInfo = ref(JSON.parse(localStorage.getItem('userInfo') || '{}'))

    window.addEventListener('storage', (e) => {
      if (e.key === 'userInfo') {
        userInfo.value = JSON.parse(e.newValue || '{}')
      }
    })

    const handleLogout = () => {
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      router.push('/login')
    }

    return {
      collapsed,
      selectedKeys,
      openKeys,
      userInfo,
      handleLogout
    }
  },
})
</script>

<style scoped>
.layout {
  min-height: 100vh;
}
.trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}
.trigger:hover {
  color: #1890ff;
}
.logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px;
}
.header-right {
  float: right;
  margin-right: 24px;
  line-height: 64px;
}
.ant-dropdown-link {
  color: rgba(0, 0, 0, 0.85);
  cursor: pointer;
  padding: 0 8px;
}
</style> 