<template>
  <div class="left-panel">
    <img
      src="@/assets/images/chuanbo-show-title.png"
      alt=""
      style="margin-top: -22px; margin-left: -15px"
    />
    <div class="tabs">
      <button
        v-for="tab in tabs"
        :key="tab"
        :class="{ active: activeTab === tab }"
        @click="activeTab = tab"
      >
        {{ tab }}
      </button>
    </div>
    <div class="leader-list" ref="leaderListRef">
      <div
        v-for="leader in filteredVisibleLeaders"
        :key="leader.id"
        class="leader-item"
        @click="emit('selectLeader', leader)"
      >
        <img :src="leader.avatar" :alt="leader.name" class="avatar" />
        <div class="info">
          <div class="name">
            <span class="en-name">{{ leader.name }}</span>
            <span v-if="leader.chineseName" class="cn-name">{{ leader.chineseName }}</span>
          </div>
          <div class="stats">
            <span>粉丝数量: {{ leader.followers }}</span>
            <span>发帖总数: {{ leader.posts }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch, nextTick, defineEmits } from 'vue';
import { useKeyNodeStore2 } from '@/store/keyNodeStore2';

const emit = defineEmits(['selectLeader']);
const store = useKeyNodeStore2();

const leaderListRef = ref(null);
const tabs = ref(["全部", "新闻媒体", "自媒体", "政府官号"]);
const activeTab = ref("全部");

const filteredVisibleLeaders = computed(() => {
  if (activeTab.value === "全部") {
    return store.visibleLeaders;
  }
  return store.visibleLeaders.filter((leader) => leader.category === activeTab.value);
});

watch(filteredVisibleLeaders, async () => {
  await nextTick();
  if (leaderListRef.value) {
    leaderListRef.value.scrollTo({
      top: leaderListRef.value.scrollHeight,
      behavior: "smooth"
    });
  }
}, { deep: true });
</script>

<style scoped>
.left-panel {
  width: 350px;
  flex-shrink: 0;
  background-color: rgba(6, 45, 90, 0.3);
  border: 1px solid #1a8bff;
  display: flex;
  flex-direction: column;
  padding: 15px;
}
.tabs {
  display: flex;
  margin-bottom: 15px;
  border-bottom: 2px solid #1a5a9c;
}
.tabs button {
  background: none;
  border: none;
  color: #a9c2e0;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}
.tabs button.active {
  color: #fff;
  font-weight: bold;
}
.tabs button.active::after {
  content: "";
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #3aa1f8;
}
.leader-list {
  width: 100%;
  height: 410px;
  flex-grow: 1;
  overflow: auto;
  scrollbar-width: none;
  color: #fff;
}
.leader-list::-webkit-scrollbar {
  width: 4px;
}
.leader-list::-webkit-scrollbar-track {
  background: transparent;
}
.leader-list::-webkit-scrollbar-thumb {
  background: #3aa1f8;
  border-radius: 2px;
}
.leader-item {
  display: flex;
  align-items: center;
  padding: 10px 5px;
  border-bottom: 1px solid rgba(58, 161, 248, 0.2);
  cursor: pointer;
}
.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 15px;
  flex-shrink: 0;
}
.info {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.name {
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.en-name {
  font-size: 16px;
  font-weight: bold;
}
.cn-name {
  font-size: 14px;
  color: #a9c2e0;
}
.stats {
  font-size: 12px;
  color: #a9c2e0;
  display: flex;
  gap: 20px;
}
</style>