<template>
	<view class="page">
		<u-navbar title="认证" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			height='40'></u-navbar>
			<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="number" v-model="idnum" placeholder="填写本人身份证号"/>
				</view>
			</view>
			<view class="tips">
				以上信息仅用于验证，我们将严格为您保密
			</view>
			<view class="btn" @click="test">
				认证
			</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#fff",
				},
				name: '',
				idnum: ''
			}
		},
		computed: {
			userId() {
				return this.$store.getters.userId;
			},
		},
		methods: {
			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) {
						uni.navigateBack({
						    delta: 1  // delta值为1时表示返回的页面层数
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.page {
		width: 750rpx;
		.tips {
			text-align: center;
			margin: 10rpx auto;
			width: 610rpx;
			font-weight: 500;
			font-size: 28rpx;
			color: #3D3D3D;
		}
		.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;
		}
		.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>
