<template>
	<view class="container">
		<view class="listBox">
			<view class="list">
				<view class="flex listItem">
					<view class="flex titleBox">
						<text class="title">手机号</text>
					</view>
					<view class="right flex">
						<input class="input" placeholder="请输入手机号" type="text" v-model="account"
							placeholder-class="placeholder" />
					</view>
				</view>
				<view class="flex listItem">
					<view class="flex titleBox">
						<text class="title">验证码</text>
					</view>
					<view class="right flex">
						<input class="input" v-model="captcha" type="text" placeholder="请填写验证码"
							placeholder-class="placeholder" />
						<view class="code" @click="verification">{{ countDown == 0 ? '验证码' : countDown }}</view>
					</view>
				</view>
				<view class="flex listItem">
					<view class="flex titleBox">
						<text class="title">新密码</text>
					</view>
					<view class="right flex">
						<input class="input" placeholder="请输入新密码" type="text" v-model="password"
							placeholder-class="placeholder" />
					</view>
				</view>
				<view class="flex listItem">
					<view class="flex titleBox">
						<text class="title">确认密码</text>
					</view>
					<view class="right flex">
						<input class="input" placeholder="再次输入密码" type="text" v-model="dbPassword"
							placeholder-class="placeholder" />
					</view>
				</view>
			</view>
		</view>
		<!-- <view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" v-model="account" type="text" placeholder="请填写手机号" placeholder-class="placeholder" />
		</view>
		
		<view class="row b-b">
			<text class="tit">新密码</text>
			<input class="input" v-model="password" type="password" placeholder="请填写新密码" placeholder-class="placeholder" />
		</view> -->
		<view class="base-buttom" :class="{'bg-gray':loding}" @click="loding?'':confirm()">提交</view>
	</view>
</template>

<script>
	import {
		verify
	} from '@/api/login.js';
	import {
		mapState
	} from 'vuex';
	import {
		registerReset
	} from '@/api/set.js';
	export default {
		data() {
			return {
				time: '', //保存倒计时对象
				countDown: 0, //倒计时
				account: '', //原密码
				dbPassword: '', //对比密码
				password: '', //新密码
				loding: false, //是否载入中
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		onLoad() {
			if (this.userInfo.phone == null) {
				this.account = '';
			} else {
				this.account = this.userInfo.phone;
				this.show = false;
			}
		},
		watch: {
			// 监听倒计时
			countDown(i) {
				if (i == 0) {
					clearInterval(this.time);
				}
			}
		},
		methods: {
			//发送验证码
			verification() {
				let obj = this;
				if (this.account == '') {
					uni.showToast({
						title:"请输入电话号码",
						icon:"none"
					})
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(this.account)) {
					uni.showToast({
						title:"请输入正确的手机号",
						icon:"none"
					})
					return;
				}
				// 判断是否在倒计时
				if (obj.countDown > 0) {
					return false;
				} else {
					obj.countDown = 60;
					obj.time = setInterval(() => {
						obj.countDown--;
					}, 1000);
					//调用验证码接口
					verify({
							phone: obj.account,
							type: ''
						})
						.then(({
							data
						}) => {})
						.catch(err => {
							console.log(err);
						});
				}
			},
			confirm(e) {
				this.loding = true;
				if (this.dbPassword != this.password) {
					this.loding = false;
					uni.showModal({
						title: '请重新输入',
						content: '两次密码输入不一致',
						showCancel: false,
					});
					return
				}
				registerReset({
						account: this.account,
						captcha: this.captcha,
						password: this.password,
					})
					.then(({
						data
					}) => {
						this.loding = false;
						uni.showToast({
							title:"修改成功",
							icon:"success"
						})
					})
					.catch(err => {
						this.loding = false;
						console.log(err);
					});
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: $page-color-base;
	}

	.container {
		padding-top: 30rpx;
	}

	.listBox {
		margin: $page-row-spacing;
		margin-top: 0;
		border-radius: 20rpx;
		overflow: hidden;
		background-color: #FFFFFF;
		
	}

	.list {
		.input {
			text-align: right;
			font-size: $font-base;
			color: $color-gray;
			width: 100%;
			
		}

		.listItem {
			padding: 35rpx 40rpx;
			border-bottom: 1px solid $page-color-light;
			.code {
				color: #5dbc7c;
				font-size: 23rpx;
				border-left: 1px solid #eeeeee;
				padding-left: 20rpx;
				flex-shrink: 0;
				text-align: center;
			}
		}

		.listIconImg {
			width: 36rpx;
		}

		.right {
			color: $font-color-light;
			font-size: $font-base;
			flex-grow: 1;

			.img {
				width: 26rpx;
			}
		}

		.titleBox {
			.title {
				color: $font-color-base;
				font-size: $font-base;
			}
		}
	}

	.bg-gray {
		background-color: $color-gray;
	}
</style>
