<template>
	<div class="case-retrieval-container layout-padding">
		<el-card shadow="never" class="h100">
			<splitpanes class="h100" Vertical>
				<pane min-size="16" max-size="25" size="16" class="left-container">
					<el-scrollbar ref="scrollBarRef">
						<el-skeleton :loading="state.typeLoading" animated :rows="10">
							<template #default>
								<v-tree-search
									:data="state.typeOptions"
									titleField="name"
									keyField="id"
									selectable
									searchPlaceholder="案例分类名称"
									@select="selectType"
									@unselect="unSelectType"
									ref="treeSearchRef"
								>
									<template #node="{ node }">
										<span>{{ node.name }}</span>
									</template>
								</v-tree-search>
							</template>
						</el-skeleton>
					</el-scrollbar>
				</pane>
				<pane class="center-container">
					<div class="input-box">
						<el-select
							v-model="state.queryParams.RetrievalType"
							placeholder="请选择"
							class="width120"
							@change="handleQuery"
							:disabled="centerLoading"
						>
							<el-option label="标题" value="0" />
							<el-option label="关键词" value="1" />
							<el-option label="摘要" value="2" />
						</el-select>
						<div class="input-with-button w100">
							<div class="flex">
								<el-input
									v-model="state.queryParams.text"
									placeholder="关键词"
									clearable
									class="mr10 w100"
									@keyup.enter="handleQuery"
									:disabled="centerLoading"
								>
								</el-input>
								<el-button type="primary" class="btn" :loading="centerLoading" @click="handleQuery"
									><SvgIcon name="ele-Search" class="mr5" />搜索</el-button
								>
								<el-button @click="resetQuery" class="default-button" :loading="centerLoading">
									<SvgIcon name="ele-Refresh" class="mr5" />重置</el-button
								>
							</div>
						</div>
					</div>
					<div style="display: flex; margin: 10px 15px 0">
						<div style="height: 32px; line-height: 32px">排序：</div>
						<el-radio-group v-model="state.queryParams.SortField" @change="handleQuery" style="align-items: normal">
							<el-radio value="pageView">浏览量</el-radio>
							<!--							<el-radio value="score">评分</el-radio>-->
							<el-radio value="creationTime">创建时间</el-radio>
						</el-radio-group>
					</div>
					<div v-loading="centerLoading" class="center-container-box" style="height: calc(100% - 110px)">
						<template v-if="state.retrievalList.length">
							<el-scrollbar>
								<div v-for="(v, i) in state.retrievalList" :key="i" class="retrieval-content-item" @click="onPreview(v)" title="查看详情">
									<h4 class="mb10 text-no-wrap retrieval-content-item-title">
										<el-tag type="danger" class="mr10" effect="dark" v-if="v.isPopular">热门案例</el-tag> {{ v.title }}
									</h4>
									<div class="flex-center-between mt10 color-info">
										<div>
											<span class="mr10">创建时间：{{ formatDate(v.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
											<span v-if="v.keywords">关键词：{{ v.keywords }}</span>
										</div>
										<div class="flex-center-align">
											<!--											<span class="flex-center-align"><SvgIcon name="ele-StarFilled" size="18px" class="mr3" />{{ v.score }}</span>-->
											<!--											<span class="flex-center-align ml10"><SvgIcon name="ele-ChatDotSquare" size="16px" class="mr3" />{{ v.commentNum }}</span>-->
											<span class="flex-center-align ml10"><SvgIcon name="ele-View" size="16px" class="mr3" />{{ v.pageView }}</span>
										</div>
									</div>
								</div>
							</el-scrollbar>
						</template>
						<el-empty v-else description="暂无结果" />
						<pagination
							@pagination="queryList"
							:total="state.total"
							v-model:current-page="state.queryParams.PageIndex"
							v-model:page-size="state.queryParams.PageSize"
							:disabled="centerLoading"
						/>
					</div>
				</pane>
				<pane min-size="20" max-size="30" size="20" class="right-container">
					<p class="flex-center-between pt10">
						<span class="font16">常用案例前10</span>
						<el-button link type="primary" @click="querySearchNum"><SvgIcon name="ele-Refresh" class="mr4" /> 刷新</el-button>
					</p>
					<el-divider />
					<p class="flex-center-between">
						<span>排名</span>
						<span>阅读次数</span>
					</p>
					<el-skeleton :loading="rightLoading" animated :rows="10">
						<template #default>
							<div class="top10 mt10" style="height: calc(100% - 90px)">
								<template v-if="topList.length">
									<el-scrollbar>
										<div class="flex-center-between top10-items" v-for="(item, index) in topList" :key="item.id">
											<p class="flex-center-align top10-items-title" @click="onPreview(item)">
												{{ index + 1 }}.
												<el-text class="text-no-wrap color-primary cursor-pointer" @click="onPreview(item)">
													<TextTooltip :content="item.title" placement="top" effect="dark" />
												</el-text>
											</p>
											<span class="top10-items-num">{{ item.pageView }}</span>
										</div>
									</el-scrollbar>
								</template>
								<el-empty v-else />
							</div>
						</template>
					</el-skeleton>
				</pane>
			</splitpanes>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="caseRetrieval">
import { onMounted, reactive, ref, defineAsyncComponent } from 'vue';
import { useRouter } from 'vue-router';
import { formatDate } from '@/utils/formatTime';
import { throttle } from '@/utils/tools';
import { Splitpanes, Pane } from 'splitpanes';
import 'splitpanes/dist/splitpanes.css';
import { VTreeSearch } from '@wsfe/vue-tree';
import { caseTreeList } from '@/api/case/type';
import { caseRetrieval, caseRetrievalTop10 } from '@/api/case/retrieval';

const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const TextTooltip = defineAsyncComponent(() => import('@/components/TextTooltip/index.vue'));
const router = useRouter(); // 路由
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		text: null, // 关键词
		RetrievalType: '0', // 检索类型 默认全文
		OrderByType: 1, // 排序类型  降序
		SortField: 'pageView',
	},
	typeOptions: [], // 案例类型
	total: 0, // 总条数
	loading: false, // 加载状态
	retrievalList: [], // 检索列表
	typeLoading: false, // 案例类型loading
});
const topList = ref<EmptyArrayType>([]); // 常用案例前10
// 获取分类
const getType = async () => {
	state.typeLoading = true;
	try {
		const { result } = await caseTreeList({ IsEnable: true });
		state.typeOptions = result ?? [];
		state.typeLoading = false;
	} catch (error) {
		state.typeLoading = false;
	}
};
// 选择分类
const selectType = (data: any) => {
	state.queryParams.CaseTypeID = data.id;
	handleQuery();
};
// 取消选择
const unSelectType = () => {
	state.queryParams.CaseTypeID = null;
	handleQuery();
};
// 预览
const onPreview = (row: any) => {
	router.push({
		name: 'casePreview',
		params: {
			id: row.id,
			isAddPv: 'isAddPv',
			tagsViewName: row.title,
		},
	});
};
// 切换tab 查询列表
const rightLoading = ref(false); // 右侧加载状态
// 常用案例前10
const querySearchNum = () => {
	rightLoading.value = true;
	caseRetrievalTop10()
		.then((res: any) => {
			topList.value = res.result ?? [];
			rightLoading.value = false;
		})
		.catch(() => {
			rightLoading.value = false;
		});
};
const centerLoading = ref(false); // 中间加载状态
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	switch (state.queryParams.RetrievalType) {
		case '0': // 标题
			state.queryParams.Title = state.queryParams.text;
			state.queryParams.Abstract = null;
			state.queryParams.Keywords = null;
			break;
		case '1': // 关键词
			state.queryParams.Keywords = state.queryParams.text;
			state.queryParams.Abstract = null;
			state.queryParams.Title = null;
			break;
		case '2': // 摘要
			state.queryParams.Abstract = state.queryParams.text;
			state.queryParams.Title = null;
			state.queryParams.Keywords = null;
			break;
	}
	queryList();
};
const queryList = () => {
	centerLoading.value = true;
	caseRetrieval(state.queryParams)
		.then((res: any) => {
			state.retrievalList = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			centerLoading.value = false;
		})
		.catch(() => {
			centerLoading.value = false;
			state.retrievalList = [];
			state.total = 0;
		});
};
/** 重置按钮操作 */
const treeSearchRef = ref<RefType>();
const resetQuery = throttle(() => {
	state.queryParams.PageIndex = 1;
	state.queryParams.PageSize = 10;
	state.queryParams.text = null;
	state.queryParams.RetrievalType = '0';
	state.queryParams.SortField = 'pageView';
	state.queryParams.Abstract = null;
	state.queryParams.Title = null;
	state.queryParams.Keywords = null;
	treeSearchRef.value.setSelected(state.queryParams.CaseTypeID, false); // 清空选择
	treeSearchRef.value.clearKeyword(); // 清空搜索关键词
	treeSearchRef.value.search(); // 搜索
  state.queryParams.CaseTypeID = null;
	queryList();

	setTimeout(() => {
		treeSearchRef.value.setExpandAll(false); // 默认全部收起
	}, 300);
}, 500);
onMounted(() => {
	getType();
	queryList();
	querySearchNum();
});
</script>

<style scoped lang="scss">
.case-retrieval-container {
	.left-container {
		height: 100%;
	}
	.center-container {
		height: 100%;
		display: flex;
		flex-direction: column;
		.input-box {
			display: flex;
		}
		.retrieval-content {
			&-item {
				border-bottom: var(--el-border);
				padding: 10px 15px;
				margin-bottom: 10px;
				cursor: pointer;
				&:last-child {
					margin-bottom: 0;
					border: none;
				}
				&:hover {
					color: var(--el-color-primary);
				}
			}
		}
	}
	.right-container {
		height: 100%;
		.top10 {
			&-items {
				margin-bottom: 20px;
				&:last-child {
					margin-bottom: 0;
				}
				&-title {
					flex: 1;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
				&-num {
					display: inline-block;
					width: 50px;
					text-align: center;
				}
			}
		}
	}
	:deep(.el-tree-node__content) {
		height: 32px;
	}
	:deep(.el-card__body) {
		height: 100%;
	}
	.keyword-box {
		display: flex;
		flex: 1;
		flex-wrap: wrap;
		line-height: 18px;
		.keyword-item {
			margin-right: 5px;
			cursor: pointer;
			color: var(--el-color-info);
			font-size: var(--el-font-size-extra-small);
			&:last-child {
				margin-right: 0;
			}
			&:hover {
				color: var(--el-color-primary);
			}
		}
	}
}
</style>
