<template>
	<view class="Box">
		<navseat :opacity='false' :title="titles" :titleshow='true' :namecolor='"#fff"' iconcolor='#fff'></navseat>
		<view class="box_item flex-between">
			<text class="top_box_one_text">手机号</text>
			<input type="number" v-model="form.account" placeholder="请输入手机号" maxlength="11" disabled="disabled" />
		</view>
		<view class="box_item flex-between">
			<text class="top_box_one_text">密码</text>
			<input type="text" v-model="form.password" placeholder="请输入密码" :password="!passwords" />
		</view>
		<view class="box_item flex-between">
			<text class="top_box_one_text">确认密码</text>
			<input type="text" v-model="form.payPassword" placeholder="确认密码" :password="!payPasswords" />
		</view>
		<view class="box_item flex-between" style="position:relative;">
			<text class="top_box_one_text">验证码</text>
			<input type="number" v-model="form.captcha" placeholder="请输入验证码" style="padding-right: 140rpx;" />
			<view class="top_box_one_texts">
				<view v-if="showText == true" class="yzm" @click="CodeRegister">{{ Recapture }}</view>
				<view v-else style="color: #ccc; background-color: #f9f9f9; border-radius:16px; padding: 10rpx;">
					{{ second }}s重新发送
				</view>
			</view>
		</view>
		<view class="flex-colum">
			<view class="Box_bottom" @click="userInfosavePayPassword">确定</view>
		</view>
	</view>
</template>
<script>
	import navseat from '@/components/navseat.vue'
	export default {
		components: {
			navseat
		},
		data() {
			return {
				form: {
					account: uni.cache.get('userinfo').mobile,
					password: '',
					payPassword: '',
					captcha: ''
				},
				titles: uni.cache.get('set_pwd') == 0 ? '修改密码' : '设置密码',
				passwords: false,
				payPasswords: false,
				// 注册定时器   初始值
				second: 60,
				showText: true,
				Recapture: '发送验证码'
			};
		},
		onLoad() {},

		methods: {
			async CodeRegister() {
				if (this.form.password == null || this.form.password == '') {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return false;
				}
				if (this.form.payPassword == null || this.form.payPassword == '') {
					uni.showToast({
						title: '请输入确认密码',
						icon: 'none'
					});
					return false;
				}
				if (!uni.$u.test.rangeLength(this.form.password, [6, 24])) {
					uni.showToast({
						title: "请输入密码长度为6-24",
						icon: "none"
					})
					return false;
				}
				if (this.form.payPassword != this.form.password) {
					uni.showToast({
						title: '密码和确认密码不一致',
						icon: 'none'
					});
					return false;
				}
				//发起验证码
				const res = await this.api.smssend({
					mobile: this.form.account,
					event: 'resetpwd'
				});
				if (res.code == 1) {
					uni.showToast({
						title: '验证码获取成功',
						icon: 'none'
					});
					// 定时器
					this.showText = false;
					this.Recapture = '重新获取';
					var interval = setInterval(() => {
						let times = --this.second;
						this.second = times < 10 ? '0' + times : times; //小于10秒补 0
					}, 1000);
					setTimeout(() => {
						clearInterval(interval);
						this.second = 60;
						this.showText = true;
					}, 60000);
				}
			},
			async userInfosavePayPassword() {
				if (this.form.account.length != 11) {
					uni.showToast({
						title: '手机号必须是11位',
						icon: 'none'
					});
					return false;
				}
				if (this.form.password == null || this.form.password == '') {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return false;
				}
				if (this.form.payPassword == null || this.form.payPassword == '') {
					uni.showToast({
						title: '请输入确认密码',
						icon: 'none'
					});
					return false;
				}
				if (!uni.$u.test.rangeLength(this.form.password, [6, 24])) {
					uni.showToast({
						title: "请输入密码长度为6-24",
						icon: "none"
					})
					return false;
				}
				if (this.form.payPassword != this.form.password) {
					uni.showToast({
						title: '密码和确认密码不一致',
						icon: 'none'
					});
					return false;
				}
				if (!uni.$u.test.rangeLength(this.form.password, [6, 24])) {
					uni.showToast({
						title: "请输入密码长度为6-24",
						icon: "none"
					})
					return false;
				}
				if (this.form.captcha == null || this.form.captcha == '') {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return false;
				}
				let res = await this.api.usersetuserpwd({
					account: this.form.account,
					password: this.form.password,
					captcha: this.form.captcha
				})
				if (res.code == 1) {
					uni.showToast({
						title: this.titles + '成功',
						icon: 'none'
					});
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/index/index?restype=' + 1
						});
					}, 1500);
				}
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: #fff;
	}

	.Box {
		padding: 0 28rpx;

		.box_one {
			margin-top: 26rpx;
			font-size: 24rpx;
			font-family: Source Han Sans CN-Regular, Source Han Sans CN;
			font-weight: 400;
			color: #999999;
		}

		.box_item {
			padding: 36rpx 0 0 0;

			.top_box_one_text {
				font-size: 28rpx;
				font-family: Source Han Sans CN-Regular, Source Han Sans CN;
				font-weight: 400;
				color: #333333;
			}

			input {
				width: 75%;
				font-size: 24rpx;
				font-family: Source Han Sans CN-Regular, Source Han Sans CN;
				font-weight: 400;
				color: #999999;
			}

			.repeat {
				position: absolute;
				bottom: 26rpx;
				right: 0;
				width: 25rpx;
				height: 17rpx;
			}

			.top_box_one_texts {
				position: absolute;
				right: 0;
				font-size: 28rpx;
				font-family: Source Han Sans CN-Regular, Source Han Sans CN;
				font-weight: 400;
				color: #5082fd;
			}
		}

		.flex-colum {
			width: 100%;
			margin-top: 156rpx;

			.Box_bottom {
				width: 558rpx;
				height: 72rpx;
				background: #5082fd;
				border-radius: 50rpx;
				font-size: 36rpx;
				font-family: Source Han Sans CN-Regular, Source Han Sans CN;
				font-weight: 400;
				color: #ffffff;
				line-height: 72rpx;
				text-align: center;
			}
		}
	}
</style>