<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">{{ `${isTariffNum}项` }}</div>
			</div>
			<div class="item">
				<div class="item-left">{{ "撤销案件梳理" }}</div>
				<div class="item-right">{{ `${cancelNum}` }}</div>
			</div>
		</div>
		<div class="content-list">
			<div class="content-item">
				<AnalysisBox title="232调查数量年度变化趋势">
					<template #header-btn>
						<div class="header-btn-box">
							<ActionButton :type="activeName === '1' ? 'active' : 'normal'" name="发起调查" @click="onStatNum('1')"></ActionButton>
							<ActionButton :type="activeName === '0' ? 'active' : 'normal'" name="结束调查" @click="onStatNum('0')"></ActionButton>
						</div>
					</template>
					<div class="box-main">
						<div class="box-head" ref="chart1"></div>
						<TipTab style="margin-top: 16px;" />
					</div>
				</AnalysisBox>
			</div>
			<div class="content-item">
				<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">
				<AnalysisBox title="关税税率">
					<div class="box-main">
						<div class="box-head" ref="chart3"></div>
						<TipTab style="margin-top: -16px;" />
					</div>
				</AnalysisBox>
			</div>
			<div class="content-item">
				<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 {
	getStatCount,
	getStatArea,
	getSearchCountry,
	getStatNum,
	getSearchTariff,
} from "@/api/marketAccessRestrictions";

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

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

const activeName = ref('1');
const chart1 = ref(null);
const onStatNum = async (event) => {
	if (event) activeName.value = event;
	try {
		const res = await getStatNum({ sortCode: 232, searchStatus:activeName.value })
		console.log('232调查数量年度变化趋势', res);
		if (res.code === 200 && res.data) {
			const chart1Data = {
				title: res.data.map(item => item.searchYorM),
				list: [
					{
						name: "232调查", value: res.data.map(item => item.searchCount)
					}
				]
			};
			nextTick(() => { createLineChart(chart1, chart1Data) });
		}
	} catch (error) {

	}
}

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

const chart3 = ref(null);
const onSearchTariff = async () => {
	try {
		const res = await getSearchTariff({ sortCode: "232" });
		console.log('关税税率', res);
		if (res.code === 200 && res.data?.length) {
			const chart3Data = [
				{ name: "税率25%+", value: res.data[0].TARIFF25 },
				{ name: "税率11%-25%", value: res.data[0].TARIFF11 },
				{ name: "税率1%-10%", value: res.data[0].TARIFF1 },
				{ name: "税率0%", value: res.data[0].TARIFF0 },
			];
			nextTick(() => { createPieChart(chart3, chart3Data) });
		}
	}catch (error) {}
}

const chart4 = ref(null);
const handleGetSearchCountry = async () => {
	try {
		const res = await getSearchCountry({ sortCode: 232 });
		if (res.code === 200 && res.data) {
			let chart4Data = res.data.map(item => ({ name: item.COUNTRY, value: item.NUM }) );
			nextTick(() => { createPieChart(chart4, chart4Data) });
		}
	} catch (error) {
		console.error("获取受调查国家分布失败", error);
	}
};

onMounted(() => {
	handleGetStat()
	onStatNum()
	handleGetStatArea()
	onSearchTariff()
	
	handleGetSearchCountry()
});
</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;
		gap: 8px;
		margin-right: 12px;
		.btn {
			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);
		}
	}
}
</style>