<template>
  <div class="resource-sidebar">
    <div class="sidebar-content">
      <div 
        v-for="category in categories" 
        :key="category.categoryID"
        class="sidebar-item"
        :class="{ active: category.categoryID === activeCategoryId }"
        @click="handleCategoryClick(category)"
      >
        <span class="category-name">{{ category.name }}</span>
        <div v-if="category.categoryID === activeCategoryId" class="active-overlay"></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { resourceCategoryApi } from '@/apis/resource';
import type { ResourceCategory } from '@/types/resource';

interface Props {
  activeCategoryId?: string;
}

withDefaults(defineProps<Props>(), {
  activeCategoryId: 'party_history'
});

const emit = defineEmits<{
  'category-change': [category: ResourceCategory];
}>();

const categories = ref<ResourceCategory[]>([]);

onMounted(async () => {
  await loadCategories();
});

async function loadCategories() {
  try {
    const res = await resourceCategoryApi.getCategoryList();
    if (res.success && res.dataList) {
      categories.value = res.dataList;
    }
  } catch (error) {
    console.error('加载分类失败:', error);
  }
}

function handleCategoryClick(category: ResourceCategory) {
  emit('category-change', category);
}
</script>

<style lang="scss" scoped>
.resource-sidebar {
  width: 180px;
  background: #FFFFFF;
  border-radius: 10px;
  padding: 20px 0;
}

.sidebar-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.sidebar-item {
  position: relative;
  height: 54px;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
  
  &:hover {
    .category-name {
      color: #C62828;
    }
  }
  
  &.active {
    .category-name {
      color: #FFFFFF;
      position: relative;
      z-index: 2;
    }
    
    .active-overlay {
      position: absolute;
      top: 5px;
      left: 10px;
      right: 10px;
      bottom: 5px;
      background: #C62828;
      border-radius: 8px;
      z-index: 1;
    
    }
  }
}

.category-name {
  font-family: 'PingFang SC';
  font-weight: 500;
  font-size: 16px;
  line-height: 24px;
  color: #334155;
  transition: color 0.3s;
}
</style>

