<template>
  <div class="base-layout">
    <!-- 顶部导航栏 -->
    <div class="base-layout__topbar">
      <div class="base-layout__topbar-title">
        <span class="base-layout__topbar-game-icon">{{ activeGameIcon }}</span>
        <span class="base-layout__topbar-game-name">{{ activeGameName }}</span>
      </div>
    </div>

    <!-- 主体区域 -->
    <div class="base-layout__main">
      <!-- 侧边栏 -->
      <div class="base-layout__sidebar" :style="{ width: sidebarWidth + 'px' }">
        <!-- 侧边栏内容 -->
        <div class="base-layout__sidebar-content">
          <GameList />
        </div>
        <!-- 侧边栏底部固定区域 -->
        <div class="base-layout__sidebar-bottom">
          <div class="base-layout__sidebar-bottom-inner">
            <UserBar />
          </div>
        </div>
        <!-- 拖拽调整宽度的手柄 -->
        <div class="base-layout__resize-handle" @mousedown="onResizeMouseDown"></div>
      </div>

      <!-- 主内容区域 -->
      <div class="base-layout__content">
        <Lobby />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import UserBar from '@/components/baseLayouts/UserBar.vue'
import GameList from '@/components/baseLayouts/GameList.vue'
import Lobby from '@/components/baseLayouts/Lobby.vue'
import { useBaseLayoutStore } from '@/stores'
import { storeToRefs } from 'pinia'
import { useResizeHandle } from '@/common/hooks/useResizeHandle'

const baseLayoutStore = useBaseLayoutStore()
const { activeGameIcon, activeGameName, sidebarWidth } = storeToRefs(baseLayoutStore)

/** 拖拽手柄事件处理器 */
const { onMouseDown: onResizeMouseDown } = useResizeHandle({
  width: sidebarWidth,
  onWidthChange: (width) => baseLayoutStore.setSidebarWidth(width)
})
</script>

<style scoped lang="scss">
.base-layout {
  display: flex;
  flex-direction: column;
  height: 100lvh;
  width: 100lvw;
  overflow: hidden;
}

.base-layout__topbar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 36px;
  padding: 0 12px;
  background-color: var(--bg-topbar);
  border-bottom: 1px solid var(--border-default);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  /* 置顶，确保阴影覆盖下方内容 */
  position: relative;
  z-index: 10;
}

.base-layout__topbar-title {
  display: flex;
  align-items: baseline;
  gap: 6px;
  height: 30px;
}

.base-layout__topbar-game-icon {
  display: flex;
  align-items: center;
  font-size: 18px;
  line-height: 1;
  height: 100%;
}

.base-layout__topbar-game-name {
  display: flex;
  align-items: center;
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
  height: 100%;
  color: var(--text-primary);
}

.base-layout__main {
  display: flex;
  flex: 1;
  width: 100%;
  min-height: 0; /* flex 子元素必须设置，允许收缩到内容以下 */
  overflow: hidden;
}

.base-layout__sidebar {
  display: flex;
  flex-direction: column;
  position: relative;
  width: 400px;
  height: 100%;
  padding: var(--space-sm);
  background-color: var(--bg-sidebar);
  /* 右边框加粗 + 阴影，与内容区形成清晰分界 */
  border-right: 2px solid var(--border-default);
  z-index: 5;
}

.base-layout__sidebar-content {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  padding-bottom: 72px;
  /* 内容区用白色卡片承载，与侧边栏底色拉开层次 */
  background-color: var(--bg-surface);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.base-layout__sidebar-bottom {
  position: absolute;
  display: flex;
  flex-shrink: 0;
  width: 100%;
  height: 72px;
  left: 0;
  bottom: 0;
  padding: var(--space-sm);
}

.base-layout__sidebar-bottom-inner {
  display: flex;
  flex: 1;
  width: 100%;
  height: 100%;
  border-radius: var(--radius-md);
  background-color: var(--bg-surface);
  /* 加重的阴影 + 微妙的品牌色顶边，让用户栏从侧边栏中"浮起" */
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06), 0 2px 8px rgba(0, 0, 0, 0.04);
  border-top: 2px solid var(--color-primary-light);
  border-left: 1px solid var(--border-light);
  border-right: 1px solid var(--border-light);
  border-bottom: 1px solid var(--border-light);
}

.base-layout__resize-handle {
  position: absolute;
  width: 6px;
  height: 100%;
  top: 0;
  right: 0;
  cursor: ew-resize;
  z-index: 10;
  background: transparent;
  transition: background-color 0.2s ease;

  &:hover {
    background-color: var(--color-primary-light);
  }

  &:active {
    background-color: var(--color-primary);
  }
}

.base-layout__content {
  display: flex;
  flex: 1;
  width: 100%;
  min-height: 0; /* flex 子元素必须设置，允许收缩 */
  overflow: hidden;
  background-color: var(--bg-body);
}
</style>
