<template>
<div class="page-box">
	<div v-if="riskInfo.riskLevel" style="margin-bottom: 16px;">
		<WarnningPane :warnningLevel="riskInfo.riskLevel" :warnningContent="riskInfo.content" />
	</div>
	<div class="introduction-wrap">
		<div class="page-left">
			<div class="box1">
				<AnalysisBox title="基本信息" :showAllBtn="false">
					<div class="box1-main">
						<div class="box1-main-left" v-if="basicInfo.img">
							<img :src="basicInfo.img" alt="" />
						</div>
						<div v-else class="box1-main-left-img-mock">
							<img class="img-mock-badge-img" src="./assets/images/badge.png">
							<p class="img-mock-badge-title">{{ basicInfo.eName }}</p>
							<p class="img-mock-badge-org">The White House</p>
						</div>
						<div class="box1-main-right">
							<div class="item">
								<div class="item-left">{{ "政令全称：" }}</div>
								<div class="item-right">{{ basicInfo.name }}</div>
							</div>
							<div class="item">
								<div class="item-left">{{ "英文全称：" }}</div>
								<div class="item-right text" v-if="basicInfo.eName?.length < 60"> {{ basicInfo.eName }} </div>
								<el-popover v-else effect="dark" :width="500" :content="basicInfo.eName" placement="top-start">
									<template #reference>
										<div class="item-right text"> {{ basicInfo.eName }} </div>
									</template>
								</el-popover>
							</div>
							<div class="item">
								<div class="item-left">{{ "相关领域：" }}</div>
								<div class="item-right tag-box">
									<AreaTag v-for="(area, index) in basicInfo.areaList" :key="index" :tagName="area.industryName"></AreaTag>
								</div>
							</div>
							<div class="item">
								<div class="item-left">{{ "签署时间：" }}</div>
								<div class="item-right text">{{ basicInfo.signTime }}</div>
							</div>
							<div class="item">
								<div class="item-left">{{ "发布机构：" }}</div>
								<div class="item-right text">
									{{ basicInfo.proposeOrgName }}
								</div>
							</div>
							<div class="item">
								<div class="item-left">{{ "政令编号：" }}</div>
								<div class="item-right text">
									{{ basicInfo.bh }}
								</div>
							</div>
							<div class="item">
								<div class="item-left">{{ "执行期限：" }}</div>
								<div class="item-right text">
									{{ basicInfo.deadline + " 天" }}
								</div>
							</div>
						</div>
					</div>
				</AnalysisBox>
			</div>
			<div>
				<AnalysisBox title="报告内容摘要" :showAllBtn="false">
					<el-empty v-if="false" style="margin: 20px 0;" description="暂无数据" :image-size="100" />
					<div class="box5-main">
						<AiSummary>
							<template #summary-content>
								<div class="box5-text">{{ box1Data }}</div>
							</template>
						</AiSummary>
					</div>
				</AnalysisBox>
			</div>
			<div>
				<AnalysisBox title="相关事件" :showAllBtn="false">
					<div class="box2-main">
						<el-empty v-if="!relatedData.length" description="暂无数据" :image-size="100" />
						<div class="box2-item" v-for="(item, index) in relatedData" :key="index">
							<div class="item-left">
								<img :src="item.imageUrl || DefaultIconNews" alt="" />
							</div>
							<div class="item-center">
								<div class="bubble-header" @click="gotoNewsDetail(item.id)">
									<span class="name">{{ item.sjbt }}</span>
									<span class="meta">{{ item.sjsj }} · {{ item.source }}</span>
								</div>
								<div class="content">{{ item.sjnr }}</div>
							</div>
						</div>
					</div>
				</AnalysisBox>
			</div>
		</div>
		<div class="page-right">
			<div class="box4">
				<AnalysisBox title="政令关键词云" :showAllBtn="false">
					<div class="box4-main">
						<el-empty v-if="!wordCloudData.length" description="暂无数据" :image-size="100" />
						<WordCloudChart v-if="wordCloudData.length" :data="wordCloudData" width="100%" height="100%" />
					</div>
				</AnalysisBox>
			</div>
			<div class="box3">
				<AnalysisBox title="发布机构" :showAllBtn="false">
					<div class="box3-top">
						<div class="box3-top-top" @click="handleToInstitution(box3TopTopData)">
							<div class="left">
								<img :src="box3TopTopData?.logo || DefaultIcon2" alt="" />
							</div>
							<div class="right">
								<div class="name">{{ box3TopTopData.name + " >" }}</div>
								<div class="ename">{{ box3TopTopData.eName }}</div>
							</div>
						</div>
						<div class="box3-top-bottom">
							<div class="box3-top-bottom-header">
								<div class="icon">
									<img src="./assets/images/box3-icon1.png" alt="" />
								</div>
								<div class="text">{{ "关键人物" }}</div>
							</div>
							<div class="box3-top-bottom-main">
								<div class="box3-top-bottom-item" v-for="(item, index) in box3TopBottomData" :key="index">
									<div class="box3-top-bottom-item-left">
										<img :src="item.avatar ? item.avatar : DefaultIcon1" alt="" />
									</div>
									<div class="box3-top-bottom-item-right">
										<div class="name text-click-hover one-line-ellipsis" @click="handleClickUser(item)">{{ item.name }}</div>
										<div class="position">{{ item.job }}</div>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="box3-bottom">
						<div class="box3-bottom-header">
							<div class="header-icon">
								<img src="./assets/images/box3-bottom-header-icon.png" alt="" />
							</div>
							<div class="header-title">{{ "机构动态" }}</div>
						</div>
						<div class="box3-bottom-main">
							<el-timeline style="max-width: 500px">
								<el-timeline-item :timestamp="item.postDate" placement="top" v-for="(item, index) in eventList?.slice(0, 3)" :key="index">
									<div class="timeline-content">{{ item.describe }}</div>
								</el-timeline-item>
							</el-timeline>
						</div>
					</div>
				</AnalysisBox>
			</div>
		</div>
	</div>
</div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import router from "@/router";
import WarnningPane from '@/components/base/WarningPane/index.vue'
import WordCloudChart from "@/components/base/WordCloundChart/index.vue"
import {
	getDecreeBasicInfo,
	getDecreeRiskSignal,
	getDecreeIssueOrganization,
	getKeyWordUp,
	getOverview,
} from "@/api/decree/introduction";
import { getDecreeRelatedEvent } from "@/api/decree/background";
import AiSummary from '@/components/base/Ai/AiSummary/index.vue'

import DefaultIcon1 from "@/assets/icons/default-icon1.png";
import DefaultIcon2 from "@/assets/icons/default-icon2.png";
import DefaultIconNews from "@/assets/icons/default-icon-news.png";

import { useGotoNewsDetail } from '@/router/modules/news';
const gotoNewsDetail = useGotoNewsDetail()

const route = useRoute();
const decodeMaybeBase64 = (raw) => {
	if (raw == null) return "";
	let s0 = String(raw).trim();
	if (!s0) return "";
	// 兼容 query 里被 URL 编码的 base64（例如 OTg%3D）
	if (s0.includes("%")) {
		try {
			const decodedOnce = decodeURIComponent(s0);
			if (decodedOnce) {
				s0 = String(decodedOnce).trim();
			}
		} catch (_) { }
	}
	// 明文数字直接返回
	if (/^\d+$/.test(s0)) return s0;
	// base64(encodeURIComponent(id)) 解码（兼容短 base64）
	if (/^[A-Za-z0-9+/_-]+={0,2}$/.test(s0)) {
		try {
			const normalized = s0.replace(/-/g, "+").replace(/_/g, "/");
			const padded = normalized + "===".slice((normalized.length + 3) % 4);
			const decoded = atob(padded);
			const decodedStr = (() => {
				try {
					return decodeURIComponent(decoded);
				} catch (_) {
					return decoded;
				}
			})();
			// 只在“解出来还是纯数字”时才视为合作限制/智库的加密 id，避免误解码影响其它模块
			if (/^\d+$/.test(String(decodedStr).trim())) {
				return String(decodedStr).trim();
			}
		} catch (_) { }
	}
	return s0;
};
const decreeId = ref(decodeMaybeBase64(route.query.id));

// 风险警告
const riskInfo = ref({
	// riskLevel: "特别重大风险",
	// content: "政令核心意图在于通过税收优惠吸引制造业回流美国，并在关键科技领域对中国进行遏制，限制中国获取先进技术、资本和市场渠道，从而延缓中国科技产业的发展速度。给半导体、新能源、人工智能等相关科技行业带来不小的短期压力。"
})
const onRiskSignalData = async () => {
	try {
		const res = await getDecreeRiskSignal({id: decreeId.value});
		console.log("风险警告", res);
		if (res.code === 200 && res.data) {
			riskInfo.value = res.data;
		} else {
			riskInfo.value = { riskLevel: "", content: "" };
		}
	} catch (error) {
		riskInfo.value = { riskLevel: "", content: "" };
		console.error("获取风险警告数据失败:", error);
	}
};

// 基本信息
const basicInfo = ref({
	img: "",
	name: "",
	eName: "",
	areaList: [],
	signTime: "",
	signPeople: "",
	bh: "",
	deadline: ""
});
const handleGetBasicInfo = async () => {
	try {
		const res = await getDecreeBasicInfo({id: decreeId.value});
		console.log("基本信息", res);
		if (res.code === 200 && res.data) {
			basicInfo.value.img = res.data.picture;
			basicInfo.value.name = res.data.name;
			basicInfo.value.eName = res.data.ename;
			basicInfo.value.areaList = res.data.industryList;
			basicInfo.value.signTime = res.data.postDate;
			basicInfo.value.bh = res.data.order;
			basicInfo.value.deadline = res.data.deadline;
			basicInfo.value.proposeOrgName = res.data.proposeOrgName;
		}
	} catch (error) {
		console.error("获取基本信息数据失败", error);
	}
};
handleGetBasicInfo();

// 政令关键词云
const wordCloudData = ref([])
const onKeyWordUp = async () => {
	try {
		const res = await getKeyWordUp({id: decreeId.value});
		console.log("政令关键词云", res);
		wordCloudData.value = res.data.slice(0, 10).map(item => ({name: item.name, value: item.count}));
	} catch (error) {
		console.error("获取政令关键词云数据失败", error);
	}
};

// 报告内容摘要
const box1Data = ref('');
const onOverview = async () => {
	try {
		const res = await getOverview({id: decreeId.value});
		console.log("报告内容摘要", res);
		box1Data.value = res.data;
	} catch (error) {
		console.error("获取报告内容摘要数据失败", error);
	}
};

// 相关事件
const relatedData = ref([]);
const handleGetRelateEvents = async () => {
	try {
		const res = await getDecreeRelatedEvent({id: decreeId.value});
		console.log("相关事件", res);
		if (res.code === 200 && res.data) {
			relatedData.value = res.data;
		} else {
			relatedData.value = [];
		}
	} catch (error) {
		relatedData.value = [];
		console.error("获取相关事件数据失败", error);
	}
};

// 发布机构
const box3TopTopData = ref({
	id: "",
	logo: "",
	name: "",
	eName: ""
});
// 机构动态
const eventList = ref([
	// {
	// 	time: "2025-07-31",
	// 	title: "美商务部发布指南，警告全球企业使用华为昇腾芯片可能违反美国出口管制。意在限制中国AI产业发展，阻碍其获得先进算力。"
	// },
	// {
	// 	time: "2025-07-25",
	// 	title: "美商务部持续对多种中国产品发起“双反”（反倾销、反补贴）调查并作出裁决，涉及产品从工业原料到日常用品，且裁定的税率普遍较高。"
	// },
	// {
	// 	time: "2025-07-21",
	// 	title: "美商务部进一步收紧对华先进半导体出口管制，将更多中国实体列入“实体清单”。限制14纳米及以下先进芯片、DRAM等对华出口"
	// }
]);
const box3TopBottomData = ref([]);
const handleGetOrgnization = async () => {
	try {
		const res = await getDecreeIssueOrganization({id: decreeId.value});
		console.log("发布机构/关键人物/机构动态", res);
		if (res.code === 200 && res.data) {
			let { id, image, name, ename } = res.data
			Object.assign(box3TopTopData.value, { id, logo: image, name, eName: ename });
			// eventList.value = res.data.newsList;
			eventList.value = res.data.orderInfoList;
			box3TopBottomData.value = res.data.personList;
		}
	} catch (error) {
		box3TopTopData.value = { id: "", logo: "", name: "", eName: "" };
		eventList.value = [];
		box3TopBottomData.value = [];
		console.error("获取发布机构数据失败", error);
	}
};
// 跳转行政机构主页
const handleToInstitution = item => {
	const curRoute = router.resolve({
		path: "/institution",
		query: { id: item.id }
	});
	window.open(curRoute.href, "_blank");
};
// 跳转人员详情
const handleClickUser = item => {
	window.sessionStorage.setItem('curTabName', item.name)
	const routeData = router.resolve({
		path: "/characterPage",
		query: {
			personId: item.id
		}
	});
	window.open(routeData.href, "_blank");
	// router.push({
	// 	path: "/characterPage",
	// 	query: {
	// 		personId: item.id
	// 	}
	// })
};

onMounted(() => {
	onOverview()
	onKeyWordUp()
	onRiskSignalData()
	handleGetRelateEvents();
	handleGetOrgnization();
});
</script>

<style lang="scss" scoped>
.page-box {
	width: 1600px;
	display: flex;
	flex-direction: column;
	padding: 16px 0;
}

.introduction-wrap {
	width: 100%;
	display: flex;

	.page-left {
		display: flex;
		flex-direction: column;
		flex: auto;
		margin-right: 16px;
		gap: 16px;

		.box1 {
			height: 414px;
			.box1-main {
				display: flex;
				padding: 0 24px;

				.box1-main-left {
					width: 395px;
					height: 332px;

					img {
						width: 100%;
					}
				}

				.box1-main-left-img-mock {
					width: 240px;
					height: 332px;
					background-color: #0b1932;
					display: flex;
					align-items: center;
					flex-direction: column;
					padding: 16px;

					.img-mock-badge-img {
						width: 80px;
						height: 80px;
					}

					.img-mock-badge-title {
						text-align: center;
						font-size: 20px;
						line-height: 28px;
						color: #fff;
					}

					.img-mock-badge-org {
						text-align: center;
						font-size: 14px;
						color: #fff;
					}
				}

				.box1-main-right {
					width: 20px;
					flex: auto;
					margin-left: 20px;
					color: rgba(59, 65, 75, 1);
					font-family: Microsoft YaHei;
					font-size: 16px;
					font-weight: 700;
					line-height: 24px;

					.item {
						display: flex;
						margin-bottom: 22px;

						.item-left {
							width: 100px;
						}

						.item-right {
							width: 20px;
							flex: auto;
						}

						.tag-box {
							display: flex;
							gap: 8px;
							flex-wrap: wrap;

							.tag {
								height: 24px;
								line-height: 24px;
								color: rgba(22, 119, 255, 1);
								font-family: Microsoft YaHei;
								font-size: 14px;
								font-weight: 400;
								box-sizing: border-box;
								border: 1px solid rgba(186, 224, 255, 1);
								border-radius: 4px;
								background: rgba(230, 244, 255, 1);
								padding: 0px 8px;
								margin-right: 8px;
							}
						}

						.text {
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
							font-weight: normal !important;
						}
					}
				}
			}
		}

		.box5-main {
			padding: 0px 22px 22px;
			.box5-text {
				padding-bottom: 20px;
				font-family: "Source Han Sans CN";
				font-weight: 400;
				font-size: 16px;
				line-height: 30px;
			}
		}

		.box2-main {
			padding: 0 22px;
			.box2-item {
				display: flex;
				height: 60px;
				width: 100%;
				margin-bottom: 2px;
				border-bottom: 1px solid rgba(240, 242, 244, 1);
				margin-top: 10px;
				.item-left {
					width: 64px;
					height: 48px;
					border-radius: 2px;
					font-size: 0px;
					img {
						width: 100%;
						height: 100%;
					}
				}
				.item-center {
					flex: auto;
					width: 20px;
					margin-left: 14px;

					.bubble-header {
						display: flex;
						align-items: center;
						cursor: pointer;

						.name {
							flex: auto;
							width: 20px;
							color: rgb(59, 65, 75);
							font-family: "Source Han Sans CN";
							font-size: 16px;
							font-weight: 700;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
							&:hover {
								text-decoration: underline;
								color: var(--color-main-active);
							}
						}

						.meta {
							margin-left: 10px;
							color: rgb(95, 101, 108);
							font-family: "Source Han Sans CN";
							font-size: 16px;
							font-weight: 400;
							letter-spacing: 0px;
							white-space: nowrap;
						}
					}

					.content {
						width: 100%;
						height: 30px;
						color: rgba(59, 65, 75, 1);
						font-family: Microsoft YaHei;
						font-size: 16px;
						font-weight: 400;
						line-height: 30px;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}
				}
			}
		}
		.box2-footer {
			margin: 20px 22px;
			display: flex;
			justify-content: space-between;
			.box2-footer-left {
				height: 20px;
				color: rgba(132, 136, 142, 1);
				font-family: Microsoft YaHei;
				font-size: 14px;
				font-weight: 400;
				line-height: 20px;
			}
		}
	}

	.page-right {
		display: flex;
		flex-direction: column;
		width: 520px;
		flex: none;
		gap: 16px;

		.box4 {
			height: 414px;
			.box4-main {
				height: 100%;
			}
		}

		.box3-top {
			border-bottom: 1px solid rgba(234, 236, 238, 1);
			padding: 0 22px;

			.box3-top-top {
				height: 88px;
				box-sizing: border-box;
				border: 1px solid rgba(234, 236, 238, 1);
				border-radius: 4px;
				background: rgba(247, 248, 249, 1);
				display: flex;
				align-items: center;
				margin: 0 auto;
				position: relative;
				padding: 0 16px;
				cursor: pointer;

				.left {
					width: 64px;
					height: 64px;
					font-size: 0px;
					img {
						width: 100%;
						height: 100%;
					}
				}

				.right {
					width: 20px;
					flex: auto;
					margin-left: 15px;
					.name {
						height: 26px;
						color: rgba(59, 65, 75, 1);
						font-family: Microsoft YaHei;
						font-size: 20px;
						font-weight: 700;
						line-height: 26px;
					}
					.ename {
						margin-top: 6px;
						height: 24px;
						color: rgba(95, 101, 108, 1);
						font-family: Microsoft YaHei;
						font-size: 16px;
						font-weight: 400;
						line-height: 24px;
					}
				}
			}
			.box3-top-bottom {
				height: 193px;
				margin: 0 auto;

				.box3-top-bottom-header {
					height: 40px;
					display: flex;
					padding-top: 14px;
					box-sizing: border-box;
					gap: 12px;

					.icon {
						margin-top: 5px;
						width: 14px;
						height: 14px;

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

					.text {
						width: 100px;
						height: 24px;
						color: rgba(59, 65, 75, 1);
						font-family: Microsoft YaHei;
						font-style: Bold;
						font-size: 16px;
						font-weight: 700;
						line-height: 24px;
						letter-spacing: 1px;
						text-align: left;
					}
				}

				.box3-top-bottom-main {
					margin-top: 2px;
					height: 130px;
					overflow: hidden;
					overflow-y: auto;
					display: flex;
					flex-wrap: wrap;
					justify-content: space-between;

					.box3-top-bottom-item {
						margin-top: 12px;
						height: 48px;
						width: 200px;
						display: flex;
						justify-content: flex-start;

						.box3-top-bottom-item-left {
							width: 48px;
							height: 48px;
							border-radius: 24px;
							overflow: hidden;

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

						.box3-top-bottom-item-right {
							margin-left: 8px;
							width: 144px;

							.name {
								width: 144px;
								height: 24px;
								color: rgba(59, 65, 75, 1);
								font-family: Microsoft YaHei;
								font-style: Bold;
								font-size: 16px;
								font-weight: 700;
								line-height: 24px;
								letter-spacing: 0px;
								text-align: left;
							}

							.position {
								margin-top: 1px;
								width: 144px;
								height: 24px;
								color: rgba(95, 101, 108, 1);
								font-family: Microsoft YaHei;
								font-style: Regular;
								font-size: 16px;
								font-weight: 400;
								line-height: 24px;
								letter-spacing: 0px;
								text-align: left;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
							}
						}
					}
				}
			}
		}
		.box3-bottom {
			padding-right: 22px;
			.box3-bottom-header {
				height: 59px;
				display: flex;

				.header-icon {
					margin-left: 22px;
					margin-top: 27px;
					width: 16px;
					height: 16px;

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

				.header-title {
					margin-left: 12px;
					margin-top: 23px;
					height: 24px;
					color: rgba(59, 65, 75, 1);
					font-family: Microsoft YaHei;
					font-size: 16px;
					font-weight: 700;
					line-height: 24px;
				}
			}

			.box3-bottom-main {
				:deep(.el-timeline) {
					padding: 8px 0px 0px 25px !important;
				}

				:deep(.el-timeline-item__node) {
					border: 4px solid var(--color-main-active) !important;
					background-color: #fff;
				}

				:deep(.el-timeline-item) {
					padding-bottom: 12px !important;
				}

				:deep(.el-timeline-item__timestamp) {
					color: var(--color-main-active) !important;
					font-family: Microsoft YaHei !important;
					font-size: 16px !important;
					font-weight: 600 !important;
					padding-top: 0px !important;
				}

				.timeline-content {
					color: var(--text-primary-65-color);
					font-family: Microsoft YaHei;
					font-size: 14px;
					font-weight: 400;
					line-height: 26px;
				}
			}
		}
	}
}
</style>