<template>
	<view class="container">
		<view class="row ">
			<text class="tit">{{$t('set.a3')}}</text>
			<input class="input" disabled v-model="account" type="text" :placeholder="$t('reg.a3')"
				placeholder-class="placeholder" />
		</view>
		<view class="row ">
			<text class="tit">{{$t('reg.a8')}}</text>
			<input class="input" v-model="password" type="password" :placeholder="$t('reg.a9')"
				placeholder-class="placeholder" />
		</view>
		<view class="row ">
			<text class="tit">{{$t('reg.b0')}}</text>
			<input class="input" v-model="repeat" type="password" :placeholder="$t('reg.b1')"
				placeholder-class="placeholder" />
		</view>
		<view class="row">
			<text class="tit">{{$t('reg.a6')}}</text>
			<input class="input" v-model="captcha" type="text" :placeholder="$t('reg.a7')" placeholder-class="placeholder" />
			<view class="code" @click="verification">{{ countDown == 0 ? $t('reg.c6') : countDown }}</view>
		</view>
		<button class="add-btn" :class="{'bg-gray':loding}"
			@click="loding?'':confirm()">{{$t('common.submit')}}</button>
	</view>
</template>

<script>
	import {
		verify
	} from '@/api/login.js';
	import {
		mapState
	} from 'vuex';
	import {
		Reset
	} from '@/api/set.js';
	// import detectEthereumProvider from '@metamask/detect-provider'
	export default {
		data() {
			return {
				time: '', //保存倒计时对象
				countDown: 0, //倒计时
				account: '', //手机号
				captcha: '', //验证码
				password: '', //密码
				repeat: '', //确认密码
				loding: false, //是否载入中
			};
		},
		computed: {
			...mapState("user", ['userInfo'])
		},
		onLoad() {
			if (this.userInfo.phone == null) {
				this.account = '';
			} else {
				this.account = this.userInfo.phone;
				this.show = false;
			}
			console.log(this.userInfo,'this.userInfo');
			uni.setNavigationBarTitle({
				title: this.$t("tab.b3"),
			});
		},
		watch: {
			// 监听倒计时
			countDown(i) {
				if (i == 0) {
					clearInterval(this.time);
				}
			}
		},
		methods: {
			//发送验证码
			verification() {
				let obj = this;
				if (this.account == '') {
					this.$api.msg(obj.$t("safe.b5"));
					return;
				}
				// if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(this.account)) {
				// 	this.$api.msg(obj.$t("safe.b8"));
				// 	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);
						});
				}
			},

			async confirm(e) {
				const obj = this;
				this.loding = true;
				Reset({
						// account: this.account,
						captcha: this.captcha,
						password: this.password,
					})
					.then(({
						data
					}) => {
						this.loding = false;
						this.$api.msg("修改成功");
					})
					.catch(err => {
						this.loding = false;
						console.log(err);
					});
			}
		}
	};
</script>

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

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 20rpx;
		background: #1F2A4A;
		margin: 0 30rpx 30rpx 30rpx;
		border-radius: 10rpx;
		color: #FFF;
		
		.tit {
			min-width: 130rpx;
			flex-shrink: 0;
			font-size: $font-base;
		}

		.input {
			flex: 1;
			font-size: $font-base;
			color: $font-color-light;
			padding-left: 20rpx;
		}

		.iconlocation {
			font-size: 36rpx;
			color: $font-color-light;
		}
	}

	.add-btn {
		background: linear-gradient(90deg, #0C5AFA, #1356FF);
		margin: 30rpx;
		color: #FFF;
		font-size: 30rpx;
		text-align: center;
		border-radius: 10rpx;
	}
	.bg-gray {
		background-color: $color-gray;
	}

	.code {
		color: #000s;
		font-size: 23rpx;
		border-left: 1px solid #eeeeee;
		width: 150rpx;
		flex-shrink: 0;
		text-align: center;
	}
</style>