<template>
	<view class="bg">
		<view class="input-box">
			<span>手机号:</span>
			<input type="number" v-model="mobile" placeholder="请输入新手机号" maxlength="11" />
		</view>
		<view class="input-box">
			<span>验证码:</span>
			<input type="number" v-model="code" placeholder="请输入短信验证码" maxlength="6" />
			<view class="btn" @click="getCode">{{text}}</view>
		</view>
		<view class="btn bottom-btn" @click="save">保存</view>
	</view>
</template>

<script>
	export default {
		name: "BindTel",
		data: function() {
			return {
				mobile: '',
				code: '',
				sendFlag: true,
				timer: null,
				text: "获取验证码"
			}
		},
		mounted: function() {
			// this.wxShare();
		},
		methods: {
			save: function() {
				if (!this.mobile || !this.IsTel(this.mobile)) {
					uni.showToast({
						title: "请输入正确的手机号码",
						icon: 'none'
					})
					return;
				}
				if (!this.code) {
					uni.showToast({
						title: "请输入验证码",
						icon: 'none'
					})
					return;
				}
				this.Post({
					code: this.code,
					mobile: this.mobile
				}, '/api/user/bindMobileCode').then(res => {
					if (res.code == 1) {
						uni.showModal({
							title: '提示',
							content: '保存成功！',
							confirmColor: '#000000',
							success: res => {
								if (res.confirm) {
									this.goBack()
								}
							}
						})
					}
				}).catch(err => {
					console.log('err:', err)
				})
			},
			getCode: function() {
				if (!this.sendFlag) return;
				if (!this.mobile || !this.IsTel(this.mobile)) {
					uni.showToast({
						title: '请输入正确的手机号码',
						icon: 'none'
					})
					return;
				}
				this.sendFlag = false;
				this.Post({
					mobile: this.mobile
				}, '/api/user/bindMobileSendMsm').then(res => {
					if (res.code == 1) {
						//  发送成功
						this.text = "发送成功";
						let time = 60;
						//  需要倒计时
						this.timer = setInterval(() => {
							time--;
							this.text = time + "s后重新获取";
							if (time == 0) {
								clearInterval(this.timer);
								this.timer = null;
								this.text = "重新发送";
								this.sendFlag = true;
							}
						}, 1000);
					} else {
						this.text = "重新发送";
						this.sendFlag = true;
					}
				})
			}
		}
	}
</script>

<style scoped>
	.bg {
		min-height: 100vh;
		text-align: center;
	}

	.input-box {
		font-size: 32rpx;
		margin: 0 30rpx;
		height: 124rpx;
		display: flex;
		align-items: center;
		border-bottom: 1rpx solid #CECECE;
	}

	.input-box span {
		margin-right: 20rpx;
		flex-shrink: 0;
		white-space: normal;
	}

	.input-box input {
		flex: 1;
		display: block;
		width: 100%;
		font-size: 32rpx;
		margin-right: 10rpx;
		min-width: 100rpx;
		text-align: left;
	}

	.btn {
		color: black;
		width: 200rpx;
		height: 53rpx;
		line-height: 53rpx;
		background: rgba(127, 212, 145, 1);
		border-radius: 27rpx;
		color: #FFFFFF;
	}

	.input-box .btn {
		padding: 0 10rpx;
		font-size: 26rpx;
		flex-shrink: 0;
	}

	.bottom-btn {
		color: black;
		margin: 50rpx auto;
		line-height: 80rpx;
		position: relative;
		font-size: 34rpx;
		text-align: center;
		width: 333rpx;
		height: 80rpx;
		background: #248BAA;
		border-radius: 40rpx;
		color: #FFFFFF;
	}
</style>