<template>
	<div class="case-wrapper">
		<div class="wrapper-header">
			<div class="header-filters">
				<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="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">
				<el-select v-model="isSort" placeholder="发布时间" style="width: 166px">
					<template #prefix>
						<div style="display: flex; align-items: center; height: 100%">
							<img src="@/assets/icons/jiangxu1.png" style="width: 14px; height: 14px" />
						</div>
					</template>
					<el-option label="按发布时间降序" value="desc" />
					<el-option label="按发布时间升序" value="asc" />
				</el-select>
			</div>
		</div>
		<div class="wrapper-main">
			<div class="left">
				<!-- 科技领域 -->
				<div class="check-box">
					<div class="check-head">
						<div class="head-name">{{ "科技领域" }}</div>
					</div>
					<el-checkbox-group class="check-list" v-model="checkedAreaList" @change="handleCheckedAreasChange">
						<el-checkbox class="check-item" v-for="item in surveyAreaList" :key="item.id" :label="item.id">
							{{ item.name }}
						</el-checkbox>
					</el-checkbox-group>
				</div>
				<!-- 发布时间 -->
				<div class="check-box">
					<div class="check-head">
						<div class="head-name">{{ "发布时间" }}</div>
					</div>
					<div class="left-main">
						<el-checkbox-group class="check-list" v-model="checkedYearList" @change="handleCheckedYearsChange">
							<el-checkbox class="check-item" v-for="item in surveyYearList" :key="item.id" :label="item.id">
								{{ item.name }}
							</el-checkbox>
						</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">{{ "232调查历程" }}</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 } from "vue";
import { Search } from "@element-plus/icons-vue";
import { getSearchAllArea, getSearchAllYear, getSurveyList } from "@/api/marketAccessRestrictions";
import SurveyHistory from "@/views/marketAccessRestrictions/com/SurveyHistory.vue"

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

// 科技领域过滤
const surveyAreaList = ref([]);
const checkedAreaList = ref(['']);
const handleGetSearchAllArea = async () => {
	try {
		const res = await getSearchAllArea({ sortCode: "232" });
		if (res.code === 200) {
			surveyAreaList.value = res.data.map(item => ({ name: item.AREANAME, id: item.AREACODE }));
		}
	} catch (error) {}
	surveyAreaList.value.unshift({ name: "全部领域", id: "" });
};
const handleCheckedAreasChange = event => {
	if (event.length && event[event.length-1] !== "") {
		checkedAreaList.value = event.filter(item => item !== "");
	} else {
		checkedAreaList.value = [""];
	}
	currentPage.value = 1;
	handleFetchSurveyList();
};

// 发布时间过滤
const surveyYearList = ref([]);
const checkedYearList = ref(['']);
const handleGetSearchAllYear = async () => {
	try {
		const res = await getSearchAllYear({ sortCode: "232" });
		if (res.code === 200) {
			let allYear = res.data.sort((a, b) => (b-a));
			let beforeYear = allYear.slice(6).join(',');
			surveyYearList.value = allYear.slice(0, 6).map(item => ({ name: item + "年", id: item }));
			if (beforeYear) surveyYearList.value.push({ name: "更早", id: beforeYear });
		}
	} catch (error) {}
	surveyYearList.value.unshift({ name: "全部时间", id: "" });
};
const handleCheckedYearsChange = event => {
	if (event.length && event[event.length-1] !== "") {
		checkedYearList.value = event.filter(item => item !== "");
	} else {
		checkedYearList.value = [""];
	}
	currentPage.value = 1;
	handleFetchSurveyList();
};

// 数据列表
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: checkedYearList.value.join(',') || null,
			Area: checkedAreaList.value.join(',') || null,
			caseStatus: filterStage.value,
			keywords: searchText.value || null,
			sortField: "date",
			sortOrder: isSort.value
		};
		const res = await getSurveyList(params);
		if (res.code === 200 && res.data) {
			surveyInfoList.value = res.data.content;
			totalDiscussNum.value = res.data.totalElements || 0;
		}
	} catch (error) {}
	listLoading.value = false;
};

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

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

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

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 {
		height: 32px;
		margin-bottom: 16px;
		display: flex;
		justify-content: space-between;
		align-items: center;

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

			.search-box {
				background-color: #fff;
				border: 1px solid rgba(234, 236, 238, 1);
				border-radius: 4px;
			}

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

	.wrapper-main {
		display: flex;
		gap: 16px;

		.left {
			width: 360px;
			min-height: 300px;
			height: fit-content;
			padding-bottom: 20px;
			border-radius: 10px;
			box-shadow: 0px 0px 15px 0px rgba(60, 87, 126, 0.2);
			background: #fff;

			.check-box {
				margin-top: 18px;
				.check-head {
					position: relative;
					margin-bottom: 12px;
					&::before {
						content: "";
						position: absolute;
						top: 0px;
						left: 0px;
						width: 8px;
						height: 100%;
						background: var(--color-primary-100);
						border-radius: 0 2px 2px 0;
					}
					.head-name {
						margin-left: 25px;
						color: var(--color-primary-100);
						font-size: 16px;
						line-height: 16px;
						font-family: Source Han Sans CN;
						font-weight: bold;
					}
				}
				.check-list {
					padding: 0 10px 0 25px;
					display: grid;
					grid-template-columns: repeat(2, 1fr);
					grid-gap: 0 12px;
					.check-item {
						width: 100%;
						height: 32px;
						:deep(.el-checkbox__label) {
							font-family: Source Han Sans CN;
							font-size: 16px;
							color: var(--text-primary-65-color);
						}
					}
				}
			}
		}

		.right {
			flex: auto;
			width: 20px;
			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;
				.icon {
					width: 22px;
					height: 18px;
					margin-left: 19px;
					img {
						width: 100%;
						height: 100%;
					}
				}
				.title {
					height: 26px;
					margin-left: 12px;
					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 {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 40px;
				margin-top: 20px;
				.footer-left {
					font-size: 14px;
					color: #5f656c;
				}
			}
		}
	}
}
</style>
