<template>
	<div class="wrap">
		<!-- <div class="sider-tab-box">
			<SiderTabs :siderList="siderTabList" @clickSiderItem="handleClickTabItem" />
		</div> -->
		<div class="top">
			<div class="box1 box" v-loading="box1Loading">
				<div class="box-header">
					<div class="header-left"></div>
					<div class="title">对华301调查年度数量趋势</div>
					<div class="warning-text">
						{{ `${inProgressCount}项调查仍在进行中` }}
					</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" id="box1Chart"></div>
				<div class="box-footer">
					<div class="box-footer-left">
						<img src="@/assets/icons/box-footer-left-icon.png" alt="" />
					</div>
					<div class="box-footer-center">
						{{ "近年来美对华301调查呈现案件频发、聚焦专利壁垒、力图阻断产业链升级的特点。" }}
					</div>
					<div class="box-footer-right">
						<img src="@/assets/icons/box-footer-right-icon.png" alt="" />
					</div>
				</div>
			</div>
			<div class="box2 box" v-loading="box2Loading">
				<div class="box-header">
					<div class="header-left"></div>
					<div class="title">301调查国家分布</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" id="box2Chart"></div>
				<div class="box-footer">
					<div class="box-footer-left">
						<img src="@/assets/icons/box-footer-left-icon.png" alt="" />
					</div>
					<div class="box-footer-center">
						{{ "现有调查以先进制造、半导体产业为主。" }}
					</div>
					<div class="box-footer-right">
						<img src="@/assets/icons/box-footer-right-icon.png" alt="" />
					</div>
				</div>
			</div>
		</div>
		<div class="bottom">
			<div class="box3 box" v-loading="box3Loading">
				<div class="box-header">
					<div class="header-left"></div>
					<div class="title">301调查 direction 及结果分布</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="box3-main" id="box3Chart"></div>
				<div class="box-footer">
					<div class="box-footer-left">
						<img src="@/assets/icons/box-footer-left-icon.png" alt="" />
					</div>
					<div class="box-footer-center">
						{{ "近年来美对华301调查呈现案件频发、聚焦专利壁垒、力图阻断产业链升级的特点。" }}
					</div>
					<div class="box-footer-right">
						<img src="@/assets/icons/box-footer-right-icon.png" alt="" />
					</div>
				</div>
			</div>
			<div class="box4 box" v-loading="box4Loading">
				<div class="box-header">
					<div class="header-left"></div>
					<div class="title">301调查领域分布</div>
					<div class="header-select-box">
						<el-select v-model="selectYear" @change="handleSelectYear" placeholder="选择时间" style="width: 120px">
							<el-option v-for="item in yearList" :key="item.value" :label="item.label" :value="item.value" />
						</el-select>
					</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="box4-main" id="box4Chart"></div>
				<div class="box-footer">
					<div class="box-footer-left">
						<img src="@/assets/icons/box-footer-left-icon.png" alt="" />
					</div>
					<div class="box-footer-center">
						{{ "主要集中在存储、芯片设计及通信领域，如长江存储、长鑫存储、华为、中兴等" }}
					</div>
					<div class="box-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 setChart from "@/utils/setChart";
import getLineChart from "./utils/lineChart";
import getBarChart from "./utils/barChart";
import getSankeyChart from "./utils/sankey";
import getPieChart from "./utils/piechart";
import getbarChart from "@/views/bill/utils/barchart";
import { getSearchCountry, getStatArea, getStatNum, getSearchDirection } from "@/api/marketAccessRestrictions";
import SiderTabs from "@/components/base/SiderTabs/index.vue"


// const siderTabList = ref([
// 	{
// 		name: '分析内11111',
// 		active: false
// 	},
// 	{
// 		name: '分析内2',
// 		active: true
// 	},
// 	{
// 		name: '分析内3',
// 		active: false
// 	},
// ])

// const handleClickTabItem = (value) => {
// 	console.log('val',value);
	
// }

const selectYear = ref("2025");

const yearList = ref([
	{
		label: "2025",
		value: "2025"
	},
	{
		label: "2024",
		value: "2024"
	},
	{
		label: "2023",
		value: "2023"
	}
]);

const inProgressCount = ref(0);
const box1Loading = ref(false);
const box2Loading = ref(false);
const box3Loading = ref(false);
const box4Loading = ref(false);

const box1ChartData = ref({
	title: [],
	data: []
});

const handleGetStatNum = async () => {
	box1Loading.value = true;
	try {
		const res = await getStatNum({
			byYorM: "12",
			sortCode: "301"
		});
		if (res.code === 200 && res.data) {
			const sortedData = res.data.sort((a, b) => parseInt(a.searchYorM) - parseInt(b.searchYorM));
			box1ChartData.value.title = sortedData.map(item => item.searchYorM);
			box1ChartData.value.data = sortedData.map(item => item.searchCount);
			inProgressCount.value = res.data.reduce((acc, cur) => acc + (cur.inSearchCount || 0), 0);

			const box1Chart = getLineChart(box1ChartData.value.title, box1ChartData.value.data);
			setChart(box1Chart, "box1Chart");
		}
	} catch (error) {
		console.error("获取调查年度数量趋势失败", error);
	} finally {
		box1Loading.value = false;
	}
};

const box2ChartData = ref({
	title: [],
	data: []
});

const box3ChartData = ref({
	nodes: [],
	links: []
});

const handleGetSearchDirection = async () => {
	box3Loading.value = true;
	try {
		const res = await getSearchDirection({
			sortCode: "301"
		});
		if (res.code === 200 && res.data) {
			const nodes = [];
			const links = [];
			const nodeNames = new Set();

			res.data.forEach(item => {
				if (item.SEARCHDIRECTION) nodeNames.add(item.SEARCHDIRECTION);
				if (item.SEARCHRESULT) nodeNames.add(item.SEARCHRESULT);

				if (item.SEARCHDIRECTION && item.SEARCHRESULT) {
					links.push({
						source: item.SEARCHDIRECTION,
						target: item.SEARCHRESULT,
						value: item.SEARCHCOUNT || 0
					});
				}
			});

			nodeNames.forEach(name => {
				nodes.push({ name });
			});

			box3ChartData.value = { nodes, links };
			const box3Chart = getSankeyChart(box3ChartData.value.nodes, box3ChartData.value.links);
			setChart(box3Chart, "box3Chart");
		}
	} catch (error) {
		console.error("获取调查方向及结果分布失败", error);
	} finally {
		box3Loading.value = false;
	}
};

const box4ChartData = ref([]);

// 切换年份
const handleSelectYear = val => {
	selectYear.value = val;
	handleGetStatArea();
};

const handleGetStatArea = async () => {
	box4Loading.value = true;
	const yearMap = {
		2023: 2023,
		2024: 2024,
		2025: 2025
	};
	const params = {
		years: yearMap[selectYear.value] || 2025,
		sortCode: 301
	};
	try {
		const res = await getStatArea(params);
		if (res.code === 200 && res.data) {
			box4ChartData.value = res.data
				.filter(item => item.SORTNAME === "301调查")
				.map(item => ({
					name: item.AREANAME,
					value: item.AREACOUNT
				}));
			const box4Chart = getPieChart(box4ChartData.value);
			setChart(box4Chart, "box4Chart");
		}
	} catch (error) {
		console.error("获取制裁领域分布失败", error);
	} finally {
		box4Loading.value = false;
	}
};

const handleGetSearchCountry = async () => {
	box2Loading.value = true;
	try {
		const res = await getSearchCountry({
			sortCode: 301,
			year: new Date().getFullYear() - 1
		});
		if (res.code === 200 && res.data) {
			box2ChartData.value = {
				title: res.data.map(item => ({
					img: item.COUNTRYIMAGE ? (item.COUNTRYIMAGE.startsWith("http") ? item.COUNTRYIMAGE : `http://${item.COUNTRYIMAGE}`) : "",
					name: item.COUNTRY
				})),
				data: res.data.map(item => item.NUM)
			};
			const box2Chart = getBarChart(box2ChartData.value.title, box2ChartData.value.data);
			setChart(box2Chart, "box2Chart");
		}
	} catch (error) {
		console.error("获取受调查国家分布失败", error);
	} finally {
		box2Loading.value = false;
	}
};

onMounted(() => {
	handleGetStatNum();
	handleGetSearchCountry();
	handleGetStatArea();
	handleGetSearchDirection();
});
</script>

<style lang="scss" scoped>
.wrap {
	overflow: hidden;
	position: relative;

	.sider-tab-box{
		position: absolute;
		left: 20px;
		top: 20px;
	}

	.box {
		width: 792px;
		height: 410px;
		border: 1px solid rgba(234, 236, 238, 1);
		border-radius: 10px;
		box-shadow: 0px 0px 20px 0px rgba(25, 69, 130, 0.1);
		background: rgba(255, 255, 255, 1);
	}

	.box-header {
		height: 46px;
		display: flex;
		position: relative;

		.header-left {
			margin-top: 18px;
			width: 8px;
			height: 20px;
			border-radius: 0 4px 4px 0;
			background: var(--color-main-active);
		}

		.title {
			margin-left: 14px;
			margin-top: 14px;
			height: 26px;
			color: var(--color-main-active);
			font-family: Microsoft YaHei;
			font-size: 20px;
			font-weight: 700;
			line-height: 26px;
		}

		.warning-text {
			position: absolute;
			top: 16px;
			right: 124px;
			height: 24px;
			color: rgba(206, 79, 81, 1);
			font-family: Microsoft YaHei;
			font-size: 18px;
			font-weight: 700;
			line-height: 24px;
			letter-spacing: 0px;
			text-align: center;
		}

		.header-select-box {
			position: absolute;
			top: 13px;
			right: 121px;
		}

		.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%;
				}
			}
		}
	}

	.box-footer {
		margin: 5px auto;
		width: 759px;
		height: 40px;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		gap: 10px;
		padding: 6px 12px 6px 12px;
		box-sizing: border-box;
		border: 1px solid rgba(231, 243, 255, 1);
		border-radius: 4px;
		background: rgba(246, 250, 255, 1);

		.box-footer-left {
			width: 19px;
			height: 20px;

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

		.box-footer-center {
			width: 666px;
			height: 24px;
			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;
		}

		.box-footer-right {
			width: 24px;
			height: 24px;

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

	.top {
		display: flex;
		justify-content: center;
		gap: 16px;
		margin: 16px auto;

		.box1 {
			.box1-main {
				height: 304px;
				// background: orange;
			}
		}

		.box2 {
			.box2-main {
				height: 304px;
				// background: orange;
			}
		}
	}

	.bottom {
		display: flex;
		justify-content: center;
		gap: 16px;
		margin: 0 auto;
		margin-bottom: 16px;

		.box3 {
			.box3-main {
				height: 304px;
				// background: orange;
			}
		}

		.box4 {
			.box4-main {
				height: 304px;
				// background: orange;
			}
		}
	}
}
</style>
