<template>
	<div class="case-wrapper">
		<div class="wrapper-header">
			<div class="search-box">
				<el-input v-model="searchText" style="width: 360px; height: 32px" placeholder="搜索调查案件" @keyup.enter="handleSearch" :suffix-icon="Search"></el-input>
			</div>
			<div class="select-box">
				<div class="paixu-btn" @click="handleSwithSort">
					<div class="icon1">
						<img v-if="isSort" src="@/assets/icons/shengxu1.png" alt="" />
						<img v-else src="@/assets/icons/jiangxu1.png" alt="" />
					</div>
					<div class="text">{{ "发布时间" }}</div>
					<div class="icon2">
						<img v-if="isSort" src="@/assets/icons/shengxu2.png" alt="" />
						<img v-else src="@/assets/icons/jiangxu2.png" alt="" />
					</div>
				</div>
			</div>
		</div>
		<div class="wrapper-main">
			<div class="left">
        <!-- 科技领域 -->
				<div class="left-box">
					<div class="left-header">
						<div class="icon"></div>
						<div class="title">{{ "科技领域" }}</div>
					</div>
					<div class="left-main">
						<el-checkbox-group class="checkbox-group" v-model="checkedAreaList" @change="handleCheckedAreasChange">
							<el-checkbox class="filter-checkbox" label="全部领域"> 全部领域 </el-checkbox>
							<el-checkbox v-for="area in surveyAreaList" :key="area.id" :label="area.id" class="filter-checkbox">
								{{ area.name }}
							</el-checkbox>
						</el-checkbox-group>
					</div>
				</div>
				<!-- 发布时间 -->
				<div class="left-box">
					<div class="left-header">
						<div class="icon"></div>
						<div class="title">{{ "发布时间" }}</div>
					</div>
					<div class="left-main">
						<el-checkbox-group class="checkbox-group" v-model="checkedSurveyYears" @change="handleCheckedYearsChange">
							<el-checkbox class="filter-checkbox" label="全部时间"> 全部时间 </el-checkbox>
							<el-checkbox v-for="year in displayedYearList" :key="year.id" :label="year.id" class="filter-checkbox">
								{{ year.name }}
							</el-checkbox>
							<div v-if="surveyYearList.length > 6" class="expand-btn" @click="isYearExpanded = !isYearExpanded">
								{{ isYearExpanded ? "收起" : "更早" }}
								<el-icon>
									<ArrowUp v-if="isYearExpanded" />
									<ArrowDown v-else />
								</el-icon>
							</div>
						</el-checkbox-group>
					</div>
				</div>
			</div>
			<div class="right">
				<div class="right-header">
					<div class="icon">
						<img src="./assets/images/right-header-icon.png" alt="" />
					</div>
					<div class="title">{{ "301调查历程" }}</div>
				</div>
				<SurveyHistory v-loading="listLoading" :surveyList="surveyInfoList"></SurveyHistory>
				<div class="right-footer">
					<div class="footer-left">{{ `共 ${totalDiscussNum} 项` }}</div>
					<div class="footer-right">
						<el-pagination @current-change="handleCurrentChange" :pageSize="pageSize" :current-page="currentPage" background layout="prev, pager, next" :total="totalDiscussNum" />
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script setup>
import { ref, onMounted, watch, computed } from "vue";
import { Search, ArrowDown, ArrowUp } from "@element-plus/icons-vue";
import { getSearchAllArea, getSearchAllYear, getSurveyList } from "@/api/marketAccessRestrictions";
import SurveyHistory from "@/views/marketAccessRestrictions/com/SurveyHistory.vue"

const isSort = ref(true); // true 升序  false 倒序
const handleSwithSort = () => {
	isSort.value = !isSort.value;
};

const surveyYearList = ref([]);
const checkedSurveyYears = ref([]);
const isYearExpanded = ref(false);

const displayedYearList = computed(() => {
	if (isYearExpanded.value) {
		return surveyYearList.value;
	}
	return surveyYearList.value.slice(0, 6);
});

const checkAllYears = ref(false);
const isIndeterminateYears = ref(false);

const handleCheckAllYearsChange = (val) => {
	checkedSurveyYears.value = val ? surveyYearList.value.map(y => y.id) : [];
	isIndeterminateYears.value = false;
};

const handleCheckedYearsChange = (value) => {
	const checkedCount = value.length;
	checkAllYears.value = checkedCount === surveyYearList.value.length;
	isIndeterminateYears.value = checkedCount > 0 && checkedCount < surveyYearList.value.length;
};

const surveyAreaList = ref([]);
const checkedAreaList = ref([]);

const checkAllAreas = ref(false);
const isIndeterminateAreas = ref(false);

const handleCheckAllAreasChange = (val) => {
	checkedAreaList.value = val ? surveyAreaList.value.map(a => a.id) : [];
	isIndeterminateAreas.value = false;
};

const handleCheckedAreasChange = (value) => {
	const checkedCount = value.length;
	checkAllAreas.value = checkedCount === surveyAreaList.value.length;
	isIndeterminateAreas.value = checkedCount > 0 && checkedCount < surveyAreaList.value.length;
};

const totalDiscussNum = ref(0);
const pageSize = ref(10);
const currentPage = ref(1);
const isInitializing = ref(true);
const listLoading = ref(false);

const surveyInfoList = ref([]);

const handleFetchSurveyList = async () => {
	listLoading.value = true;
	try {
		const params = {
			currentPage: currentPage.value - 1,
			pageSize: pageSize.value,
			sortCode: "301",
			publishYear: checkAllYears.value ? "" : checkedSurveyYears.value.toString(),
			Area: checkAllAreas.value ? "" : checkedAreaList.value.toString(),
			// keywords: searchText.value,
			sortField: "date",
			sortOrder: isSort.value ? "asc" : "desc"
		};
		const res = await getSurveyList(params);
		if (res.code === 200 && res.data) {
			surveyInfoList.value = res.data.content;
			totalDiscussNum.value = res.data.totalElements || 0;
		}
	} catch (error) {
		console.error("获取调查列表失败", error);
	} finally {
		listLoading.value = false;
	}
};

const handleCurrentChange = (val) => {
	currentPage.value = val;
	handleFetchSurveyList();
};

const handleSearch = () => {
	currentPage.value = 1;
	handleFetchSurveyList();
};

watch(
	[checkedSurveyYears, checkedAreaList, isSort],
	() => {
		if (isInitializing.value) return;
		currentPage.value = 1;
		handleFetchSurveyList();
	},
	{
		deep: true
	}
);

const handleGetSearchAllArea = async () => {
	try {
		const res = await getSearchAllArea({
			sortCode: '301'
		});
		if(res.code === 200 && res.data) {
			surveyAreaList.value = res.data.map(item => {
				return {
					name: item.AREANAME,
					id: item.AREACODE
				};
			});
			// 默认选中全部
			checkAllAreas.value = true;
			handleCheckAllAreasChange(true);
		}
	} catch (error) {
	}
}

const handleGetSearchAllYear = async () => {
	try {
		const res = await getSearchAllYear({
			sortCode: '301'
		});
		if(res.code === 200 && res.data) {
			// 排序并格式化
			const sortedYears = res.data.sort((a, b) => b - a);
			surveyYearList.value = sortedYears.map(item => {
				return {
					name: item + '年',
					id: item
				};
			});
			// 默认选中全部
			checkAllYears.value = true;
			handleCheckAllYearsChange(true);
		}
	} catch (error) {
		
	}
}

onMounted(async () => {
	await Promise.all([handleGetSearchAllArea(), handleGetSearchAllYear()]);
	isInitializing.value = false;
	handleFetchSurveyList();
});
</script>

<style lang="scss" scoped>
.case-wrapper {
	width: 1600px;
	margin: 0 auto;
	padding: 20px 0;
	
	.wrapper-header {
		width: 100%;
		height: 32px;
		display: flex;
		margin-bottom: 16px;
		justify-content: space-between;
		.search-box {
			background-color: #fff;
			border: 1px solid rgba(234, 236, 238, 1);
			border-radius: 4px;
		}
		.select-box {
			height: 32px;
			box-sizing: border-box;
			.paixu-btn {
				display: flex;
				width: 120px;
				height: 32px;
				box-sizing: border-box;
				border: 1px solid rgba(230, 231, 232, 1);
				border-radius: 4px;
				background: rgba(255, 255, 255, 1);
				&:hover {
					background: var(--color-bg-hover);
				}
				cursor: pointer;
				.icon1 {
					width: 11px;
					height: 14px;
					margin-top: 10px;
					margin-left: 9px;
					img {
						width: 100%;
						height: 100%;
					}
				}
				.text {
					height: 19px;
					color: rgba(95, 101, 108, 1);
					font-family: Microsoft YaHei;
					font-size: 14px;
					font-weight: 400;
					line-height: 18px;
					letter-spacing: 0px;
					text-align: left;
					margin-top: 7px;
					margin-left: 9px;
				}
				.icon2 {
					width: 10px;
					height: 5px;
					margin-top: 5px;
					margin-left: 13px;
					img {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
	}
	
	.wrapper-main {
		display: flex;
		gap: 16px;
		
		.left {
			width: 360px;
			min-height: 560px;
			height: fit-content;
			padding-bottom: 20px;
			border-radius: 10px;
			box-shadow: 0px 0px 15px 0px rgba(60, 87, 126, 0.2);
			background: #fff;

			.left-box {
				margin-top: 17px;

				.left-header {
					display: flex;
					align-items: center;

					.icon {
						width: 8px;
						height: 16px;
						background: var(--color-main-active);
						border-radius: 0 2px 2px 0;
					}

					.title {
						margin-left: 17px;
						color: var(--color-main-active);
						font-size: 16px;
						font-weight: 700;
					}
				}
			}

			.checkbox-group {
				padding: 10px 0 0 25px;
				.filter-checkbox {
					width: 130px;
					margin-bottom: 8px;
					height: 32px;
					:deep(.el-checkbox__label) {
						font-size: 16px;
						color: #5f656c;
					}
				}

				.expand-btn {
					color: var(--color-main-active);
					font-size: 14px;
					cursor: pointer;
					display: flex;
					align-items: center;
					margin-top: 4px;

					.el-icon {
						margin-left: 4px;
					}
				}
			}
		}

		.right {
			width: 20px;
			flex: auto;
			min-height: 700px;
			border-radius: 10px;
			box-shadow: 0px 0px 15px 0px rgba(60, 87, 126, 0.2);
			background: rgba(255, 255, 255, 1);
			overflow: hidden;
			.right-header {
				height: 48px;
				display: flex;
				align-items: center;
				.icon {
					width: 22px;
					height: 18px;
					margin-left: 19px;
					img {
						width: 100%;
						height: 100%;
					}
				}
				.title {
					height: 26px;
					margin-left: 19px;
					color: var(--color-main-active);
					font-family: Microsoft YaHei;
					font-style: Bold;
					font-size: 20px;
					font-weight: 700;
					line-height: 26px;
					letter-spacing: 0px;
					text-align: left;
				}
			}
			.right-footer {
				box-sizing: border-box;
				padding-top: 15px;
				height: 60px;
				display: flex;
				justify-content: space-between;
				.footer-left {
					color: rgba(59, 65, 75, 1);
					font-family: Microsoft YaHei;
					font-size: 14px;
					font-weight: 400;
					line-height: 18px;
					margin-left: 24px;
					margin-top: 6px;
				}
				.footer-right {
					margin-right: 24px;
				}
			}
		}
	}
}
</style>