<template>
	<view class="main-content ">
		<view class="home-banner">
			<view class="home-banner-left">
				<img src="/static/serverImg/home/address.png" alt=""
					style="width: 41.67rpx;height:  41.67rpx;margin-right: 8rpx;">
				<text>{{ data.address }}</text>
			</view>
			<view>
				<up-icon name="bell" :size="25" color="rgba(46, 46, 46, 1)"></up-icon>
				<!-- <img src="/static/serverImg/home/message.png" alt="" style="width: 50rpx;height: 50rpx;"> -->
			</view>
		</view>
		<view class="home-main">
			<view class="custom-swiper">
				<up-swiper :list="list3" indicator indicatorMode="line" circular></up-swiper>
			</view>
		</view>

		<view class="home-grid">
			<Client />
		</view>

		<!-- <view class="home-grid hot-box">
			<view v-for="item in hotList" :key="item.key" class="hot-item">
				<view>
					<view>{{item.name}}</view>
					<view>{{item.text}}</view>
				</view>
				<up-icon :name="listItem.iconName" :size="22"></up-icon>
			</view>
		</view> -->

		<view class="home-ranking">
			<ServIces :leftList="leftList" :rightList="rightList" v-if="userType == 1"></ServIces>
			<RankingList v-if="userType === 2" />
		</view>

		<up-loadmore style="margin-top: 40rpx;margin-bottom: 40rpx;" :status="loadmoreInfo.status"
			:loadmoreText="loadmoreInfo.loadmoreTex" :loadingText="loadmoreInfo.loadingText"
			:nomoreText="loadmoreInfo.nomoreText" @loadmore="handleLoadmore" />


	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		onMounted
	} from 'vue';
	import {
		onLoad,
		onShow,
		onReachBottom
	} from "@dcloudio/uni-app";
	import RankingList from '@/pages/common/rankingList/index.vue';
	import ServIces from "@/components/Services/services.vue"
	import {
		Client
	} from "@/components/Client/new_file.vue"
	import {
		volunteerinfolist,
	} from "@/api/volunteerDetailsApi/details.js"
	import {
		slideshow
	} from '@/api/home.js'

	const listData = ref([])
	const leftList = ref([])
	const rightList = ref([])
	const userType = uni.getStorageSync('userType') //读取本地存储

	const data = reactive({
		address: '重庆市永川区',
		queryValue: ''
	})
	const list3 = ref([]);

	const hotList = [{
			key: 1,
			icon: '/static/img/构建.png',
			name: '专业辅导',
			text: '与家长协同制定成长计划',
		},
		{
			key: 2,
			icon: '/static/img/构建.png',
			name: '暖心陪护',
			text: '倾听烦恼、缓解孤独感',
		},
	]
	const hotList2 = [{
			key: 1,
			icon: '/static/img/构建.png',
			name: '专业辅导',
			text: '与家长协同制定成长计划',
		},
		{
			key: 2,
			icon: '/static/img/构建.png',
			name: '暖心陪护',
			text: '倾听烦恼、缓解孤独感',
		},
		{
			key: 3,
			icon: '/static/img/构建.png',
			name: '专业辅导',
			text: '与家长协同制定成长计划',
		},
		{
			key: 4,
			icon: '/static/img/构建.png',
			name: '暖心陪护',
			text: '倾听烦恼、缓解孤独感',
		},
	]

	// 分页
	const pages = ref({
		current: 1,
		pageSize: 10,
		total: 0
	})

	const loadmoreInfo = ref({
		status: 'loadmore',
		loadingText: '努力加载中...',
		loadmoreText: '点击加载更多~',
		nomoreText: '已经到底啦～'
	})

	// 加载更多
	async function handleLoadmore(e) {
		if (pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)) {
			pages.value.current += 1;
			loadmoreInfo.value.status = 'loading';
			await getList();
		} else {
			loadmoreInfo.value.status = 'nomore';
		}
	}

	const getList = async () => {
		try {
			loadmoreInfo.value.status = 'loading';

			// 请求时传递分页参数
			const res = await volunteerinfolist({
				pageNumber: pages.value.current, // 当前页码
				pageSize: pages.value.pageSize // 每页大小
			});

			console.log(res, '>>>>>>>res');

			if (!res || !res.rows) {
				return;
			}

			// 判断是否已经到了最后一页
			if (pages.value.current >= Math.ceil(res.total / pages.value.pageSize)) {
				loadmoreInfo.value.status = 'nomore';
			} else {
				loadmoreInfo.value.status = 'loadmore';
			}

			// 将数据分成左右两列
			res.rows.forEach((item, index) => {
				index % 2 !== 0 ? leftList.value.push(item) : rightList.value.push(item);
			});

			pages.value.total = res.total;
		} catch (error) {
			leftList.value = [];
			rightList.value = [];
			loadmoreInfo.value.status = 'loadmore';
			pages.value.total = 0;
			console.error('Error fetching data:', error);
		}
	};

	const getBanners = async () => {
		try {
			const res = await slideshow(7);
			if (res.code === 200 && res.data.picture) {
				list3.value = res.data.picture.split(',');
			}
		} catch (error) {
			console.log('error', error);

		}
	}

	onReachBottom(() => {
		if (pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)) {
			pages.value.current = pages.value.current + 1
			loadmoreInfo.value.status = 'nomore'
			getList()
		}
	})

	onShow(() => {
		getList()
		getBanners();
	})
</script>

<style scoped lang="scss">
	.main-content {
		.home-banner {
			display: flex;
			align-items: center;
			justify-content: space-between;
			background: rgba(255, 255, 255, 1);
			box-shadow: 0rpx 0rpx 0rpx rgba(0, 0, 0, 0), 0rpx 0rpx 0rpx rgba(0, 0, 0, 0), 0rpx 2.08rpx 4.17rpx rgba(0, 0, 0, 0.05);
			padding: 20rpx 16rpx;
		}

		.home-banner-left {
			display: flex;
			align-items: center;

		}

		.home-main {
			padding: 12px 16px;

			.custom-swiper {}

			.home-grid {
				margin-bottom: 32px;
			}

		}

		.home-ranking {
			padding: 12px 16px;
		}
	}

	.hot-box {
		padding: 12px 16px;
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		/* 3 列，每列等宽 */
		gap: 32rpx;

		.hot-item {
			.hot-item {
				padding: 12px 16px;
			}
		}
	}
</style>