<template>
	<div class="case-wrapper">
		<div class="wrapper-header">
			<div class="search-box">
				<el-input
					v-model="searchText"
					style="width: 300px; height: 32px"
					placeholder="搜索调查案件"
					@keyup.enter="handleSearch"
				>
					<template #append
						><el-button @click="handleSearch"
							><el-icon><Search color="#84888E" /></el-icon></el-button
					></template>
				</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-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>
							<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 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">{{ "301调查历程" }}</div>
				</div>
				<div class="right-main" v-loading="listLoading">
					<div class="item" v-for="(item, index) in surveyInfoList" :key="index" @click="handleToSingleCase(item)">
						<div class="item-left">{{ item.time }}</div>
						<div class="item-center">
							<div class="icon">
								<!-- <img :src="item.img" alt="" /> -->
							</div>
							<div class="line" v-if="index !== surveyInfoList.length - 1"></div>
						</div>
						<div class="item-right">
							<div class="item-right-header">
								<div class="tag tag1">{{ item.tag }}</div>
								<div class="title">{{ item.title }}</div>
								<div class="status">
									<div class="status-icon"></div>
									<div class="status-text">{{ item.status }}</div>
								</div>
							</div>
							<div class="item-right-content">
								{{ item.content }}
							</div>
							<div class="item-right-footer">
								<div class="area-box" v-for="(val, idx) in item.areaList" :key="idx">
									<div class="area">{{ val }}</div>
								</div>
								<div class="flag-box">
									<div class="flag" v-for="(vall, idxx) in item.countryList" :key="idxx">
										<img :src="vall" alt="" style="width: 100%; height: 100%; border-radius: 50%;" />
									</div>
								</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 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.map(item => {
				return {
					time: item.SEARCHDATEZH || item.SEARCHDATE,
					title: item.SEARCHNAME,
					content: item.SEARCHNAME, // 或者其他字段，API文档没给出具体描述字段，暂用名称
					status: item.CASESTATUS,
					areaList: item.searchArea || [],
					countryList: item.countryImage || [],
					tag: item.SORTCODE || "301",
					id: item.SEARCHID
				};
			});
			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 handleToSingleCase = item => {
  console.log(item)
	// router.push('/marketSingleCaseLayout/overview')
	const curRoute = router.resolve({
		path: "/marketSingleCaseLayout/overview",
		query: {
			id: route.query.id + '',
      searchId: item.id + ''
		}
	});
	window.open(curRoute.href, "_blank");
};

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: 100%;
	// height: 100%;
	overflow: hidden;
	.wrapper-header {
		width: 1600px;
		height: 32px;
		margin: 16px auto;
		display: flex;
		justify-content: space-between;
		.search-box {
			width: 300px;
			display: flex;
		}
		.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 {
		width: 1600px;
		// height: 900px;
		margin: 0 auto;
		display: flex;
		justify-content: space-between;
		.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: rgba(255, 255, 255, 1);
			.left-box1 {
				margin-top: 17px;
				// height: 260px;
				.left-box1-header {
					display: flex;
					.icon {
						width: 8px;
						height: 16px;
						margin-top: 4px;
						border-radius: 2px 2px 0 0;
						background: var(--color-main-active);
					}
					.title {
						height: 2px;
						margin-left: 17px;
						color: var(--color-main-active);
						font-family: Microsoft YaHei;
						font-size: 16px;
						font-weight: 700;
						line-height: 24px;
					}
				}
			}
			.left-box2 {
				margin-top: 17px;
				.left-box2-header {
					display: flex;
					.icon {
						width: 8px;
						height: 16px;
						margin-top: 4px;
						border-radius: 2px 2px 0 0;
						background: var(--color-main-active);
					}
					.title {
						height: 2px;
						margin-left: 17px;
						color: var(--color-main-active);
						font-family: Microsoft YaHei;
						font-size: 16px;
						font-weight: 700;
						line-height: 24px;
					}
				}
			}
		}
		.right {
			margin-left: 16px;
			width: 1224px;
			min-height: 700px;
			height: 770px;
			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;
				border-bottom: 1px solid rgba(234, 236, 238, 1);
				.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-main {
				padding-top: 6px;
				height: 660px;
				border-bottom: 1px solid rgba(230, 231, 232, 1);
        overflow: auto;
				.item {
					height: 154px;
					display: flex;
					cursor: pointer;
					&:hover {
						background: var(--color-bg-hover) !important;
					}
					.item-left {
						width: 100px;
						color: var(--color-main-active);
						font-family: Microsoft YaHei;
						font-style: Bold;
						font-size: 16px;
						font-weight: 700;
						line-height: 24px;
						letter-spacing: 1px;
						text-align: right;
						padding-right: 16px;
					}
					.item-center {
						width: 30px;
						height: 154px;
						.icon {
							width: 30px;
							height: 30px;
							border-radius: 15px;
							background: orange;
						}
						.line {
							margin-left: 14px;
							width: 2px;
							height: 124px;
							border-radius: 1.5px;
							background: rgba(234, 236, 238, 1);
						}
					}
					.item-right {
						padding-left: 20px;
						.item-right-header {
							height: 26px;
							display: flex;
							align-items: center;
							.tag {
								width: 48px;
								height: 24px;
								padding: 0 8px;
								box-sizing: border-box;
								font-family: Microsoft YaHei;
								font-style: Bold;
								font-size: 16px;
								font-weight: 700;
								line-height: 24px;
								letter-spacing: 1px;
								text-align: left;
								border-radius: 4px;
							}
							.tag1 {
								border: 1px solid rgba(145, 202, 255, 1);
								background: rgba(230, 244, 255, 1);
								color: rgba(5, 95, 194, 1);
							}
							.tag2 {
								border: 1px solid rgba(255, 213, 145, 1);
								background: rgba(255, 247, 230, 1);
								color: rgba(250, 140, 22, 1);
							}
							.tag3 {
								border: 1px solid rgba(211, 173, 247, 1);
								background: rgba(249, 240, 255, 1);
								color: rgba(114, 46, 209, 1);
							}
							.title {
								margin-left: 9px;
								width: 880px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
								height: 26px;
								color: rgba(59, 65, 75, 1);
								font-family: Microsoft YaHei;
								font-style: Bold;
								font-size: 20px;
								font-weight: 700;
								line-height: 26px;
								letter-spacing: 0px;
								text-align: left;
							}
							.status {
								margin-left: 9px;
								display: flex;
								width: 76px;
								height: 24px;
								justify-content: space-between;
								align-items: center;
								.status-icon {
									width: 4px;
									height: 4px;
									border-radius: 2px;
									background: var(--color-main-active);
								}
								.status-text {
									color: var(--color-main-active);
									font-family: Microsoft YaHei;
									font-style: Regular;
									font-size: 16px;
									font-weight: 400;
									line-height: 24px;
									letter-spacing: 0px;
									text-align: right;
								}
							}
						}
						.item-right-content {
							margin-top: 9px;
							width: 1022px;
							height: 60px;
							color: rgba(95, 101, 108, 1);
							font-family: Microsoft YaHei;
							font-style: Regular;
							font-size: 16px;
							font-weight: 400;
							line-height: 30px;
							letter-spacing: 0px;
							text-align: justify;
						}
						.item-right-footer {
							margin-top: 9px;
							display: flex;
							gap: 8px;
							.area-box {
								display: flex;
								.area {
									height: 24px;
									padding: 0 8px;
									border-radius: 4px;
									background: rgba(231, 243, 255, 1);
									color: var(--color-main-active);
									font-family: Microsoft YaHei;
									font-style: Regular;
									font-size: 14px;
									font-weight: 400;
									line-height: 24px;
									letter-spacing: 0px;
									text-align: left;
								}
							}
							.flag-box {
								display: flex;
								.flag {
									width: 24px;
									height: 24px;
									border-radius: 12px;
									background: orange;
								}
							}
						}
					}
				}
			}
			.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;
				}
			}
		}
	}
}
.checkbox-group{
  padding-top: 10px;
  margin-left: 16px;
  .expand-btn {
    width: 140px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-main-active);
    font-size: 14px;
    cursor: pointer;
    user-select: none;
    &:hover {
      opacity: 0.8;
    }
    .el-icon {
      margin-left: 4px;
    }
  }
}
.filter-checkbox {
  width: 140px;
  margin-bottom: 8px;
}
</style>