<template>
	<div class="wrap">
		<div class="header">
			<div class="header-left">
				<img :src="institutionInfo.logo ? institutionInfo.logo : DefaultIcon2" alt="" />
			</div>
			<div class="header-right">
				<div class="title">{{ institutionInfo.name }}</div>
				<div class="en-title">{{ institutionInfo.enName }}</div>
				<div class="desc">{{ institutionInfo.desc }}</div>
				<div class="tag-box">
					<div class="tag" v-for="(tag, index) in showTagList" :key="index">
						{{ tag }}
					</div>
				</div>
			</div>
			<!-- <div class="header-btn">
				<div class="icon">
					<img src="@/assets/images/links-icon.png" alt="" />
				</div>
				<div class="text">{{ "查看官网" }}</div>
			</div> -->
		</div>
		<div class="tab-box">
			<div class="tab" @click="handleClickTab(item)" :class="{ tabActive: activeTabName == item.name }"
				v-for="(item, index) in tabList" :key="index">
				{{ item.name }}
			</div>
		</div>
		<div class="main">
			<InsDetail v-if="activeTabName === '机构详情'"></InsDetail>
			<!-- <Deepdig v-else-if="activeTabName === '深度挖掘'"></Deepdig>
			<Sanction v-else></Sanction> -->
		</div>
	</div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from "vue";
import { useRoute } from "vue-router";
import InsDetail from "./insDetail/index.vue";
import Deepdig from "./deepdig/index.vue";
import Sanction from "./sanction/index.vue";
import { getGovOrgBasicInfo } from "@/api/institution/index";

import DefaultIcon2 from "@/assets/icons/default-icon2.png";
import { ElMessage } from "element-plus";

const route = useRoute();

const decodeMaybeBase64 = (raw) => {
	if (raw == null) return "";
	let s0 = String(raw).trim();
	if (!s0) return "";
	if (s0.includes("%")) {
		try {
			const decodedOnce = decodeURIComponent(s0);
			if (decodedOnce) s0 = String(decodedOnce).trim();
		} catch (_) { }
	}
	// 非 base64 直接返回（机构 id 含字母数字，明文也要支持）
	if (!/^[A-Za-z0-9+/_-]+={0,2}$/.test(s0)) return s0;

	const encodeBase64Param = (val) => {
		const s = String(val ?? "").trim();
		if (!s) return "";
		try {
			return btoa(encodeURIComponent(s));
		} catch (_) {
			return s;
		}
	};
	const normalizeBase64 = (s) => {
		const normalized = String(s ?? "").replace(/-/g, "+").replace(/_/g, "/");
		return normalized + "===".slice((normalized.length + 3) % 4);
	};

	try {
		const padded = normalizeBase64(s0);
		const decoded = atob(padded);
		const decodedStr = (() => {
			try {
				return decodeURIComponent(decoded);
			} catch (_) {
				return decoded;
			}
		})();
		const reEncoded = encodeBase64Param(decodedStr);
		if (reEncoded && normalizeBase64(reEncoded) === padded) {
			return decodedStr;
		}
	} catch (_) { }
	return s0;
};
const institutionId = computed(() => decodeMaybeBase64(route.query.id));

const institutionInfo = ref({
	name: "",
	enName: "",
	desc: "",
	tagList: [],
	logo: ""
});

const showTagList = computed(() => {
	if(institutionInfo.value.tagList.length > 10) {
		return institutionInfo.value.tagList.slice(0,10)
	} else {
		return institutionInfo.value.tagList
	}
})

const handleGetInfo = async () => {
	const params = {
		id: institutionId.value
	};
	try {
		const res = await getGovOrgBasicInfo(params);
		console.log("机构信息", res);
		if (res.code === 200 && res.data) {
			institutionInfo.value.logo = res.data.logoUrl;
			institutionInfo.value.name = res.data.orgName;
			institutionInfo.value.enName = res.data.orgNameEn;
			institutionInfo.value.desc = res.data.orgIntroduction;
			institutionInfo.value.name = res.data.orgName;
		}
	} catch (error) { }
};

handleGetInfo();

const activeTabName = ref("机构详情");

const tabList = ref([
	{
		name: "机构详情"
	},
	{
		name: "深度挖掘"
	},
	{
		name: "对华制裁"
	}
]);

const handleClickTab = val => {
	if (val.name === '深度挖掘' || val.name === '对华制裁') {
		ElMessage.warning('当前功能开发中，敬请期待！')
		return
	}
	activeTabName.value = val.name;
	window.sessionStorage.setItem("institutionActiveTabName", val.name);
};

onMounted(() => {
	if (window.sessionStorage.getItem("institutionActiveTabName")) {
		activeTabName.value = window.sessionStorage.getItem("institutionActiveTabName");
	}
});

onUnmounted(() => {
	window.sessionStorage.removeItem("institutionActiveTabName");
});
</script>

<style lang="scss" scoped>
.wrap {
	width: 100%;
	height: 100%;
	overflow: hidden;
	overflow-y: auto;
	background-color: rgba(247, 248, 249, 1);
	background-image: url("./assets/images/bg.png");
	background-repeat: no-repeat;
	background-size: 100% 100%;
	padding-top: 16px;

	.header {
		width: 1600px;
		height: 200px;
		margin: 0 auto 16px;
		box-sizing: border-box;
		border: 1px solid rgba(255, 255, 255, 1);
		border-radius: 10px;
		box-shadow: 0px 0px 20px 0px rgba(25, 69, 130, 0.1);
		background: rgba(255, 255, 255, 0.8);
		display: flex;
		position: relative;

		.header-left {
			width: 160px;
			height: 160px;
			margin: 20px;

			img {
				width: 100%;
				height: 100%;
			}
		}

		.header-right {
			margin-left: 24px;
			width: 1350px;
			overflow: hidden;

			.title {
				margin-top: 26px;
				height: 42px;
				color: rgba(59, 65, 75, 1);
				font-family: Microsoft YaHei;
				font-size: 32px;
				font-weight: 700;
				line-height: 42px;
				letter-spacing: 0px;
				text-align: left;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			.en-title {
				margin-top: 8px;
				height: 24px;
				color: rgba(59, 65, 75, 1);
				font-family: Microsoft YaHei;
				font-size: 16px;
				font-weight: 400;
				line-height: 24px;
				letter-spacing: 0px;
				text-align: left;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			.desc {
				height: 48px;
				margin-top: 6px;
				color: rgba(59, 65, 75, 1);
				font-family: Microsoft YaHei;
				font-size: 16px;
				font-weight: 400;
				line-height: 24px;
				letter-spacing: 0px;
				text-align: justify;
				overflow: hidden;
				display: -webkit-box;
				/* 3. 限制显示的行数（修改数字即可改变行数） */
				-webkit-line-clamp: 2;
				/* 4. 设置文字垂直排列方向 */
				-webkit-box-orient: vertical;
				/* 5. 隐藏超出部分 */
				overflow: hidden;
				/* 6. 显示省略号 */
				text-overflow: ellipsis;
				/* 可选：修复文字间距/换行问题 */
				word-break: break-all;
			}

			.tag-box {
				margin-top: 14px;
				display: flex;
				gap: 8px;

				.tag {
					height: 24px;
					padding: 0px 8px;
					border-radius: 4px;
					background: rgba(231, 243, 255, 1);
					color: var(--color-main-active);
					font-family: Microsoft YaHei;
					font-size: 14px;
					font-weight: 400;
					line-height: 20px;
					letter-spacing: 0px;
				}
			}
		}

		.header-btn {
			position: absolute;
			top: 26px;
			right: 30px;
			width: 120px;
			height: 36px;
			border-radius: 6px;
			background: rgba(5, 95, 194, 1);
			display: flex;
			gap: 3px;
			justify-content: center;
			align-items: center;
			cursor: pointer;

			.icon {
				width: 16px;
				height: 16px;

				img {
					width: 100%;
					height: 100%;
				}
			}

			.text {
				height: 22px;
				color: rgba(255, 255, 255, 1);
				font-family: Microsoft YaHei;
				font-size: 16px;
				font-weight: 400;
				line-height: 22px;
			}
		}
	}

	.tab-box {
		width: 1600px;
		height: 64px;
		margin: 0 auto;
		box-sizing: border-box;
		border: 1px solid rgba(255, 255, 255, 1);
		border-radius: 10px;
		box-shadow: 0px 0px 20px 0px rgba(25, 69, 130, 0.1);
		background: rgba(255, 255, 255, 0.8);
		display: flex;
		justify-content: space-between;
		align-items: center;

		.tab {
			width: 526px;
			height: 54px;
			box-sizing: border-box;
			border: 2px solid transparent;
			border-radius: 10px;
			text-align: center;
			line-height: 50px;
			color: rgba(59, 65, 75, 1);
			font-family: Microsoft YaHei;
			font-size: 20px;
			font-weight: 400;
			letter-spacing: 0px;
			cursor: pointer;

			&:hover {
				background: rgba(231, 243, 255, 1);
			}
		}

		.tabActive {
			border: 2px solid rgba(174, 214, 255, 1);
			background: rgba(231, 243, 255, 1);
			color: rgba(5, 95, 194, 1);
			font-size: 24px;
			font-weight: 700;
		}
	}

	.main {
		height: 800px;
		width: 1600px;
		margin: 16px auto;
	}
}
</style>