<template>
  <aside class="floating-sidebar">
    <!-- 侧边栏内容 -->
    <div class="sidebar-content">
      <!-- 菜单列表 -->
      <nav class="sidebar-nav">
        <div
          v-for="menu in menus"
          :key="menu.menuID || menu.url"
          class="sidebar-item"
          :class="{ 
            active: isActive(menu),
            'theme-resource': props.theme === 'resource'
          }"
          @click="handleClick(menu)"
        >
          <div class="sidebar-link">
            <span class="link-text">{{ menu.name }}</span>
          </div>
        </div>
      </nav>
    </div>
  </aside>
</template>

<script setup lang="ts">
import type { SysMenu } from '@/types';
import { useRouter, useRoute } from 'vue-router';

// Props
interface Props {
  menus: SysMenu[];
  activePath?: string;
  theme?: 'default' | 'resource'; // 新增主题属性
}

const props = withDefaults(defineProps<Props>(), {
  theme: 'default'
});

// Emits
const emit = defineEmits<{
  'menu-click': [menu: SysMenu];
}>();

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

// 检查菜单是否激活
function isActive(menu: SysMenu): boolean {
  if (props.activePath) {
    return props.activePath === menu.url;
  }
  if (!menu.url) return false;
  return route.path === menu.url;
}

// 处理点击
function handleClick(menu: SysMenu) {
  // 触发点击事件
  emit('menu-click', menu);
  
  // 如果有URL，进行路由跳转
  if (menu.url) {
    router.push(menu.url);
  }
}
</script>

<style lang="scss" scoped>
.floating-sidebar {
  width: 180px;
  background: #FFFFFF;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

.sidebar-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  gap: 20px;

  /* 滚动条样式 */
  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-track {
    background: transparent;
  }

  &::-webkit-scrollbar-thumb {
    background: #ddd;
    border-radius: 2px;

    &:hover {
      background: #bbb;
    }
  }
}

.sidebar-item {
  position: relative;
  height: 54px;
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s;

  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 4px;
    height: 20px;
    background: transparent;
    transition: background 0.2s;
  }

  &.active {
    &::before {
      background: #C62828;
    }

    .sidebar-link {
      color: #C62828;
    }
  }

  &:hover {
    .sidebar-link {
      color: #C62828;
    }
  }
  
  // Resource theme - 红色背景高亮
  &.theme-resource {
    padding: 0 10px;
    
    &:hover {
      background: rgba(198, 40, 40, 0.05);
    }
    
    &.active {
      background: #C62828;
      border-radius: 8px;
      margin: 5px 10px;
      height: 44px;
      
      &::before {
        display: none; // 隐藏左侧指示条
      }
      
      .sidebar-link {
        color: #FFFFFF;
      }
      
      &::after {
        content: '';
        position: absolute;
        right: -103px;
        top: 3px;
        width: 48.53px;
        height: 50.54px;
        background: linear-gradient(134deg, rgba(255, 255, 255, 1) 26%, rgba(255, 255, 255, 0) 86%);
        opacity: 0.23;
        pointer-events: none;
      }
    }
  }
}

.sidebar-link {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 0 16px;
  color: #334155;
  font-size: 16px;
  font-weight: 500;
  font-family: 'PingFang SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.5;
  transition: color 0.3s;
  user-select: none;

  .link-text {
    text-align: center;
  }
  
  .theme-resource & {
    margin-left: 32px; // Resource theme 文字左移
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .floating-sidebar {
    width: 160px;
  }
}
</style>
