<template>
	<view class="common_page adffc" :style="{'height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title="我的档案" bgColor="#FFFFFF"></cus-header>
		<view 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" @click="changeMember(item,index)">
					<view class="cl_item" :class="{'active':midx===index}">
						<text>{{item.name}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="list" v-if="list.length">
			<up-list @scrolltolower="scrolltolower" style="height: 100%;">
				<up-list-item v-for="(item, index) in list" :key="index">
					<ArchivesBox :item="item" @handleReviewFile="handleReview"></ArchivesBox>
				</up-list-item>
			</up-list>
			<ArchivesBox v-for="(item,index) in list" :key="index"></ArchivesBox>
		</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 ArchivesBox from '@/components/pages/archivesBox/index.vue'
	import PageEmpty from '@/components/pageEmpty/index.vue'
	import { ref, getCurrentInstance, onMounted } from 'vue'
	const { proxy } = getCurrentInstance()
	
	const midx = ref('')
	const scrollLeft = ref(0)
	const memberList = ref([])
	const isOver = ref(false)
	const queryParams = ref({
		page:1,
		limit:10,
		memberId:'',
		userId:''
	})
	const list = ref([])
	
	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 = () => {
		proxy.$api.get('/core/family/member/page',queryParams.value).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			memberList.value = res.data.list||[];
		})
	}
	
	const getList = () => {
		proxy.$api.get('/core/activity/signup/memberProfileList',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.age = getAge(l.idCard)
			})
			queryParams.value.page++;
			if(res.data.list.length===0) isOver.value = true;
		})
	}
	
	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 scrolltolower = () => {
		if(isOver.value) return
		getList()
	}
	
	const handleReview = (data) => {
		uni.navigateTo({
			url:'/pagesMy/archivesDetail?activityId='+data?.activityId+'&memberId='+data?.memberId
		})
	}
	
	onMounted(()=>{
		try{
			queryParams.value.userId = uni.getStorageSync('userInfo')&&JSON.parse(uni.getStorageSync('userInfo')).id;
			getMemberList()
			getList()
		}catch(e){
			//TODO handle the exception
		}
	})
</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 0 40rpx;
		box-sizing: border-box;
		
		.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;
		}
	}
</style>