<template>
	<div class="wrap">
		<div class="content-top">
			<div class="item">
				<div class="item-left-box">
					<div class="item-left1">{{ "总调查案件数" }}</div>
					<div class="item-left2">{{ "1980-2025" }}</div>
				</div>
				<div class="item-right">{{ `${totalCaseNum}项` }}</div>
			</div>
			<div class="item">
				<div class="item-left">{{ "仍在调查中的案件" }}</div>
				<div class="item-right">{{ `${onCaseNum}项` }}</div>
			</div>
			<div class="item">
				<div class="item-left">{{ "涉及中企数量" }}</div>
				<div class="item-right">{{ `${relateCnNum}家` }}</div>
			</div>
			<div class="item">
				<div class="item-left">{{ "胜诉/和解率" }}</div>
				<div class="item-right">{{ `${windRate}` }}</div>
			</div>
		</div>
		<div class="content-list">
			<div class="content-item" v-loading="box1Loading">
				<AnalysisBox title="美国对华337调查年度趋势">
					<div class="box-main">
						<div class="box-head" ref="chart1"></div>
						<TipTab style="margin-top: 16px;" />
					</div>
				</AnalysisBox>
			</div>
			<div class="content-item" v-loading="box2Loading">
				<AnalysisBox title="调查案件领域分布">
					<div class="box-main">
						<div class="box-head" ref="chart2"></div>
						<TipTab style="margin-top: -16px;" />
					</div>
				</AnalysisBox>
			</div>
		</div>
		<div class="content-list">
			<div class="content-item" v-loading="box3Loading">
				<AnalysisBox title="中国公司受调查情况">
					<template #header-btn>
						<div class="header-btn-box">
							<div class="btn" :class="{ btnActive: btnActiveName === '调查次数' }" @click="handleClickBox3Btn('调查次数')">
								调查次数
							</div>
							<div class="btn" :class="{ btnActive: btnActiveName === '注册地分布' }" @click="handleClickBox3Btn('注册地分布')">
								注册地分布
							</div>
						</div>
					</template>
					<div class="box-main">
						<div v-show="btnActiveName === '调查次数'" class="box-head" id="chart3"></div>
						<div v-show="btnActiveName === '注册地分布'" class="box-head2">
							<div class="map-box-left">
								<div class="map-box-left-item" v-for="(item, index) in mapData" :key="index">
									<div class="map-box-left-item-left">{{ index + 1 }}</div>
									<div class="map-box-left-item-center">{{ item.name }}</div>
									<div class="map-box-left-item-right">
										{{ item.value + "次" }}
									</div>
								</div>
							</div>
							<div class="map-box-right" id="chartMap"></div>
						</div>
						<TipTab />
					</div>
				</AnalysisBox>
			</div>
			<div class="content-item" v-loading="box4Loading">
				<AnalysisBox title="调查结果分布">
					<div class="box-main">
						<div class="box-head" ref="chart4"></div>
						<TipTab style="margin-top: -16px;" />
					</div>
				</AnalysisBox>
			</div>
		</div>
	</div>
</template>
<script setup>
import { ref, onMounted, nextTick } from "vue";
import setChart from "@/utils/setChart";
import TipTab from "@/components/base/TipTab/index.vue"

import { getStatCount, getStatcnOrgCount, getSearchResult, getStatArea, getStatNum } from "@/api/marketAccessRestrictions";

import createLineChart from "@/views/marketAccessRestrictions/marketAccessHome/utils/baseLineChart";
import createPieChart from "@/views/marketAccessRestrictions/marketAccessHome/utils/basePiechart.js";
import getBarChart from "./utils/barChart";
import getMapChart from "./utils/mapChart";

const box1Loading = ref(false);
const box2Loading = ref(false);
const box3Loading = ref(false);
const box4Loading = ref(false);
const btnActiveName = ref("调查次数");

const provinceCoords = {
	"北京": [116.46, 39.92],
	"上海": [121.48, 31.22],
	"天津": [117.2, 39.13],
	"重庆": [106.54, 29.59],
	"河北": [114.48, 38.03],
	"山西": [112.53, 37.87],
	"内蒙古": [111.65, 40.82],
	"辽宁": [123.38, 41.8],
	"吉林": [125.35, 43.88],
	"黑龙江": [126.63, 45.75],
	"江苏": [118.78, 32.04],
	"浙江": [120.19, 30.26],
	"安徽": [117.27, 31.86],
	"福建": [119.3, 26.08],
	"江西": [115.89, 28.68],
	"山东": [117.0, 36.65],
	"河南": [113.65, 34.76],
	"湖北": [114.31, 30.52],
	"湖南": [113.0, 28.21],
	"广东": [113.23, 23.16],
	"广西": [108.33, 22.84],
	"海南": [110.35, 20.02],
	"四川": [104.06, 30.67],
	"贵州": [106.71, 26.57],
	"云南": [102.73, 25.04],
	"西藏": [91.11, 29.97],
	"陕西": [108.95, 34.27],
	"甘肃": [103.73, 36.03],
	"青海": [101.74, 36.56],
	"宁夏": [106.27, 38.47],
	"新疆": [87.68, 43.77],
	"香港": [114.17, 22.28],
	"澳门": [113.54, 22.19],
	"台湾": [121.5, 25.05]
};

// 数量统计
const totalCaseNum = ref(0)
const onCaseNum = ref(0)
const relateCnNum = ref(0)
const windRate = ref('0%')
const handleGetStat = async () => {
	try {
		const res = await getStatCount({sortCode: '337'})
		console.log('337数量统计', res);
		if (res.code === 200 && res.data) {
			totalCaseNum.value = res.data.allCaseNum
			onCaseNum.value = res.data.underCaseNum
			relateCnNum.value = res.data.involvinChinese
			windRate.value = res.data.winRate
		}
	} catch (error) {
		console.error('337数量统计error', error);
	}
}

handleGetStat()

const handleGetStatcnOrgCount = async (type) => {
	box3Loading.value = true;
	try {
		const res = await getStatcnOrgCount({
			type,
			sortCode: "337"
		});
		console.log('中国公司受调查情况', res);

		if (res.code === 200 && res.data) {
			if (type === "01") {
				chart3Data.value = {
					name: res.data.map(item => item.ORGNAME),
					value: res.data.map(item => item.ORGCOUNT)
				};
				nextTick(() => {
					let chart3 = getBarChart(chart3Data.value.name, chart3Data.value.value);
					setChart(chart3, "chart3");
				});
			} else if (type === "02") {
				mapData.value = res.data.map(item => {
					const name = item.ORGPROVINCE//.replace(/省|市|自治区|特别行政区/g, "");
					return {
						name: item.ORGPROVINCE,
						value: item.PROVINCECOUNT,
						coord: provinceCoords[name] || [0, 0]
					};
				});
				nextTick(() => {
					let chartMap = getMapChart(mapData.value);
					setChart(chartMap, "chartMap");
				});
			}
		}
	} catch (error) {
		console.error("获取中国公司受调查情况失败", error);
	} finally {
		box3Loading.value = false;
	}
};

const handleClickBox3Btn = name => {
	btnActiveName.value = name;
	if (name === "调查次数") {
		handleGetStatcnOrgCount("01");
	} else {
		handleGetStatcnOrgCount("02");
	}
};

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

const chart1 = ref(null);
const handleGetStatNum = async () => {
	box1Loading.value = true;
	try {
		const res = await getStatNum({
			byYorM: "12",
			sortCode: "337"
		});
		if (res.code === 200 && res.data) {
			const sortedData = res.data.sort((a, b) => parseInt(a.searchYorM) - parseInt(b.searchYorM));
			chart1Data.value.title = sortedData.map(item => item.searchYorM);
			chart1Data.value.list = [
				{ name: "调查数量", value: sortedData.map(item => item.searchCount) }
			];
			nextTick(() => { createLineChart(chart1, chart1Data.value) });
		}
	} catch (error) {
		console.error("获取年度趋势数据失败", error);
	} finally {
		box1Loading.value = false;
	}
};

const chart2Data = ref([]);
const chart2 = ref(null);
const handleGetStatArea = async () => {
	box2Loading.value = true;
	try {
		const res = await getStatArea({ sortCode: "337" });
		console.log('调查案件领域分布', res);
		if (res.code === 200 && res.data) {
			chart2Data.value = res.data.filter(item => item.sortcode === "337" || item.sortname === "337调查").map(item => ({
				name: item.areaname,
				value: item.areacount
			}));
			nextTick(() => { createPieChart(chart2, chart2Data.value) });
		}
	} catch (error) {
		console.error("获取调查案件领域分布失败", error);
	} finally {
		box2Loading.value = false;
	}
};

const chart3Data = ref({
	name: [],
	value: []
});

const mapData = ref([]);

const chart4Data = ref([]);
const chart4 = ref(null)
const handleGetSearchResult = async () => {
	box4Loading.value = true;
	try {
		const res = await getSearchResult({
			sortCode: "337"
		});
		if (res.code === 200 && res.data) {
			chart4Data.value = res.data.map(item => ({
				name: item.RESULTNAME,
				value: item.RESULTNUM
			}));
			nextTick(() => { createPieChart(chart4, chart4Data.value) });
		}
	} catch (error) {
		console.error("获取调查结果分布失败", error);
	} finally {
		box4Loading.value = false;
	}
};

onMounted(() => {
	handleGetStatNum();
	handleGetStatArea();
	handleGetStatcnOrgCount(btnActiveName.value === "调查次数" ? "01" : "02");
	handleGetSearchResult();
});
</script>

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

	.content-top {
		display: flex;
		gap: 16px;

		.item {
			width: 20px;
			flex: auto;
			height: 80px;
			border-radius: 10px;
			box-shadow: 0px 0px 15px 0px rgba(60, 87, 126, 0.2);
			background: rgba(255, 255, 255, 1);
			position: relative;
			display: flex;
			align-items: center;
			justify-content: space-between;

			&::before {
				position: absolute;
				content: "";
				width: 4px;
				height: 49px;
				background: var(--color-main-active);
				left: 0;
				top: 15px;
			}

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

			.item-right {
				margin-right: 35px;
				height: 24px;
				color: var(--color-main-active);
				font-family: Microsoft YaHei;
				font-size: 30px;
				font-weight: 700;
				line-height: 24px;
			}

			.item-left-box {
				margin-left: 30px;
				font-size: 16px;
				line-height: 24px;
				.item-left1 {
					color: rgba(59, 65, 75, 1);
					font-family: Microsoft YaHei;
					font-weight: 700;
				}

				.item-left2 {
					color: rgba(95, 101, 108, 1);
					font-family: Microsoft YaHei;
				}
			}
		}
	}

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

	.header-btn-box {
		display: flex;
		.btn {
			margin-left: 8px;
			height: 28px;
			padding: 0 8px;
			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);
		}
	}

	.box-head2 {
		height: 100%;
		width: 100%;
		padding-top: 10px;
		display: flex;

		.map-box-left {
			width: 20px;
			flex: auto;
			height: 100%;

			.map-box-left-item {
				display: flex;
				align-items: center;
				height: 36px;
				margin-left: 20px;

				.map-box-left-item-left {
					width: 24px;
					height: 24px;
					border-radius: 12px;
					background: #e7f3ff;
					margin-right: 12px;
					color: #0a57a6;
					text-align: center;
					line-height: 24px;
				}

				.map-box-left-item-center {
					height: 36px;
					line-height: 36px;
					width: 20px;
					flex: auto;
				}

				.map-box-left-item-right {
					min-width: 80px;
					padding-right: 10px;
					text-align: right;
				}
			}
		}

		.map-box-right {
			width: 450px;
			height: 100%;
		}
	}
}
</style>