<template>
	<div class="case-wrapper">
		<div class="wrapper-header">
			<div class="header-filters">
				<div class="search-box">
					<el-input
						v-model="searchText"
						style="width: 240px; height: 32px"
						placeholder="搜索调查案件"
						@keyup.enter="handleSearch"
					>
						<template #prefix>
							<el-icon><Search /></el-icon>
						</template>
					</el-input>
				</div>
				<div class="dropdown-filters">
					<el-select v-model="filterStage" placeholder="全部阶段" class="filter-select" clearable>
						<el-option label="全部阶段" value="" />
						<el-option label="调查中" value="1" />
						<el-option label="调查结束" value="0" />
					</el-select>
					<el-select v-model="filterParty" placeholder="全部原告/被告" class="filter-select" clearable>
						<el-option label="全部原告/被告" value="" />
					</el-select>
					<el-select v-model="filterReason" placeholder="全部原因" class="filter-select" clearable>
						<el-option label="全部原因" value="" />
					</el-select>
				</div>
			</div>
			<div class="select-box">
				<div class="paixu-btn" @click="handleSwithSort">
					<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-box2">
					<div class="left-box2-header">
						<div class="icon"></div>
						<div class="title">{{ "科技领域" }}</div>
					</div>
					<div class="left-box2-main">
						<div class="checkbox-group">
							<el-checkbox
								v-model="checkAllAreas"
								:indeterminate="isIndeterminateAreas"
								@change="handleCheckAllAreasChange"
								class="filter-checkbox"
							>
								全部领域
							</el-checkbox>
							<div class="area-grid">
								<el-checkbox
									v-for="area in surveyAreaList"
									:key="area.id"
									v-model="checkedAreaList"
									:label="area.id"
									class="filter-checkbox"
									@change="handleCheckedAreasChange"
								>
									{{ area.name }}
								</el-checkbox>
							</div>
						</div>
					</div>
				</div>
				<div class="left-box1">
					<div class="left-box1-header">
						<div class="icon"></div>
						<div class="title">{{ "发布时间" }}</div>
					</div>
					<div class="left-box1-main">
						<div class="checkbox-group">
							<el-checkbox
								v-model="checkAllYears"
								:indeterminate="isIndeterminateYears"
								@change="handleCheckAllYearsChange"
								class="filter-checkbox"
							>
								全部时间
							</el-checkbox>
							<el-checkbox
								v-for="year in displayedYearList"
								:key="year.id"
								v-model="checkedSurveyYears"
								:label="year.id"
								class="filter-checkbox"
								@change="handleCheckedYearsChange"
							>
								{{ 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>
						</div>
					</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">{{ "232调查历程" }}</div>
				</div>
				<div class="right-main" v-loading="listLoading">
					<div
						class="timeline-item"
						v-for="(item, index) in surveyInfoList"
						:key="index"
						@click="handleToSingleCase(item)"
					>
						<div class="timeline-date">
							<div class="date-text">{{ item.timeZH || item.time }}</div>
						</div>
						<div class="timeline-line-box">
							<div class="timeline-icon">
								<img v-if="item.sortImageUrl" :src="item.sortImageUrl" alt="" />
								<div v-else class="default-dot"></div>
							</div>
							<div class="timeline-line" v-if="index !== surveyInfoList.length - 1"></div>
						</div>
						<div class="timeline-content-card">
							<div class="card-header">
								<div class="tag-box">
									<div class="tag-232">{{ item.tag }}</div>
									<div class="title">{{ item.title }}</div>
								</div>
								<div class="status" :class="{ 'status-active': item.status === '调查中' }">
									<span class="dot">•</span> {{ item.status }}
								</div>
							</div>
							<div class="card-body">
								{{ item.content }}
							</div>
							<div class="card-footer">
								<div class="area-tag" v-for="(area, idx) in item.areaList" :key="idx">
									{{ area }}
								</div>
							</div>
						</div>
					</div>
				</div>
				<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 router from "@/router";
import { useRoute } from "vue-router";
import { Search, ArrowDown, ArrowUp } from "@element-plus/icons-vue";
import { getSearchAllArea, getSearchAllYear, getSurveyList } from "@/api/marketAccessRestrictions";

const route = useRoute();

// 顶部过滤项
const searchText = ref("");
const filterStage = ref("");
const filterParty = ref("");
const filterReason = ref("");
const isSort = ref(false); // false 降序

const handleSwithSort = () => {
	isSort.value = !isSort.value;
};

// 科技领域过滤
const surveyAreaList = ref([]);
const checkedAreaList = ref([]);
const checkAllAreas = ref(true);
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 surveyYearList = ref([]);
const checkedSurveyYears = ref([]);
const checkAllYears = ref(true);
const isIndeterminateYears = ref(false);
const isYearExpanded = ref(false);

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

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 surveyInfoList = ref([]);
const totalDiscussNum = ref(0);
const pageSize = ref(10);
const currentPage = ref(1);
const isInitializing = ref(true);
const listLoading = ref(false);

const handleFetchSurveyList = async () => {
	listLoading.value = true;
	try {
		const params = {
			currentPage: currentPage.value - 1,
			pageSize: pageSize.value,
			sortCode: "232",
			publishYear: checkAllYears.value ? "" : checkedSurveyYears.value.toString(),
			Area: checkAllAreas.value ? "" : checkedAreaList.value.toString(),
			caseStatus: filterStage.value,
			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.map(item => {
				return {
					time: item.SEARCHDATE,
					timeZH: item.SEARCHDATEZH,
					title: item.SEARCHNAME,
					content: item.SEARCHDESC || item.SEARCHNAME,
					status: item.CASESTATUS || "调查中",
					areaList: item.searchArea || [],
					tag: item.SORTCODE || "232",
					id: item.SEARCHID,
					sortImageUrl: item.SORTIMAGEURL
				};
			});
			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();
};

const handleToSingleCase = item => {
	const curRoute = router.resolve({
		path: "/marketSingleCaseLayout/overview",
		query: {
			id: "232",
			searchId: item.id + ""
		}
	});
	window.open(curRoute.href, "_blank");
};

// 监听过滤条件
watch([checkedSurveyYears, checkedAreaList, isSort, filterStage, filterParty, filterReason], () => {
	if (isInitializing.value) return;
	currentPage.value = 1;
	handleFetchSurveyList();
});

const handleGetSearchAllArea = async () => {
	try {
		const res = await getSearchAllArea({ sortCode: "232" });
		if (res.code === 200 && res.data) {
			surveyAreaList.value = res.data.map(item => ({
				name: item.AREANAME,
				id: item.AREACODE
			}));
			handleCheckAllAreasChange(true);
		}
	} catch (error) {}
};

const handleGetSearchAllYear = async () => {
	try {
		const res = await getSearchAllYear({ sortCode: "232" });
		if (res.code === 200 && res.data) {
			const sortedYears = res.data.sort((a, b) => b - a);
			surveyYearList.value = sortedYears.map(item => ({
				name: item + "年",
				id: item
			}));
			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-top: 20px;

	.wrapper-header {
		height: 32px;
		margin-bottom: 16px;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.header-filters {
			display: flex;
			gap: 16px;
			align-items: center;

			.dropdown-filters {
				display: flex;
				gap: 12px;
				.filter-select {
					width: 140px;
					:deep(.el-input__wrapper) {
						background-color: #fff;
					}
				}
			}
		}

		.select-box {
			.paixu-btn {
				display: flex;
				align-items: center;
				gap: 8px;
				height: 32px;
				border: 1px solid #e6e7e8;
				border-radius: 4px;
				background: #fff;
				cursor: pointer;
				padding: 0 12px;
				.text {
					font-size: 14px;
					color: #5f656c;
				}
				.icon2 {
					width: 10px;
					img {
						width: 100%;
					}
				}
			}
		}
	}

	.wrapper-main {
		display: flex;
		justify-content: space-between;

		.left {
			width: 300px;
			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-box1,
			.left-box2 {
				margin-top: 17px;
				.left-box1-header,
				.left-box2-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;
				display: flex;
				flex-direction: column;

				.area-grid {
					display: grid;
					grid-template-columns: repeat(2, 1fr);
					gap: 4px;
				}

				.filter-checkbox {
					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: 1284px;
			min-height: 700px;
			border-radius: 10px;
			box-shadow: 0px 0px 15px 0px rgba(60, 87, 126, 0.2);
			background: #fff;
			padding-bottom: 20px;

			.right-header {
				height: 48px;
				display: flex;
				align-items: center;
				border-bottom: 1px solid #eaeeef;
				padding: 0 20px;
				.icon {
					width: 22px;
					img {
						width: 100%;
					}
				}
				.title {
					margin-left: 12px;
					font-size: 20px;
					font-weight: 700;
					color: var(--color-main-active);
				}
			}

			.right-main {
				padding: 30px 40px;

				.timeline-item {
					display: flex;
					cursor: pointer;
					&:hover {
						.timeline-content-card {
							background-color: #f5f7fa;
						}
					}

					.timeline-date {
						width: 100px;
						text-align: right;
						padding-right: 20px;
						.date-text {
							font-size: 16px;
							font-weight: 700;
							color: var(--color-main-active);
							line-height: 24px;
						}
					}

					.timeline-line-box {
						width: 40px;
						display: flex;
						flex-direction: column;
						align-items: center;
						position: relative;
						.timeline-icon {
							width: 32px;
							height: 32px;
							z-index: 2;
							background: #fff;
							display: flex;
							align-items: center;
							justify-content: center;
							img {
								width: 100%;
								height: 100%;
							}
							.default-dot {
								width: 12px;
								height: 12px;
								border-radius: 50%;
								background: orange;
							}
						}
						.timeline-line {
							width: 2px;
							flex: 1;
							background: #eaeeef;
							margin: 4px 0;
						}
					}

					.timeline-content-card {
						flex: 1;
						// border: 1px solid #eaeeef;
						border-radius: 8px;
						padding: 16px 20px;
						margin-left: 10px;
						margin-bottom: 24px;
						transition: all 0.3s;

						.card-header {
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-bottom: 12px;
							.tag-box {
								display: flex;
								align-items: center;
								gap: 12px;
								.tag-232 {
									padding: 2px 10px;
									border: 1px solid #b37feb;
									background: #f9f0ff;
									color: #722ed1;
									border-radius: 4px;
									font-weight: 700;
									font-size: 16px;
								}
								.title {
									font-size: 18px;
									font-weight: 700;
									color: #3b414b;
								}
							}
							.status {
								font-size: 16px;
								color: #84888e;
								.dot {
									margin-right: 4px;
								}
							}
							.status-active {
								color: var(--color-main-active);
							}
						}

						.card-body {
							font-size: 16px;
							color: #5f656c;
							line-height: 1.6;
							margin-bottom: 16px;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 2;
							line-clamp: 2;
							overflow: hidden;
						}

						.card-footer {
							display: flex;
							gap: 8px;
							.area-tag {
								padding: 2px 12px;
								background: #e6f7ff;
								border: 1px solid #91d5ff;
								color: #1890ff;
								border-radius: 4px;
								font-size: 14px;
							}
						}
					}
				}
			}

			.right-footer {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 40px;
				margin-top: 20px;
				.footer-left {
					font-size: 14px;
					color: #5f656c;
				}
			}
		}
	}
}
</style>
