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

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

interface Props {
  activeTagID?: string;
}

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

const emit = defineEmits<{
  'category-change': [category: Tag]; // 改为Tag类型
}>();

const categories = ref<Tag[]>([]); // 改为使用Tag类型（tagType=1表示文章分类）

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

async function loadCategories() {
  try {
    // 使用新的标签API获取文章分类标签（tagType=1）
    const res = await resourceTagApi.getTagsByType(1); // 1 = 文章分类标签
    if (res.success && res.dataList) {
      categories.value = res.dataList;
    }
  } catch (error) {
    console.error('加载分类失败:', error);
  }
}

function handleCategoryClick(category: Tag) {
  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>

