<template>
	<view class="page">
		<view class="fixed">
			<u-navbar title="认证" :border-bottom="false" :background="bgc" title-color='#fff' title-size='36'
				height='45'  back-icon-color='#fff'></u-navbar>
		</view>
		
		<view class="backimg">
		
		</view>
		<view class="iptsbox">
			<view class="top_tip">
				请输入您的有效身份信息
			
			</view>
			<view class="li">
				<view class="left">
					姓名
				</view>
				<input type="text" v-model="name" placeholder="请填写姓名" placeholder-style='' />
			</view>
			<view class="li">
				<view class="left">
					身份证号
				</view>
				<input type="text" v-model="idnum" placeholder="请填写身份证号" placeholder-style='' />
			</view>
		</view>
		<view class="btn_box">
			<view class="btn"  @click="test">
				完成
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: " ",
				},
				name: '',
				idnum: null,
				istrue:false
			}
		},
		watch: {
		    idnum(newVal) {
		        // 正则表达式用于校验 15 位或 18 位身份证号码
		          const regex = /^\d{17}(\d|X|x)$/;
		                this.istrue = regex.test(newVal);
			
		    }
		},
		computed: {
			userId() {
				return this.$store.getters.userId;
			},
		},
		onLoad() {
			this.getinfo()
		},
		methods: {
			getinfo() {
				uni.showLoading({
					title:'加载中'
				})
				this.$u.get("/getInfo").then((res) => {
				
					if (res.code == 200) {
						uni.hideLoading()
						this.$store.commit('SET_USERID', res.user.userId);
						this.userinfo = res.user
						
					} else {
						setTimeout(()=>{
							this.getinfo()
							
						},2000)
					}
				});
			},
			validateName(name) {
				const nameRegex = /^[\u4e00-\u9fa5·]{2,20}$/;
				return nameRegex.test(name);
			},
			validateIdnum(idnum) {
				const idnumRegex =
					/^(^[1-9]\d{7}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$)$/;
				return idnumRegex.test(idnum);
			},
			test() {
				if (!this.validateName(this.name)) {
					uni.showToast({
						title: '请输入有效的中文姓名',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (!this.validateIdnum(this.idnum)) {
					uni.showToast({
						title: '请输入有效的身份证号',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				let data = {
					realName: this.name,
					idCard: this.idnum,
					userId: this.userId,
				};
				this.$u.get('/appVerify/user/authentication?', data).then((res) => {
					if (res.code === 200) {
						if(res.data.isok){
							uni.redirectTo({
								url:"/pages/index/index"
							})
						}else{
							uni.showToast({
								title: '身份信息与姓名不符',
								icon: 'none',
								duration: 2000
							});
						}
					
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.page {
		width: 750rpx;
		.btn_box{
			position: fixed;
			left: 0;
			bottom: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 750rpx;
			height: 174rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 8rpx 20rpx 0rpx rgba(0,0,0,0.3);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			.btn{
				display: flex;
				align-items: center;
				justify-content: center;
				width: 682rpx;
				height: 84rpx;
				background: #4297F3;
				border-radius: 42rpx 42rpx 42rpx 42rpx;
				font-weight: 600;
				font-size: 36rpx;
				color: #FFFFFF;
			}
		}
		.iptsbox{
			margin: 0 auto;
			margin-top: -234rpx;
			width: 676rpx;
			padding-bottom: 54rpx;
			// height: 574rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			.top_tip{
				display: flex;
				align-items: center;
				padding-left: 38rpx;
				width: 676rpx;
				height: 118rpx;
				background: #EFEFEF;
				border-radius: 20rpx 20rpx 0rpx 0rpx;
				font-weight: 600;
				font-size: 36rpx;
				color: #3D3D3D;
			}
			.li{
			
				padding-left: 40rpx;
				margin: 0 auto;
					margin-top: 46rpx;
				display: flex;
				align-items: center;
				width: 624rpx;
				height: 92rpx;
				background: #EFEFEF;
				border-radius: 46rpx 46rpx 46rpx 46rpx;
				.left{
					width: 130rpx;
					font-weight: 400;
					font-size: 32rpx;
					color: #3D3D3D;
				}
				input {
					margin-left: 44rpx;
					font-weight: 400;
					font-size: 24rpx;
					color: #3D3D3D;
				
					.input-placeholder {
						font-weight: 400;
						font-size: 24rpx;
						color: #C7C7C7;
					}
				}
			}
		}
		.tit{
			margin: 20rpx auto;
			width: 608rpx;
			height: 76rpx;
			font-family: AlibabaPuHuiTi, AlibabaPuHuiTi;
			font-weight: 600;
			font-size: 28rpx;
			color: #3D3D3D;
		}
		
		.backimg {
			// position: fixed;
			width: 750rpx;
			height: 696rpx;
		
			z-index: -10;
			background-image: url('https://lxnapi.ccttiot.com/bike/img/static/uNA5HihiOsxz8vpYxbda');
			background-size: cover;
			/* 背景图片等比缩放以覆盖整个容器 */
			background-position: center;
			
		}
		.fixed {
			z-index: 999;
			position: fixed;
			top: 0;
		}
		.tips {
			text-align: center;
			margin: 10rpx auto;
			width: 610rpx;
			font-weight: 500;
			font-size: 28rpx;
			color: #3D3D3D;
		}
		.imgbox{
			width: 100%;
			display: flex;
			align-items: center;justify-content: center;
			.img{
				margin: 10rpx auto;
				width: 428rpx;
				height: 292.32rpx;
			}
		}
	
		.btn {
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 10rpx auto;
			width: 610rpx;
			height: 90rpx;
			background: #979797;
			border-radius: 16rpx;
			font-weight: 500;
			font-size: 40rpx;
			color: #FFFFFF;
		}
		.act1{
			background: #4C97E7;
		}
		
		.ipt_box {
		
			margin: 0 auto;
			margin-top: -234rpx;
			width: 610rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08);
			border-radius: 12rpx;
			background: #FFFFFF;
			.line {
				margin-top: 34rpx;
				margin-bottom: 34rpx;
				width: 586rpx;
				height: 2rpx;
				background: #F3F3F3;
			}

			.li {
				display: flex;
				align-items: center;

				.left {
					width: 96rpx;
					font-weight: 400;
					font-size: 24rpx;
					color: #979797;
				}

				input {
					margin-left: 44rpx;
					font-weight: 400;
					font-size: 24rpx;
					color: #3D3D3D;

					.input-placeholder {
						font-weight: 400;
						font-size: 24rpx;
						color: #C7C7C7;
					}
				}
			}
		}
	}
</style>