<template>
	<view class="common_page adffc" :style="{'height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title="义工时长" bgColor="#FFFFFF"></cus-header>
		<view class="member" v-if="!isFamilyMember">
			<scroll-view class="scroll-view_H" scroll-x="true" scroll-with-animation="true" :scroll-left="scrollLeft">
				<view class="scroll-view-item_H" :id="'svih_'+index" v-for="(item,index) in memberList" :key="index" @click="changeMember(item,index)">
					<view class="cl_item" :class="{'active':midx===index}">
						<text>{{item.name}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="hours">义工时长：{{volunteerHours||0}}<text>小时</text></view>
		<view class="list" v-if="list.length">
			<up-list @scrolltolower="scrolltolower" style="height: 100%;">
				<up-list-item class="list-item" v-for="(item, index) in list" :key="index">
					<view class="time">{{item.createDate||""}}</view>
					<view class="content adfacjb">
						<view class="left adfac">
							<image src="https://oss.familydaf.cn/sxsnfile/20251218/681b23140cde486a8bf4b6844ce9b2e3.png"></image>
							<view class="texts">
								<view class="p">{{item.activityName??''}}</view>
								<view class="p tip">{{item.memberName||''}} - {{item.typeName??''}}</view>
							</view>
						</view>
						<view class="right adfac">
							<text>+{{item.volunteerHours??0}}</text>
						</view>
					</view>
				</up-list-item>
			</up-list>
		</view>
		<view class="dataEmpty" v-else>
			<page-empty text="暂无义工时长记录"></page-empty>
		</view>
	</view>
</template>

<script setup name="">
	import CusHeader from '@/components/CusHeader/index.vue'
	import PageEmpty from '@/components/pageEmpty/index.vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { ref, getCurrentInstance } from 'vue'
	const { proxy } = getCurrentInstance()
	
	const queryParams = ref({
		page:1,
		limit:10,
		memberId:'',
		userId:''
	})
	const midx = ref(0)
	const scrollLeft = ref(0)
	const memberList = ref([])
	const volunteerHours = ref(0)
	const isOver = ref(false)
	const isFamilyMember = ref(false)
	const list = ref([])
	
	const scrolltolower = () => {
		if(isOver.value) return
		getList()
	}
	
	const initList = () => {
		queryParams.value.page = 1;
		isOver.value = false;
		list.value = [];
	}
	const changeMember = (item,index) => {
		midx.value = index;
		queryParams.value.memberId = item.id;
		initList()
		getList()
	}
	
	const getMemberList = () => {
		let query = JSON.parse(JSON.stringify(queryParams.value))
		query.limit = -1;
		proxy.$api.get('/core/family/member/page',query).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			memberList.value = [{id:'',name:'全部'}].concat(res.data.list||[]);
		})
	}
	
	const getList = () => {
		proxy.$api.get('/core/love/value/record/volunteerHoursList',queryParams.value).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			list.value= [...list.value,...res.data.list];
			queryParams.value.page++
			if(res.data.list.length===0) isOver.value = true;
		})
	}
	
	onLoad(options=>{
		volunteerHours.value = options.volunteerHours||0;
		isFamilyMember.value = uni.getStorageSync('isFamilyMember')?true:false;
		queryParams.value.userId = uni.getStorageSync('userInfo')&&JSON.parse(uni.getStorageSync('userInfo')).id;
		if(uni.getStorageSync('familyMemberInfo')) queryParams.value.memberId = JSON.parse(uni.getStorageSync('familyMemberInfo')).id;
		getMemberList()
		getList()
	})
</script>

<style scoped lang="scss">
	.scroll-view_H {
		white-space: nowrap;
		width: 100%;
	}
	
	.scroll-view-item_H {
		display: inline-block;
		height: 100%;
		margin-left: 30rpx;
		&:first-child{
			margin-left: 0;
		}
	}
	
	.common_page{
		.member{
			width: 100%;
			height: 56rpx;
			margin-top: 20rpx;
			margin-bottom: 16rpx;
			box-sizing: border-box;
			.cl_item{
				padding: 0 26rpx;
				height: 56rpx;
				background: #FFFFFF;
				border-radius: 10rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 26rpx;
				color: #252525;
				line-height: 56rpx;
				text-align: center;
				&.active{
					background: #B7F358;
				}
			}
		}
		
		.hours{
			padding: 0 24rpx;
			height: 120rpx;
			background: #FFFFFF;
			border-radius: 24rpx;
			margin-top: 20rpx;
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #151B29;
			line-height: 120rpx;
			text{
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #989998;
				line-height: 120rpx;
				margin-left: 5rpx;
			}
		}
		
		.list{
			height: calc(100% - 160rpx);
			background: #FFFFFF;
			border-radius: 24rpx;
			margin-top: 20rpx;
			padding: 0 21rpx;
			&-item{
				padding: 36rpx 0;
				border-bottom: 1rpx solid #E7E7E7;
				.time{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #989998;
					line-height: 24rpx;
				}
				.content{
					margin-top: 36rpx;
					.left{
						width: calc(100% - 200rpx);
						image{
							width: 64rpx;
							height: 64rpx;
							display: table;
						}
						.texts{
							width: 100%;
							margin-left: 20rpx;
							.p{
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 32rpx;
								color: #151B29;
								line-height: 32rpx;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
								&.tip{
									font-size: 24rpx;
									color: #989998;
									line-height: 24rpx;
									margin-top: 18rpx;
								}
							}
						}
					}
					.right{
						width: 200rpx;
						justify-content: flex-end;
						text{
							font-family: DINAlternate, DINAlternate;
							font-weight: bold;
							font-size: 36rpx;
							color: #252525;
							line-height: 42rpx;
							text-align: right;
						}
					}
				}
			}
		}
	}
</style>