<template>
	<view class="phone">
		<view class="titlle">提示：</view>
		<view class="tips">请输入登录APP的手机号进行认证</view>
		<view class="phone-bd">
			<view class="item">
				<view class="txt">手机号</view>
				<input v-model="phone" placeholder="请输入手机号" type="number" />
			</view>
			<view class="item">
				<view class="txt">验证码</view>
				<input v-model="code" placeholder="请输入短信验证码" type="number" />
				<view class="code" @click="sendCode()">{{count == 60?'获取验证码': count+'s'}}</view>
			</view>
		</view>
		<view class="phone-fd">
			<view class="btn" @click="toReg()">确定</view>
		</view>
	</view>
</template>

<script>
	let timer = null;
	export default {
		data() {
			return {
				phone: "",
				code: "",
				count: 60,
				isClikc: true
			};
		},
		methods: {
			//发送验证码
			async sendCode() {
				const that = this;
				if(!that.phone){
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "手机号不能为空！",
					})
					return
				}
				if(!/^1[3456789]\d{9}$/.test(that.phone)){
				  uni.showToast({
				  	title: '手机号格式不能为空!',
					position: "bottom",
				  	icon: "none",
				  })
				  return ;
				}
				if(that.isClick == false){
					return ;
				}
				const { code, data, msg } = await that.$api.sendSmsCode({
					phone: that.phone
				});
				if(code == 200){
					that.isClick = false;
					timer = setInterval(()=>{
						if(that.count == 0){
							clearInterval(timer);
							that.count = 60;
							that.isClick = true;
						}else{
							that.count -- 
						}
					},1000)
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "发送成功"
					})
				}else{
					that.isClick = true;
				}
			},
			//去登录
			async toReg() {
				const that = this;
				if(!that.phone){
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "手机号不能为空！",
					})
					return
				}
				if(!/^1[3456789]\d{9}$/.test(that.phone)){
				  uni.showToast({
				  	title: '手机号格式不能为空!',
					position: "bottom",
				  	icon: "none",
				  })
				  return ;
				}
				if(!that.code){
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "验证码不能为空！",
					})
					return
				}
				const {code, data, msg} = await this.$api.phoneAuth({
					userId: uni.getStorageSync("userInfo").id,
					code: this.code,
					phone: this.phone
				})
				if(code == 200){
					uni.showToast({
						title: "提交成功"
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta: 1
						})
					},2000)
				}else{
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: msg
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
.phone{
	padding: 25rpx;
	overflow: hidden;
	.title{
		color: #222222;
		font-weight: 500;
		font-size: 30rpx;
	}
	.tips{
		font-weight: 500;
		font-size: 24rpx;
		color: #999999;
		margin-top: 20rpx;
	}
	&-fd{
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 50rpx;
		z-index: 22;
		.btn{
			width: 700rpx;
			line-height: 98rpx;
			background: linear-gradient(0deg, #000000, #3D3B38);
			border-radius: 98rpx;
			font-weight: 400;
			font-size: 34rpx;
			text-align: center;
			color: #FFFFFF;
			margin: 0 auto;
		}
	}
	&-bd{
		width: 100%;
		overflow: hidden;
		background: #FFFFFF;
		padding: 0 30rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		margin-top: 30rpx;
		.item{
			display: flex;
			align-items: center;
			border-top: 1px solid #EAEAEA;
			padding: 50rpx 0 30rpx;
			&:first-child{
				border-top-color: #FFFFFF;
			}
			input{
				flex: 1;
				font-weight: 400;
				font-size: 30rpx;
				color: #333;
				margin-left: 30rpx;
			}
			.code{
				width: 170rpx;
				line-height: 60rpx;
				background: #000000;
				border-radius: 60rpx;
				text-align: center;
				font-weight: 500;
				font-size: 26rpx;
				color: #FFFFFF;
			}
		}
	}
}
</style>