<template>
	<div class="wrap">
		<div class="content-list">
			<div class="content-item" v-loading="box1Loading">
				<AnalysisBox title="数量变化趋势">
					<template #header-btn>
						<div class="warning-text">{{ `${inProgressCount}项调查仍在进行中` }}</div>
					</template>
					<div class="box-main">
						<div class="box-head" ref="box1Chart"></div>
						<TipTab text="美对华301调查案件的数量变化趋势，数据来源：美国贸易代表办公室官网" style="margin-top: 16px" />
					</div>
				</AnalysisBox>
			</div>
			<div class="content-item" v-loading="box2Loading">
				<AnalysisBox title="国家分布情况">
					<template #header-btn>
						<el-select v-model="box2Paarams.years" @change="handleGetSearchCountry()" placeholder="选择时间" style="width:120px; margin-right:12px;">
							<el-option v-for="item in yearList" :key="item.value" :label="item.label" :value="item.value" />
						</el-select>
					</template>
					<div class="box-main">
						<div class="box-head" id="box2Chart"></div>
						<TipTab text="美301调查所涉及的国家分布情况，数据来源：美国贸易代表办公室官网" style="margin-top: 16px" />
					</div>
				</AnalysisBox>
			</div>
		</div>
		<div class="content-list">
			<div class="content-item" v-loading="box3Loading">
				<AnalysisBox title="调查方向及结果分布">
					<div class="box-main">
						<div class="box-head" id="box3Chart"></div>
						<TipTab text="美301调查方向及结果分布情况，数据来源：美国贸易代表办公室官网" style="margin-top: 16px;" />
					</div>
				</AnalysisBox>
			</div>
			<div class="content-item" v-loading="box4Loading">
				<AnalysisBox title="领域分布情况">
					<template #header-btn>
						<el-select v-model="box4Paarams.years" @change="handleGetStatArea()" placeholder="选择时间" style="width:120px; margin-right:12px;">
							<el-option v-for="item in yearList" :key="item.value" :label="item.label" :value="item.value" />
						</el-select>
					</template>
					<div class="box-main">
						<div class="box-head" ref="box4Chart"></div>
						<TipTab text="美对华301调查案件的领域分布情况，数据来源：美国贸易代表办公室官网" style="margin-top: -16px;" />
					</div>
				</AnalysisBox>
			</div>
		</div>
	</div>
</template>

<script setup>
import { ref, onMounted, nextTick, reactive } from "vue";
import setChart from "@/utils/setChart";
import getBarChart from "./utils/barChart";
import getSankeyChart from "./utils/sankey";
import {
	getSearchCountry,
	getStatArea,
	getStatNum,
	getSearchDirection
} from "@/api/marketAccessRestrictions";
import createLineChart from "@/views/marketAccessRestrictions/utils/baseLineChart";
import createPieChart from "@/views/marketAccessRestrictions/utils/basePiechart.js";
import { getNearYearList } from "@/views/marketAccessRestrictions/utils/index.ts";
const yearList = getNearYearList();

const inProgressCount = ref(0);

const box1Chart = ref(null);
const box1ChartData = ref({
	title: [],
	list: []
});
const box1Loading = ref(false);
const handleGetStatNum = async () => {
	box1Loading.value = true;
	try {
		const res = await getStatNum({ byYorM: "12", sortCode: "301" });
		console.log('数量变化趋势', res)
		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.list = [{ name: "301调查", value: sortedData.map(item => item.searchCount) }]
			inProgressCount.value = res.data.reduce((acc, cur) => acc + (cur.inSearchCount || 0), 0);
			nextTick(() => { createLineChart(box1Chart, box1ChartData.value) })
		}
	} catch (error) {
		console.error("获取调查年度数量趋势失败", error);
	}
	box1Loading.value = false;
};

const box2ChartData = ref({
	title: [],
	data: []
});
const box2Paarams = reactive({
	sortCode: 301,
	years: '2025'
})
const box2Loading = ref(false);
const handleGetSearchCountry = async () => {
	box2Loading.value = true;
	try {
		const res = await getSearchCountry(box2Paarams);
		console.log('国家分布情况', res)
		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);
	}
	box2Loading.value = false;
};

const box3ChartData = ref({
	nodes: [],
	links: []
});
const box3Loading = ref(false);
const handleGetSearchDirection = async () => {
	box3Loading.value = true;
	try {
		const res = await getSearchDirection({ sortCode: "301" });
		console.log('调查方向及结果分布', res)
		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);
	}
	box3Loading.value = false;
};

const box4ChartData = ref([]);
const box4Chart = ref(null)
const box4Paarams = reactive({
	sortCode: 301,
	years: '2025'
})
const box4Loading = ref(false);
const handleGetStatArea = async () => {
	box4Loading.value = true;
	try {
		const res = await getStatArea(box4Paarams);
		console.log('领域分布情况', res)
		if (res.code === 200 && res.data) {
			box4ChartData.value = res.data.map(item => ({name: item.areaname, value: item.areacount}));
		} else {
			box4ChartData.value = []
		}
	} catch (error) {
		box4ChartData.value = []
	}
	nextTick(() => { createPieChart(box4Chart, box4ChartData.value) })
	box4Loading.value = false;
};

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

<style lang="scss" scoped>
.wrap {
	width: 1600px;
	margin: 0 auto;
	padding: 20px 0;
	display: flex;
	flex-direction: column;
	gap: 16px;

	.content-list {
		height: 410px;
		display: flex;
		gap: 16px;

		.content-item {
			width: 20px;
			flex: auto;
			height: 100%;
			.box-main {
				width: 100%;
				height: 100%;
				padding: 12px 30px 20px;
				display: flex;
				flex-direction: column;
				.box-head {
					height: 20px;
					flex: auto;
				}
			}
		}
	}

	.warning-text {
		margin-right: 12px;
		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;
	}
}
</style>
