<template>
	<view class="content">
		<img class="loginBack" src="https://douhuo-storage.oss-cn-beijing.aliyuncs.com/images/2023/06/06/5682d339246af0f44b4984c849288aa5.png">
		<img class="loginBottom" src="https://douhuo-storage.oss-cn-beijing.aliyuncs.com/images/2023/06/06/caa46039d88517ee8ba22b6c32dd47a1.png">
		<view class="title">
			<view class="title-name">
				立即注册
			</view>
			<view class="title-tips">
				纵有疾风起，人生不言弃
			</view>
		</view>
		<view class="info">
			<view class="info-cont">
				<!-- 输入手机号相关 -->
				<view class="inputs">
					<input type="number" placeholder="请输入手机号" maxlength="11" v-model="phone" />
				</view>
				<!-- 获取验证码 -->
				<view class="inputs">
					<input type="number" placeholder="请输入验证码" maxlength="4" v-model="code" />
					<button @click="getPhoneCode" class="sms-btn" :disabled="phone == '' || getSms != ''" type="default" size="mini">{{getSms ? '重新发送' + smsTime + 's': '发送验证码'}}</button>
				</view>
				<!-- 输入密码 -->
				<view class="inputs">
					<input type="text" :password="passwordState" placeholder="密码须包含大小写，最少8位" v-model="password" />
					<image class="inputs-see" @click="seeClick" :src="seeState ? '../../static/icon/see_active.png' : '../../static/icon/see.png'" mode="aspectFill"></image>
				</view>
				<!-- 输入密码 -->
				<view class="inputs">
					<input type="text" :password="passwordAgain" placeholder="请再次输入密码" v-model="confirmation" />
					<image class="inputs-see" @click="seeAgain" :src="againState ? '../../static/icon/see_active.png' : '../../static/icon/see.png'" mode="aspectFill"></image>
				</view>
				<!-- 输入邀請碼 -->
				<view class="inputs" v-if="newUser"> 
					<input type="text" placeholder="请输入邀请码" v-model="parentId" />
				</view>
				<button class="btn" type="default" :disabled="phone == '' || code == ''" @click="gainSms">立即注册</button>
				<view class="forget">
					<view class="forget-label" @click="$Router.push({name: 'Login'})">已有账号，立即登录</view>
				</view>
			</view>
			
			<!-- 用户登录注册协议 -->
			<view class="agreement">
			    <checkbox-group @change="radioChange">
			        <checkbox color="#da2b56" :checked="checked" style="transform: scale(.55)" class="radioGroup" />
			    </checkbox-group>
				<view class="agreement-text">
				    我已阅读并同意抖火法律<view @click="$Router.push({name: 'Agreement', params: {name : 'secret'}})">《隐私协议》</view>和<view @click="$Router.push({name: 'Agreement', params: {name : 'protocol'}})">《服务协议》</view>
				</view>
			</view>
		</view>
		
		<!-- 图文验证码 -->
		<view class="graphBack" v-if="graphState"></view>
		<view class="graphCont" v-if="graphState">
			<view class="graphWhite">
				<view class="graphWhite-title">
					请完成验证码
				</view>
				<view class="inputs">
					<input class="inputs-input" type="text" placeholder="输入图片中的字符" v-model="newCaptcha" />
					<view class="graph-img" @click="replaceClick">
						<image :src="graphImg" mode="widthFix"></image>
						<view class="graph-tips">看不清？换一张</view>
					</view>
				</view>
				<view class="graphBtn">
					<view class="graphBtn-lable" @click="graphState = false">
						取消
					</view>
					<view class="graphBtn-lable" @click="checkCaptcha">
						确定
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { Captcha, smsAuth, Register } from '@/apis/interfaces/auth'
	export default {
		data() {
			return {
				graphImg    : '',  // 图形码
				newCaptcha  : '',  // 填写图形码
				captchaKey  : '',  // 校验图形码传回去的值
				phone		: '',  // 手机号码
				password    : '',  // 密码
				confirmation: '',  // 再次输入密码
				code		: '',  // 短信验证码
				parentId    : '',  // 是否有上级id 邀请码
				checked     : false, // 勾选协议
				getSms  	: '',
				smsTime		: 60,
				graphState	: false, // 图文弹出层
				newUser     : '',
				seeState    : false,  //小眼睛
				againState  : false,  //小眼睛-再次输入密码
				passwordState: true,
				passwordAgain: true
			}
		},
		mounted(){
		   this.parentId = this.$Route.query.invite_code || ''
		},
		methods: {
			// 第一步  获取图形码
			getPhoneCode() {
				if(this.phone) {
					this.CaptchaData();
					this.graphState = true
					return
				}
				uni.showToast({
					title: '请输入手机号',
					icon: "none"
				})
			},
			
			// 图形码数据
			CaptchaData() {
				Captcha().then(res => {
					this.graphImg  = res.img
					this.captchaKey= res.key
				}).catch(err => {
            		uni.showToast({
            			title: err.message,
            			icon: "none"
            		})
            	})
			},
			
			// 看不清，换一张 - 图形码数据
			replaceClick() {
				this.CaptchaData();
				
				// 清除图形码数据
				this.newCaptcha = ''
			},
			
			// 需校验图形验证码
			checkCaptcha() {
				let outTime
				smsAuth({
					mobileNo   : this.phone,
					captcha	   : this.newCaptcha,
					captcha_key: this.captchaKey,
					type	   : 'register'
				}).then(res => {
					uni.showToast({
						title: res.message,
						icon: "none"
					})
					// 关闭弹框
					this.graphState = false
					this.getSms = true
					outTime = setInterval(() => {
						if (this.smsTime <= 1) {
							this.getSms = false
							this.smsTime = 60
							clearInterval('outTime')
						}
						this.smsTime -= 1
					}, 1000)
				}).catch(err => {
            		uni.showToast({
            			title: err.message,
            			icon: "none"
            		})
            	})
			},
			
			// 用户注册
			gainSms() {
				if(this.checked) {
					Register({
						username			 : this.phone,
						password			 : this.password,
						password_confirmation: this.confirmation,
						code				 : this.code,
						parent_id			 : this.parentId
					}).then(res => {
						// 存储用户token
						this.$store.commit('setToken', res.token_type + ' ' + res.access_token)
						
						// 回到首页
						this.$Router.replaceAll({name: 'Index'})
						// this.$Router.back()
					}).catch(err => {
						uni.showToast({
							title: err.message,
							icon: "none"
						})
					})
				} else {
				    uni.showToast({
				    	title: '请勾选用户隐私和服务协议',
				    	icon: "none"
				    })
				}
			},
			
			// 勾选协议
			radioChange() {
			    this.checked = !this.checked
			},
			
			// 查看密码
			seeClick() {
				this.seeState      = !this.seeState
				this.passwordState = !this.passwordState
			},
			
			// 查看密码-再次
			seeAgain() {
				this.againState    = !this.againState
				this.passwordAgain = !this.passwordAgain
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	.loginBack {
		width: 35%;
		position: fixed;
		top: 0;
		right: 0;
	}
	.loginBottom {
		width: 60%;
		position: fixed;
		bottom: 0;
		left: 0;
	}
	
	
	.title {
		padding: $padding*4 $padding * 3 $padding*2;
		box-sizing: border-box;
		.title-name {
			font-size: $title-size + 10;
			margin-bottom: $margin - 10;
		}
		.title-tips {
			color: #8e8e8e;
			font-size: $title-size-m;
		}
	}
	
	.info {
		padding: $padding * 3 0;
		box-sizing: border-box;
		position: absolute;
		z-index: 6;
		width: 100%;
		.info-cont {
			padding: 0 $padding * 3;
			box-sizing: border-box;
		}
		.inputs {
			background-color: #f7f8fa;
			height: 100rpx;
			line-height: 100rpx;
			margin-bottom: $margin + 20;
			box-sizing: border-box;
			color: #868686;
			font-size: $title-size-m;
			padding: 0 $padding + 10;
			border-radius: $radius-m;
			box-sizing: border-box;
			position: relative;
			overflow: hidden;
			.inputs-see {
				position: absolute;
				right: $padding;
				top: $padding;
				width: 38rpx;
				height: 38rpx;
			}
			input {
				font-weight: normal;
				height: 100%;
			}
			.sms-btn {
				position: absolute;
				right: 0;
				top: 0;
				background-color: #da2b56;
				height: 100rpx;
				line-height: 100rpx;
				color: #ffffff;
				border-radius: 0;
				font-size: $title-size-lg;
				border: none !important;
				&[disabled] {
					background-color: #ff8da9;
				}
			}
		}
		.btn {
			background-image: linear-gradient(to right, #da2b56, #FBAF3B);
			color: #ffffff;
			border-radius: $radius-m;
			height: 100rpx;
			line-height: 100rpx;
			padding: 0;
			margin: $margin * 3 0 0;
			&[disabled] {
				background-image: linear-gradient(to right, #ff8da9, #ffd0a2);
			}
		}
		.forget {
			line-height: 120rpx;
			font-size: $title-size-m;
			text-align: right;
			color: #7f8391;
		}
	}
	.graphBack {
		position: fixed;
		width: 100vw;
		height: 100vh;
		left: 0;
		top: 0;
		z-index: 9;
		background-color: rgba(0, 0, 0, .5);
	}
	.graphCont {
		position: absolute;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-box-pack: center;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		z-index: 10;
		padding: 0 $padding*2.5;
		box-sizing: border-box;
		.graphWhite {
			background-color: #ffffff;
			border-radius: $radius-m;
			.graphWhite-title {
				text-align: center;
				line-height: 110rpx;
				font-size: $title-size + 4;
			}
			.inputs {
				display: flex;
				padding: 10rpx $padding $padding;
				box-sizing: border-box;
				.inputs-input {
					border: 2rpx solid #d2d2d2;
					border-radius: $radius-m;
					height: 82rpx;
					line-height: 82rpx;
					padding: 0 $padding - 10;
					box-sizing: border-box;
					font-size: $title-size-lg;
					width: calc(100% - 230rpx);
				}
				.graph-img {
					width: 200rpx;
					margin-left: 30rpx;
					image {
						width: 100%;
					}
					.graph-tips {
						font-size: $title-size-sm;
						color: #9a9a9a;
						text-align: center;
						line-height: 48rpx;
					}
				}
			}
			.graphBtn {
				display: flex;
				line-height: 90rpx;
				border-top: 2rpx solid #d2d2d2;
				.graphBtn-lable {
					text-align: center;
					flex: 2;
					border-right: 2rpx solid #d2d2d2;
					font-size: $title-size-lg;
					color: $mian-color;
					&:last-child {
						border-color: transparent;
					}
				}
			}
		}
	}
	
	.agreement {
		padding: $padding 0 $padding - 20 $padding - 10;
		box-sizing: border-box;
		display: flex;
		font-size: $title-size-sm;
		width: 100%;
		line-height: 60rpx;
		color: #979797;
		.radioGroup {
			font-size: $title-size-sm;
		}
		.agreement-text {
			display: flex;
			width: 100%;
			view {
				color: $mian-color;
				padding: 0 5rpx;
			}
		}
	}
</style>
