<template>
	<view>
		<u-navbar title="找回密码" title-color="#101010"></u-navbar>
		<view class="main">
			<view class="original-password">
				<text>手机号码</text><u-input  v-model="form.telephone" style="padding: 0 0 0 20px"  maxlength="11" type="number"  placeholder="请填写手机号"  />
			</view>
			<view class="new-password">
				<text>验证码</text><u-input  v-model="form.verifyCode" style="padding: 0 0 0 20px" maxlength="6" type="number"  placeholder="6位验证码"  />
				<text class="verification-code" :style="isCodeTipsColor ? 'color: #999999;' : ''" @click="getCode" >{{codeTips}}</text>
			</view>
			<view class="new-password">
				<text>新密码</text><u-input  v-model="form.password" style="padding: 0 0 0 20px" class="password-input" type="password"  placeholder="创建6-16位组合新密码" :password-icon="true" />
			</view>
		</view>
		<u-verification-code :seconds="sendMsgSecond" ref="uCode" @change="codeChange" @end="end" @start="start"
			change-text="(Xs)">
		</u-verification-code>
		<!-- 确认修改 -->
		<button class="confirm"  @click="retrievePassword" >提交</button>
	</view>
</template>

<script>
	import * as API from '@/apis/pagejs/user.js'
	import {
		checkPhone
	} from '@/apis/utils'
	export default {
		data() {
			return {
				form: {
					verifyCode:"",
					telephone: '',
					password: '',
				},
				isSendMsgIng: false,
				isCodeTipsColor: false,
				sendMsgSecond: 60,
				codeTips: '',
			}
		},
		onReady() {
			var time = this.carhelp.get("getvcodetime");
			if (time) {
				//this.$refs.uCode.start();
				var nowtime = new Date().getTime()
				var differ = (nowtime - time) / 1000
				if (differ < 2 * 60) {
					this.sendMsgSecond = 2 * 60 - parseInt(differ)
					this.isSendMsgIng = true;
					this.$refs.uCode.start();
					this.isCodeTipsColor = true;
				}
			}
		},
		methods: {
			retrievePassword(){
				
				var checkPhoneResult = checkPhone(this.form.telephone);
				
				if (!this.form.telephone || checkPhoneResult != true) {
					uni.showToast({
						title: checkPhoneResult,
						icon: "none"
					})
					return;
				}
				
				if (!this.form.telephone) {
					uni.showToast({
						title: "请输入手机号",
						icon: "none"
					})
					return
				}
				if (!this.form.verifyCode) {
					uni.showToast({
						title: "请输入验证码",
						icon: "none"
					})
					return
				}
				if (!this.form.password) {
					uni.showToast({
						title: "请输入新密码",
						icon: "none"
					})
					return
				}
				if (this.form.password.length<6||this.form.password.length>16) {
					uni.showToast({
						title: "请输入6-16位组合新密码",
						icon: "none"
					})
					return
				}
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.retrievePassword(this.form).then((response) => {
					uni.hideLoading();
					// this.form.password=""
					// this.form.oldPassword=""
					uni.showModal({
						title:"提示",
						content:"密码修改成功！",
						showCancel:false,
						success() {
							uni.reLaunch({
								url:"/pages/login/login"
							})
						}
					})
					
					
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
				
			},
			codeChange(text) {
				this.codeTips = text;
			},
			//倒计时
			end() {
				this.sendMsgSecond = 60;
				this.isSendMsgIng = false;
				this.isCodeTipsColor = false;
			},
			// 获取验证码
			getCode() {
				if (this.$refs.uCode.canGetCode) {} else {
					uni.showToast({
						title: '倒计时结束后再发送',
						icon: "none"
					})
					return
				}
			
				var checkPhoneResult = checkPhone(this.form.telephone);
			
				if (checkPhoneResult !== true) {
					uni.showToast({
						icon: "none",
						title: checkPhoneResult,
					})
					return;
				}
				this.$refs.uCode.start();
				this.isCodeTipsColor = true;
			},
			start() {
				if (!this.isSendMsgIng) {
					uni.showLoading({
						title: "加载中",
						mask: true,
					})
					API.getVerifyCode(this.form).then((response) => {
						uni.hideLoading();
						this.carhelp.set("getvcodetime", new Date().getTime());
			
						if (!"") {
							//倒计时
							uni.showToast({
								icon: "none",
								title: "发送成功"
							})
						} else {
							uni.showToast({
								icon: "none",
								title: "您的验证码已经发送[5分钟有效]，请勿重复点击"
							})
						}
					}).catch(error => {
						uni.showToast({
							title: error,
							icon: "none"
						})
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main{
		background-color: #fff;
		margin-top: 24rpx;
		.original-password,.new-password{
			line-height: 96rpx;
			padding:0 32rpx;
			display: flex;
			align-items: center;
			border-bottom: 1px solid rgba(221,221,221,1);
			/deep/.u-input__input{
				height: 96rpx;
			}
			
		}
		text{
			display: inline-block;
			width: 128rpx;
			color: rgba(51,51,51,1);
			font-size: 32rpx;
		}
		.verification-code{
			width: 160rpx;
			color: rgba(22,119,255,1);
			font-size: 32rpx;
		}
	}
  // 确认修改
  .confirm{
	  border-radius: 8px;
	  background-color: rgba(22,119,255,1);
	  color: rgba(255,255,255,1);
	  font-size: 32rpx;
	  line-height: 88rpx;
	  margin: 24rpx 32rpx;
  }
</style>
