<template>
	<view class="favorites-page">
		<scroll-view class="search-list" scroll-y :refresher-enabled="true" :refresher-triggered="refreshing"
			@refresherrefresh="onRefresh" @scrolltolower="onLoadMore">
			<template v-for="item in list" :key="item.jobId">
				<recruitment :dataSource="item"></recruitment>
				<view class="driver"></view>
			</template>
			<view class="loading-more">
				<text v-if="loadingMore">加载中...</text>
				<text v-else-if="noMoreData">没有更多数据了</text>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		collectCancel,
		collectGetCollectList,
		getAppJobVO, tagGetListPage
	} from '../../api';
	import {
		ref
	} from 'vue';
	import {
		delay
	} from '../../utils';
	let currentPage = 1
	const list = ref([])
	const refreshing = ref(false)
	const loadingMore = ref(false)
	const noMoreData = ref(false)
	const pageSize = ref(10)

	const hanleGetList = async (params = {}) => {
		const res = await collectGetCollectList({
			pageNum: currentPage,
			pageSize: pageSize.value,
			...params
		})
		console.log(res)
		res.data.records = res.data.records.map((item) => ({
			...item,
			isCollect: true
		}))

		if (currentPage === 1) {
			// 刷新或首次加载
			list.value = res.data.records
		} else {
			// 加载更多

			if (res.data.records.length > 0) {
				list.value.records = [...list.value.records, ...res.data.records]
				currentPage++
			} else {
				noMoreData.value = true
			}

		}
	}
	
	onLoad(() => {
		currentPage = 1
		hanleGetList()
	})



	const onRefresh = async () => {
		console.log("刷新")
		refreshing.value = true
		loadingMore.value = false
		noMoreData.value = false
		try {
			await delay(1000)
			currentPage = 1
			await hanleGetList()
		} catch (err) {
			console.log(err)
			uni.showToast({
				title: '刷新失败',
				icon: 'none'
			})
		} finally {
			refreshing.value = false
		}
	}

	const onLoadMore = async () => {
		// 如果已经在加载或没有更多数据，则不执行
		console.log("dao dibule")
		if (loadingMore.value || noMoreData.value) return

		loadingMore.value = true
		currentPage++

		try {
			await delay(1000)
			await hanleGetList()
		} catch (error) {
			uni.showToast({
				title: '加载失败',
				icon: 'none'
			})
			currentPage--
		} finally {
			loadingMore.value = false
		}
	}
</script>

<style lang="scss">
	.favorites-page {
		// 页面容器设置
		height: 100vh;
		display: flex;
		flex-direction: column;
		background-color: #f8f8f8;
		// 列表样式 - 可滚动区域
		.search-list {
			height: 100vh;
			box-sizing: border-box;
			padding: 4px;
			// flex: 1;
			// overflow-y: auto;

			// 隐藏滚动条但保留滚动功能
			::-webkit-scrollbar {
				display: none;
			}

			-ms-overflow-style: none;
			scrollbar-width: none;
		}

		.driver {
			width: 100%;
			height: 1px;
			background-color: #eee;
		}

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