<template>
	<view class="login-register" v-if="userStore.showLoginModal">
		<div class="lr-box" v-if="!userStore.isRegister">
			<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/c79318ee-e219-4f01-9dfb-91d7180a0972.png" class="lr-box-topbg" mode="widthFix"></image>
			<div class="lr-box-top adfacjb">
				<image class="lr-box-top-left" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/64d76db1-be28-4ba2-a8f0-57e2408beebc.png"></image>
				<image class="lr-box-top-right" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/c3c4fa76-32d2-4d11-915d-5e655c6b5c72.png" @tap="close"></image>
			</div>
			<div class="lr-box-title">
				<p class="p">欢迎来到善行少年</p>
				<p class="p tip">首次登录需要简单填写信息即可进入</p>
			</div>
			<div class="lr-box-form">
				<div class="lr-box-form-item adfacjb">
					<div class="lr-box-form-item-left adfac">
						<div class="text" style="margin-right: 32rpx;"><span style="color: #F4657A;">*</span>手机号码</div>
						<up-input v-model="register.phone" border="none" placeholder="请输入手机号码"></up-input>
					</div>
					<div class="lr-box-form-item-right">
						<button class="yjsq" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">一键授权</button>
					</div>
				</div>
				<div class="lr-box-form-item adfacjb">
					<div class="lr-box-form-item-left adfac">
						<div class="text"><span style="color: #F4657A;">*</span>头像</div>
						<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/a2944f51-2c7b-41e7-8206-8a3be1f76d11.png" v-if="!register.avatar"></image>
						<image :src="register.avatar"></image>
					</div>
					<div class="lr-box-form-item-right">
						<button class="yjsq" open-type="chooseAvatar" @bindchooseavatar="onChooseAvatar">设置</button>
					</div>
				</div>
				<div class="lr-box-form-item adfacjb">
					<div class="lr-box-form-item-left">
						<div class="text"><span style="color: #F4657A;">*</span>用户名</div>
					</div>
					<div class="lr-box-form-item-right">
						<up-input v-model="register.userName" border="none" inputAlign="right" placeholder="请输入真实姓名"></up-input>
					</div>
				</div>
				<div class="lr-box-form-item adfacjb">
					<div class="lr-box-form-item-left">
						<div class="text">家庭公益名称</div>
					</div>
					<div class="lr-box-form-item-right">
						<up-input v-model="register.nonprofitName" border="none" inputAlign="right" placeholder="请输入至少三个字"></up-input>
					</div>
				</div>
				<div class="lr-box-form-item adfacjb">
					<div class="lr-box-form-item-left">
						<div class="text">家庭公益口号</div>
					</div>
					<div class="lr-box-form-item-right">
						<up-input v-model="register.nonprofitNumber" border="none" inputAlign="right" placeholder="家庭公益口号"></up-input>
					</div>
				</div>
			</div>
			<div class="lr-box-btns">
				<div class="btn" @tap="toRegister">立即注册</div>
				<div class="btn login" @tap="readLogin">已有账号，去登录</div>
			</div>
		</div>
		<div class="lr-box login" v-else>
			<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/c79318ee-e219-4f01-9dfb-91d7180a0972.png" class="lr-box-topbg" mode="widthFix"></image>
			<div class="lr-box-top login adfacjb">
				<image class="lr-box-top-left login" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/d0f892c8-1f96-4c86-b62b-a8b027b4bf6e.png"></image>
				<image class="lr-box-top-right" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/c3c4fa76-32d2-4d11-915d-5e655c6b5c72.png" @tap="close"></image>
			</div>
			<div class="lr-box-memo">公益献爱心 真情暖人心！</div>
			<div class="lr-box-agree adfac">
				<image v-if="agree" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/87b5b244-d14f-43cd-991b-4ac9f48d909e.png" @tap="changeAgree"></image>
				<image v-else src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/b8a5cabd-57f8-4ad7-9677-f6372423c50a.png" @tap="changeAgree"></image>
				<div class="text">我已阅读并同意<span>《善行少年小程序隐私政策》</span>及<span>《善行少年服务协议》</span></div>
			</div>
			<div class="lr-box-login">
				<button class="phone-login" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumberLogin">手机号登录</button>
			</div>
		</div>
	</view>
</template>

<script setup name="">
	import { ref, getCurrentInstance } from 'vue'
	const { proxy } = getCurrentInstance()
	import { useUserStore } from '@/common/stores/user.js';
	const userStore = useUserStore();
	
	const register = ref({
		phone:null,
		avatar:null,
		userName:null,
		nonprofitName:null,
		nonprofitNumber:null,
	})
	const agree = ref(false)
	
	const close = () => {
		userStore.closeLoginModal();
	}
	
	const decryptPhoneNumber = e => {
		if(e.detail.code) getPhone(e.detail.code);
	}
	
	const onChooseAvatar = e => {
		
	}
	
	const getPhone = code => {
		
	}
	
	const toRegister = () => {
		if(!proxy.$reg.mobile(register.value.phone)) return proxy.$showToast('请输入正确的手机号码')
		if(!register.value.avatar) return proxy.$showToast('请设置头像')
		if(!register.value.userName) return proxy.$showToast('请输入真实姓名')
		
		userStore.register();
	}
	
	const readLogin = () => {
		userStore.isRegister = true;
	}
	
	const changeAgree = () => {
		agree.value = !agree.value;
	}
	
	
	const decryptPhoneNumberLogin = e => {
		if(e.detail.code) toPhoneLogin(e.detail.code);
	}
	
	const toPhoneLogin = code => {
		if(!agree.value) return proxy.$showToast('请勾选隐私政策和服务协议')
		userStore.login({});
	}
</script>

<style scoped lang="scss">
	.login-register{
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, .4);
		z-index: 9999;
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
		.lr-box{
			background: #F7F7F7;
			border-radius: 24rpx 24rpx 0rpx 0rpx;
			padding: 58rpx 30rpx 68rpx;
			position: relative;
			&.login{
				padding: 32rpx 40rpx 88rpx;
			}
			&-topbg{
				width: 100%;
				position: absolute;
				left: 0;
				top: 0;
			}
			&-top{
				position: relative;
				padding: 0 10rpx 0 34rpx;
				&.login{
					padding: 0;
				}
				&-left{
					width: 158rpx;
					height: 88rpx;
					&.login{
						width: 224rpx;
						height: 48rpx;
					}
				}
				&-right{
					width: 28rpx;
					height: 28rpx;
				}
			}
			&-title{
				position: relative;
				margin-top: 18rpx;
				padding-left: 34rpx;
				.p{
					font-family: PingFangSC, PingFang SC;
					font-weight: 600;
					font-size: 44rpx;
					color: #151B29;
					line-height: 62rpx;
					&.tip{
						font-weight: 400;
						font-size: 26rpx;
						color: rgba(21,27,41,0.61);
						line-height: 37rpx;
						margin-top: 10rpx;
					}
				}
			}
			&-form{
				position: relative;
				margin-top: 10rpx;
				&-item{
					height: 120rpx;
					background: #FFFFFF;
					border-radius: 16rpx;
					border: 1rpx solid #E0E1E4;
					padding: 0 30rpx;
					margin-top: 20rpx;
					&-left{
						width: 370rpx;
						.text{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 30rpx;
							color: #151B29;
							line-height: 42rpx;
						}
						image{
							width: 60rpx;
							height: 60rpx;
							margin-left: 28rpx;
						}
					}
					&-right{
						width: calc(100% - 370rpx);
						padding-left: 20rpx;
						box-sizing: border-box;
						.yjsq{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 28rpx;
							color: #0593FE;
							line-height: 40rpx;
							background-color: transparent;
							text-align: right;
							padding: 0;
							&::after{
								width: auto;
								border: none;
							}
						}
					}
				}
			}
			&-btns{
				margin-top: 20rpx;
				padding: 0 10rpx;
				overflow: hidden;
				.btn{
					width: 100%;
					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;
					margin-top: 20rpx;
					&.login{
						background: #ECEEF5;	
					}
				}
			}
			&-memo{
				position: relative;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 26rpx;
				color: #151B29;
				line-height: 36rpx;
				letter-spacing: 4rpx;
				margin-top: 41rpx;
			}
			&-agree{
				position: relative;
				margin-top: 95rpx;
				image{
					width: 48rpx;
					height: 48rpx;
				}
				.text{
					width: calc(100% - 48rpx);
					padding-left: 19rpx;
					box-sizing: border-box;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #151B29;
					line-height: 36rpx;
					label{
						color: #E77687;
					}
				}
			}
			&-login{
				margin-top: 122rpx;
				.phone-login{
					width: 100%;
					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;
					&::after{
						border: none;
					}
				}
			}
		}
	}
</style>