<template>
	<view class="Box">
		<view class="box_box">
			<view class="box_item flex-between">
				<text class="top_box_one_text">当前账号</text>
				<input type="number" v-model="mobile" placeholder="请输入手机号" maxlength="11" disabled="disabled" />
			</view>
			<view class="box_item flex-between" style="position:relative;">
				<text class="top_box_one_text"></text>
				<input type="number" v-model="form.checkCode" 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="box_item flex-between">
				<text class="top_box_one_text">新密码</text>
				<input type="number" v-model="form.password" placeholder="请输入6位数字交易密码" maxlength="6"
					:password="!passwords" />
			</view>
			<view class="box_item flex-between">
				<text class="top_box_one_text">确认密码</text>
				<input type="number" v-model="form.payPassword" placeholder="请再次输入密码" maxlength="6"
					:password="!payPasswords" />
			</view>
		
		</view>
		<view class="flex-colum">
			<view class="Box_bottom" @click="userInfosavePayPassword">确定</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				isPwd: uni.cache.get('userInfo').isPwd,
				mobile: uni.cache.get('userInfo').telephone.slice(0, 3)+'****'+uni.cache.get('userInfo').telephone.slice(7),
				form: {
					mobile: uni.cache.get('userInfo').telephone,
					password: '',
					payPassword: '',
					checkCode: ''
				},
				passwords: false,
				payPasswords: false,
				// 注册定时器   初始值
				second: 60,
				showText: true,
				Recapture: '获取验证码'
			};
		},
		onLoad() {
			

		},
		methods: {
			async CodeRegister() {
				const res = await this.api.phoneValidateCode({
					// post  手机验证码
					phone: this.form.mobile
				});
				if (res) {
					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 loginwxuserInfo() {
				let res = await this.api.loginwxuserInfo({
					userId: uni.cache.get('userInfo').id
				})
				if (res.code == 0) {
					uni.cache.set('userInfo', res.data);
				}
			},
			async userInfosavePayPassword() {

				if (this.form.mobile.length != 11 && this.isPwd == 0) {
					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 (this.form.password.length != 6 || this.form.payPassword.length != 6) {
					uni.showToast({
						title: '密码必须是6位',
						icon: 'none'
					});
					return false;
				}
				if (this.form.payPassword != this.form.password) {
					uni.showToast({
						title: '密码和确认密码不一致',
						icon: 'none'
					});
					return false;
				}
				if (this.form.checkCode == null || this.form.checkCode == '') {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return false;
				}
				let res = await this.api.loginresetPwd({
					pwd: this.form.password,
					code: this.form.checkCode
				})

				if (res.code == 0) {
					if (uni.cache.get('userInfo').isPwd != 0) {
						uni.showToast({
							title: '修改成功',
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: '设置成功',
							icon: 'none'
						});
					}
					// 获取用户信息
					this.loginwxuserInfo()
					setTimeout(() => {
						uni.navigateBack();
					}, 1000);
				}
			}
		}
	};
</script>

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

	.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_box {
			background: #fff;
			padding: 0 32rpx;

			.box_item {
				border-bottom: 1rpx solid #E5E5E5;
				.top_box_one_text {
					font-family: Source Han Sans CN, Source Han Sans CN;
					font-weight: 500;
					font-size: 28rpx;
					color: #333333;
				}

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


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

					.yzm {
						padding: 8rpx 20rpx;
						background: #E5E5E5;
						border-radius: 24rpx;
						font-weight: 500;
						font-size: 24rpx;
						color: #333333;
					}
				}
			}
			.box_item:nth-child(1){
				border-bottom: none;
				border-top: 1rpx solid #E5E5E5;
				input{
					border-bottom: 1rpx solid #E5E5E5;
					color: #333;
				}
			}
		}


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

			.Box_bottom {
				width: 100%;
				height: 96rpx;
				line-height: 96rpx;
				background: #999;
				border-radius: 48rpx;
				font-size: 32rpx;
				font-family: Source Han Sans CN-Regular, Source Han Sans CN;
				font-weight: 400;
				color: #ffffff;
				text-align: center;
			}
		}
	}
</style>