<template>
  <div id="alld">
    <div id="top">
      <div class="top1">
        <h3>小颠公告栏</h3>
      </div>
      <div class="top2">
        <p>站主发癫中请勿靠近</p>
      </div>
    </div>
    <div id="cont">
      <div class="cont1">
        <h3>小颠片刻</h3>
        <p>左眼右右眼左，四十五度成就美</p>
      </div>
      <div class="cont2">
        <el-menu :default-active="activeIndex" class="el-menu-vertical-demo" @select="handleSelect">
          <el-menu-item index="/home">
            <el-icon>
            </el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/articlelist">
            <el-icon>
            </el-icon>
            <span>目录</span>
          </el-menu-item>
          <el-menu-item index="/nonsense">
            <el-icon>
            </el-icon>
            <span>疯言疯语</span>
          </el-menu-item>
        </el-menu>
      </div>
    </div>
    <div id="bot" :class="{ 'botrelative': scrollY }">
      <el-tabs v-model="activeName" :stretch="true" class="demo-tabs">
        <el-tab-pane label="个人简介" name="first">
          <div class="mylogo">
            <el-avatar class="mylogo_avatar" :src="state.circleUrl" />
          </div>
          <a href="http://www.qf1121.top/">
            <h6 class="mylogo_name logo-text">清疯不颠</h6>
          </a>
          <h6 class="mylogo_description">重度精神失常患者</h6>
          <div class="stat-container">
            <div>
              <a href="#" class="stat-link">
                <span class="site-state-item-count">{{ articleCount }}</span>
                <span class="site-state-item-name">文章</span>
              </a>
            </div>
            <div>
              <a href="#" class="stat-link" @click.prevent="showCategoriesModel">
                <span class="site-state-item-count">{{ categoryCount }}</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
            <div>
              <a href="#" class="stat-link" @click.prevent="showAttributesModel">
                <span class="site-state-item-count">{{ AttributeCount }}</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="功能" name="second">
          <div>还在开发中.....</div>
        </el-tab-pane>
      </el-tabs>
    </div>


  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { categoryService } from "@/services";

// 当前激活菜单
const activeIndex = ref('/:type')
const router = useRouter()
const activeName = ref('first')
const state = reactive({
  circleUrl: '/blogicon.jpg',
  squareUrl: 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png',
  sizeList: ['small', '', 'large'] as const,
})

// 分类相关状态
// defineEmits
const emit = defineEmits(['update-data', 'CategoryModal', 'AttributeModal'])
const categoryCount = ref(0)
const AttributeCount = ref(0)
const articleCount = ref(0)

// 标签相关状态
// 处理菜单选择跳转
const handleSelect = (key: string) => {
  router.push({ path: key })
}

// 路由切换时同步菜单高亮
router.beforeEach((to) => {
  activeIndex.value = to.path
})
// 分类树遍历函数
const traverseCategoryTree = (tree) => {
    categoryCount.value = tree.length
    tree.forEach((category) => {
      category.children.forEach((child) => {
        articleCount.value += child.articlecount || 0
      })
      AttributeCount.value += category.children.length || 0
    })
  }
// 获取分类数据
const fetchCategoriestree = async () => {

  // 分类数据状态
  try {
    const response = await categoryService.getCategoryTree()
    // 如果API返回的数据结构不包含count属性，我们可以模拟一些数据
    traverseCategoryTree(response.data || [])
    return response.data || []
  } catch (error) {
    console.error('获取分类失败:', error)
    // 如果API调用失败，使用模拟数据
    return []
  }
}


// 向父组件传递标签数据
const sendData = () => {
  const data = { fetchCategoriestree: fetchCategoriestree() }
  emit('update-data', data)
}

// 显示标签蒙版
const showAttributesModel = () => {
  emit('AttributeModal', { ifmodal: true })
}
// 显示分类蒙版
const showCategoriesModel = () => {
  emit('CategoryModal', { ifmodal: true })
}


// 控制底部模块吸顶效果
const scrollY = ref(false)
const handleScroll = () => {
  scrollY.value = window.scrollY > 1100
}

// 生命周期管理事件监听，防止内存泄漏
onMounted(() => {
  window.addEventListener('scroll', handleScroll)
  sendData() // 组件挂载时获取标签数据和分类数据
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
</script>

<style scoped>
/* 整体布局外层，每个子div底部间距 */
#alld div {
  margin-bottom: 15px;
}

/* 顶部公告栏样式 */
#top {
  height: 100px;
  border-radius: 10px;
  background-color: rgba(102, 161, 216, 0.9);
  /* 蓝色半透明背景 */
}

#alld #top .top1 {
  padding-top: 20px;
  margin-bottom: 0;
  text-align: center;
  color: white;
}

#alld #top .top2 {
  text-align: center;
  color: white;
}

/* 内容区域样式 */
#cont {
  padding: 0 0 10px 0;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.9);
  /* 白色半透明背景 */
}

#cont .cont1 {
  margin-bottom: 5px;
}

#cont .cont2 {
  margin-bottom: 0px;
}

.cont1 {
  text-align: center;
  padding: 25px 10px 25px 10px;
  background-color: rgba(102, 161, 216, 0.9);
  /* 蓝色半透明背景 */
  border-radius: 10px 10px 0 0;
}

.cont1 h3 {
  margin-bottom: 10px;
}

.cont1 p {
  color: white;
  font-size: 14px;
}

/* 菜单样式 */
.cont2 .el-menu-vertical-demo {
  display: block;
  background-color: rgba(0, 0, 0, 0);
  /* 白色半透明背景 */
}

.cont2 .el-menu-vertical-demo li {
  font-size: 14px;
  height: 35px;
}

.cont2 .el-menu-vertical-demo .el-menu-item:hover {
  background-color: rgba(64, 158, 255, 0.9);
}

.cont2 .el-menu-vertical-demo .el-menu-item.is-active:hover {
  color: black;
  /* 蓝色半透明背景 */
}

.cont2 .el-menu-vertical-demo .el-menu-item.is-active {
  color: var(--nav-is-active);
}

/* 分类列表样式 */
.cont3 {
  margin-top: 20px;
  padding: 15px;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.9);
}

.cont3 h3 {
  text-align: center;
  color: #333;
  margin-bottom: 15px;
  font-size: 16px;
}

.category-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.category-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  background-color: rgba(240, 240, 240, 0.5);
}

.category-item:hover {
  background-color: rgba(52, 152, 219, 0.2);
  transform: translateX(5px);
}

.category-name {
  color: #333;
  font-size: 14px;
}

.category-count {
  color: #7f8c8d;
  font-size: 12px;
  background-color: rgba(127, 140, 141, 0.1);
  padding: 2px 8px;
  border-radius: 12px;
}

/* 底部标签页样式 */
#bot {
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.9);
  /* 白色半透明背景 */
  padding: 10px;
}

.site-state-item-count {
  display: block;
  text-align: center;
  color: #32325d;
  font-weight: bold;
}

.demo-tabs {
  transition: all 0.3s ease-in-out;
}

.el-tabs__nav-scroll .el-tabs__nav .el-tabs__item {
  margin-left: 100px;
  padding: 10px 20px;
}

.mylogo_name {
  font-size: 13px;
}

.mylogo_description {
  font-size: 13px;
  opacity: 0.8;
  color: #c21f30;
  margin: 10px 0;
}

.stat-container {
  display: flex;
  gap: 10px;
  justify-content: center;
  font-size: 10px;

}


.stat-container div:not(:first-child) {
  border-left: 1px solid #ccc;
  padding-left: 10px;
}

/* 头像样式 */
#pane-first .mylogo {
  text-align: center;
  padding: 6px;
  margin-bottom: 0px;
}

.mylogo_avatar {
  height: 60px;
  width: 60px;
}

/* 直接应用到el-avatar组件上的悬浮效果 */
.el-avatar.mylogo_avatar {
  transition: transform 0.3s ease;
}

.el-avatar.mylogo_avatar:hover {
  transform: scale(1.2);
}

/* 吸顶效果 */
.botrelative {
  position: sticky;
  top: 20px;
  z-index: 100;
  animation: slideDown 0.3s ease;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}


</style>