<template>
  <div class="left-panel">
    <img
      src="../../../assets/images/leaderTitle.png"
      alt=""
      class="headerImage"
      style="margin-top: -22px; margin-left: -15px"
    />
    <div class="tabs">
      <div class="tabs-switch">
        <div
          class="switch-item"
          v-for="tab in tabs"
          :key="tab"
          @click="activeTab = tab"
          :class="{ 'tabsSwich-active': activeTab === tab }"
        >
          {{ tab }}
        </div>
      </div>
    </div>
    <div class="leader-list" ref="leaderListRef">
      <div
        v-for="(leader, index) in filteredVisibleLeaders"
        :key="leader.id"
        class="leader-item"
        @click="emit('selectLeader', leader)"
      >
        <div class="order">{{ index + 1 }}</div>
        <img :src="leader.avatar" :alt="leader.name" class="avatar" />
        <div class="user-info">
          <div class="username">
            <span class="en-name">{{ leader.name }}</span>
            <span v-if="leader.chineseName" class="cn-name">({{ leader.chineseName }})</span>
          </div>
          <div class="userState">
            <div class="userState-fancy">
              粉丝数:
              <p>{{ leader.followers }}</p>
            </div>
            <div class="userState-monitor-count">
              发帖数:
              <p>{{ leader.posts }}</p>
            </div>
          </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 lang="less">
.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;
  height: 100%;
}
.tabs {
  padding: 10px 20px;
}
.tabs-switch {
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
}
.switch-item {
  flex: 1;
  padding: 4px 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid #20406e;
  font-size: 14px;
  cursor: pointer;
  color: #cccccc9d;
}
.switch-item:first-child {
  border-radius: 5px 0 0 5px;
}
.switch-item:last-child {
  border-radius: 0px 5px 5px 0px;
}
.tabsSwich-active {
  width: 100%;
  height: 100%;
  color: #fff;
  opacity: 1;
  background-color: #236291;
  border: 1px solid #3fa9f5;
}
.leader-list {
  width: 100%;
  height: 430px;
  margin-top: 10px;
  padding-right: 5px;
  overflow: auto;
  color: #fff;
}
.leader-list::-webkit-scrollbar {
  width: 3px;
  height: 5px;
}
.leader-list::-webkit-scrollbar-thumb {
  background: rgba(147, 210, 255, 0.3);
  border-radius: 4px;
}
.leader-list::-webkit-scrollbar-thumb:hover {
  background: rgba(147, 210, 255, 0.5);
}
.leader-item {
  width: 100%;
  height: 80px;
  padding: 10px 0px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  border-bottom: 0.5px solid rgba(0, 113, 188, 0.5);
}
.order {
  color: #fff;
  font-size: 16px;
  font-style: normal;
  font-weight: 400;
  margin-right: 15px;
}
.avatar {
  width: 48px;
  height: 48px;
  border-radius: 5px;
  flex-shrink: 0;
}
.user-info {
  flex: 1;
  padding-left: 15px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.username {
  color: #fff;
  font-size: 16px;
  font-family: "微软雅黑";
}
.cn-name {
  font-size: 14px;
  color: #a9c2e0;
  margin-left: 5px;
}
.userState {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  font-size: 13px;
  color: #cccccc9d;
  div {
    display: flex;
    p {
      color: #fff;
      margin-left: 5px;
    }
  }
}

.userState-monitor-count {
  width: 100px; /* 固定宽度确保对齐 */
  justify-content: flex-start; /* 内容靠左对齐 */
}
</style>