<template>
  <div class="person-table-container">
    <!-- 表头 -->
    <div class="table-header">
      <div class="header-item" style="width: 332px;padding-left: 78px;">人物</div>
      <div class="header-item" style="width: 197px;">涉华观点次数</div>
      <div class="header-item" style="width: 209px;">主流媒体引用次数</div>
    </div>

    <!-- 表格内容 -->
    <div class="table-body">
      <div class="table-row" v-for="(item, index) in personList" :key="index">
        <!-- 人物信息列 -->
        <div class="row-col col-person">
          <div style="margin: 7px 12px 7px 24px;">
            <img :src="item.avatar" class="avatar" alt="avatar" />
            <div class="person-tags"> 
              <div class="person-tag-bg" v-for="(tag, tIdx) in item.tags" :key="tIdx">
                <img :src="getTagIconUrl(tag)" class="tag-icon" alt="tag" />
              </div>
            </div>
          </div>
          <div class="person-info" @click="handleClickToCharacter(item.personId)">
            <div class="person-name">{{ item.name }}</div>
            <div class="person-position">{{ item.position }}</div>
          </div>
        </div>

        <!-- 涉华观点次数列 -->
        <div class="row-col col-count">
          <el-progress :percentage="getProgress(item.chinaRelatedCount)" :show-text="false"
            style="/* 矩形 244 */ width: 82px; height: 8px" :status="getStatus(getProgress(item.chinaRelatedCount))" />
          <span class="count-text">{{ item.chinaRelatedCount }}次</span>
        </div>

        <!-- 媒体引用次数列 -->
        <div class="row-col col-count">
          <el-progress :percentage="getProgress(item.mediaQuoteCount)" :show-text="false"
            style="/* 矩形 244 */ width: 82px; height: 8px" :status="getStatus(getProgress(item.mediaQuoteCount))" />

          <span class="count-text">{{ item.mediaQuoteCount }}次</span>
        </div>
      </div>
    </div>
  </div>
</template> 
<script setup>
import { ref,onMounted,defineProps,watch } from "vue";
import {getMainCharactersView } from "@/api/technologyFigures/technologyFigures";
import { useCharacterNav } from "../utils/useCharacterNav";
const { handleClickToCharacter } = useCharacterNav();
const props = defineProps({
  persontypeid: {
    type: String,
    default: "004"
    },
  yearSelect:{
    type: String,
    default: "2025"
  }
});

watch(() => [props.persontypeid,props.yearSelect], (val) => {
  
  
  handlegetMainCharactersViewFn();
})

const getTagIconUrl = (tag) => {
  // 用 import.meta.glob 预加载所有图标，支持动态匹配
  const icons = import.meta.glob('../assets/images/header-icon*.png', { eager: true, as: 'url' });
  // 拼接对应路径，匹配预加载的图标
  const iconPath = `../assets/images/header-icon${tag}.png`;
  // 兜底：如果没有对应tag的图片，返回默认图或空
  return icons[iconPath] || '';
};
// 获取主要人物涉华观点统计
const handlegetMainCharactersViewFn = async () => {
	const params = {
		personTypeId: props.persontypeid,
    year:props.yearSelect
	};
	try {
    personList.value = [];
		const res = await getMainCharactersView(params);
		console.log("主要人物涉华观点统计", res);
		if (res.code === 200) {
			personList.value = res.data.map(item=>{
        return{
          avatar: item.personImage,
          name: item.personName,
          position: item.positionTitle,
          tags: ["1", "2"],
          chinaRelatedCount: item.remarksCount,
          mediaQuoteCount: item.remarksCount,
          personId:item.personId
        }
        
      });
		}
	} catch (error) {
      console.error("获取主要人物涉华观点统计失败:", error);
    }
};

onMounted(async () => {
  handlegetMainCharactersViewFn();
});

const personList = ref([]);
// 进度条状态
const getStatus = _percent => {
  const percent = _percent;
  if (percent >= 90) {
    return "exception";
  } else if (percent >= 50) {
    return "warning";
  } else {
    return "success";
  }
};
// 计算进度条宽度（基于最大值的百分比）
const getMaxCount = () => {
  const counts = personList.value.flatMap(item => [item.chinaRelatedCount || 0, item.mediaQuoteCount || 0]);
  if (counts.length === 0) return 1;
  return Math.max(...counts, 1);
};
const getProgress = count => (count / getMaxCount()) * 100;
</script>

<style scoped>
.person-table-container {
  width: 738px;
  height: 360px;
  margin: 24px 27px;
  font-family: "Microsoft YaHei", sans-serif;
}

/* 表头样式 */
.table-header {
  /* 容器 47 */
  display: flex;
  width: 738px;
  height: 60px;
  box-sizing: border-box;
  border-top: 1px solid rgba(230, 231, 232, 1);
  color: rgba(59, 65, 75, 1);

  font-family: Microsoft YaHei;
  font-size: 16px;
  font-weight: 700;
  line-height: 22px;
  letter-spacing: 0px;
  align-items: center;
  text-align: center;
}

.header-item {
  /* flex: 1; */
  text-align: left;
}



/* 表格内容样式 */
.table-body {
  /* background-color: #fff; */
  height: 300px;
  overflow-y:scroll;
  overflow-x:hidden;
}

.table-row {
  display: flex;
  align-items: center;
  padding: 0;

  /* 容器 92 */
  width: 738px;
  height: 60px;
}

.table-body .table-row:nth-of-type(odd) {
  background-color: rgba(247, 248, 249, 1);
}

.table-body .table-row:nth-of-type(even) {
  background-color: #ffffff;
}

.row-col {
  flex: 1;
  display: flex;
  align-items: center;
}

/* 人物列样式 */
.col-person {
  align-items: flex-start;
}

.avatar {
  /* 椭圆形 */
  width: 42px;
  height: 42px;
  border-radius: 50%;
  object-fit: cover;
}

.person-info {
  display: flex;
  flex-direction: column;
  
}

.person-name {
  margin-top: 5px;
  color: rgba(59, 65, 75, 1);

  font-family: Microsoft YaHei;
  font-size: 16px;
  font-weight: 700;
  line-height: 24px;
  letter-spacing: 0px;
  text-align: left;
  width: 200px;
  white-space:nowrap;
  overflow: hidden;   
  text-overflow: ellipsis;
  
}

.person-position {
  width: 253px;
  color: rgba(95, 101, 108, 1);

  font-family: Microsoft YaHei;
  font-size: 16px;
  font-weight: 400;
  line-height: 24px;
  letter-spacing: 0px;
  text-align: left;
}

.person-tags {
  display: flex;
  margin-top: -20px;
  width: 42px;

  text-align: center;
}

.person-tag-bg {
  /* 椭圆 6 */
  width: 20px;
  height: 20px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 50%;
}

.tag-icon {
  width: 16px;
  height: 16px;
  object-fit: contain;
}

/* 次数列样式 */
.col-count {
  gap: 11px;
}

.progress-bar {
  height: 8px;
  border-radius: 4px;
  background-color: #ccc;
  min-width: 20px;
}

.count-text {
  font-size: 14px;
  color: #333;
  white-space: nowrap;
}
</style>
