<template>
	<view class="page">
		<u-navbar title="认证" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			height='45'></u-navbar>
			<view class="tit">
				为了防范身份信息被冒用,需进行身份认证以确保 是本人操作
			</view>
			<view class="imgbox">
				<image class="img" src="https://lxnapi.ccttiot.com/bike/img/static/uxAqR8zizZddzadit9ms" mode=""></image>
			</view>
			
		<view class="ipt_box">
			<view class="li">
				<view class="left">
					姓名
				</view>
				<input type="text" v-model="name" placeholder="填写本人真实姓名" placeholder-style='' />
			</view>
			<view class="line"></view>
			<view class="li">
				<view class="left">
					身份证号
				</view>
				<input type="text" v-model="idnum" placeholder="填写本人身份证号" />
			</view>
		</view>
		<view class="tips">
			以上信息仅用于验证,我们将严格为您保密
		</view>
		<view class="btn" :class="istrue?'act1':''" @click="test">
			认证
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#fff",
				},
				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("/getAppInfo").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.navigateBack({
								delta: 1 // delta值为1时表示返回的页面层数
							});
						}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;
		.tit{
			margin: 20rpx auto;
			width: 608rpx;
			height: 76rpx;
			font-family: AlibabaPuHuiTi, AlibabaPuHuiTi;
			font-weight: 600;
			font-size: 28rpx;
			color: #3D3D3D;
		}
		.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 {
			padding: 30rpx 12rpx;
			margin: 48rpx auto;
			width: 610rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08);
			border-radius: 12rpx;

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