<template>
	<view class="forget">
		<u-toast ref="uToast" />
		<u-navbar back-text=" " title="修改密码" :background="{backgroundColor: 'transparent'}"></u-navbar>
		<image class="bgc" src="../../static/img/group.png" mode=""></image>
		<view class="con">
			<view class="h2">修改密码</view>
			<view class="form">
				<view class="input">
					<u-input v-model="phone" maxlength="11" :clearable="false" type="number" :border="border" placeholder="请输入手机号" />
				</view>
				<view class="input sms flex">
					<u-input v-model="ver_code" :clearable="false" type="text" :border="border" placeholder="请输入验证码" />
					<view class="wrap">
						<u-toast ref="uToast"></u-toast>
						<u-verification-code :seconds="seconds" start-text="获取验证码" @end="end" @start="start" ref="uCode" @change="codeChange"></u-verification-code>
						<view class="tips" @tap="getCode">{{tips}}</view>
					</view>
				</view>
				<view class="input">
					<u-input v-model="password" :clearable="false" type="password" :border="border" placeholder="请输入密码" />
				</view>
				<view class="input">
					<u-input v-model="confirm_password" :clearable="false" type="password" :border="border" placeholder="请重新输入密码" />
				</view>
			</view>
			<view class="btn" @click="ForgetPassword()">
				修改并登陆
			</view>
		</view>
		<view class="agreement u-flex u-row-center">
			<u-checkbox-group @change="checkboxGroupChange">
				<u-checkbox v-model="checked" shape="circle" active-color="#167FFF">
					<view class="text">已阅读并同意<text @click="agreement">《隐私协议》</text></view>
				</u-checkbox>
			</u-checkbox-group>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: '',
				ver_code: '',
				password: '',
				confirm_password: "",
				checked: true,
				seconds: 60,
				tips: '获取验证码',
			}
		},
		onLoad(option) {},
		methods: {
			agreement() {
				uni.navigateTo({
					url: '/pages/login/agreement',
				})
			},
			checkboxGroupChange(e) {
				console.log(this.checked);
			},
			codeChange(text) {
				this.tips = text;
			},
			// 获取短信
			getCode() {

				console.log('发送验证码')
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					// uni.showLoading({
					// 	title: '正在获取验证码'
					// })
					console.log(this.phone)
					if (this.phone == '') {
						this.$refs.uToast.show({
							title: '请输入手机号',
							type: 'error ',
						})
						return;
					}
					if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phone))) {
						this.$refs.uToast.show({
							title: '请输入正确的手机号格式',
							type: 'warning ',
						})
						return;
					}
					this.$http.smsSend({ phone: this.phone, event: 'forgetpwd' })
						.then(res => {
							if (res.data.code == 200) {
								this.$u.toast('验证码已发送');
								this.$refs.uCode.start();
							} else {
								this.$refs.uToast.show({
									title: res.data.msg,
									type: 'error ',
								})
							}
						})
					// setTimeout(() => {
					// 	uni.hideLoading();
					// 	// 这里此提示会被this.start()方法中的提示覆盖
					// 	this.$u.toast('验证码已发送');
					// 	// 通知验证码组件内部开始倒计时
					// 	this.$refs.uCode.start();
					// }, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			// 登录
			ForgetPassword() {
				if (this.phone == '') {
					this.$refs.uToast.show({
						title: '请输入手机号',
						type: 'error ',
					})
					return;
				}
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phone))) {
					this.$refs.uToast.show({
						title: '请输入正确的手机号格式',
						type: 'warning ',
					})
					return;
				}
				if (this.ver_code == '') {
					this.$refs.uToast.show({
						title: '请输入验证码',
						type: 'error ',
					})
					return;
				}
				if (this.password == '') {
					this.$refs.uToast.show({
						title: '请输入密码',
						type: 'error ',
					})
					return;
				}
				if (this.confirm_password == '') {
					this.$refs.uToast.show({
						title: '请输入确认密码',
						type: 'error ',
					})
					return;
				}
				if (this.confirm_password != this.password) {
					this.$refs.uToast.show({
						title: '两次输入密码不一致',
						type: 'error ',
					})
					return;
				}
				if (!this.checked) {
					this.$refs.uToast.show({
						title: '请选中并阅读隐私协议',
						type: 'error ',
					})
					return;
				}
				this.$http.ForgetPassword({
						confirm_password: this.confirm_password,
						password: this.password,
						phone: this.phone,
						ver_code: this.ver_code
					})
					.then(res => {
						console.log(res)
						if (res.data.code == 200) {
							uni.setStorageSync('token', res.data.result.token);
							this.getUserInfo()
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'error ',
							})
						}
					})
			},
			getUserInfo() {
				this.$http.getUserInfo()
					.then(res => {
						if (res.data.code == 200) {
							// 是否信息完整
							if (res.data.result.completeInformation != 2) {
								uni.navigateTo({
									url: '/pages/login/perfect',
								})
							} else {
								uni.navigateTo({
									url: '/pages/index/index',
								})
							}
						}
					})
			},
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
	}

	.forget {
		position: relative;
		height: 100%;
		overflow: hidden;
		box-sizing: border-box;
		padding: 0 100rpx;

		.bgc {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
		}

		.con {
			position: relative;
			height: 100%;
			color: #000;

			.h2 {
				font-size: 56rpx;
				padding-top: 80rpx;
			}

			.form {
				margin-top: 80rpx;
			}

			.u-scroll-box {
				display: block;

				.u-tab-item {
					margin-right: 60rpx;
				}
			}

			.btn {
				margin-top: 100rpx;
				height: 80rpx;
				text-align: center;
				line-height: 80rpx;
				background: $color;
				border-radius: 40rpx;
				font-size: 28rpx;
				color: #fff;
			}
		}

		.agreement {
			position: fixed;
			left: 0;
			width: 750rpx;
			bottom: 70rpx;
			text-align: center;
			font-size: 24rpx;

			.text {
				font-size: 24rpx;
				text{
					color: $color;
				}
			}
		}
	}
</style>