<template>
  <a-config-provider :locale="locale">
    <a-layout class="app-layout">
      <!-- 头部 -->
      <a-layout-header class="app-header">
        <div class="header-content">
          <a-row align="middle" justify="space-between">
            <a-col>
              <a-space align="center">
                <!-- <a-icon type="gateway" size="large" /> -->
                <div class="logo"></div>
              </a-space>
            </a-col>
            <a-col>
              <a-space size="middle">
                <span class="header-status-item">
                  <span :class="['status-badge', dataStore.serialConnected ? 'on' : 'off']" />
                  <span>串口 {{ dataStore.serialConnected ? '已连接' : '未连接' }}</span>
                </span>
                <span class="header-status-item">
                  <span :class="['status-badge', dataStore.mqttConnected ? 'on' : 'off']" />
                  <span>MQTT {{ dataStore.mqttConnected ? '已连接' : '未连接' }}</span>
                </span>
              </a-space>
            </a-col>
          </a-row>
        </div>
      </a-layout-header>

      <a-layout>
        <!-- 左侧菜单 -->
        <a-layout-sider
          class="app-sider"
          width="256"
          :collapsible="true"
          v-model:collapsed="collapsed"
          :trigger="null"
          breakpoint="lg"
          collapsed-width="80"
        >
          <div class="app-title">配线架配置系统</div>
          <a-menu
            mode="inline"
            :selected-keys="[selectedKey]"
            :collapsed="collapsed"
            @click="handleMenuClick"
            theme="dark"
            class="side-menu"
          >
            <a-menu-item key="/real-time-status" icon="<area-chart />">
              <router-link to="/real-time-status">实时状态</router-link>
            </a-menu-item>
            <a-menu-item key="/system-config" icon="<setting />">
              <router-link to="/system-config">系统配置</router-link>
            </a-menu-item>
            <a-menu-item key="/led-debug" icon="<bulb />">
              <router-link to="/led-debug">LED调试</router-link>
            </a-menu-item>
            <a-menu-item key="/port-status" icon="<api />">
              <router-link to="/port-status">端口状态</router-link>
            </a-menu-item>
            <a-menu-item key="/panel-status" icon="<appstore />">
              <router-link to="/panel-status">面板状态</router-link>
            </a-menu-item>
            <a-menu-item key="/alarm-status" icon="<alert />">
              <router-link to="/alarm-status">告警显示</router-link>
            </a-menu-item>
            <a-menu-item key="/env-sensor" icon="<dashboard />">
              <router-link to="/env-sensor">环境传感器</router-link>
            </a-menu-item>
            <a-menu-item key="/jumper-status" icon="<link-outlined />">
              <router-link to="/jumper-status">跳线器状态</router-link>
            </a-menu-item>
            <a-menu-item key="/ota-upgrade" icon="<cloud-upload-outlined />">
              <router-link to="/ota-upgrade">OTA升级</router-link>
            </a-menu-item>
            <a-menu-item key="/network-config" icon="<wifi />">
              <router-link to="/network-config">网络配置</router-link>
            </a-menu-item>
            <a-menu-item key="/about" icon="<info-circle />">
              <router-link to="/about">关于</router-link>
            </a-menu-item>
          </a-menu>
        </a-layout-sider>

        <!-- 主要内容区域 -->
        <a-layout-content class="app-content">
          <div class="content-container">
            <router-view />
          </div>
        </a-layout-content>
      </a-layout>

      <!-- 页脚 -->
      <a-layout-footer class="app-footer">
        <p class="footer-text">&copy; {{ new Date().getFullYear() }} 配线架配置系统 v1.0.0 | 实时数据通信平台</p>
      </a-layout-footer>
    </a-layout>
  </a-config-provider>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useDataStore } from './stores/dataStore'
import { initWebSocket } from './utils/websocket'
import zhCN from 'ant-design-vue/es/locale/zh_CN'

// 初始化数据存储
const dataStore = useDataStore()
const route = useRoute()
const locale = zhCN
const collapsed = ref(false)

// 计算当前选中的菜单项
const selectedKey = computed(() => {
  return route.path
})

// 处理菜单点击
const handleMenuClick = (e) => {
  console.log('菜单点击:', e.key)
}

// 监听路由变化
watch(() => route.path, (newPath) => {
  console.log('路由变化:', newPath)
})

// 组件挂载时初始化WebSocket连接
onMounted(() => {
  initWebSocket(dataStore)
})
</script>

<style scoped>
/* 应用布局 */
.app-layout {
  min-height: 100vh;
}

/* 头部样式 */
.app-header {
  background-color: #001529;
  padding: 0;
  height: 64px;
  line-height: 64px;
  position: fixed;
  width: 100%;
  z-index: 10;
}

.header-content {
  max-width: 100%;
  margin: 0 auto;
  padding: 0 24px;
  height: 100%;
}

.app-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: #fff;
    margin: 10px;
    font-size: 15px;
    margin-bottom: 0;;
}

/* 侧边栏样式 */
.app-sider {
  background-color: #001529;
  position: fixed;
  left: 0;
  top: 64px;
  height: calc(100vh - 64px);
  z-index: 9;
  transition: all 0.3s;
}

.logo {
  height: 60px;
  width: 94px;
  position: absolute;
  top: 0px;
  background: url('/vite.svg') center no-repeat;
  background-size: contain;
}

.side-menu {
  height: calc(100% - 64px);
  padding: 20px 0;
  padding-top: 0px;
}

.side-menu .ant-menu-item {
  margin: 0;
  padding: 0 20px;
  height: 50px;
  line-height: 50px;
}

.side-menu .ant-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.side-menu .ant-menu-item-selected {
  background-color: #1890ff;
}

/* 主要内容区域 */
.app-content {
  padding: 24px;
  background-color: #f0f2f5;
  min-height: calc(100vh - 64px);
  margin-left: 256px;
  margin-top: 64px;
  transition: all 0.3s;
}

.app-sider.collapsed + .app-content {
  margin-left: 80px;
}

.content-container {
  max-width: 100%;
  margin: 0 auto;
}

/* 页脚样式 */
.app-footer {
  text-align: center;
  background-color: #001529;
  color: rgba(255, 255, 255, 0.65);
  font-size: 14px;
  padding: 16px 0;
  margin-left: 256px;
  transition: all 0.3s;
}

.app-sider.collapsed + .app-content + .app-footer {
  margin-left: 80px;
}

.footer-text {
  margin: 0;
}

/* 响应式调整 */
@media (max-width: 1024px) {
  .app-sider {
    transform: translateX(-100%);
  }
  
  .app-sider.collapsed {
    transform: translateX(0);
  }
  
  .app-content {
    margin-left: 0;
  }
  
  .app-footer {
    margin-left: 0;
  }
}

@media (max-width: 768px) {
  .app-content {
    padding: 16px;
  }
  
  .header-content {
    padding: 0 16px;
  }
  
  .app-title {
    font-size: 18px;
  }
}

/* 路由链接样式 */
:deep(.ant-menu-item a) {
  color: rgba(255, 255, 255, 0.65);
  width: 100%;
  display: flex;
  align-items: center;
}

:deep(.ant-menu-item a:hover) {
  color: #fff;
}

:deep(.ant-menu-item-selected a) {
  color: #fff;
}  

/* 修复侧边栏滚动问题 */
:deep(.ant-layout-sider-children) {
  overflow-y: auto;
  height: 100%;
}

.status-badge {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.status-badge.on {
  background-color: #52c41a;
}
.status-badge.off {
  background-color: #ff4d4f;
}

.header-status-item {
  color: #fff;
  font-size: 12px;
}
.header-status-item span {
  vertical-align: middle;
}
.header-status-item .status-badge {
  margin-right: 4px;
}
</style>