<template>
  <el-container class="layout-container">
    <el-aside v-if="!isMobile" class="sidebar" width="200px">
      <div class="logo">
        XuqmGroup 运营平台
      </div>
      <el-menu
        router
        :default-active="$route.path"
        background-color="#1d2129"
        text-color="#c9d1d9"
        active-text-color="#409eff"
        class="nav-menu"
      >
        <el-menu-item v-for="item in navItems" :key="item.path" :index="item.path">
          <el-icon><component :is="item.icon" /></el-icon>
          <span>{{ item.label }}</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-drawer
      v-model="drawerVisible"
      direction="ltr"
      :with-header="false"
      size="240px"
      class="mobile-drawer"
    >
      <div class="drawer-brand">
        XuqmGroup 运营平台
      </div>
      <el-menu
        router
        :default-active="$route.path"
        background-color="#1d2129"
        text-color="#c9d1d9"
        active-text-color="#409eff"
        class="nav-menu"
        @select="drawerVisible = false"
      >
        <el-menu-item v-for="item in navItems" :key="item.path" :index="item.path">
          <el-icon><component :is="item.icon" /></el-icon>
          <span>{{ item.label }}</span>
        </el-menu-item>
      </el-menu>
    </el-drawer>

    <el-container class="content-shell">
      <el-header class="header">
        <div v-if="isMobile" class="mobile-header-left">
          <el-button text circle class="menu-button" @click="drawerVisible = true">
            <el-icon><Menu /></el-icon>
          </el-button>
          <span class="mobile-title">运营平台</span>
        </div>
        <div v-else class="header-spacer" />
        <el-button link class="logout-button" @click="logout">退出登录</el-button>
      </el-header>
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { Avatar, Bell, Menu, Monitor, TrendCharts, Grid, Document, Warning, Promotion } from '@element-plus/icons-vue'

const router = useRouter()
const isMobile = ref(false)
const drawerVisible = ref(false)

const navItems = computed(() => [
  { path: '/tenants', label: '租户管理', icon: Avatar },
  { path: '/statistics', label: '服务运营总览', icon: TrendCharts },
  { path: '/service-requests', label: '服务开通审核', icon: Bell },
  { path: '/apps', label: '应用管理', icon: Grid },
  { path: '/push', label: 'Push 诊断', icon: Promotion },
  { path: '/operation-logs', label: '操作日志', icon: Document },
  { path: '/system-logs', label: '服务日志', icon: Monitor },
  { path: '/risk-control', label: '风控配置', icon: Warning },
])

function logout() {
  localStorage.removeItem('ops_token')
  router.push('/login')
}

function updateViewport() {
  isMobile.value = window.innerWidth < 768
  if (!isMobile.value) {
    drawerVisible.value = false
  }
}

watch(
  () => router.currentRoute.value.path,
  () => {
    drawerVisible.value = false
  },
)

onMounted(() => {
  updateViewport()
  window.addEventListener('resize', updateViewport)
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', updateViewport)
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
  overflow: hidden;
  background: linear-gradient(180deg, #f7f9fc 0%, #eef2f7 100%);
}
.sidebar {
  background: #1d2129;
  flex: 0 0 200px;
  box-shadow: 8px 0 24px rgba(15, 23, 42, 0.08);
}
.logo,
.drawer-brand {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: bold;
  font-size: 16px;
  border-bottom: 1px solid #2d3142;
}
.nav-menu {
  border-right: none;
}
.content-shell {
  min-width: 0;
}
.header {
  background: #fff;
  border-bottom: 1px solid #e8e8e8;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 16px;
  box-shadow: 0 1px 0 rgba(15, 23, 42, 0.03);
  position: sticky;
  top: 0;
  z-index: 9;
}
.header-spacer {
  flex: 1;
}
.mobile-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}
.mobile-title {
  font-size: 16px;
  font-weight: 700;
  color: #111827;
}
.menu-button {
  color: #111827;
}
.logout-button {
  flex-shrink: 0;
}
.main-content {
  padding: 16px;
  overflow: auto;
}
.mobile-drawer :deep(.el-drawer__body) {
  padding: 0;
  background: #1d2129;
}

@media (max-width: 767px) {
  .layout-container {
    display: block;
    min-height: 100vh;
    height: auto;
  }

  .header {
    padding: 0 12px;
  }

  .main-content {
    padding: 12px;
  }
}
</style>
