<template>
	<view class="container login">
		<view class="login-main">
			<!-- <view class="line"></view> -->
			<view class="nav-item"
				  :class="{ current: tabCurrentIndex === 1 }" 
				  @click="tabClick(1)">
				  使用手机修改
			</view>					
			<view class="nav-item" 
				  :class="{ current: tabCurrentIndex === 2 }" 
				  @click="tabClick(2)">
				  使用邮箱修改
			</view>
		</view>
		<!-- 手机修改 -->
		<view class="content" v-if="tabCurrentIndex === 1">
			<view class="login-form clearfix">
				<view class="login-input">
					<input type="number" v-model="phoneParams.mobileOrEmail"  maxlength="11" class="input" placeholder="请输入手机号" placeholder-class="placeholder"/>
				</view>
			</view>
			<view class="login-form clearfix">
				<view class="login-input code">
					<input type="text" v-model="imageCode"  maxlength="4" class="input" placeholder="请输入右侧图形验证码"  placeholder-class="placeholder"/>
				</view>
				<view class="login-input img-btn">
					<view class="vscodeimg">
						<image :src="imageCodeUrl" mode=""></image>
					</view>
					<view class="vscod-refresh" @click.stop="getVerificationCode">
						<text class="iconfont icon-shuaxin"></text>
						<text class="ref-text">刷新</text>
					</view>
				</view>
			</view>
			<view class="login-form clearfix">
				<view class="login-input code">
					<input type="number" v-model="phoneParams.smsCode"  maxlength="6" class="input" placeholder="请输入短信验证码"  placeholder-class="placeholder"/>
				</view>
				<view class="login-input btn" :class="[isMobileDisabled  ? 'disabled' : '']" >
					<button type="button"
							@click.stop="getMobileCodeFn"
							:disabled="isMobileDisabled" 
							class="input" >
							{{ mobileCodeText }}
					</button>
				</view>
			</view>
			<view class="login-form clearfix">
				<view class="login-input pwd">
					<input v-show="isShowEye" type="text" v-model="phoneParams.password"  maxlength="16" class="input" placeholder="请输入新密码，必须为8-16位字母数字的组合" autocomplete="new-password"  placeholder-class="placeholder"/>
					<input v-show="!isShowEye" type="password" v-model="phoneParams.password"  :password="true" maxlength="16" class="input" placeholder="请输入新密码，必须为8-16位字母数字的组合" autocomplete="new-password"  placeholder-class="placeholder"/>
					<view class="iconfont" :class="isShowEye ? 'icon-kejian1' : 'icon-bukejian'"  @click="passwordEye"></view>
				</view>
			</view>
			<view class="login-form clearfix">
				<view class="login-input pwd">
					<input v-show="isShowEyes" type="text" v-model="phoneParams.passwordConfirm"  maxlength="16" class="input" placeholder="请确认密码" autocomplete="new-password"  placeholder-class="placeholder"/>
					<input v-show="!isShowEyes" type="password" v-model="phoneParams.passwordConfirm"  :password="true" maxlength="16" class="input" placeholder="请确认密码" autocomplete="new-password"  placeholder-class="placeholder"/>
					<view class="iconfont" :class="isShowEyes ? 'icon-kejian1' : 'icon-bukejian'"  @click="passwordEyes"></view>
				</view>
			</view>
			<view class="login-form clearfix" style="border: 0;">
				<view class="login-btn"  @click="confirmMobilePassword">修改密码</view>
			</view>
		</view>
		<!-- 邮箱修改 -->
		<view class="content" v-else>
			<view class="login-form clearfix">
				<view class="login-input">
					<input class="input" type="text" v-model="emailParams.mobileOrEmail"  maxlength="30" placeholder="请输入邮箱地址"  placeholder-class="placeholder"/>
				</view>
			</view>
			<view class="login-form clearfix">
				<view class="login-input code">
					<input class="input" type="number" v-model="emailParams.smsCode" maxlength="6"  placeholder="请输入邮箱验证码"  placeholder-class="placeholder"/>
				</view>
				<view class="login-input btn" :class="[isEmialDisabled ? 'disabled' : '']">
					<button class="input"  
							type="button"  
							@click.stop="getEmailCodeFn"
							:disabled="isEmialDisabled">
							{{ emailCodeText }}
					</button>
				</view>
			</view>
			<view class="login-form clearfix">
				<view class="login-input pwd">
					<input v-show="isShowEye" type="text" v-model="emailParams.password"  maxlength="16" class="input" placeholder="请输入新密码，必须为8-16位字母数字的组合" autocomplete="new-password"  placeholder-class="placeholder"/>
					<input v-show="!isShowEye" type="password" v-model="emailParams.password"  :password="true" maxlength="16" class="input" placeholder="请输入新密码，必须为8-16位字母数字的组合" autocomplete="new-password"  placeholder-class="placeholder"/>
					<view class="iconfont" :class="isShowEye ? 'icon-kejian1' : 'icon-bukejian'"  @click="passwordEye"></view>
				</view>
			</view>
			<view class="login-form clearfix">
				<view class="login-input pwd">
					<input v-show="isShowEyes" type="text" v-model="emailParams.passwordConfirm"  maxlength="16" class="input" placeholder="请确认密码" autocomplete="new-password"  placeholder-class="placeholder"/>
					<input v-show="!isShowEyes" type="password" v-model="emailParams.passwordConfirm"  :password="true" maxlength="16" class="input" placeholder="请确认密码" autocomplete="new-password"  placeholder-class="placeholder"/>
					<view class="iconfont" :class="isShowEyes ? 'icon-kejian1' : 'icon-bukejian'"  @click="passwordEyes"></view>
				</view>
			</view>
			<view class="login-form clearfix" style="border: 0;">
				<view class="login-btn"  @click="confirmEmailPassword">修改密码</view>
			</view>
		</view>
	</view>
</template>

<script>
	import authorize from '@/common/config/authorize.js' 
	
	export default{
		data() {
			return{
				tabCurrentIndex: 1,
				isShowEye:false,	//控显
				isShowEyes:false,	//控显
				iconEyes:'icon-yanjing_yincang_o',
				iconEyen:'icon-yanjing_xianshi_o',
				isEmialDisabled:false,//获取邮箱验证码按钮
				isMobileDisabled:false,//获取手机短信按钮
				imageCode:'',			//图形验证码
				imageCodeUrl:'',		//图形验证码地址
				imageCodetoken:'',		//图形校验token
				count: '',				//倒计时
				emailCodeText: '获取验证码',
				mobileCodeText: '获取验证码',
				codeTime: null,
				phoneParams: {
					mobileOrEmail :'',
					smsCode : '',
					password : '',
					passwordConfirm : '',
					status:1
				},
				emailParams: {
					mobileOrEmail :'',
					smsCode : '',
					password : '',
					passwordConfirm : '',
					status:2
				},
			}
		},
		onLoad(option) {
			this.getVerificationCode()
		},
		methods:{
			confirmMobilePassword(){//手机号修改
				if( this.phoneParams.mobileOrEmail == ''){
					this.$util.msg('请输入手机号',2000);
					return
				}
				if(!this.$reg.isMobile(this.phoneParams.mobileOrEmail)){
					this.$util.msg('手机格式不正确',2000);
					return
				}
				if( this.phoneParams.smsCode == ''){
					this.$util.msg('请输入手机验证码',2000);
					return
				}
				if(!this.$reg.isMobileCode(this.phoneParams.smsCode)){
					this.$util.msg('验证码格式不正确',2000);
					return
				}
				if( this.phoneParams.password == ''){
					this.$util.msg('请输入密码',2000);
					return
				}
				if(!this.$reg.checkPwd(this.phoneParams.password)){
					this.$util.msg('密码必须为8-16位字母数字的组合',2000);
					return
				}
				if( this.phoneParams.passwordConfirm == ''){
					this.$util.msg('请再次确认密码',2000);
					return
				}
				if( this.phoneParams.passwordConfirm !== this.phoneParams.password){
					this.$util.msg('两次输入的密码不一致',2000);
					return
				}
				this.UserService.ModifyMobilePassword(this.phoneParams).then(res =>{
					this.$util.msg(res.msg,2000,true,'success')
					setTimeout(()=>{
						uni.navigateBack({
							delta: 1
						});
					},2000)
				}).catch( error =>{
					this.$util.msg(error.msg,2000);
				})
			},
			confirmEmailPassword(){//邮箱修改密码
				 if( this.emailParams.mobileOrEmail == ''){
					this.$util.msg('请输入邮箱',2000);
				 	return
				 }
				 if(!this.$reg.isEmail(this.emailParams.mobileOrEmail)){
					this.$util.msg('请输入正确的邮箱地址',2000);
				 	return
				 }
				 if( this.emailParams.smsCode == ''){
					this.$util.msg('请输入邮箱验证码',2000);
				 	return
				 }
				 if(!this.$reg.isMobileCode(this.emailParams.smsCode)){
					this.$util.msg('验证码格式不正确',2000);
				 	return
				 }
				 if( this.emailParams.password == ''){
					this.$util.msg('请输入密码',2000);
				 	return
				 }
				 if(!this.$reg.checkPwd(this.emailParams.password)){
					this.$util.msg('密码必须为8-16位字母数字的组合',2000);
				 	return
				 }
				 if( this.emailParams.passwordConfirm == ''){
					this.$util.msg('请再次确认密码',2000);
				 	return
				 }
				 if( this.emailParams.passwordConfirm !== this.emailParams.password){
					this.$util.msg('两次输入的密码不一致',2000);
				 	return
				 }
				this.UserService.ModifyMobilePassword(this.emailParams).then(res =>{
					this.$util.msg(res.msg,2000,true,'success')
					setTimeout(()=>{
						uni.navigateBack({
							delta: 1
						});
					},2000)
				}).catch( error =>{
					this.$util.msg(error.msg,2000);
				})
			},
			getVerificationCode(){//图形验证
				this.PublicService.GetImgVerifyCode().then(res => {
					this.imageCodeUrl = res.data.baseImage
					this.imageCodetoken = res.data.token
				})
			},
			getEmailCodeFn(){//获取邮箱验证码
				if( this.emailParams.mobileOrEmail == ''){
					this.$util.msg('请输入邮箱地址',2000);
					return
				}
				if(!this.$reg.isEmail(this.emailParams.mobileOrEmail)){
					this.$util.msg('请输入正确的邮箱地址',2000);
					return
				}
				this.isEmialDisabled = true;
				this.PublicService.GetUserEmailCode({email:this.emailParams.mobileOrEmail,status:3}).then(res =>{
					this.$util.msg('邮箱验证码已发送',2000);
					const TIME_COUNT = 60;
			     	if (!this.codeTime) {
			       		this.count = TIME_COUNT;
			       		this.isEmialDisabled = true;
			       		this.codeTime = setInterval(() => {
			       			if (this.count > 1 && this.count <= TIME_COUNT) {
			         			this.count--
			         			this.emailCodeText = this.count +'s重新发送'
			        		} else {
				         		this.isEmialDisabled = false;
				         		clearInterval(this.codeTime)
				         		this.codeTime = null
								this.emailCodeText = '获取验证码'
			        		}
			       		},1000)
			      	}
				}).catch( error =>{
					this.$util.msg(error.msg,2000);
					this.isEmialDisabled = false;
				})
			},
			getMobileCodeFn(){//获取手机验证码
				if( this.phoneParams.mobileOrEmail == ''){
					this.$util.msg('请输入手机号',2000);
					return
				}
				if(!this.$reg.isMobile(this.phoneParams.mobileOrEmail)){
					this.$util.msg('请输入正确的手机号',2000);
					return
				}
				if( this.imageCode == ''){
					this.$util.msg('请输入图形验证码',2000);
					return
				}
				let params = {
						mobile:this.phoneParams.mobileOrEmail,
						activateCodeType:1,
						platformType:2,
						imgCode:this.imageCode,
						token:this.imageCodetoken,
					}
				this.isMobileDisabled = true;
				this.PublicService.GetRegisterMobileCode(params).then(res =>{
					this.$util.msg('验证短信已发送',2000);
					const TIME_COUNT = 60;
			     	if (!this.codeTime) {
			       		this.count = TIME_COUNT;
			       		this.isMobileDisabled = true;
			       		this.codeTime = setInterval(() => {
			       			if (this.count > 1 && this.count <= TIME_COUNT) {
			         			this.count--
			         			this.mobileCodeText = this.count +'s重新发送'
			        		} else {
				         		this.isMobileDisabled = false;
				         		clearInterval(this.codeTime)
				         		this.codeTime = null
								this.mobileCodeText = '获取验证码'
			        		}
			       		},1000)
			      	}
				}).catch( error =>{
					this.$util.msg(error.msg,2000);
					this.isMobileDisabled = false;
				})
			},
			tabClick(index) {//tab切换
				this.tabCurrentIndex = index;
				this.password = ''
				this.passwordCheck = ''
				if(this.tabCurrentIndex == 1){
					this.getVerificationCode()
				}
			},
			passwordEye() {//密码显隐操作
				this.isShowEye = !this.isShowEye;
			},
			passwordEyes() {//密码显隐操作
				this.isShowEyes = !this.isShowEyes;
			}
		}
	}
</script>

<style lang="scss">
	.login{
		width: 100%;
		height: auto;
		.login-main{
			width: 100%;
			display: flex;
			height: 80rpx;
			border-top: 1px solid #F7F7F7;
			margin-bottom: 80rpx;
			position: relative;
			.nav-item{
				display: flex;
				flex: 1;
				flex-direction: column;
				align-items: center;
				line-height: 80rpx;
				color: #666666;
				font-size: $font-size-28;
				background: #F7F7F7;
				&.current{
					color:$color-system;
					background: #FFFFFF;
					
				}
			}
		}
		.login-form{
			width: 702rpx;
			height: auto;
			margin: auto;
			border-bottom: 2rpx solid #e1e1e1;
			.login-input{
				width: 100%;
				height: 60rpx;
				padding: 24rpx 0;
				border-radius: 14rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				position: relative;
				&.pwd{
					.iconfont{
						position: absolute;
						right: 0;
						top: 0;
						font-size: 44rpx;
						color: #999999;
						z-index: 99;
						width: 96rpx;
						height: 96rpx;
						line-height: 96rpx;
						text-align: center;
					}
				}
				&.code{
					width: 310rpx;
					float: left;
					margin-right: 20rpx;
				}
				&.btn{
					width: 220rpx;
					height: 64rpx;
					float: right;
					background: $btn-confirm;
					padding: 0;
					border-radius: 44rpx;
					margin-top: 20rpx;
					.input{
						width: 220rpx;
						height: 64rpx;
						line-height: 64rpx;
						padding: 0;
						color: #FFFFFF;
						background: $btn-confirm;
						text-align: center;
						border-radius: 44rpx;
						&.none{
							background: #F7F7F7;
						}
					}
					&.disabled{
						background: #F7F7F7;
						.input{
							background: #F7F7F7;
							color: #999999;
						}
					}
				}
				&.img-btn{
					width: 290rpx;
					height: 64rpx;
					padding: 0;
					float: right;
					background: #FFFFFF;
					display: block;
					margin-top: 10rpx;
					.vscodeimg{
						width: 160rpx;
						height: 64rpx;
						float: left;
						display: flex;
						flex-direction: column;
						align-items: center;
						border-radius: 6rpx;
						margin-top: 10rpx;
						image{
							width: 180rpx;
							height: 88rpx;
							border-radius: 6rpx;
						}
					}
					.vscod-refresh{
						width: 100rpx;
						float: right;
						margin-top: 25rpx;
						.icon-shuaxin{
							font-size: 36rpx;
							color: #999999;
							float: left
						}
						.ref-text{
							font-size: 24rpx;
							color: #333333;
							float: right;
						}
					}
				}
				.input{
					width: 100%;
					height: 100%;
					font-size: $font-size-28;
					line-height: 40rpx;
					color: #333333;
					border-radius: 14rpx;
				}
			}
		}
		.login-btn{
			width: 600rpx;
			height: 88rpx;
			border-radius: 44rpx;
			font-size: $font-size-28;
			line-height: 88rpx;
			color: #FFFFFF;
			margin: 0 auto;
			text-align: center;
			background: $btn-confirm;
			margin-top: 100rpx;
		}
		.model-authorization{
			width: 100%;
			height: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 999;
			.authorization{
				width: 518rpx;
				height: 320rpx;
				position: absolute;
				background: rgba(255,255,255,.7);
				left: 0;
				right: 0;
				bottom: 0;
				top: 0;
				margin: auto;
				.to-btn{
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					margin: auto;
					width: 70%;
					height: 88rpx;
					font-size: $font-size-28;
					line-height: 88rpx;
					color: #FFFFFF;
					text-align: center;
					border-radius: 44rpx;
				}
			}
		}
	}
	.placeholder{
		color: #b2b2b2;
	}
</style>
