<template>
	<view :class="[AppTheme]" class="all_box">
		<view class="loginstatus">
			<view class="sign_in_title">找回密码</view>
			<!-- 登录信息 -->
			<view class="login_infon">
				<view class="inputline">
					<u-input placeholder="请输入手机号" v-model="loginphone" border="bottom" fontSize="22">
						<template slot="suffix">
							<u-button class="bg-primary" @tap="getCode" :disabled="!ifsend" :text="codemess"
								:type="ifsend?'success':'warning'" shape="circle"></u-button>
						</template>
					</u-input>
				</view>
				<view class="inputline">
					<u-input placeholder="请输入验证码" v-model="verificationcode" border="bottom" fontSize="22"></u-input>
				</view>
				<view class="inputline">
					<u-input placeholder="请输入新密码" v-model="loginpassword" border="bottom" fontSize="22"></u-input>
				</view>
			</view>
			<!-- 登录按钮 -->
			<view class="login_button bg-linear-gradient" @click="forgetpassword()">
				提交
			</view>
		</view>
	</view>
</template>
<script>
	import loginapi from "@/api/login/login.js"
	export default {
		data() {
			return {
				primary: this.$theme.primary,
				// 登录手机号
				loginphone: null,
				// 验证码
				verificationcode: null,
				//密码
				loginpassword: null,
				//验证码是否可以发送
				ifsend: true,
				// 验证码状态
				codemess: '获取验证码'
			}
		},
		//下拉刷新
		onPullDownRefresh() {
			uni.startPullDownRefresh({
				success() {
					uni.stopPullDownRefresh()
				}
			})
		},
		methods: {
			// 获取验证码
			getCode() {
				let that = this
				if (that.loginphone !== null && that.loginphone !== undefined && that.loginphone.length === 11) {
					var myreg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
					if (myreg.test(that.loginphone)) {
						uni.showLoading({
							title: '正在获取验证码'
						})
						setTimeout(() => {
							uni.hideLoading()
							//获取验证码
							let params = {
								mobile: that.loginphone
							}
							loginapi.sendvecode(params).then(res => {
								if (parseInt(res.status) === 1) {
									uni.showToast({
										icon: 'none',
										title: '验证码已发送'
									})
									that.ifsend = false
								} else {
									uni.showToast({
										icon: 'none',
										title: '验证码发送失败,请稍后再试'
									})
								}
							})
						}, 1000)
						let num = 60
						that.timeer = setInterval(function() {
							if (num > 0) {
								that.codemess = num + 's后重新获取'
								num = num - 1
							} else {
								clearInterval(that.timeer)
								that.ifsend = true
								that.codemess = '重新获取验证码'
							}
						}, 1000)
					} else {
						uni.showToast({
							icon: 'none',
							title: '请输入有效的手机号'
						})
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: '请输入有效的手机号'
					})
				}
			},
			// 找回密码
			forgetpassword() {
				let that = this
				if (that.loginphone !== null && that.loginphone !== undefined && that.loginphone.length === 11) {
					if (that.verificationcode !== null && that.verificationcode !== undefined && that.verificationcode
						.length > 0) {
						if (that.loginpassword !== null && that.loginpassword !== undefined && that.loginpassword.length >
							0) {
							//验证完毕，执行验证码登录
							let parms = {
								uminfo: that.loginphone,
								code: that.verificationcode,
								newpass: that.loginpassword
							}
							loginapi.editpass(parms).then(res => {
								if (parseInt(res.status) === 200) {
									// 修改成功
									try {
										uni.clearStorageSync()
										uni.showToast({
											icon: 'none',
											title: '修改成功,请重新登录'
										})
										setTimeout(function() {
											uni.redirectTo({
												url: '/pagesC/pages/login/login'
											})
										}, 1000)

									} catch (e) {
										uni.showToast({
											icon: 'none',
											title: e
										})
									}
								} else {
									uni.showToast({
										icon: 'none',
										title: '修改失败' + res.msg
									})
								}
							})
						} {
							uni.showToast({
								icon: 'none',
								title: '请输入正确的密码'
							})
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: '请输入正确的验证码'
						})
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的账号'
					})
				}
			}
		}
	}
</script>
<style lang="scss" scoped>
	.all_box {
		.loginstatus {
			.sign_in_title {
				padding: 240rpx 10% 0 10%;
				width: 80%;
				color: #000000;
				font-size: 45rpx;
				font-weight: 600;
			}

			.login_infon {
				padding: 60rpx 10%;
				width: 80%;

				.inputline {
					margin: 20rpx 0;
				}
			}

			.login_button {
				margin: 70rpx 10% 25rpx 10%;
				padding: 24rpx 0;
				width: 80%;
				text-align: center;
				color: #FFFFFF;
				font-size: 32rpx;
				border-radius: 50rpx;
			}
		}
	}
</style>
