<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" @click="handleClickToCharacter(item.personId)">
          <div style="margin: 7px 12px 7px 24px; position: relative;">
            <img :src="item.avatar" class="avatar" alt="avatar" />
            <div class="person-tags">
              <div class="person-tag-bg" v-if="item.party === 'Republican' || item.party === '共和党'">
                <img :src="getTagIconUrl('1')" class="tag-icon" alt="tag" />
              </div>
              <div class="person-tag-bg" v-else-if="item.party && item.party !== 'Republican' && item.party !== '共和党'">
                <img :src="getTagIconUrl('2')" class="tag-icon" alt="tag" />
              </div>
              <div class="person-tag-bg person-tag-right" v-if="isHouse(item.congress)">
                <img :src="getCongressIconUrl('congress1')" class="tag-icon" alt="congress" />
              </div>
              <div class="person-tag-bg person-tag-right" v-else-if="isSenate(item.congress)">
                <img :src="getCongressIconUrl('congress2')" class="tag-icon" alt="congress" />
              </div>
            </div>
          </div>
          <div class="person-info">
            <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) => {
  const icons = import.meta.glob('../assets/images/header-icon*.png', { eager: true, as: 'url' });
  const iconPath = `../assets/images/header-icon${tag}.png`;
  return icons[iconPath] || '';
};
const getCongressIconUrl = (type) => {
  const icons = import.meta.glob('../assets/images/congress*.svg', { eager: true, as: 'url' });
  const iconPath = `../assets/images/${type}.svg`;
  return icons[iconPath] || '';
};
const isHouse = (congress) => {
  if (!congress) return false;
  return congress.includes('众议院') || congress.toLowerCase().includes('house');
};
const isSenate = (congress) => {
  if (!congress) return false;
  return congress.includes('参议院') || congress.toLowerCase().includes('senate');
};
// 获取主要人物涉华观点统计
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,
          party: item.party || '',
          congress: item.congress || '',
          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;
  cursor: pointer;
}

.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;
  justify-content: space-between;
  margin-top: -20px;
  width: 42px;
  text-align: center;
}

.person-tag-right {
}

.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>
