<template>
	<view class="container">
		<view class="title-img">
			<!-- <image class="title-image" src="../../static/img/hlogo.png" mode="widthFix"></image> -->
		</view>
		<view class="title-font">
			芊扦龙虾
		</view>
		<view class="login_text">
			<view class="login_input flex_item">
				<image class="login_img" src="../../static/img/user.png" mode="widthFix"></image>
				<view class="login_name"><input class="uni-input" type="text" v-model="phone" focus
						placeholder="请输入手机" /></view>
			</view>
			<view class="login_input flex_item">
				<image class="login_img" src="../../static/img/pwd.png" mode="widthFix"></image>
				<view class="login_name"><input class="uni-input" type="password" v-model="password" focus
						placeholder=" 请输入新的不少于6位的密码" /></view>
			</view>
			<view class="login_input flex_item">
				<image class="login_img" src="../../static/img/pwd.png" mode="widthFix"></image>
				<view class="login_name"><input class="uni-input" type="password" v-model="password2" focus
						placeholder="请重复输入新密码" /></view>
			</view>
			<view class="login_input flex">
				<image class="login_img" src="../../static/img/send.png" mode="widthFix"></image>
				<view class="login_name flex">
					<input class="uni-input width" v-model="code" type="number" focus placeholder="请输入验证码" />
					<view class="code" @click="verification">{{ countDown == 0 ? '发送验证码' : countDown }}</view>
				</view>
			</view>
			<view class="uni-button uni-button-green" @click="updatalogin">确认修改</view>
		</view>
	</view>
</template>
<script>
	import {
		registerReset
	} from '@/api/set.js';
	import {
		verify
	} from '@/api/login.js';
	export default {
		data() {
			return {
				phone: '', //用户
				code: '', //验证码
				password2: '',
				password: '',
				time: '', //保存倒计时对象
				countDown: 0 //倒计时
			};
		},
		onLoad() {},
		watch: {
			// 监听倒计时
			countDown(i) {
				if (i == 0) {
					clearInterval(this.time);
				}
			}
		},
		methods: {
			updatalogin() {
				let obj = this;
				if (obj.phone == '') {
					obj.$api.msg('请输入手机');
					return;
				}
				if (!/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(obj.phone) && !
					/(^1[3|4|5|6|7|8|9][0-9]{9}$)/.test(this.phone)) {
					obj.$api.msg('请输入正确的手机');
					return;
				}
				if (obj.password == '') {
					obj.$api.msg('请输入密码');
					return;
				}
				if (obj.password2 == '') {
					obj.$api.msg('请再次输入密码');
					return;
				}
				if (obj.password2 != obj.password) {
					obj.$api.msg('两次密码不正确');
					return;
				}
				if (obj.code == '') {
					obj.$api.msg('请输入验证码');
					return;
				}
				registerReset({
						account: obj.phone, //账号
						password: obj.password,
						password2: obj.password2,
						type: 1,
						captcha: obj.code
					})
					.then(function(e) {
						obj.$api.msg(e.msg);
						uni.navigateTo({
							url: '/pages/public/login'
						});
					})
					.catch(e => {
						console.log(e);
					});
			},
			//发送验证码
			verification() {
				let obj = this;
				if (this.phone == '') {
					this.$api.msg('请输入邮箱号码');
					return;
				}
				if (!/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(obj.phone) && !
					/(^1[3|4|5|6|7|8|9][0-9]{9}$)/.test(this.phone)) {
					obj.$api.msg('请输入正确的邮箱或手机');
					return;
				}
				// 判断是否在倒计时
				if (obj.countDown > 0) {
					return false;
				} else {
					obj.countDown = 60;
					obj.time = setInterval(() => {
						obj.countDown--;
					}, 1000);
					//调用验证码接口
					verify({
							phone: obj.phone,
							type: 'login'
						})
						.then(({
							data
						}) => {
							uni.showToast({
								title: '验证码已发送',
								duration: 2000,
								position: 'top',
								icon: 'none'
							});
						})
						.catch(err => {
							console.log(err);
						});
				}
			}
		}
	};
</script>

<style lang="scss">
	page {
		min-height: 100%;
		background-color: #ffffff;

		.container {
			width: 100%;
		}
	}

	.title-img {
		margin: 100rpx auto 0;
		width: 160rpx;

		.title-image {
			width: 160rpx;
		}
	}

	.title-font {
		margin-top: 10rpx;
		text-align: center;
		font-size: 32rpx;
		font-family: Source Han Sans CN;
		font-weight: bold;
		color: #333333;
	}

	.login_text {
		position: relative;
		padding: 100rpx 102rpx;
		background-color: #ffffff;
		border-top-left-radius: 40rpx;
		border-top-right-radius: 40rpx;

		.login_input {
			margin-top: 20rpx;
			padding: 26rpx;
			border-radius: 21rpx;
			border-bottom: 1px solid #eeeeee;

			.login_img {
				width: 30rpx;
				flex-shrink: 0;
			}

			.uni-input {
				text-align: left;
				width: 470rpx;
				font-size: 28rpx !important;
			}

			.login_name {
				margin-left: 20rpx;
				color: #333333;
			}
		}

		.other {
			margin-top: 60rpx;

			.fenge {
				width: 30%;
				height: 2rpx;
				background-color: #eeeeee;
			}

			.qita {
				font-size: 28rpx;
				color: #999999;
			}
		}

		.weixin {
			width: 75rpx;
			height: 75rpx;
			margin: 25rpx auto;
		}

		.weixin image {
			width: 100%;
			height: 100%;
		}

		.weixin_text {
			text-align: center;
			font-size: 28rpx;
			color: #999999;
		}

		.forget {
			font-size: 28rpx;
			width: 100%;
			text-align: right;
			color: #999999;
		}

		.uni-button-green {
			text-align: center;
			background: linear-gradient(90deg, #CA57DC, #65B2E9);
			border-radius: 10rpx;
			color: #ffffff;
			margin: 40rpx 10rpx;
		}

		.uni-button-green-plain {
			margin: 40rpx 10rpx;
			background: linear-gradient(90deg, #CA57DC, #65B2E9);
			border-radius: 10rpx;
			color: #ffffff;
		}

		.uni-button {
			height: 85rpx;
			line-height: 85rpx;
		}
	}

	.code {
		padding-right: 30rpx;
		font-size: 23rpx;
		width: 150rpx;
		flex-shrink: 0;
		text-align: center;
		color: #2E58FF;

		background: linear-gradient(0deg, #CA57DC 0%, #65B2E9 100%);
		-webkit-background-clip: text;
		-webkit-text-fill-color: transparent;
	}

	.width {
		width: 325rpx !important;
	}

	.login {
		background: #db292b;
		margin-top: 96rpx;
		color: #ffffff;
		text-align: center;
		padding: 26rpx 0rpx;
		border-radius: 20rpx;
	}
</style>