<template>
	<view class="forget">
		<tm-nav-bar left-icon="arrow-left" title="忘记密码" @clickLeft="back" />
		<view class="content">
			<!-- 主体 -->
			<view class="main">
				<view class="tips">若你忘记了密码，可在此重置新密码。</view>
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="请输入手机号码"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="请输入新密码"
					isShowPass
				></wInput>
				<view class="main-list oBorder">
					<view class="" style="color: red;">
					</view>
					<input class="main-input" v-model="imgCode" placeholder="请输入图形验证码"/>
					<image @click="reset()" style="width: 200rpx; height: 40rpx" :src="img64" mode=""></image>
				</view>
				
				<wInput
					v-model="verCode"
					type="number"
					maxlength="4"
					placeholder="验证码"
					
					isShowCode
					codeText="获取重置码"
					setTime="30"
					ref="runCode"
					@setCode="getVerCode()"
				></wInput>
			</view>
			
			<wButton 
				class="wbutton"
				text="重置密码"
				:rotate="isRotate" 
				@click.native="startRePass()"
			></wButton>

		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	export default {
		data() {
			return {
				phoneData: "", //电话
				passData: "", //密码
				verCode:"", //验证码
				isRotate: false, //是否加载旋转
				imgCode: '',
				captcha_id: '',
				img64:'',
			}
		},
		components:{
			wInput,
			wButton
		},
		mounted() {
			_this= this;
			this.getImgcode()
		},
		methods: {
			// 刷新二维码
			reset() {
				this.getImgcode()
			},
			getImgcode() {
				this.$store.dispatch('api/code',{
					'type' :2
				}).then(res => {
					this.img64 = res.captcha_src;
					this.captcha_id = res.captcha_id
				})
			},
			getVerCode(){
				if (!this.imgCode) {
					uni.showToast({
					    icon: 'none',
						position: 'bottom',
					    title: '请输入图形验证码'
					});
					return false;
				}
				
				//获取验证码
				if (_this.phoneData == '') {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不能为空'
				    });
				    return false;
				}
				
				uni.showToast({
				    icon: 'none',
					position: 'bottom',
				    title: '验证码发送中'
				});
				
				this.$store.dispatch('api/sendSms', {
					type:2, 
					mobile: this.phoneData, 
					captcha_id:  this.captcha_id,
					captcha_code: this.imgCode
				}).then(res => {
					this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
				})
				
			},
			startRePass() {
				//重置密码
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length != 11) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
			    if (this.passData.length < 6) {
			        uni.showToast({
			            icon: 'none',
						position: 'bottom',
			            title: '密码不正确'
			        });
			        return false;
			    }
				if (this.verCode.length != 4) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '验证码不正确'
				    });
				    return false;
				}
				
				_this.isRotate=true
				setTimeout(function(){
					_this.isRotate=false
				},3000)
				this.$store.dispatch('api/forgotPassword', {
					mobile: this.phoneData, 
					password:  this.passData,
					code: this.verCode
				}).then(res => {
					 this.back()
				})
			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
	.main-list{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		/* height: 36rpx; */   /* Input 高度 */
		color: #333333;
		padding: 40rpx 32rpx;
		margin:32rpx 0;
	}
	.img{
		width: 32rpx;
		height: 32rpx;
		font-size: 32rpx;
	}
	.main-input{
		flex: 1;
		text-align: left;
		font-size: 28rpx;
		/* line-height: 100rpx; */
		padding-right: 10rpx;
		margin-left: 20rpx;
	}
	.vercode {
		color: rgba(0,0,0,0.7);
		font-size: 24rpx;
		/* line-height: 100rpx; */
	}
	.vercode-run {
		color: rgba(0,0,0,0.4) !important;
	}
	.oBorder{
	    border: none;
	    border-radius: 2.5rem ;
	    -webkit-box-shadow: 0 0 60rpx 0 rgba(43,86,112,.1) ;
	    box-shadow: 0 0 60rpx 0 rgba(43,86,112,.1) ;
	}
</style>

