<route lang="json5" type="page">
	{
	layout: 'default',
	style: {
	navigationStyle: 'custom',
	navigationBarTitleText: '公文/通知公告/法律法规/上级制度/厂级制度',
	},
	}
</route>

<template>
	<PageLayout :navTitle="type">
		<view class="container">
			<wd-search v-model="keyword" hide-cancel placeholder-left placeholder="搜索" shape="round" @change="onChange"
				class="search-box"></wd-search>
			<!-- 加载提示 -->
			<wd-loading v-if="loading && pageNo === 1" class="loading-tip">加载中...</wd-loading>
			<!-- 列表内容 -->
			<view v-for="(item, i) in list" :key="i">
				<wd-card :title="item.title" @click="getList(item.path)">
					<view class="card-content">
						<view class="meta-info">
							<wd-icon name="time" size="14px" color="#999"></wd-icon>
							<text class="meta-text">{{item.time}}</text>
							<wd-icon name="usergroup" size="14px" color="#999" style="margin-left: auto;"></wd-icon>
							<text class="meta-text">
								{{item.depart.length > 19 ? item.depart.substring(0, 18) + '...': item.depart}}</text>
						</view>
					</view>
				</wd-card>
			</view>
			<view class="load-more" v-if="loading && pageNo > 1">
				<wd-loading size="16px">正在加载...</wd-loading>
			</view>
		</view>
	</PageLayout>
</template>

<script setup>
	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh
	} from '@dcloudio/uni-app'
	import {
		queryDocumentApi,
		queryNoticeApi,
		querySuperiorSystemApi,
		queryFactorySystemApi,
		queryRegulationsApi
	} from '@/api/pages/file'
	let pageNo = 1
	let pageSize = 10
	let loading = false
	const list = ref([]) //文件列表
	const keyword = ref('') //查询绑定值
	const type = ref('') //绑定查询文件种类
	var ifH5 = false;

	const handleClickLeft = () => {
		uni.navigateBack()
	}

	const getList = (e) => {
		loading = true;
		const handlers = {
			'公文': getDocumentList,
			'通知公告': getNoticeList,
			'上级制度': getSuperiorSystemList,
			'厂级制度': getFactorySystemList,
			'法律法规': getRegulationsList
		};

		if (e === 1) {
			handlers[type.value]?.();
		} else if (type.value == '厂级制度') {
			uni.navigateTo({
				url: `./detail?data=${e}`
			})
		} else if (type.value !== '通知公告') {
			// 非通知公告类型执行预览
			onlinePreView(e);
		}
	}

	/*公文*/
	const getDocumentList = () => {
		queryDocumentApi({
			pageNo,
			pageSize,
			fwbt: formatSearchkey()
		}).then((res) => {
			if (res.success) {
				list.value = [...list.value, ...formatObj(res.result.records, 'fwbt', 'fwtime', 'wjlb',
					'wjbt')]
			}
			loading = false
		}).catch((err) => {
			loading = false
		})
	}

	/*通知公告*/
	const getNoticeList = () => {
		queryNoticeApi({
			pageNo,
			pageSize,
			neirong: formatSearchkey()
		}).then((res) => {
			if (res.success) {
				list.value = [...list.value, ...formatObj(res.result.records, 'neirong', 'createTime', 'fbdw')]
			}
			loading = false
		}).catch((err) => {
			loading = false
		})
	}

	/*上级制度*/
	const getSuperiorSystemList = () => {
		querySuperiorSystemApi({
			pageNo,
			pageSize,
			zdmc: formatSearchkey()
		}).then((res) => {
			if (res.success) {
				list.value = [...list.value, ...formatObj(res.result.records, 'zdmc', 'updateTime2', 'zbbm',
					'sszd')]
			}
			loading = false
		}).catch((err) => {
			loading = false
		})
	}

	/*厂级制度*/
	const getFactorySystemList = () => {
		queryFactorySystemApi({
			pageNo,
			pageSize,
			zdmc: formatSearchkey()
		}).then((res) => {
			if (res.success) {
				list.value = [...list.value, ...formatObj(res.result.records, 'zdmc', 'fatime',
					'zbbm_dictText', 'id')]
			}
			loading = false
		}).catch((err) => {
			loading = false
		})
	}

	/*法律法规*/
	const getRegulationsList = () => {
		queryRegulationsApi({
			pageNo,
			pageSize,
			flfgmc: formatSearchkey()
		}).then((res) => {
			if (res.success) {
				list.value = [...list.value, ...formatObj(res.result.records, 'flfgmc', 'updateTime2',
					'fabubumen', 'mingcheng')]
			}
			loading = false
		}).catch((err) => {
			console.log('err', err);
		})
	}

	const formatObj = (arr, title, time, depart, path) => {
		return arr.map((item) => ({
			...item,
			title: item[title],
			time: item[time],
			depart: item[depart],
			path: item[path]
		}))
	}

	const formatSearchkey = () => {
		if (keyword.value.trim()) {
			return '*' + keyword.value + '*'
		}
	}

	const onlinePreView = (e) => {
		console.log(e)
		uni.navigateTo({
			url: `/pages/onlinePreview/detail?data=${e}`
		});
	};

	const onChange = () => {
		pageNo = 1
		list.value = []
		getList(1)
	}

	onReachBottom(() => {
		if (loading) return
		pageNo++
		getList(1)
	})

	onPullDownRefresh(() => {
		pageNo = 1
		list.value = []
		getList(1)
		uni.stopPullDownRefresh()
	})

	onLoad((options) => {
		type.value = options.title
		getList(1)
		//#ifdef H5 || MP-WEIXIN
		ifH5 = true;
		//#endif
	});
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		background-color: #f7f7f7;
	}

	.search-box {
		margin: 16px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
		border-radius: 20px;

		// If you want to style the inner input
		::v-deep .wd-search__content {
			background-color: #fff;

			.wd-search__input {
				font-size: 14px;
				color: #333;
			}

			.wd-search__placeholder {
				color: #999;
			}
		}

		// If you want to style the search icon
		::v-deep .wd-search__search-icon {
			color: #999;
		}
	}

	.card-content {
		padding: 8px 0;

		.meta-info {
			display: flex;
			align-items: center;
			font-size: 12px;
			color: #666;

			.meta-text {
				margin-left: 4px;
			}
		}
	}

	.loading-tip {
		padding: 20px 0;
		text-align: center;
	}

	.load-more {
		padding: 16px 0;
		text-align: center;
		font-size: 14px;
		color: #999;
	}
</style>