<template>
	<view class="common_page adffc" :style="{'height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title="家庭成员" bgColor="#FFFFFF"></cus-header>
		<view class="add">
			<view class="btn adfacjc" @click="handleAdd">
				<image src="https://oss.familydaf.cn/sxsnfile/20251218/53f6db8797e14d86ae7f81af03a3d971.png"></image>
				<text>添加</text>
			</view>
		</view>
		<template v-if="list.length">
			<view class="list">
				<up-list @scrolltolower="scrolltolower" style="height: 100%;">
					<up-list-item v-for="(item, index) in list" :key="index">
						<view class="box adfacjb">
							<view class="box-left adfac" @click="handleEdit(item,index)">
								<view class="box-left-edit">
									<image src="https://oss.familydaf.cn/sxsnfile/20251218/d5230de5913447c0864780312d5812fe.png"></image>
								</view>
								<view class="box-left-info">
									<view class="box-left-info-top adfac">
										<view class="name">{{item.name||''}}</view>
										<image class="sex" v-if="item.gender==1" src="https://oss.familydaf.cn/sxsnfile/20251218/473d5677fbdb4106acdb9a163377d27f.png"></image>
										<image class="sex" v-else-if="item.gender==0" src="https://oss.familydaf.cn/sxsnfile/20251218/02bc40a1c47b40f1a36b55cd0553211c.png"></image>
										<view class="age" :class="{'women':item.gender==1,'man':item.gender==0}">{{item.age}}岁</view>
									</view>
									<view class="box-left-info-bottom">
										身份证 {{item.idCardCopy}}
									</view>
								</view>
							</view>
							<view class="box-right" @click="handleDelete(item,index)">
								<image src="https://oss.familydaf.cn/sxsnfile/20251218/76399b32ad2c42f4ad227655ef0a19f5.png"></image>
							</view>
						</view>
					</up-list-item>
				</up-list>
			</view>
		</template>
		<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 { ref, onMounted, getCurrentInstance } from 'vue'
	const { proxy } = getCurrentInstance()
	
	const queryParams = ref({
		page:1,
		limit:10,
		userId:''
	})
	const isOver = ref(false)
	const list = ref([])
	
	const scrolltolower = () => {
		if(isOver.value) return
		getMemberList()
	}
	
	const getMemberList = () => {
		proxy.$api.get('/core/family/member/page',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)
				l.idCardCopy = l.idCard.replace(/^(\d{6})(\d{8})(\d{3}[\dX])$/i,'$1********$3')
			})
			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 handleAdd = () => {
		uni.navigateTo({
			url:'/pagesMy/familyMemberVindicate'
		})
	}
	
	const handleEdit = (item,index) => {
		uni.navigateTo({
			url:'/pagesMy/familyMemberVindicate?id='+item.id
		})
	}
	const handleDelete = (item,index) => {
		uni.showModal({
			title:'温馨提示',
			content:`是否确认删除家庭成员【${item.name}】?`,
			success: (res) => {
				if(res.confirm){
					proxy.$api.del('/core/family/member',[item.id]).then(({data:res})=>{
						if(res.code!==0) return proxy.$showToast(res.msg)
						proxy.$showToast('删除成功')
						queryParams.value.page = 1;
						isOver.value = false;
						list.value = [];
						getMemberList()
					})
				}
			}
		})
	}
	
	onMounted(()=>{
		try{
			queryParams.value.userId = JSON.parse(uni.getStorageSync('userInfo')).id;
			getMemberList()
		}catch(e){
			//TODO handle the exception
		}
	})
</script>

<style scoped lang="scss">
	.common_page{
		padding-bottom: 60rpx;
		.add{
			margin-top: 20rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 0 0;
			padding: 30rpx 24rpx 20rpx;
			.btn{
				width: 100%;
				background: rgba(112,207,82,0.08);
				border-radius: 24rpx;
				border: 1rpx dotted #70CF52;
				padding: 21rpx 0;
				image{
					width: 36rpx;
					height: 36rpx;
				}
				text{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 28rpx;
					color: #70CF52;
					line-height: 40rpx;
					margin-left: 18rpx;
					letter-spacing: 2rpx;
				}
			}
		}
		
		.list{
			flex: 1;
			overflow: auto;
			background: #FFFFFF;
			border-radius: 0 0 20rpx 20rpx;
			padding: 0 24rpx;
			.box{
				padding: 37rpx 0;
				box-shadow: inset 0rpx -1rpx 0rpx 0rpx #F2F2F2;
				&-left{
					width: calc(100% - 76rpx);
					&-edit{
						width: 36rpx;
						height: 36rpx;
						image{
							width: 100%;
							height: 100%;
						}
					}
					&-info{
						width: calc(100% - 36rpx);
						padding-left: 20rpx;
						box-sizing: border-box;
						&-top{
							.name{
								font-family: PingFang-SC, PingFang-SC;
								font-weight: bold;
								font-size: 32rpx;
								color: #151B29;
								line-height: 32rpx;
							}
							.sex{
								width: 44rpx;
								height: 32rpx;
								margin-left: 16rpx;
							}
							.age{
								border-radius: 13rpx;
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 20rpx;
								line-height: 24rpx;
								padding: 4rpx 10rpx;
								margin-left: 13rpx;
								&.women{
									background: rgba(244,101,122,0.14);
									color: #F4657A;
								}
								&.man{
									background: rgba(5,169,254,0.12);
									color: #05A9FE;
								}
							}
						}
						&-bottom{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #989998;
							line-height: 24rpx;
							margin-top: 23rpx;
						}
					}
				}
				
				&-right{
					width: 36rpx;
					height: 36rpx;
					image{
						width: 100%;
						height: 100%;
					}
				}
			}
		}
		
		.empty{
			margin-top: 300rpx;
			text-align: center;
			font-size: 32rpx;
			color: #666666;
		}
	}
</style>