<template>
	<view class="container">
		<view class="body">
			<!-- 输入框区域 -->
			<view class="body_row mt-40">
				<view class="row_text">姓名</view>
				<input class="row_input" type="text" placeholder="请输入姓名" placeholder-style="color:#CCCCCC" v-model="name" />
			</view>

			<view class="body_row mt-30">
				<view class="row_text">编号</view>
				<input class="row_input" type="number" placeholder="请输入编号" placeholder-style="color:#CCCCCC" v-model="number" />
			</view>

			<view class="body_row mt-30">
				<view class="row_text">身份证</view>
				<input class="row_input" type="number" placeholder="请输入身份证" placeholder-style="color:#CCCCCC" v-model="identity" />
			</view>

			<!-- 确定按钮区域 -->
			<view class="body_btn" @click="handleConfirm">确认</view>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { myRequest } from '@/utils/api.js'

// 姓名
const name = ref('')
// 编号
const number = ref('')
// 身份证号码
const identity = ref('')

onLoad(() => {})

// 点击确认按钮回调
const handleConfirm = () => {
	const flag = verifyValue()
	if (flag) {
		bindReq()
	}
}

// 绑定请求
const bindReq = async () => {
	const res = await myRequest({
		url: '/wanzai/api/smartUser/bindStudent',
		method: 'post',
		data: {
			userId: uni.getStorageSync('userInfo').id,
			name: name.value,
			cardNo: number.value,
			idCard: identity.value
		}
	})
	// console.log(res)
	uni.showToast({
		title: res.message,
		icon: 'none',
		duration: 2000
	})
	if (res.code == 200) {
		setTimeout(() => {
			uni.redirectTo({
				url: '/pages/home/home'
			})
		}, 2000)
	}
}

// 验证表格数据是否符合规范
const verifyValue = () => {
	// 姓名验证规则
	const regName = /^[\u4e00-\u9fa5]{2,4}$/
	// 身份证号码验证规则
	const regIdentity = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/

	if (!name.value) {
		uni.showToast({
			title: '请输入姓名',
			icon: 'none'
		})
		return false
	}
	if (!regName.test(name.value)) {
		uni.showToast({
			title: '姓名格式错误',
			icon: 'none'
		})
		return false
	}
	if (!number.value) {
		uni.showToast({
			title: '请输入编号',
			icon: 'none'
		})
		return false
	}
	if (!identity.value) {
		uni.showToast({
			title: '请输入身份证',
			icon: 'none'
		})
		return false
	}
	if (!regIdentity.test(identity.value)) {
		uni.showToast({
			title: '证件号格式错误',
			icon: 'none'
		})
		return false
	}
	return true
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	min-height: 100vh;
	background-color: #f1f6fe;

	.body {
		position: relative;
		padding: 0 20rpx;
		margin-top: 20rpx;
		height: calc(100vh - 20rpx);
		background-color: #fff;

		.body_row {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 90rpx;
			font-size: 28rpx;

			.row_text {
				flex: 1;
				padding-right: 30rpx;
				text-align-last: justify;
			}

			.row_input {
				box-sizing: border-box;
				padding: 0 30rpx;
				width: 590rpx;
				height: 90rpx;
				border-radius: 15rpx;
				background-color: #f2f2f2;
			}
		}

		.mt-40 {
			margin-top: 40rpx;
		}

		.mt-30 {
			margin-top: 30rpx;
		}

		.body_btn {
			position: absolute;
			bottom: 150rpx;
			left: 227rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 297rpx;
			height: 100rpx;
			color: #fff;
			font-size: 32rpx;
			border-radius: 8rpx;
			background-color: #1e7dfb;
		}
	}
}
</style>
