<template>
	<view class="container">
		<view class="list-box">
			<view class="flex_item list-title">
				<view class="tip"></view>
				<view class="title">支付密码</view>
			</view>
			<view class="list-cell flex_item">
				<view class="cell-name">新密码</view>
				<input type="password" v-model="password" placeholder="请输入不小于6位数的密码" />
			</view>
			<view class="list-cell flex_item">
				<view class="cell-name">重复密码</view>
				<input type="password" v-model="password2" placeholder="请重复输入密码" />
			</view>
			<view class="list-cell flex_item">
				<view class="cell-name nameCode">验证码</view>
				<view class="code-box flex">
					<input type="number" v-model="code" placeholder="请输入验证码" />
					<view class="code" @click="verification">{{ countDown == 0 ? '验证码' : countDown }}</view>
				</view>
			</view>
		</view>
		<view class="submit" @click="updatalogin">确认</view>
	</view>
</template>
<script>
import { updatalogin } from '@/api/set.js';
import { verify } from '@/api/login.js';
export default {
	data() {
		return {
			phone: '', //用户
			code: '', //验证码
			password2:'',
			password:'',
			time: '', //保存倒计时对象
			countDown: 0 //倒计时
		};
	},
	onLoad(option){
		let userInfo = uni.getStorageSync('userInfo') || '';
		this.phone = userInfo.account
	},
	onShow() {
		
	},
	watch: {
		// 监听倒计时
		countDown(i) {
			if (i == 0) {
				clearInterval(this.time);
			}
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		this.loadData();
	},
	methods: {
		updatalogin() {
			let obj = this;
			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;
			}
			updatalogin({
				account: obj.phone, //账号
				password:obj.password,
				password2:obj.password2,
				type:2,
				captcha: obj.code
			}).then(function(e) {
				obj.$api.msg(e.msg);
				uni.switchTab({
					url: '/pages/user/user'
				})
			}).catch((e) => {
				console.log(e);
			});
		},
		//发送验证码
		verification() {
			let obj = this;
			if (this.phone == '') {
				this.$api.msg('请输入电话号码');
				return;
			}
			if (this.phone.length < 11) {
				this.$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 }) => {})
					.catch(err => {
						console.log(err);
					});
			}
		},
	}
};
</script>

<style lang="scss">
page {
	min-height: 100%;
	background-color: #ffffff;
	.container {
		width: 100%;
		padding: 27rpx 31rpx;
		
	}
}
.list-box{
	.list-title{
		font-size: 30rpx;
		font-weight: 500;
		color: #333333;
		margin-bottom: 61rpx;
		.tip{
			width: 2rpx;
			height: 30rpx;
			background: #4C5D97;
			margin-right:17rpx ;
		}
	}
	.list-cell{
		margin-bottom: 70rpx;
		.cell-name{
			width: 30%;
			font-size: 26rpx;
			font-weight: 500;
			color: #333333;
		}
		.nameCode{
			width: 42% !important;
		}
		.code-box{
			width: 100% !important;
			input{
				width: 50%;
			}
			.code{
				font-size: 26rpx;
				font-weight: 500;
				color: #44969D;;
			}
		}
		input{
			width: 60%;
			text-align: left;
			font-size: 26rpx;
			font-weight: 500;
		}
	}
}
	.submit {
		background: linear-gradient(90deg, #60BAB0, #60BAB0, #45969B);
		margin-top: 20rpx;
		color: #FFFFFF;
		text-align: center;
		padding: 20rpx 0rpx;
		border-radius: 50rpx;
		margin-top: 60rpx;
	}
</style>
