<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="captcha-inputs">
					<view class="captcha-campus-inputs campus-inputs">
						<input class="campus-form-input" type="text" placeholder="请输入图文验证码" v-model="newCaptcha" />
					</view>
					<image class="campus-form-graph" :src="captchaImg" mode="widthFix" @click="replaceClick"></image>
				</view>
				
				<view class="inputs captcha-inputs">
					<input class="campus-form-input" type="number" placeholder="请输入验证码" v-model="code" maxlength="4"></input>
					<button @click="checkCaptcha" class="campus-form-code ":disabled="phone == ''" type="default" size="mini">{{getSms ? '重新发送' + smsTime + 's': '发送验证码'}}</button>
				</view>
				
				<button class="btn" type="default" @click="LoginSms" :disabled="phone == '' || code == ''">立即登录</button>
				<view class="forget">
					<view class="forget-label" @click="$Router.push({name: 'Register'})">我要注册</view>
					<view class="forget-label forget-after" @click="$Router.push({name: 'PasswordLogin'})">密码登录</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: {page : 3}})">《隐私协议》</view>和<view @click="$Router.push({name: 'Agreement', params: {page : 4}})">《服务协议》</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { Login, Captcha, smsAuth, authSms } from '@/apis/interfaces/auth'
	export default {
		data() {
			return {
				phone		 : '',     // 手机号码
				password	 : '',     // 登录密码
				checked 	 : false,  // 勾选协议
				seeState     : false,  //小眼睛
				passwordState: true,
				disabled     : false,
				code		 : '',  // 短信验证码
				getSms  	 : '',
				smsTime		 : 60,
				newCaptcha   : '',  // 填写图形码
				captcha      : '',     // 输入图形码
				captchaImg   : '',     // 图形码
			}
		},
		
		// 生命周期函数--监听页面显示
		onShow() {
			// 获取图形码
			this.captchaInfo()
		},
		
		methods: {
			// 图形码
			captchaInfo() {
				Captcha().then(res=>{
					this.captchaImg = res.img
					this.captchaKey = res.key
				}).catch(err=>{
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			
			// 看不清，换一张 - 图形码数据
			replaceClick() {
				this.captchaInfo();
				
				// 清除图形码数据
				this.newCaptcha = ''
			},
			
			// 需校验图形验证码
			checkCaptcha() {
				if(this.phone) {
					let outTime
					smsAuth({
						mobileNo   : this.phone,
						captcha	   : this.newCaptcha,
						captcha_key: this.captchaKey
					}).then(res => {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
						
						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"
						})
					})
					return
				}
				uni.showToast({
					title: '请输入手机号',
					icon: "none"
				})
			},
			
			// 勾选协议
			radioChange() {
			    this.checked = !this.checked
			},
			
			// 验证码登录
			LoginSms() {
				if(this.checked) {
					authSms({
						mobileNo	: this.phone,
						parent_id	: '',
						code		: this.code
					}).then(res => {
						// 存储用户token
						this.$store.commit('setToken', res.token_type + ' ' + res.access_token)
						
						// 回到首页
						this.$Router.replaceAll({name: 'Index'})
					}).catch(err => {
						uni.showToast({
							title: err.message,
							icon: "none"
						})
					})
				} else {
				    uni.showToast({
				    	title: '请勾选用户隐私和服务协议',
				    	icon: "none"
				    })
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.loginBack {
		width: 35%;
		position: fixed;
		top: 0;
		right: 0;
	}
	.loginBottom {
		width: 60%;
		position: fixed;
		bottom: 0;
		left: 0;
	}
	
	.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;
			}
		}
	}
	
	.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;
			.inputs-see {
				position: absolute;
				right: $padding;
				top: $padding;
				width: 38rpx;
				height: 38rpx;
			}
			input {
				font-weight: normal;
				height: 100%;
			}
		}
		.btn {
			background-image: linear-gradient(to right, #da2b56, #FBAF3B);
			color: #ffffff;
			border-radius: $radius-m;
			font-size: $title-size;
			height: 100rpx;
			line-height: 100rpx;
			padding: 0;
			margin: $margin * 3 0 0;
			&[disabled] {
				background-image: linear-gradient(to right, #ff8da9, #ffd0a2);
			}
		}
		.forget {
			display: flex;
			line-height: 120rpx;
			font-size: $title-size-m;
			.forget-label {
				flex: 2;
				text-align: center;
				color: #000000;
			}
			.forget-after {
				position: relative;
				&::after {
					position: absolute;
					content: '';
					left: 0;
					bottom: calc(50% - 15rpx);
					width: 2rpx;
					height: 30rpx;
					background-color: #f6f6f6;
				}
			}
		}
	}
	
	.captcha-inputs {
		height: 100rpx;
		display: flex;
		margin-top: $margin + 20;
		.campus-form-graph {
			width: 220rpx;
			margin-top: 20rpx;
			border-radius: 5rpx;
		}
		.captcha-campus-inputs{
			flex: 1;
			margin-right: 20rpx;
		}
		
		.campus-inputs {
			background-color: #f7f8fa;
			height: 100rpx;
			line-height: 100rpx;
			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;
			input {
				font-weight: normal;
				height: 100%;
			}
		}
		
		.campus-form-code{
			position: absolute;
			top: 0;
			right: 30rpx;
			padding: 0;
			margin: 0;
			background-color: transparent;
			font-size: 30rpx;
			height: 100rpx;
			line-height: 100rpx;
			color: #f25448;
			&::after{
				border: none;
			}
			&[disabled]{
				color: rgba($color: #f25448, $alpha: .5);
			}
		}
	}
</style>
