<template>
	<view class="content">
		<img class="loginBack" src="@/static/imgs/login_back.png">
		<img class="loginBottom" src="@/static/imgs/login_bottom.png">
		<view class="title">
			<view class="title-name">
				找回密码
			</view>
			<view class="title-tips">
				请输入新密码并验证手机号，密码规则：密码+数字
			</view>
		</view>
		<view class="info">
			<!-- 输入手机号相关 -->
			<view class="inputs">
				<input type="number" placeholder="请输入手机号" maxlength="11" v-model="phone" />
			</view>
			<!-- 获取验证码 -->
			<view class="inputs">
				<input type="text" placeholder="请输入验证码" maxlength="4" v-model="code" />
				<button @click="getPhoneCode" class="sms-btn ":disabled="phone == ''" type="default" size="mini">{{getSms ? '重新发送' + smsTime + 's': '发送验证码'}}</button>
			</view>
			<!-- 输入密码 -->
			<view class="inputs">
				<input type="text" placeholder="请输入新密码" password v-model="password" />
			</view>
			<!-- 再次确认密码 -->
			<view class="inputs">
				<input type="text" placeholder="再次输入新密码" password v-model="confirmation" />
			</view>
			<button class="btn" type="default" :disabled="phone == '' || getSms == ''" @click="gainSms">确认修改</button>
			<view class="forget">
				<view class="forget-label" @click="$Router.push({name: 'Login'})">密码登录</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 class="modifyBack" v-if="modifyPop"></view>
		<view class="modifyCont" v-if="modifyPop">
			<view class="modifyWhite">
				<image class="modifyWhite-img" src="@/static/imgs/modifySuccess.png" mode="aspectFill"></image>
				<view class="modifyWhite-title">
					修改成功
				</view>
				<view class="modifyWhite-text">
					您的密码已经修改生效，请重新登录
				</view>
				<view class="modifyWhite-btn" @click="loginClick">
					去登录
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import { Captcha, smsAuth, resetPassword } from '@/apis/interfaces/auth'
	export default {
		data() {
			return {
				graphImg    : '',  // 图形码
				captchaKey  : '',  // 校验图形码传回去的值
				newCaptcha  : '',  // 填写图形码
				phone		: '',  // 手机号码
				password    : '',  // 密码
				confirmation: '',  // 再次输入密码
				code		: '',  // 短信验证码
				getSms  	: '',
				smsTime		: 60,
				graphState	: false, // 图文弹出层
				modifyPop   : false  // 修改成功弹出框
			}
		},
		
		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
				}).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() {
				resetPassword({
					username			 : this.phone,
					password			 : this.password,
					password_confirmation: this.confirmation,
					code				 : this.code
				}).then(res => {
					// 修改成功弹出框
					this.modifyPop = true
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: "none"
					})
				})
			},
			
			// 去登录
			loginClick() {
				this.$Router.replaceAll({name: 'Login'})
				// 修改成功弹出框
				this.modifyPop = false
			}
		}
	}
</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;
		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;
			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;
					}
				}
			}
		}
	}
	
	// 修改成功弹出框
	.modifyBack {
		position: fixed;
		width: 100vw;
		height: 100vh;
		left: 0;
		top: 0;
		z-index: 9;
		background-color: rgba(0, 0, 0, .8);
	}
	.modifyCont {
		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;
		.modifyWhite {
			padding: $padding + 10 $padding * 2;
			box-sizing: border-box;
			background-color: #ffffff;
			border-radius: $radius-m;
			text-align: center;
			.modifyWhite-img {
				width: 260rpx;
				height: 260rpx;
				margin: 0 atuo;
			}
			.modifyWhite-title {
				line-height: 80rpx;
				font-weight: 600;
				font-size: $title-size + 4;
			}
			.modifyWhite-text {
				font-size: $title-size-m;
			}
			.modifyWhite-btn {
				background-image: linear-gradient(to right, #da2b56, #FBAF3B);
				color: #ffffff;
				border-radius: $radius-m;
				height: 90rpx;
				line-height: 90rpx;
				padding: 0;
				margin: $margin * 2 0 0;
				text-align: center;
			}
		}
	}
</style>
