<template>
	<view>
		<view style="width:100%;height:516rpx;position: relative;">
			<view class="navImage">
				<image src="../../static/login_bgground@2x.png" style="width:100%;height:516rpx;"></image>
			</view>
			<!-- <view style="color:#fff;" class="nvaTitle">访客密码</view> -->
		</view>
		<view>
			<view class="info">
				<view class="topType"></view>
				<view class="info-title">注册</view>
				<view class="item">
					<view class="left-icon">
						<image src="../../static/register_icon_name@2x.png" style="width: 100%;height: 100%;"></image>
					</view>
					<input class="uni-input item-input" v-model="form.name"  placeholder="请输入真实姓名" />
				</view>
				<view class="item">
					<view class="left-icon">
						<image src="../../static/register_icon_identity@2x.png" style="width: 100%;height: 100%;"></image>
					</view>
					<input class="uni-input item-input" v-model="form.idNumber"  placeholder="请输入身份证号" />
				</view>
				<view class="item">
					<view class="left-icon">
						<image src="../../static/login_icon_phone@2x.png" style="width: 100%;height: 100%;"></image>
					</view>
					<input class="uni-input item-input" v-model="form.phone"  placeholder="请输入手机号" type="number" @input="changePhone" />
				</view>
				<view class="item">
					<view class="left-icon">
						<image src="../../static/login_icon_code@2x.png" style="width: 100%;height: 100%;"></image>
					</view>
					<input class="uni-input item-input" style="width:190rpx;" v-model="form.code" placeholder="请输入验证码" type="number" />
					<view style="margin-left: 150rpx;font-size: 24rpx;text-align: center;line-height: 40rpx;" v-show="isGet" @tap="getCode"
					 class="getCode" :class="{codeActive:phoneLength==11}">获取验证码</view>
					<view v-show="isShow" @tap="againGetCode" style="width: 160rpx; margin-left: 116rpx;font-size: 24rpx;text-align: center;line-height: 45rpx;color:rgba(41,138,253,1);"><text
						 @tap="againGetCode" class="cx" :class="{cxActive:num==0}">重新获取</text><text v-show="num>0">({{num}}s)</text>
					</view>
				</view>
				<!--注册按钮 -->
				<view class="info-btn" @tap="register" :class="{active:form.name && form.idNumber && form.phone && form.code}">
					注册
				</view>
				<!-- 现在登录 -->
				<view class="loginNow" @tap="loginNow()">
					现在登录
				</view>
			</view>
		</view>
		<!-- 注册成功弹窗 -->
		<uni-popup ref="popup" type="center">
			<view style="width: 600rpx;height: 522rpx;background-color: #fff;border-radius: 10rpx;" class="popupBox">
				<image src="../../static/success_popup@2x.png" style="width:100%;height: 100%;margin-top: -70rpx;"></image>
				<!-- <view class="popupImage">
					
				</view> -->
				<view class="popupTitle">恭喜您，注册成功！</view>
				<button class="popupBtn" @tap="backLogin">返回登录</button>
				<view class="popupClose" @tap="popupClose">
					<image src="../../static/icon_colse@2x.png" style="width: 100%;height: 100%;"></image>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from "../../components/uni-popup/uni-popup.vue"
	export default {
		components: {
			uniPopup
		},
		data() {
			return {
				phoneLength: 0, //手机号码的长度
				isPopup: false, //判断弹窗是否显示
				isShow: false, //判断是否显示重新获取
				isGet: true, //判断是否显示获取验证码
				// 倒计时数字
				num: 59,
				// 倒计时
				timer: null,
				//表单绑定值
				form: {
					name: '', //姓名
					idNumber: '', //身份证号
					phone: '', //手机号
					code: '', //验证码
				}
			}
		},
		methods: {
			// 监听输入手机号码的长度
			changePhone() {
				this.phoneLength = this.form.phone.length
			},
			// 注册成功弹窗关闭
			popupClose() {
				this.$refs.popup.close()
			},
			// 返回登录
			backLogin() {
				uni.navigateTo({
					url: "../login/login"
				})
			},
			// 现在登录
			loginNow() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
		
			// 获取验证码
			getCode() {
				if (this.phoneLength != 11) {
					return
				} else {
					let phoneReg = /^1[3|4|5|7|8][0-9]{9}$/; //手机号正则校验
					if (!phoneReg.test(this.form.phone)) {
						uni.showToast({
							icon: 'none',
							title: '请输入正确的手机号码格式',
							duration: 2000
						});
					} else {
						this.isShow = true
						this.isGet = false
						const TIME_COUNT = 60;
						// 判断当前timer是否有值
						if (!this.timer) {
							this.num = TIME_COUNT;
							this.show = false;
							this.timer = setInterval(() => {
								// 判断当前num值大于0并且小于等于60执行
								if (this.num > 0 && this.num <= TIME_COUNT) {
									this.num--;
								} else {
									//否则就清除定时
									clearInterval(this.timer);
									this.timer = null;
								}
							}, 1000)
						}
						this.http.httpRequest('/wxapplet/ownersid/shortmessaging', 'get', {
							phoneNumBer: this.form.phone
						}, true).then((res) => {

						})
					}

				}

			},
			//重新获取验证码
			againGetCode() {
				this.getCode()
			},
			// 注册
			register() {
				// 判断输入框是否都有值  是可以点击注册 否不可以点击
				if (!this.form.name || !this.form.idNumber || !this.form.code || !this.form.phone) {
					return
				} else {
					let idNumerReg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; //身份证正则
					let nameReg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/
					if (!nameReg.test(this.form.name)) {
						uni.showToast({
							icon: 'none',
							title: '请输入正确的姓名格式',
							duration: 2000
						});
					} 
					else if (!idNumerReg.test(this.form.idNumber)) {
						console.log(this.form.idNumber)
						uni.showToast({
							icon: 'none',
							title: '身份证号码格式不正确',
							duration: 2000
						});
					} else {
						uni.showLoading({
							mask: true,
							title: '加载中'
						})
						this.http.httpRequest('/wxapplet/ownersid/add', 'post', {
							loginName: this.form.name,
							idcardNum: this.form.idNumber,
							phoneNumBer: this.form.phone,
							authcode: this.form.code
						}, true).then((res) => {
							// 判断接口是否请求成功 0 成功 1 步成功
							if (res.code == 0) {
								uni.hideLoading()
								this.$refs.popup.open()
							} else {
								uni.hideLoading()
								uni.showToast({
									title: res.msg,
									icon: 'none'
								})
								// this.form.phone=''
								// this.form.code=''
								this.isShow = false
								this.isGet = true
								this.num = 0
							}

						}).catch(() => {
							uni.hideLoading()
						})

					}

				}

			}
		},

	}
</script>

<style>
	.getCode {
		color: rgba(163, 197, 237, 1);
	}

	.codeActive {
		color: rgba(41, 138, 253, 1);
	}

	.cx {
		color: rgba(163, 197, 237, 1);
	}

	.cxActive {
		color: rgba(41, 138, 253, 1);
	}

	.popupClose {
		width: 44rpx;
		height: 44rpx;
		position: absolute;
		right: 20rpx;
		top: 20rpx;

	}

	.popupBtn {
		width: 342rpx;
		height: 80rpx;
		position: absolute;
		bottom: 80rpx;
		left: 128rpx;
		background: rgba(41, 138, 253, 1);
		color: #FFFFFF;
		font-size: 32rpx;
	}

	.popupTitle {
		width: 260rpx;
		height: 40rpx;
		font-size: 28rpx;
		color: #999999;
		position: absolute;
		bottom: 232rpx;
		left: 170rpx;
	}

	.popupImage {
		width: 100%;
		height: 300rpx;
		/* 	background-image: url(../../static/success_popup@2x.png);
     position: absolute;
	 top: -64rpx; */

	}

	.loginNow {
		width: 116rpx;
		height: 40rpx;
		font-size: 28rpx;
		color: #333333;
		margin: 0 auto;
		margin-top: 48rpx;
	}

	.info-btn {
		width: 342rpx;
		height: 88rpx;
		background: rgba(163, 197, 237, 1);
		opacity: 1;
		border-radius: 14rpx;
		font-size: 32rpx;
		text-align: center;
		line-height: 88rpx;
		color: rgba(255, 255, 255, 1);
		margin: 0 auto;
		margin-top: 44rpx;
	}

	.item-input {
		width: 100%;
		margin-left: 20rpx;
		height: 40rpx;
		/* margin-top: 5rpx; */
		font-size: 32rpx;
		color: #999999;
		color: #333333;
		opacity: 1;
	}

	.active {
		background: rgba(41, 138, 253, 1);
	}

	.left-icon {
		width: 40rpx;
		height: 40rpx;
		/* border: 1rpx solid #555555; */
		/* box-sizing: border-box; */
	}

	.item {
		display: flex;
		width: 531rpx;
		height: 59rpx;
		margin: 0 auto;
		border-bottom: 2px solid rgba(247, 247, 247, 1);
		opacity: 1;
		margin-top: 50rpx;
	}

	.info-box {
		width: 90%;
		margin: 0 auto;
		border: 1px solid #808080;
	}

	.info-title {
		width: 100%;
		height: 48rpx;
		font-size: 34rpx;
		text-align: center;
		line-height: 48rpx;
		font-family: PingFang SC;
		font-weight: bold;
		position: absolute;
		top: 30rpx;
	}

	.topType {
		width: 150rpx;
		height: 150rpx;
		border-radius: 50%;
		background: #FFFFFF;
		margin: 0 auto;
		margin-top: -50rpx;
		border: 1rpx solide #C0C0C0;
	}

	.navImage {}

	.info {
		width: 602rpx;
		height: 624rpx;
		background: rgba(255, 255, 255, 1);
		box-shadow: 0px 6px 30px rgba(41, 138, 253, 0.15);
		opacity: 1;
		position: absolute;
		top: 484rpx;
		left: 74rpx;
		border-radius: 20rpx;
	}

	.btn {
		width: 702rpx;
		height: 90rpx;
		background: rgba(41, 138, 253, 1);
		opacity: 1;
		border-radius: 18rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		color: rgba(255, 255, 255, 1);
		text-align: center;
		line-height: 90rpx;
		position: absolute;
		top: 990rpx;
		left: 27rpx;
	}

	.nvaTitle {
		position: absolute;
		text-align: center;
		max-width: 400rpx;
		overflow: hidden;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		font-size: 32rpx;
		margin: auto;
	}
</style>
