<template>
	<div class="wrapper" v-loading="loading">
		<div class="left">
			<div class="box1">
				<div class="box-header">
					<div class="header-left"></div>
					<div class="title">基本信息</div>
					<div class="header-right">
						<div class="icon">
							<img src="@/assets/icons/box-header-icon1.png" alt="" />
						</div>
						<div class="icon">
							<img src="@/assets/icons/box-header-icon2.png" alt="" />
						</div>
						<div class="icon">
							<img src="@/assets/icons/box-header-icon3.png" alt="" />
						</div>
					</div>
				</div>
				<div class="box1-main">
					<div class="box1-item">
						<div class="box1-item-left">{{ "案卷编号：" }}</div>
						<div class="box1-item-right">{{ baseInfo.SEARCHNUM || '-' }}</div>
					</div>
					<div class="box1-item">
						<div class="box1-item-left">{{ "调查时间：" }}</div>
						<div class="box1-item-right">
							{{ baseInfo.SEARCHDATE || '-' }}
						</div>
					</div>
					<div class="box1-item">
						<div class="box1-item-left">{{ "调查概括：" }}</div>
						<div class="box1-item-right">
							{{ baseInfo.SEARCHDESC || '-' }}
						</div>
					</div>
					<div class="box1-item">
						<div class="box1-item-left">{{ "涉及专利：" }}</div>
						<div class="box1-item-right">
							{{ baseInfo.patentCode || '-' }}
						</div>
					</div>
					<div class="box1-item">
						<div class="box1-item-left">{{ "调查背景：" }}</div>
						<div class="box1-item-right">
							{{ baseInfo.SEARCHBACKGROUND || '-' }}
						</div>
					</div>
					<div class="box1-item">
						<div class="box1-item-left">{{ "调查范围：" }}</div>
						<div class="box1-item-right">{{ baseInfo.SEARCHSCOPE || '-' }}</div>
					</div>
					<div class="box1-item">
						<div class="box1-item-left">{{ "法律依据：" }}</div>
						<div class="box1-item-right1">
							<div class="box1-item-right1-item" v-for="(item, index) in reasonList" :key="index">
								{{ item }}
							</div>
							<div v-if="!reasonList.length">-</div>
						</div>
					</div>
					<div class="box1-item">
						<div class="box1-item-left">{{ "调查结论：" }}</div>
						<div class="box1-item-right">
							{{ baseInfo.SEARCHRESULTBASIC || '-' }}
						</div>
					</div>
					<div class="box1-item">
						<div class="box1-item-left">{{ "增加关税：" }}</div>
						<div class="box1-item-right">{{ baseInfo.ISTARIFF === 'Y' ? `是，增加${baseInfo.ADDTARIFF}%` : '否' }}</div>
					</div>
				</div>
			</div>
		</div>
		<div class="right">
			<div class="box2" v-loading="box2Loading">
				<div class="box-header">
					<div class="header-left"></div>
					<div class="title">调查脉络</div>
					<div class="header-right">
						<div class="icon">
							<img src="@/assets/icons/box-header-icon1.png" alt="" />
						</div>
						<div class="icon">
							<img src="@/assets/icons/box-header-icon2.png" alt="" />
						</div>
						<div class="icon">
							<img src="@/assets/icons/box-header-icon3.png" alt="" />
						</div>
					</div>
				</div>
				<div class="box2-main">
					<div class="box2-line-box"></div>
					<div
						class="box2-item"
						:class="{ box2ItemFooter: index % 2 }"
						v-for="(item, index) in timeLineList"
						:key="index"
					>
						<div class="point" :class="{ pointFooter: index % 2 }">
							<img src="@/assets/images/dot.png" alt="" />
						</div>
						<div class="box2-item-header">
							<div class="title">{{ item.time }}</div>
						</div>
						<div class="box2-item-content">
							{{ item.content }}
						</div>
					</div>
					<div class="arrow-left">
						<img src="@/assets/icons/card-btn-left.png" alt="" />
					</div>
					<div class="arrow-right">
						<img src="@/assets/icons/card-btn-right.png" alt="" />
					</div>
				</div>
				<div class="box2-footer">
					<div class="box2-footer-left">
						<img src="@/assets/icons/box-footer-left-icon.png" alt="" />
					</div>
					<div class="box2-footer-center">
						{{
							`美国 232 调查认定，钕铁硼永磁体（关键领域核心材料）进口威胁其国家安全，因中国主导全球市场、美本土产能薄弱，未提关税，提议扶持本土供应链。`
						}}
					</div>
					<div class="box2-footer-right">
						<img src="@/assets/icons/box-footer-right-icon.png" alt="" />
					</div>
				</div>
			</div>
			<div class="box3" v-loading="box3Loading">
				<div class="box-header">
					<div class="header-left"></div>
					<div class="title">调查结论</div>
					<div class="header-right">
						<div class="icon">
							<img src="@/assets/icons/box-header-icon2.png" alt="" />
						</div>
						<div class="icon">
							<img src="@/assets/icons/box-header-icon3.png" alt="" />
						</div>
					</div>
				</div>
				<div class="box3-main">
					<div class="box3-main-item" v-for="(item, index) in surveyResult" :key="index">
						<div class="box3-main-item-header">
							<div class="header-left">{{ item.title }}</div>
							<div class="header-right">
								<div class="icon">
									<img src="./assets/images/open.png" alt="" />
								</div>
								<div class="text">{{ "跳转原文" }}</div>
							</div>
						</div>
						<div class="box3-main-item-content" v-for="(val, idx) in item.data" :key="idx">
							<div class="content-item">{{ val }}</div>
						</div>
					</div>
				</div>
				<div class="box3-footer">
					<div class="box3-footer-left">
						<img src="@/assets/icons/box-footer-left-icon.png" alt="" />
					</div>
					<div class="box3-footer-center">
						{{
							`美国 232 调查认定，钕铁硼永磁体（关键领域核心材料）进口威胁其国家安全，因中国主导全球市场、美本土产能薄弱，未提关税，提议扶持本土供应链。`
						}}
					</div>
					<div class="box3-footer-right">
						<img src="@/assets/icons/box-footer-right-icon.png" alt="" />
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import { getSearchBlurb, getSearchContext, getSearchConclusion } from "@/api/marketAccessRestrictions";

const route = useRoute();
const loading = ref(false);
const box2Loading = ref(false);
const box3Loading = ref(false);
const baseInfo = ref({});
const reasonList = ref([]);
const timeLineList = ref([]);
const surveyResult = ref([]);

const handleGetSearchBlurb = async () => {
	loading.value = true;
	try {
		const res = await getSearchBlurb({
			searchId: route.query.searchId,
			sortCode: "232"
		});
		if (res.code === 200 && res.data) {
			const data = res.data;
			baseInfo.value = data;

			// 法律依据
			if (data.billData && data.billData !== "null") {
				reasonList.value = [data.billData];
			} else {
				reasonList.value = [];
			}
		}
	} catch (error) {
		console.error("获取调查详情失败", error);
	} finally {
		loading.value = false;
	}
};

const handleGetSearchContext = async () => {
	box2Loading.value = true;
	try {
		const res = await getSearchContext({
			searchId: route.query.searchId
		});
		if (res.code === 200 && res.data) {
			timeLineList.value = res.data.map(item => ({
				time: item.CONTTIME,
				content: item.CONTDESC
			}));
		}
	} catch (error) {
		console.error("获取事件脉络失败", error);
	} finally {
		box2Loading.value = false;
	}
};

const handleGetSearchConclusion = async () => {
	box3Loading.value = true;
	try {
		const res = await getSearchConclusion({
			searchId: route.query.searchId
		});
		if (res.code === 200 && res.data) {
			surveyResult.value = res.data.map(item => {
				// 将 CONTENT 按换行符分割，并过滤掉空行
				const contentList = item.CONTENT ? item.CONTENT.split(/\r\n|\n/).filter(line => line.trim()) : [];
				return {
					title: item.TITLE,
					data: contentList
				};
			});
		}
	} catch (error) {
		console.error("获取调查结论失败", error);
	} finally {
		box3Loading.value = false;
	}
};

onMounted(() => {
	handleGetSearchBlurb();
	handleGetSearchContext();
	handleGetSearchConclusion();
});
</script>

<style lang="scss" scoped>
.wrapper {
	width: 100%;
	// height: 1191px;
	padding: 0 160px;
	display: flex;
	.box-header {
		height: 56px;
		display: flex;
		position: relative;
		.header-left {
			margin-top: 20px;
			width: 8px;
			height: 16px;
			border-radius: 0 4px 4px 0;
			background: var(--color-main-active);
		}
		.title {
			margin-left: 14px;
			margin-top: 16px;
			height: 24px;
			line-height: 24px;
			color: var(--color-main-active);
			font-family: Microsoft YaHei;
			font-size: 18px;
			font-weight: 700;
		}
		.header-btn-box {
			position: absolute;
			top: 14px;
			right: 52px;
			display: flex;
			.btn {
				margin-left: 8px;
				height: 28px;
				padding: 0 8px;
				box-sizing: border-box;
				border: 1px solid rgba(230, 231, 232, 1);
				border-radius: 4px;
				background: rgba(255, 255, 255, 1);
				text-align: center;
				line-height: 28px;
				color: rgba(59, 65, 75, 1);
				font-family: Microsoft YaHei;
				font-size: 16px;
				font-weight: 400;
				cursor: pointer;
			}
			.btnActive {
				border: 1px solid var(--color-main-active);
				background: rgba(246, 250, 255, 1);
				color: var(--color-main-active);
			}
		}
		.header-info {
			height: 22px;
			position: absolute;
			right: 84px;
			top: 17px;
			display: flex;
			justify-content: flex-end;
			.icon {
				margin-top: 3px;
				width: 14px;
				height: 14px;
				margin-right: 8px;
				img {
					width: 100%;
					height: 100%;
				}
			}
			.text {
				height: 22px;
				color: rgba(95, 101, 108, 1);
				font-family: Microsoft YaHei;
				font-size: 14px;
				font-weight: 400;
				line-height: 22px;
			}
		}
		.header-right {
			position: absolute;
			top: 14px;
			right: 12px;
			display: flex;
			justify-content: flex-end;
			gap: 4px;
			.icon {
				width: 28px;
				height: 28px;
				img {
					width: 100%;
					height: 100%;
				}
			}
		}
	}
	.left {
		width: 520px;
		.box1 {
			margin-top: 16px;
			width: 520px;
			height: 837px;
			border-radius: 4px;
			box-shadow: 0px 0px 15px 0px rgba(60, 87, 126, 0.2);
			background: rgba(255, 255, 255, 1);
			.box1-main {
				margin-top: 6px;
				margin-left: 22px;
				.box1-item {
					display: flex;
					margin-bottom: 16px;
					.box1-item-left {
						width: 100px;
						height: 24px;
						color: rgba(59, 65, 75, 1);
						font-family: Microsoft YaHei;
						font-size: 16px;
						font-weight: 700;
						line-height: 24px;
						margin-top: 3px;
						margin-bottom: 3px;
					}
					.box1-item-right {
						width: 346px;
						color: rgba(59, 65, 75, 1);
						font-family: Microsoft YaHei;
						font-size: 16px;
						font-weight: 400;
						line-height: 24px;
					}
					.box1-item-right1 {
						.box1-item-right1-item {
							width: 346px;
							color: rgba(59, 65, 75, 1);
							font-family: Microsoft YaHei;
							font-size: 16px;
							font-weight: 400;
							line-height: 24px;
						}
					}
				}
			}
		}
	}
	.right {
		width: 1064px;
		margin-left: 16px;
		.box2 {
			margin-top: 16px;
			width: 1064px;
			height: 410px;
			border-radius: 4px;
			box-shadow: 0px 0px 15px 0px rgba(60, 87, 126, 0.2);
			background: rgba(255, 255, 255, 1);
			.box2-main {
				margin: 0 auto;
				margin-top: 10px;
				height: 250px;
				// width: 1054px;
				// overflow-x: auto;
				// overflow-y: hidden;
				display: flex;
				justify-content: center;
				position: relative;
				padding-left: 120px;
				.box2-line-box {
					position: absolute;
					left: 0;
					top: 114px;
					height: 8px;
					width: 100%;
					background: url("@/assets/icons/line-bg.png") repeat;
				}
				.box2-item {
					width: 320px;
					height: 120px;
					// background: pink;
					position: relative;
					box-sizing: border-box;
					padding-left: 13px;
					border-left: 1px solid #0a57a6;
					margin-left: -100px;
					.box2-item-header {
						display: flex;
						width: 240px;
						.title {
							color: var(--color-main-active);
							height: 26px;
							font-family: Microsoft YaHei;
							font-size: 16px;
							font-weight: 700;
						}
					}
					.box2-item-content {
						width: 300px;
						min-height: 48px;
						max-height: 96px;
						color: rgba(95, 101, 108, 1);
						font-family: Microsoft YaHei;
						font-size: 16px;
						font-weight: 400;
						line-height: 24px;
						overflow: hidden;
					}
					.point {
						position: absolute;
						left: -8px;
						bottom: -7px;
						width: 15px;
						height: 15px;
						img {
							width: 100%;
							height: 100%;
						}
					}
					.pointFooter {
						position: absolute;
						left: -8px;
						top: -7px;
						width: 15px;
						height: 15px;
						img {
							width: 100%;
							height: 100%;
						}
					}
					.time {
						height: 24px;
						color: var(--color-main-active);
						font-family: Microsoft YaHei;
						font-size: 16px;
						font-weight: 700;
						line-height: 24px;
						position: absolute;
						bottom: -36px;
						left: 0;
					}
				}
				.box2ItemFooter {
					margin-top: 118px;
					margin-left: -120px;
					box-sizing: border-box;
					padding-top: 20px;
				}
				.arrow-left {
					width: 24px;
					height: 48px;
					position: absolute;
					left: 0;
					top: 95px;
					cursor: pointer;
					img {
						width: 100%;
						height: 100%;
					}
				}
				.arrow-right {
					width: 24px;
					height: 48px;
					position: absolute;
					right: 0;
					top: 95px;
					cursor: pointer;
					img {
						width: 100%;
						height: 100%;
					}
				}
			}
			.box2-footer {
				margin: 24px auto 0;
				width: 1032px;
				height: 64px;
				box-sizing: border-box;
				border: 1px solid rgba(231, 243, 255, 1);
				border-radius: 4px;
				background: rgba(246, 250, 255, 1);
				display: flex;
				align-items: center;
				gap: 13px;
				padding: 0 12px;
				.box2-footer-left {
					width: 19px;
					height: 20px;
					img {
						width: 100%;
						height: 100%;
					}
				}
				.box2-footer-center {
					width: 939px;
					color: rgba(5, 95, 194, 1);
					font-family: Microsoft YaHei;
					font-size: 16px;
					font-weight: 400;
					line-height: 24px;
					letter-spacing: 0px;
					text-align: justify;
				}
				.box2-footer-right {
					width: 24px;
					height: 24px;
					img {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
		.box3 {
			margin-top: 16px;
			margin-bottom: 35px;
			width: 1064px;
			// height: 714px;
			border-radius: 4px;
			box-shadow: 0px 0px 15px 0px rgba(60, 87, 126, 0.2);
			background: rgba(255, 255, 255, 1);
			padding-bottom: 18px;
			.box3-main {
				width: 1016px;
				// height: 550px;
				margin: 0 auto;
				overflow: hidden;
				overflow-y: auto;
				.box3-main-item {
					.box3-main-item-header {
						width: 1016px;
						height: 48px;
						box-sizing: border-box;
						border-bottom: 1px solid rgba(234, 236, 238, 1);
						border-top: 1px solid rgba(234, 236, 238, 1);
						background: rgba(247, 248, 249, 1);
						display: flex;
						justify-content: space-between;
						align-items: center;
						.header-left {
							margin-left: 12px;
							height: 24px;
							color: rgba(59, 65, 75, 1);
							font-family: Microsoft YaHei;
							font-size: 18px;
							font-weight: 700;
							line-height: 24px;
							letter-spacing: 0px;
							text-align: justify;
						}
						.header-right {
							display: flex;
							margin-right: 22px;
							gap: 5px;
							.icon {
								width: 16px;
								height: 16px;
								img {
									width: 100%;
									height: 100%;
								}
							}
							.text {
								height: 14px;
								color: rgba(5, 95, 194, 1);
								font-family: Microsoft YaHei;
								font-size: 12px;
								font-weight: 400;
								line-height: 14px;
								letter-spacing: 0px;
								text-align: justify;
							}
						}
					}
					.box3-main-item-content {
						.content-item {
							width: 1014px;
							padding: 12px 23px 12px 44px;
							color: rgba(59, 65, 75, 1);
							font-family: Microsoft YaHei;
							font-size: 16px;
							font-weight: 400;
							line-height: 30px;
							letter-spacing: 0px;
							text-align: justify;
							border-bottom: 1px solid rgba(234, 236, 238, 1);
						}
					}
				}
			}
			.box3-footer {
				margin: 24px auto 0;
				width: 1032px;
				height: 64px;
				box-sizing: border-box;
				border: 1px solid rgba(231, 243, 255, 1);
				border-radius: 4px;
				background: rgba(246, 250, 255, 1);
				display: flex;
				align-items: center;
				gap: 13px;
				padding: 0 12px;
				.box3-footer-left {
					width: 19px;
					height: 20px;
					img {
						width: 100%;
						height: 100%;
					}
				}
				.box3-footer-center {
					width: 939px;
					color: rgba(5, 95, 194, 1);
					font-family: Microsoft YaHei;
					font-size: 16px;
					font-weight: 400;
					line-height: 24px;
					letter-spacing: 0px;
					text-align: justify;
				}
				.box3-footer-right {
					width: 24px;
					height: 24px;
					img {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
	}
}
</style>
