<template>
	<view class="tab_page" :style="{'height':h+'px', 'padding-top':mt+'px'}">
		<up-navbar title=" " bgColor="transparent">
			<template #left></template>
		</up-navbar>
		<image src="https://oss.familydaf.cn/sxsnfile/20251218/0ac9931a34bd432a8a44ef619fa51362.png" class="top_bg_img" mode="widthFix"></image>
		<view class="info">
			<view class="info-top adfacjb">
				<view class="info-top-left adfac">
					<image class="avatar" :src="userInfo?.avatarPath||'https://oss.familydaf.cn/sxsnfile/20251218/3821654e080945998d464f3c3aa64122.png'"></image>
					<view class="text" @click.prevent="showLogin">
						<view class="text-top">{{userInfo?userInfo?.realName:'未登录'}}</view>
						<view class="text-bottom lv" v-if="userInfo" @click.stop="handleTurnPage('/pagesMy/levelDetail')">Lv&nbsp;&nbsp;&nbsp;{{userInfo?.userLevel||0}}&nbsp;&nbsp;></view>
						<view class="text-bottom" v-else>去登录注册&nbsp;>></view>
					</view>
				</view>
				<view class="info-top-right" @click="handleTurnPage('/pagesMy/information')">
					<image src="https://oss.familydaf.cn/sxsnfile/20251218/a4bdf4248ddd44909e8707c83fe33265.png"></image>
				</view>
			</view>
			<view class="info-tip">
				<view class="info-tip-pre adfacjb" @click="handleTurnPage('/pagesMy/information')">
					<view class="left">家庭公益名称：{{userInfo?userInfo.welfareName:'暂无'}}</view>
					<view class="right">
						<image src="https://oss.familydaf.cn/sxsnfile/20251218/938f573288ad401fbd1e57404a870059.png"></image>
					</view>
				</view>
				<view class="info-tip-pre adfacjb" @click="handleTurnPage('/pagesMy/information')">
					<view class="left">家庭公益口号：{{userInfo?userInfo.welfareSlogan:'暂无'}}</view>
					<view class="right">
						<image src="https://oss.familydaf.cn/sxsnfile/20251218/938f573288ad401fbd1e57404a870059.png"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="box" style="margin-top: 40rpx;">
			<view class="box-title">公益记录</view>
			<view class="box-card adfacjb">
				<view class="box-card-pre adffcac red" @click="handleTurnPage('/pagesMy/heartNumber?loveValue='+numInfo?.loveValue)">
					<image class="top" src="https://oss.familydaf.cn/sxsnfile/20251218/046c057625704329a478d535cd1b4364.png"></image>
					<view class="num">{{(numInfo?.loveValue||0).toFixed(2)}}</view>
					<view class="text adf">
						<text>我的爱心值</text>
						<image src="https://oss.familydaf.cn/sxsnfile/20251218/2e8332c5f36a45cab3af336aa324d20c.png"></image>
					</view>
				</view>
				<view class="box-card-pre adffcac purple" @click="handleTurnPage('/pagesMy/archives')">
					<image class="top" src="https://oss.familydaf.cn/sxsnfile/20251218/ee5fffc2b10647ef9492150aed6bb29d.png"></image>
					<view class="num">{{(numInfo?.myArchives||0).toFixed(2)}}</view>
					<view class="text adf">
						<text>我的档案</text>
						<image src="https://oss.familydaf.cn/sxsnfile/20251218/6e9e879da03944a4911b48550e915641.png"></image>
					</view>
				</view>
				<view class="box-card-pre adffcac orange" @click="handleTurnPage('/pagesMy/volunteerHours?volunteerHours='+numInfo?.volunteerHours)">
					<image class="top" src="https://oss.familydaf.cn/sxsnfile/20251218/3fdb7e3c0d3c444ab03dd5f51a5e3fe0.png"></image>
					<view class="num">{{(numInfo?.volunteerHours||0).toFixed(2)}}</view>
					<view class="text adf">
						<text>义工时长(h)</text>
						<image src="https://oss.familydaf.cn/sxsnfile/20251218/625a9a7d454c4e7985bd7e12a9f1af93.png"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="box" @click="handleTurnPage('/pagesMy/achievement')">
			<view class="box-title">我的成就</view>
			<view class="box-achievement adfacjb">
				<view class="box-achievement-left">收获<span>{{numInfo?.myMedals||0}}</span>项成就</view>
				<view class="box-achievement-right adfacjb">
					<view class="imgs"></view>
					<view class="jt">
						<image src="https://oss.familydaf.cn/sxsnfile/20251218/938f573288ad401fbd1e57404a870059.png"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="box">
			<view class="box-title">其他功能</view>
			<view class="box-other adfacjb">
				<view class="box-other-pre adffcac" @click="handleTurnPage('/pagesMy/practice')">
					<image src="https://oss.familydaf.cn/sxsnfile/20251218/f6d2c342d9414909b4626e34909917c7.png"></image>
					<text>申领社会实践记录</text>
				</view>
				<view class="box-other-pre adffcac" @click="handleTurnPage('/pagesMy/familyMember')">
					<image src="https://oss.familydaf.cn/sxsnfile/20251218/248ed751f8bf4821b29cd236ec2dc78b.png"></image>
					<text>家庭成员</text>
				</view>
				<view class="box-other-pre adffcac" @click="handleTurnPage('/pagesMy/exclusiveScroll')">
					<image src="https://oss.familydaf.cn/sxsnfile/20251218/7ee3c42e8408489fb0768406d72b73b4.png"></image>
					<text>我的专享卷</text>
				</view>
			</view>
		</view>
		<CusTabbar :tabbarIndex="2"></CusTabbar>
		<login-register></login-register>
	</view>
</template>

<script setup name="">
	import CusTabbar from '@/components/CusTabbar/index.vue'
	import { ref, onMounted, watch, nextTick, getCurrentInstance } from 'vue'
	const { proxy } = getCurrentInstance();
	import { useUserStore } from '@/common/stores/user';
	const userStore = useUserStore();
	import { useGlobalShare } from '@/common/composables/useGlobalShare';
	const { isLogin } = useGlobalShare();
	
	const userInfo = ref(null)
	const numInfo = ref(null)
	
	const showLogin = () => {
		if(userInfo.value) return handleTurnPage('/pagesMy/information')
		userStore.openLoginModal();
	}
	
	const handleTurnPage = (url) => {
		if(!isLogin()) return
		if(url == '/pagesMy/achievement'){
			url = `/pagesMy/achievement?numInfo=`+JSON.stringify(numInfo.value)
		}
		uni.navigateTo({ url })
	}
	
	const getUserInfo = () => {
		try{
			proxy.$api.get(`/wx/${JSON.parse(uni.getStorageSync('userInfo')).id}`).then(({data:res})=>{
				if(res.code!==0) return proxy.$showToast(res.msg)
				userInfo.value = res.data;
				uni.setStorageSync('userInfo',JSON.stringify(userInfo.value));
			})
		}catch(e){
			userInfo.value = null
		}
	}
	
	const getUserNums = () => {
		proxy.$api.get(`/wx/userWelfareData/${JSON.parse(uni.getStorageSync('userInfo')).id}`).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			numInfo.value = res.data;
		})
	}
	
	watch(()=>userStore.token,(newVal,oldVal)=>{
		if(oldVal!=newVal&&newVal){
			setTimeout(()=>{
				uni.showToast({
					title: '登录成功',
					icon: 'success'
				});
			},200)
			nextTick(()=>{
				getUserInfo()
				getUserNums()
			})
		}
	})
	
	onMounted(()=>{
		if(uni.getStorageSync('userInfo')){
			getUserInfo()
			getUserNums()
		}
	})
</script>

<style scoped lang="scss">
	.tab_page{
		.info{
			margin-top: 40rpx;
			position: relative;
			padding: 0 24rpx 0 16rpx;
			&-top{
				&-left{
					.avatar{
						width: 148rpx;
						height: 148rpx;
						border-radius: 50%;
					}
					.text{
						margin-left: 24rpx;
						&-top{
							font-family: PingFang-SC, PingFang-SC;
							font-weight: bold;
							font-size: 40rpx;
							color: #151B29;
							line-height: 40rpx;
						}
						&-bottom{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 26rpx;
							color: #151B29;
							line-height: 26rpx;
							margin-top: 26rpx;
							&.lv{
								width: 137rpx;
								height: 42rpx;
								background: url('https://oss.familydaf.cn/sxsnfile/20251218/b6e6f5b45ad34f91bbaf66c9890fc2cd.png') no-repeat;
								background-size: 100% 100%;
								font-family: PingFangSC, PingFang SC;
								font-weight: 600;
								font-size: 26rpx;
								color: #FFFFFF;
								line-height: 38rpx;
								padding-left: 16rpx;
								box-sizing: border-box;
								margin-top: 18rpx;
							}
						}
					}
				}
				&-right{
					image{
						width: 48rpx;
						height: 48rpx;
					}
				}
			}
			&-tip{
				margin-top: 4rpx;
				&-pre{
					margin-top: 36rpx;
					.left{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #252525;
						line-height: 28rpx;
					}
					.right{
						image{
							width: 32rpx;
							height: 32rpx;
						}
					}
				}
			}
		}
		.box{
			background: #FFFFFF;
			border-radius: 24rpx;
			padding: 40rpx 24rpx 36rpx;
			margin-top: 20rpx;
			position: relative;
			&-title{
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 36rpx;
				color: #151B29;
				line-height: 40rpx;
			}
			&-card{
				margin-top: 72rpx;
				&-pre{
					width: calc(100% / 3 - 8rpx);
					height: 200rpx;
					position: relative;
					background-size: 100% 100%;
					background-repeat: no-repeat;
					.top{
						width: 82rpx;
						height: 82rpx;
						position: absolute;
						left: 50%;
						margin-left: -41rpx;
						top: -32rpx;
					}
					.num{
						font-family: DINAlternate, DINAlternate;
						font-weight: bold;
						font-size: 40rpx;
						line-height: 40rpx;
						text-align: center;
						margin-top: 86rpx;
					}
					.text{
						margin-top: 20rpx;
						text{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							line-height: 24rpx;
						}
						image{
							width: 26rpx;
							height: 26rpx;
							margin-left: 6rpx;
						}
					}
					&.red{
						background-image: url('https://oss.familydaf.cn/sxsnfile/20251218/57acefa1a0084d1e980002e165668bc9.png');
						.num,.text text{
							color: #FC5768;
						}
					}
					&.purple{
						background-image: url('https://oss.familydaf.cn/sxsnfile/20251218/e0f97b6ca8864d2ca0c3dc48b07dae58.png');
						.num,.text text{
							color: #8360E8;
						}
					}
					&.orange{
						background-image: url('https://oss.familydaf.cn/sxsnfile/20251218/eabb899f7ab14010b61fb30ed24bd818.png');
						.num,.text text{
							color: #FF8200;
						}
					}
				}
			}
			&-achievement{
				margin-top: 24rpx;
				&-left{
					width: 210rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #676775;
					line-height: 24rpx;
					label{
						font-weight: bold;
						font-size: 32rpx;
						color: #EA3044;
						line-height: 32rpx;
						margin: 0 7rpx;
					}
				}
				&-right{
					width: calc(100% - 210rpx);
					.jt{
						image{
							width: 32rpx;
							height: 32rpx;
						}
					}
				}
			}
			&-other{
				margin-top: 39rpx;
				&-pre{
					width: calc(100% / 3);
					image{
						width: 48rpx;
						height: 48rpx;
					}
					text{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #151B29;
						line-height: 33rpx;
						text-align: center;
						margin-top: 16rpx;
					}
				}
			}
		}
	}
</style>