<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" v-model="account" type="text" placeholder="请填写新手机号"
							placeholder-class="placeholder" />
					</view>
				</view> -->
			</view>
		</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,
		binding
	} from '@/api/set.js';
	export default {
		data() {
			return {
				time: '', //保存倒计时对象
				countDown: 0, //倒计时
				account: '', //手机号
				captcha: '', //验证码
				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: {
			//发送验证码
			verification() {
				let obj = this;
				if (this.account == '') {
					this.$api.msg('请输入电话号码');
					return;
				}
				if (this.account.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.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/user/user'
							});
						}, 1000);
					})
					.catch(err => {
						obj.loding = false;
						console.log(err);
					});
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: $page-color-base;
	}
	.container{
		padding-top: 30rpx;
	}
	.listBox {
		margin: $page-row-spacing;
		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;
		}

		.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;
	}

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