<template>
	<view>
		<view class="content">
			<view class="pageTop  x-c">
				<view class="loginBox">
					<view class="login-item">
						<view class="input-account">
							<input v-model="phone" placeholder="手机号" type="text" />
						</view>
						<view class="line"></view>
					</view>

					<view class="login-item">
						<view class="input-pwd">
							<input v-model="newPassword" placeholder="新密码(6-15个字符)" type="password" />
						</view>
						<view class="line"></view>
					</view>
					<view class="login-item">
						<view class="input-pwd">
							<input v-model="confirmPassword" placeholder="确认密码" type="password" />
						</view>
						<view class="line"></view>
					</view>
					<view class="login-item">
						<view class="input-yzcode x-bc">
							<input v-model="code" placeholder="验证码" type="number" />
							<view style="color:#666;flex-shrink: 0;" @click="sendSms">
								{{coolDown==0 ? '获取验证码': `${coolDown}秒后重试`}}
							</view>
						</view>
						<view class="line"></view>
					</view>
					<view class="btns">
						<view class="login-btn" @click="resetPasswordLogin">设置新密码</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		login,
		getUserInfo,
		sendCode,
		resetPassword
	} from '@/api/user'

	export default {
		data() {
			return {
				//userName:"CEadmin01",
				//password:"ADMIN01",
				phone: "",
				code: "",
				newPassword: "",
				confirmPassword: "",
				coolDown: 0,
				timer: null,
				btnLoading: false
			}
		},
		onLoad(option) {
			let that = this;
			uni.$on('getRegistrationID', function(data) {
				that.registrationID = data;
			});
			this.$getRegistrationID();
			this.btnLoading = false
		},
		onShow() {

		},
		onUnload() {
			uni.$off('getRegistrationID');
		},
		mounted() {

		},
		methods: {
			resetPasswordLogin() {
				if(this.btnLoading) return
				if (!uni.$u.test.mobile(this.phone)) {
					uni.showToast({
						title: "请输入正确的手机号",
						icon: 'none',
					});
					return
				}
				if (this.$isEmpty(this.newPassword)) {
					uni.showToast({
						title: "请输入新密码",
						icon: 'none',
					});
					return
				}
				if (this.newPassword.length<6||this.newPassword.length>15 ) {
					uni.showToast({
						title: "密码长度应该在6到15个字符",
						icon: 'none',
					});
					return
				}
				if (this.$isEmpty(this.confirmPassword)) {
					uni.showToast({
						title: "请输入确认密码",
						icon: 'none',
					});
					return
				}
				if (this.newPassword!=this.confirmPassword) {
					uni.showToast({
						title: "两次输入密码不一致",
						icon: 'none',
					});
					return
				}
				if (this.$isEmpty(this.code)) {
					uni.showToast({
						title: "请输入验证码",
						icon: 'none',
					});
					return
				}

				this.btnLoading = true
				const param = {
					phone: this.phone,
					code: this.code,
					newPassword: this.newPassword,
					confirmPassword: this.confirmPassword,
				}
				resetPassword(param).then(res=>{
					this.btnLoading = false
					if(res.code == 200) {
						uni.showToast({
							title: '设置成功',
							icon: 'none',
						});
						setTimeout(()=>{
							uni.redirectTo({
								url: '/pages/auth/login'
							})
						},1500)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
						});
					}
				}).catch(()=>{
					this.btnLoading = false
				})
			},
			sendSms() {
				if (this.coolDown > 0) {
					return
				}
				if (!uni.$u.test.mobile(this.phone)) {
					uni.showToast({
						title: "请输入正确的手机号",
						icon: 'none',
					});
					return
				}
				console.log(uni.$u.test.mobile(this.phone))

				this.coolDown = 60;
				sendCode({
					phone: this.phone
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '验证码已发送',
							icon: 'none',
						});
						this.coolDown = 60;
						this.timer = setInterval(() => {
							this.coolDown--;
							if (this.coolDown === 0) {
								clearInterval(this.timer);
								this.timer = null;
							}
						}, 1000);
					} else {
						this.timer && clearInterval(this.timer);
						this.coolDown = 0
						uni.showToast({
							title: res.msg,
							icon: 'none',
						});
					}
				},
				rej => {
					this.timer && clearInterval(this.timer);
					this.coolDown = 0;
				}).catch(() => {
					this.timer && clearInterval(this.timer);
					this.coolDown = 0;
				})
			},
			login() {
				if (this.$isEmpty(this.userName)) {
					uni.showToast({
						title: "请输入帐号",
						icon: 'none',
					});
					return
				}
				if (this.$isEmpty(this.password)) {
					uni.showToast({
						title: "请输入密码",
						icon: 'none',
					});
					return
				}
				var data = {
					userName: this.userName,
					password: this.password,
					jpushId: this.registrationID || uni.getStorageSync("registrationID")
				};
				var that = this;
				uni.showLoading({
					title: "处理中..."
				});
				login(data).then(res => {
						uni.hideLoading()
						if (res.code == 200) {

							// #ifdef APP-PLUS
							//const  jyJPush = uni.requireNativePlugin('JY-JPush');

							// #endif 

							uni.setStorageSync('AppToken', res.data.token);
							this.$Router.pushTab({
								name: 'home'
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					},
					rej => {}
				);

			},
			goToRegister() {
				this.$navTo('./register');
			},
			goToFindPass() {
				this.$navTo('./findPass');
			},
			getRegistrationID() {
				// #ifdef APP-PLUS

				// #endif 
			}
		},
		beforeDestroy() { // 组件卸载时清掉定时器，防止内存泄漏
			this.timer && clearInterval(this.timer);
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		height: calc(100vh);
		width: 100%;
		justify-content: space-between;
		//padding-top: calc(100% - 32vh);
	}

	.pageTop {
		display: flex;
		flex-direction: column;
		width: 100%;
	}

	.content .head {
		text-align: center;
		width: 100%;
		height: 522rpx;
		background: url(/static/image/login/top_bg.png) no-repeat 0 center;
		background-size: cover;
		box-sizing: border-box;
	}

	.content .head image {
		width: 150rpx;
		height: 150rpx;
		border-radius: 10rpx;
		box-shadow: 0px 0px 20rpx rgba(0, 0, 0, 0.2);

	}

	.title {
		color: #141414;
		margin: 50upx 0upx 30upx 0rpx;
		font-size: 38rpx;
		font-weight: 500;
	}

	.desc {
		color: #686866;
		padding: 0 0 30rpx 0rpx;
		font-size: 28rpx;
	}

	.loginBox {
		padding: 10px 10px 30rpx;
		width: calc(100% - 20px);
		margin-top: 0rpx;
		background: #FDFDFD;
		//box-shadow: 0rpx 4rpx 27rpx 0rpx rgba(155,155,155,0.25);
		border-radius: 7rpx;

	}

	.login-item p {
		text-align: left;
	}

	.line {
		height: 0.5rpx;
		background-color: #efefef;
		margin-top: 10rpx;
	}

	.input-account {
		margin-top: 20rpx;
		margin-bottom: 0rpx;
		border-radius: 40rpx;
		border: solid 0rpx #efefef;
		height: 80rpx;
		width: 100%;
		background: url(/static/account.png) no-repeat 0 center;
		background-size: 30rpx 30rpx;
		background-position: 30rpx;
	}

	.input-yzcode {
		margin-top: 20rpx;
		margin-bottom: 0rpx;
		border-radius: 40rpx;
		border: solid 0rpx #efefef;
		height: 80rpx;
		width: 100%;
		background: url(/static/image/login/cz_icon.png) no-repeat 0 center;
		background-size: 30rpx 30rpx;
		background-position: 30rpx;
	}

	.input-pwd {
		margin-top: 40rpx;
		margin-bottom: 20rpx;
		border-radius: 40rpx;
		border: solid 0rpx #efefef;
		height: 80rpx;
		width: 100%;
		background: url(/static/password.png) no-repeat 0 center;
		background-size: 30rpx 30rpx;
		background-position: 30rpx;
	}

	input {
		margin-left: 80rpx;
		height: 80rpx;
		line-height: 80rpx
	}

	.footer {
		color: #686866;
		width: 100%;
		position: fixed;
		text-align: center;
		bottom: 60upx;
		font-size: 28rpx;
	}

	.btns {
		margin: 60rpx 0rpx;
	}

	.login-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 80rpx;
		background: linear-gradient( 135deg, #FF5267 0%, #FF233C 100%);
		box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
		border-radius: 40rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(255, 255, 255, 1);
	}

	.reg-box {
		padding-bottom: 20rpx;
		margin: 0 10px;

		.reg-btn {
			font-size: 16px;
			color: #FF233C;
		}
	}

	.pageBottom {
		height: 260rpx;
		width: 75%;
		display: flex;
		flex-direction: column;
	}

	.tips {
		color: #999;
		font-size: 36rpx;
	}

	.menu {
		margin-top: 30rpx;

		image {
			width: 78rpx;
			height: 78rpx;
		}
	}
</style>