<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'

export interface SidebarMenuItem {
  key: string
  icon: string
  label: string
  path: string
}

const props = withDefaults(defineProps<{
  menus: SidebarMenuItem[]
  brandIcon?: string
  brandTitle?: string
  mobileOpen?: boolean
}>(), {
  brandIcon: '📊',
  brandTitle: '运维',
  mobileOpen: false,
})

const emit = defineEmits<{
  (e: 'update:mobileOpen', value: boolean): void
}>()

const route = useRoute()
const router = useRouter()

const activeKey = computed(() => route.meta.activeMenu as string || '')

function handleMenuClick(item: SidebarMenuItem) {
  if (props.mobileOpen) {
    emit('update:mobileOpen', false)
  }
  router.push(item.path)
}

function closeMobile() {
  emit('update:mobileOpen', false)
}
</script>

<template>
  <Teleport to="body">
    <div
      v-if="mobileOpen"
      class="sidebar-overlay"
      @click="closeMobile"
    ></div>
  </Teleport>

  <aside class="sidebar" :class="{ open: mobileOpen }">
    <div class="sidebar-brand">
      <span class="brand-icon">{{ brandIcon }}</span>
      <span class="brand-title">{{ brandTitle }}</span>
    </div>
    <nav class="sidebar-nav">
      <a
        v-for="item in menus"
        :key="item.key"
        :class="['nav-item', { active: activeKey === item.key }]"
        href="javascript:void(0)"
        @click="handleMenuClick(item)"
      >
        <span class="nav-icon">{{ item.icon }}</span>
        <span class="nav-label">{{ item.label }}</span>
      </a>
    </nav>
  </aside>
</template>

<style scoped lang="scss">
.sidebar-overlay {
  display: none;

  @media screen and (max-width: 768px) {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99;
  }
}

.sidebar {
  position: fixed;
  left: 0;
  top: 0;
  width: 200px;
  height: 100vh;
  background: linear-gradient(180deg, #1e293b 0%, #334155 100%);
  z-index: 100;
  transition: transform 0.3s ease;

  @media screen and (max-width: 768px) {
    transform: translateX(-200px);

    &.open {
      transform: translateX(0);
    }
  }

  @media screen and (max-width: 480px) {
    width: 180px;
  }
}

.sidebar-brand {
  color: #fff;
  font-size: 18px;
  font-weight: 700;
  padding: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  gap: 8px;

  @media screen and (max-width: 480px) {
    font-size: 14px;
    padding: 14px;
  }
}

.brand-icon {
  font-size: 20px;

  @media screen and (max-width: 480px) {
    font-size: 16px;
  }
}

.sidebar-nav {
  padding: 16px 0;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  color: #94a3b8;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.2s;
  cursor: pointer;

  &:hover {
    background: rgba(255, 255, 255, 0.05);
    color: #fff;
  }

  &.active {
    background: $primary;
    color: #fff;
  }

  @media screen and (max-width: 480px) {
    padding: 10px 14px;
    font-size: 12px;
    gap: 10px;
  }
}

.nav-icon {
  font-size: 16px;
  width: 20px;
  text-align: center;

  @media screen and (max-width: 480px) {
    font-size: 14px;
    width: 18px;
  }
}
</style>