<template>
	<view class="common_page adffc" :style="{'height':h+'px', 'padding-top':mt+'px', 'padding-bottom':pb+'rpx'}">
		<cus-header title="申领社会实践记录" bgColor="#FFFFFF"></cus-header>
		<div class="tab adf">
			<div class="tab-pre adfacjc" :class="{'active':tidx===1}" @tap="changeTab(1)">可申领</div>
			<div class="tab-pre adfacjc" :class="{'active':tidx===2}" @tap="changeTab(2)">已申领</div>
		</div>
		<div class="member">
			<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" @tap="changeMember(item,index)">
					<view class="cl_item" :class="{'active':midx===index}">
						<text>{{item.name}}</text>
					</view>
				</view>
			</scroll-view>
		</div>
		<template v-if="tidx===1">
			<div class="list" v-if="list.length">
				<up-list @scrolltolower="scrolltolower" style="height: 100%;">
					<up-list-item v-for="(item, index) in list" :key="index">
						<practice-box :item="item" @itemCheck="handleCheck"></practice-box>
					</up-list-item>
				</up-list>
			</div>
			<div class="dataEmpty" v-else>
				<page-empty text="暂无已申领记录"></page-empty>
			</div>
			<div class="btn" @click="handleApply">申领社会实践记录</div>
		</template>
		<template v-else-if="tidx===2">
			<div class="list" v-if="yslList.length">
				<up-list @scrolltolower="scrolltolower" style="height: 100%;">
					<up-list-item v-for="(item, index) in yslList" :key="index">
						<div class="ysl-box">
							<div class="ysl-box-title">{{'感恩有你 温暖前行'}}</div>
							<div class="ysl-box-tip">申领时间：{{'2025-06-01 ～ 2025-07-01'}}</div>
							<div class="ysl-box-tip">申 领 人：{{'张琳琳'}}</div>
							<div class="ysl-box-btn" @tap="handleDetail(item)">查看</div>
						</div>
					</up-list-item>
				</up-list>
			</div>
			<div class="dataEmpty" v-else>
				<page-empty text="暂无已申领记录"></page-empty>
			</div>
		</template>
	</view>
</template>

<script setup name="">
	import CusHeader from '@/components/CusHeader/index.vue'
	import PageEmpty from '@/components/pageEmpty/index.vue'
	import PracticeBox from '@/components/pages/practiceBox/index.vue'
	import { ref, getCurrentInstance, onMounted } from 'vue'
	const { proxy } = getCurrentInstance()
	
	const pb = ref(184)
	const tidx = ref(1)
	const midx = ref(0)
	const scrollLeft = ref(0)
	const queryParams = ref({
		page:1,
		limit:10,
		userId:'',
		memberId:''
	})
	const isOver = ref(false)
	const memberList = ref([])
	const list = ref([])
	const yslList = ref([])

	const changeTab = index => {
		tidx.value = index;
		pb.value = index===1?184:40;
		getDataByTab()
	}
	
	const changeMember = (item,index) => {
		midx.value = index;
		queryParams.value.memberId = item.id;
		getDataByTab()
	}
	
	const getDataByTab = () => {
		queryParams.value.page = 1;
		isOver.value = false;
		list.value = [];
		yslList.value = [1];
		if(!queryParams.value.memberId) queryParams.value.memberId = memberList.value[0]?.id;
		if(tidx.value===1){
			getKslList()
		}else if(tidx.value===2){
			getYslList()
		}
	}
	
	const handleDetail = item => {
		uni.navigateTo({
			url:'/pagesMy/practiceRecord'
		})
	}
	
	const scrolltolower = () => {
		if(isOver.value) return
		getDataByTab()
	}
	
	const getKslList = () => {
		proxy.$api.get('/core/social/practice/record/claimPage',queryParams.value).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			list.value = [...list.value,...res.data.list]
			list.value.forEach(l=>{
				l.activityStartTime = new Date(l.activityStartTime).Format('yyyy-MM-dd');
				l.activityEndTime = new Date(l.activityEndTime).Format('yyyy-MM-dd');
				l.check = false;
				l.age = getAge(l.idCard);
			})
			queryParams.value.page++;
			if(res.data.list.length===0) isOver.value = true
		})
	}
	
	const getYslList = () => {
		proxy.$api.get('/core/social/practice/record/claimedPage',queryParams.value).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			// yslList.value = [...yslList.value,...res.data.list]
			// queryParams.value.page++;
			// if(res.data.list.length===0) isOver.value = true
		})
	}
	
	const getMemberList = () => {
		proxy.$api.get('/core/family/member/page',{page:1,limit:-1}).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			memberList.value = res.data.list;
			getDataByTab()
		})
	}
	
	const isValid = (idCard) => {
		// 正则表达式校验18位身份证号码（最后一位可以是数字或X/x）
		const regex = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
		return typeof idCard === 'string' && regex.test(idCard);
	  }
	
	const getAge = (idCard) => {
		if (!isValid(idCard)) return 0
	
		// 从身份证的第7位开始，截取8位作为出生日期字符串 (YYYYMMDD)
		const birthDateStr = idCard.substring(6, 14);
		const birthYear = parseInt(birthDateStr.substring(0, 4), 10);
		const birthMonth = parseInt(birthDateStr.substring(4, 6), 10);
		const birthDay = parseInt(birthDateStr.substring(6, 8), 10);
	
		const today = new Date();
		const currentYear = today.getFullYear();
		const currentMonth = today.getMonth() + 1; // getMonth() 返回 0-11
		const currentDay = today.getDate();
	
		// 计算周岁
		let age = currentYear - birthYear;
	
		// 如果当前月份小于出生月份，或者月份相同但日期小于出生日期，说明今年的生日还没过
		if (currentMonth < birthMonth || (currentMonth === birthMonth && currentDay < birthDay)) {
		  age--;
		}
	
		return age < 0 ? 0 : age; 
	}
	
	const handleCheck = (item) => {
		item.check = !item.check;
	}
	
	const handleApply = () => {
		
	}
	
	onMounted(()=>{
		queryParams.value.userId = JSON.parse(uni.getStorageSync('userInfo')).id;
		getMemberList();
	})
</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{
		padding: 0;
		box-sizing: border-box;
		.tab{
			width: 100%;
			height: 102rpx;
			background: #FFFFFF;
			&-pre{
				width: 50%;
				height: 102rpx;
				position: relative;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 32rpx;
				color: #676775;
				line-height: 48rpx;
				&.active{
					font-weight: bold;
					font-size: 36rpx;
					color: #252525;
					&::after{
						content: '';
						width: 42rpx;
						height: 6rpx;
						background: #B7F358;
						border-radius: 3rpx;
						position: absolute;
						left: 50%;
						margin-left: -21rpx;
						bottom: 6rpx;
					}
				}
			}
		}
	
		.member{
			width: 100%;
			height: 56rpx;
			padding: 0 30rpx;
			margin-top: 20rpx;
			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;
				}
			}
		}

		.list{
			padding: 0 24rpx;
			flex: 1;
			overflow-y: auto;
			margin-top: 4rpx;
			
			.ysl-box{
				margin-top: 20rpx;
				padding: 36rpx 24rpx;
				position: relative;
				background: linear-gradient(45deg, #FFFFFF 80%, #F2FFE8 100%);
				&-title{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 32rpx;
					color: #151B29;
					line-height: 40rpx;
				}
				&-tip{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #676775;
					line-height: 24rpx;
					margin-top: 30rpx;
				}
				&-btn{
					width: 118rpx;
					height: 64rpx;
					background: #B7F358;
					border-radius: 45rpx;
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 26rpx;
					color: #151B29;
					line-height: 64rpx;
					text-align: center;
					letter-spacing: 2rpx;
					position: absolute;
					right: 24rpx;
					bottom: 40rpx;
				}
			}
		}
		
		.btn{
			width: calc(100% - 210rpx);
			height: 90rpx;
			background: #B7F358;
			border-radius: 45rpx;
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #151B29;
			line-height: 90rpx;
			text-align: center;
			letter-spacing: 2rpx;
			position: fixed;
			left: 105rpx;
			bottom: 64rpx;
		}
	}
</style>