<template>
	<view class="container">
		<view class="bg">
			<image src="../../static/user/setphone.png" mode=""></image>
		</view>
		<view class="main">
			<view class="row b-b">
				<image class="tit" src="../../static/user/phone.png" mode="heightFix"></image>
				<input class="input" v-model="account" type="text" placeholder="请填写手机号"
					placeholder-class="placeholder" />
			</view>
			<view class="row b-b">
				<image class="tit" src="../../static/user/code.png" mode="heightFix"></image>
				<input class="input" v-model="captcha" type="text" placeholder="请填写验证码"
					placeholder-class="placeholder" />
				<view class="code" @click="verification">{{ countDown == 0 ? '发送验证码' : countDown }}</view>
			</view>
			<button class="add-btn" :class="{ 'bg-gray': loding }" @click="loding ? '' : confirm()">确认绑定</button>
		</view>
	</view>
</template>

<script>
	import {
		verify
	} from '@/api/login.js';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		registerReset,
		binding
	} from '@/api/set.js';
	import {
		getUserInfo
	} from '@/api/login.js';
	export default {
		data() {
			return {
				time: '', //保存倒计时对象
				countDown: 0, //倒计时
				account: '', //手机号
				captcha: '', //验证码
				password: '', //新密码
				loding: false //是否载入中
			};
		},
		watch: {
			// 监听倒计时
			countDown(i) {
				if (i == 0) {
					clearInterval(this.time);
				}
			}
		},
		computed: {
			...mapState(['userInfo'])
		},
		onLoad() {
			if (this.userInfo.phone == null) {
				this.account = '';
			} else {
				this.account = this.userInfo.phone;
				this.show = false;
			}
		},
		methods: {
			...mapMutations('user', ['setUserInfo', 'setOrderInfo']),
			//发送验证码
			verification() {
				let obj = this;
				if (this.account == '') {
					this.$api.msg('请输入电话号码');
					return;
				}
				if (!/(^1[2|3|4|5|7|8|9][0-9]{9}$)/.test(this.account)) {
					this.$api.msg('请输入正确的手机号');
					return;
				}
				// 判断是否在倒计时
				if (obj.countDown > 0) {
					return false;
				} else {
					obj.countDown = 60;
					obj.time = setInterval(() => {
						obj.countDown--;
					}, 1000);
					//调用验证码接口
					verify({
							phone: obj.account,
							type: 'BDING_CODE'
						})
						.then(({
							data
						}) => {})
						.catch(err => {
							console.log(err);
						});
				}
			},
			confirm(e) {
				let obj = this;
				obj.loding = true;
				binding({
						phone: obj.account,
						captcha: obj.captcha
					})
					.then(({
						data
					}) => {
						obj.$api.msg('绑定成功！');
						setTimeout(function() {
							obj.loding = false;
							uni.switchTab({
								url: '/pages/index/index'
							});
						}, 1000);
					})
					.catch(err => {
						obj.loding = false;
						console.log(err);
					});
			}
		}
	};
</script>

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

	.bg {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		height: 920rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.main {
		position: relative;
		z-index: 2;
		padding-top: 490rpx;
	}

	.row {
		display: flex;
		align-items: center;
		padding: 0 140rpx;
		height: 110rpx;

		.tit {
			height: 40rpx;
		}

		.input {
			margin-left: 40rpx;
			flex: 1;
			font-size: 30rpx;
			color: $font-color-dark;
		}

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

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 90rpx auto 0;
		width: 600rpx;
		height: 90rpx;
		background: #52c696;
		border-radius: 10rpx;
		font-size: 34rpx;
		font-family: SourceHanSansCN;
		font-weight: 400;
		color: #FFFFFF;
	}

	.bg-gray {
		background-color: $color-gray;
	}

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