<template>
  <div id="allstyle">
    <div class="page-header">
      <h3>文章目录</h3>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-state-container">
      <el-skeleton :count="1" />
    </div>

    <!-- 错误状态 -->
    <div v-else-if="error" class="error-state-container">
      <el-alert :title="error" type="error" show-icon />
      <el-button type="primary" @click="fetchCategories">重新加载</el-button>
    </div>
    <!-- 分类列表 -->
    <div v-else-if="categoriestree.length > 0" class="article-content" id="category-list">
      <p><strong></strong></p>
      <div class="alert alert-primary"><strong><span class="alert-inner-text">文章分类如下，点击跳转</span> </strong></div>
      <div v-for="categoryGroup in categoriestree" :key="categoryGroup.id" class="category-group-container">
        <div v-if="calculateTotalArticles(categoryGroup.children) > 0">
          <h2 id="header-id-1">{{ categoryGroup.name }}</h2>
          <span class="badge badge-primary">共 {{ calculateTotalArticles(categoryGroup.children) }} 篇</span>
          <ul class="category-item-list">
            <div v-for="attribute in categoryGroup.children" :key="attribute.id">
              <li v-if="attribute.articlecount && attribute.articlecount > 0">
                &nbsp;&nbsp;<a class="category-link" @click="handleCategoryClick(attribute)"><kbd>{{
                  attribute.attributename}}</kbd></a>
                &nbsp; — —({{ attribute.articlecount }})
              </li>
            </div>
          </ul>
        </div>

      </div>
    </div>

    <!-- 空状态 -->
    <div v-else class="empty-state-container">
      <el-empty description="暂无分类" />
    </div>

  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { ref, onMounted } from 'vue'
// 由于模块 '@/services' 没有导出的成员 'CategoryService'，请确认正确的导出名称后修改此处
// 以下代码仅作示例，实际需根据 '@/services' 模块的真实导出调整
import { ElMessage } from 'element-plus'
import { categoryService } from '@/services'
import { useGlobalStore } from '@/store/globalStore'

const globalStore = useGlobalStore()
const router = useRouter()

// 响应式状态
const categoriestree = ref<any[]>([])
const loading = ref(false)
const error = ref('')

/**
 * 获取文章分类列表
 */
const fetchCategories = async () => {
  try {
    loading.value = true;
    const res = await categoryService.getCategoryTree();
    if (res.code !== 200) {
      return []
    }

    categoriestree.value = res.data;
    // console.log('获取分类列表成功:', categoriestree.value);
  } catch (err) {
    console.error('获取分类列表失败:', err);
    ElMessage.error('获取分类列表失败，请稍后重试');
  } finally {
    loading.value = false;
  }
}
/**
 * 过滤出有文章的分类组
 * @param {any[]} categoryGroups - 分类组数组
 * @returns {any[]} - 有文章的分类组数组
 */
const filterCategoriesWithArticles = (categoryGroups: any[]) => {
  return categoryGroups.filter(group =>
    group.children.some(child => child.articlecount && child.articlecount > 0)
  )
}
/**
 * 计算文章总数
 * @param {any[]} categoryGroups - 分类组数组
 * @returns {number} - 文章总数
 */
const calculateTotalArticles = (categoryGroups: any[]) => {
  let TotalArticles = 0
  if (categoryGroups.length > 0) {
    categoryGroups.forEach(group => {
      TotalArticles += group.articlecount
    })
  }
  return TotalArticles
}
/**
* 处理分类点击事件
* 注意：现在实际上使用的是属性ID而不是分类ID
* @param {string | number} attributeId - 属性ID
*/
const handleCategoryClick = (attribute: any) => {
  globalStore.removeValue('attribute')
  globalStore.setValue('attribute', {
    id: attribute.attributeid,
    name: attribute.attributename
  })
  router.push({
    path: '/home/aericleattribute',

  })
}



/**
 * 组件挂载时获取分类列表
 */
onMounted(() => {
  fetchCategories()
})
</script>

<style scoped>
/* 页面标题样式 */
.page-header {
  text-align: center;
  padding: 20px;
  margin-bottom: 20px;
}

.page-header h1 {
  color: #2c3e50;
  font-size: 1.8rem;
}

/* 文章内容区域 */
.article-content {
  padding: 15px;
}

/* 分类组样式 */
.category-group-container ul li {
  display: flex;
  align-items: center;
}

/* 分类链接样式 */
.category-group-container ul li .category-link::before {
  background-color: rgba(0, 0, 0, 0);
}

.category-group-container ul li .category-link:hover {
  background-color: rgb(245, 247, 250, 0.7);
}

/* 提示框样式 */
.alert:not(.alert-secondary) {
  color: #fff;
}

.alert {
  font-size: .875rem;
  padding: 1rem 1.5rem;
  border: 0;
  border-radius: .25rem;
}

.alert-primary {
  color: #fff;
  border-color: #7889e8;
  background-color: #7889e8;
}

.alert-inner-text {
  font-weight: bold;
}

/* 键盘样式 */
.category-link kbd {
  color: #fff;
  font-size: 14px;
  padding: 4px 8px;
  border-radius: 4px;
  background-color: #2c3e50;
}

.alert {
  position: relative;
  margin-bottom: 1rem;
  padding: 1rem 1.5rem;
  border: .0625rem solid transparent;
  border-radius: .25rem;
}

/* 分类组容器样式 */
.category-group-container {
  text-align: left;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 24px;
}

/* 徽章样式 */
.badge {
  padding: .25em .4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: .25rem;
  margin: 10px 6px;
}

/* 分类标题样式 */
.category-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 2px solid #ecf0f1;
}

.category-header h2 {
  color: #34495e;
  font-size: 1.4rem;
  margin: 0;
}

/* 项目列表样式 */
.project-content-list {
  list-style: none;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 15px;
  margin-top: 20px;
}

.project-content-item {
  display: flex;
  align-items: center;
  padding: 10px 15px;
  border-radius: 12px;
  background-color: rgba(245, 247, 250, 0.7);
  transition: transform 0.3s ease;
  gap: 10px;
}

.project-content-item:hover {
  transform: translateY(-2px);
  background-color: rgba(236, 240, 241, 0.9);
}

/* 分类链接样式 */
.category-link {
  position: relative;
  text-decoration: none;
  color: #34495e;
  padding: 10px 15px;
  border-radius: 8px;
  transition: all 0.3s ease;
  display: inline-block;
  z-index: 1;
  overflow: hidden;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 1rem;
}

/* 透明方块效果 */
.category-link::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  z-index: -1;
  transition: all 0.3s ease;
  transform: scale(0.95);
  opacity: 0.8;
  background: rgba(255, 255, 255, 0.5);
}

/* 悬浮效果 */
.category-link:hover::before {
  transform: scale(1.1);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
  background: rgba(145, 196, 238, 0.85);
}

/* 分类计数样式 */
.category-count {
  color: #7f8c8d;
  font-size: 0.9rem;
}

/* 标签样式 */
.badge {
  text-transform: uppercase;
}

.badge-primary {
  color: #2643e9;
  background-color: rgba(203, 210, 246, .5);
}

.badge {
  font-size: 66%;
  font-weight: 600;
  line-height: 1;
  display: inline-block;
  padding: .35rem .375rem;
  transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
  text-align: center;
  vertical-align: baseline;
  white-space: nowrap;
  border-radius: .25rem;
}

/* 加载状态 */
.loading-state-container {
  padding: 40px 20px;
  text-align: center;
}

/* 错误状态 */
.error-state-container {
  text-align: center;
  padding: 40px 20px;
}

.error-state-container .el-button {
  margin-top: 16px;
}

/* 空状态 */
.empty-state-container {
  text-align: center;
  padding: 60px 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header {
    padding: 15px;
  }

  .page-header h1 {
    font-size: 1.5rem;
  }

  .article-content {
    padding: 15px;
  }

  .category-group-container {
    padding: 15px;
    margin-bottom: 15px;
  }

  .category-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .project-content-list {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .category-header h2 {
    font-size: 1.2rem;
  }
}
</style>