<template>
	<view class="">
		<navigation>Reset Password</navigation>
		<view class="login-body">
			<view class="input-item flex">
				<view class="login-sigh flex">
					<view class="imgcon">
						<image src="../../../static/login/list_icon_phone@2x.png" mode=""></image>
					</view>
					<span class="quhao">+{{areacode}}</span>
				</view>
				<input type="text" value="" placeholder="mobile number"
					placeholder-style="color: #BFC2CCFF;font-size:14px;" v-model="mobile"
					@input="btnControl" />
			</view>
			<view class="input-item flex">
				<view class="login-sigh login-sigh2  flex">
					<view class="imgcon">
						<image src="../../../static/login/list_icon_img@2x.png" mode=""></image>
					</view>
				</view>
				<input class="input_pcod" value="" placeholder="Picture Code"
					placeholder-style="color: #BFC2CC;font-size:14px;" v-model="picCode" />
				<view class="login-sigh3 flex">
					<image :src="imgcodeUrl" mode="scaleToFill" class="img1"></image>
					<image src="../../../static/login/icon_refresh@2x.png" mode="" class="img2" @click="getPicCode()">
					</image>
				</view>

			</view>
		<view class="input-item flex">
			<view class="login-sigh login-sigh2  flex">
				<view class="imgcon">
					<image src="../../../static/login/list_icon_code@2x.png" mode=""></image>
				</view>
			</view>
			<input value="" placeholder="Verification code"
				placeholder-style="color: #BFC2CCFF;font-size:14px;"
				v-model="infoCode" />
			<view class="wrap">
				<u-toast ref="uToast"></u-toast>
				<u-verification-code :seconds="seconds" ref="uCode" @change="codeChange"
					start-text="OTP" change-text="xs" end-text="OTP"></u-verification-code>
				<u-button  @tap="getCode" style="color: #954DE3FF; border-radius: 4px;font-size: 15px;width: 20px; height: 24px;line-height: 40px;">{{tips}}</u-button>
		
			</view>
		</view>
			<view class="input-item flex">
				<view class="login-sigh login-sigh2  flex">
					<view class="imgcon">
						<image src="../../../static/login/list_icon_key@2x.png" mode=""></image>
					</view>
				</view>
				<input type="password" value="" placeholder="new password"
					placeholder-style="color: #BFC2CCFF;font-size:14px;"
					v-model="newPassword" />
			</view>
			<button class="login-btn" @click="resetPassword()" :class="btnNum==1?'on':''"
				:disabled="btnNum==1?false:true">
				Confirm
			</button>

		</view>
		<tab-bar :selectActive="5"></tab-bar>
	</view>
</template>

<script>
	import api from '@/utils/api'
	import utils from '@/utils'
	import cont from "@/components/navigation/navigation.vue"
	export default {
		data() {
			return {
				tips: 'OTP',

				seconds: 10,
				mark: 'sms_user_forget_password',
				mobile: '',
				imgcodeUrl: '',
				newPassword: '',
				infoCode: '',
				picCode: '',
				areacode: '',
				btnNum: 0,
			}
		},

		onLoad() {
			this.getPicCode();
			this.areacode = uni.getStorageSync('siteInfo').areaCode;


		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			
			btnControl() {
				this.btnNum = 1;
				if (this.mobile == '') {
					this.btnNum = 0;
				}

			},
			getCode() {
				if (this.picCode == '') {
					uni.showToast({
						title: 'The graphic verification code is empty',
						icon: 'none',
						duration: 1500
					})
					return;
				}
				if (this.mobile == '') {
					uni.showToast({
						title: 'Please input mobile phone number',
						icon: 'none',
						duration: 1500
					})
					return;
				}
				var a = /^[0-9]{9}$/;
				if (!a.test(this.mobile)) {
					uni.showToast({
						title: 'Wrong format of mobile phone number',
						icon: 'none',
						duration: 1500
					})
					return;
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: 'Getting captcha'
					})

					setTimeout(() => {
						uni.hideLoading();
						this.$refs.uCode.start();
					}, 2000);
					this.countryCode = uni.getStorageSync('siteInfo').countryCode
					const post = api.sendSmsCode({
						"mobile": this.mobile,
						"mark": this.mark,
						"countryCode": this.countryCode,
						"code": this.picCode
					})
					post.then(res => { 
							var that = this;
							uni.showToast({
								title: 'Successfully',
								icon: 'none',
								duration: 2500
							})
							this.getPicCode();
						})
						.catch(e => {
							uni.showToast({
								title: e,
								icon: 'none',
								duration: 2500
							})
							this.getPicCode();
						})
				} else {
					this.$u.toast('Send after the countdown');
				}

			},
			getPicCode() {
				
				this.imgcodeUrl = "https://apitest.luck-work.com/api/home/imgCode?uuid=" + this.$u.guid(20);
			},

			resetPassword() {
				var a = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{5,19}$/;

				if (this.mobile == '') {
					uni.showToast({
						title: 'Please input mobile phone number',
						icon: 'none',
						duration: 1500
					})
					return;
				}
				var a = /^[0-9]{4,14}$/;
				if (!a.test(this.mobile)) {
					uni.showToast({
						title: 'Wrong format of mobile phone number',
						icon: 'none',
						duration: 1500
					})
					return;
				}
				if (this.infoCode == '') {
					uni.showToast({
						title: ' Please enter the SMS verification code',
						icon: 'none',
						duration: 1500
					})
					return;
				}

				if (this.picCode == '') {
					uni.showToast({
						title: ' Please enter the Graphic verification code',
						icon: 'none',
						duration: 1500
					})
					return;
				}
				if (!a.test(this.newPassword)) {
					uni.showToast({
						title: 'Incorrect password format,a password requires a combination of numbers and letters',
						icon: 'none',
						duration: 1500
					})
					return;
				}
             const post = api.forgotPassword({
            'mobile': this.mobile,
            'code': this.infoCode,
            'newPassword': this.newPassword,
             })
             post.then(res => {
             	if (res.errCode == 'SUCCESS') {
             		uni.showToast({
             			title: 'Password modified successfully',
             			icon: 'success',
             			duration: 1500
             		})
             	} else {
             		uni.showToast({
             			title: res.errMsg,
             			icon: '',
             			duration: 1500
             		})
             	}
             	console.log(res.data);
             	})
             	.catch(e => {
             		uni.showToast({
             			title: e,
             			icon: 'none',
             			duration: 2500
             		})
             	})
			}
		},
	}
</script>

<style>
	@import './index.css';
</style>
