<template>
	<view class="container">
		<view class="body" v-if="form.name">
			<view class="box">
				<view class="box_key">学生姓名：</view>
				<view class="box_value">
					<input class="input" type="text" placeholder="请输入学生姓名" placeholder-style="color:#CCCCCC;font-size:28rpx" v-model="form.name" />
				</view>
			</view>

			<view class="box">
				<view class="box_key">手机号：</view>
				<view class="box_value">
					<input class="input" type="text" placeholder="请输入手机号" placeholder-style="color:#CCCCCC;font-size:28rpx" v-model="form.phone" />
				</view>
			</view>

			<!-- <view class="box">
				<view class="box_key">陪同人数：</view>
				<view class="box_value">
					<input class="input" type="text" placeholder="请输入陪同人数" placeholder-style="color:#CCCCCC;font-size:28rpx" v-model="form.peerNum" />
				</view>
			</view> -->

			<view class="box">
				<view class="box_key">校区：</view>
				<view class="box_value">
					<picker @change="bindPickerChange" :value="currtIndex" :range="schoolList" range-key="school">
						<view class="select" :class="{ active: currtIndex != null }">
							{{ currtIndex != null ? schoolList[currtIndex].school : '请选择校区' }}
							<uni-icons type="down" size="20" color="#ccc"></uni-icons>
						</view>
					</picker>
				</view>
			</view>

			<view class="box">
				<view class="box_key">车牌号：</view>
				<view class="box_value" @click="showKeyboard">
					<view class="select" :class="{ active: form.carNumber }">
						{{ form.carNumber ? form.carNumber : '请选择车牌号' }}
						<uni-icons type="down" size="20" color="#ccc"></uni-icons>
					</view>
					<xm-keyboard-v2 ref="xmKeyboard" @confirm="confirm"></xm-keyboard-v2>
				</view>
			</view>

			<view class="box">
				<view class="box_key">到访开始时间：</view>
				<view class="box_value">
					<uni-datetime-picker type="datetime" :start="Date.now()" v-model="form.startTime" @change="changeTime">
						<view class="select" :class="{ active: form.startTime }">
							{{ form.startTime ? form.startTime : '请选择到访开始时间' }}
							<uni-icons type="down" size="20" color="#ccc"></uni-icons>
						</view>
					</uni-datetime-picker>
				</view>
			</view>

			<view class="box">
				<view class="box_key">到访结束时间：</view>
				<view class="box_value">
					<uni-datetime-picker
						type="datetime"
						:start="form.startTime"
						:end="form.startTime.substring(0, 10) + ' 23:59:59'"
						v-model="form.endTime"
						:disabled="!form.startTime"
					>
						<view class="select" :class="{ active: form.endTime }">
							{{ form.endTime ? form.endTime : '请选择到访结束时间' }}
							<uni-icons type="down" size="20" color="#ccc"></uni-icons>
						</view>
					</uni-datetime-picker>
				</view>
			</view>

			<view class="box">
				<view class="box_key">访问事由：</view>
				<view class="box_value">
					<input class="input" type="text" placeholder="请输入访问事由" placeholder-style="color:#CCCCCC;font-size:28rpx" v-model="form.visitorReason" disabled />
				</view>
			</view>

			<!-- 提交按钮区域 -->
			<view class="btn" @click="handleSubmit">提交</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { getSchoolListDataReq, addVisitorRecordReq, getVisitorRecordReq } from '@/api/index.js'

const form = ref({
	phone: '', // 手机号
	name: '', // 学生姓名
	cardId: '', // 证件号
	peerNum: 0, // 同行人数不能为空
	visitorReason: '学校报道', // 访问事由
	schoolId: '', // 校区id
	school: '', // 校区名称不能为空 只能为墨轩湖校区或黄家湖校区
	startTime: '', // 到访开始时间
	endTime: '', // 到访结束时间
	carNumber: '' // 车牌号
})

// 校区索引
const currtIndex = ref(null)
// 校区数组
const schoolList = ref([])

// 车牌弹窗DOM
const xmKeyboard = ref()

onLoad(() => {
	getVisitorRecord()
})

const getVisitorRecord = async () => {
	const res = await getVisitorRecordReq()
	// console.log(res)
	if (res.code == 200) {
		if (res.data) {
			uni.reLaunch({
				url: '/pages/mySubscribe/mySubscribe'
			})
		} else {
			let info = uni.getStorageSync('studentInfo')
			form.value.name = info.name
			form.value.phone = info.phone
			// form.value.cardId = info.cardId

			if (info.avs.length) {
				form.value.peerNum = info.avs.length
			}

			// 获取校区数据
			getSchoolListData()
		}
	}
}

// 获取校区数据
const getSchoolListData = async () => {
	const res = await getSchoolListDataReq()
	// console.log(res)
	if (res.code == 200) {
		schoolList.value = res.data

		let info = uni.getStorageSync('studentInfo')

		if (info.schoolId) {
			schoolList.value.forEach((ele, index) => {
				if (ele.id == info.schoolId) {
					currtIndex.value = index
				}
			})
			form.value.school = info.school
			form.value.schoolId = info.schoolId
		}
	}
}

// 选择校区回调
const bindPickerChange = (e) => {
	// console.log(e)
	currtIndex.value = e.detail.value
	form.value.school = schoolList.value[currtIndex.value].school
	form.value.schoolId = schoolList.value[currtIndex.value].id
}

// 打开选择车牌弹窗
const showKeyboard = () => {
	xmKeyboard.value.toShow()
}

// 选择车牌弹窗完成按钮
const confirm = (e) => {
	// console.log(e)
	form.value.carNumber = e
}

const changeTime = () => {
	form.value.endTime = ''
}

// 提交按钮回调
const handleSubmit = () => {
	if (!form.value.name) {
		uni.showToast({
			title: '请输入学生姓名',
			icon: 'none'
		})
		return
	}

	if (!form.value.phone) {
		uni.showToast({
			title: '请输入手机号',
			icon: 'none'
		})
		return
	}

	let reg = /^1[3-9]\d{9}$/
	if (!reg.test(form.value.phone)) {
		uni.showToast({
			title: '手机号格式错误',
			icon: 'none'
		})
		return
	}

	// if (!form.value.peerNum) {
	// 	uni.showToast({
	// 		title: '请输入同行人数',
	// 		icon: 'none'
	// 	})
	// 	return
	// }

	if (currtIndex.value == null) {
		uni.showToast({
			title: '请选择校区',
			icon: 'none'
		})
		return
	}

	if (!form.value.carNumber) {
		uni.showToast({
			title: '请输入车牌号',
			icon: 'none'
		})
		return
	}

	if (!form.value.startTime) {
		uni.showToast({
			title: '请选择到访开始时间',
			icon: 'none'
		})
		return
	}

	if (!form.value.endTime) {
		uni.showToast({
			title: '请选择到访结束时间',
			icon: 'none'
		})
		return
	}

	if (!form.value.visitorReason) {
		uni.showToast({
			title: '请输入访问事由',
			icon: 'none'
		})
		return
	}

	submit()
}

// 提交请求
const submit = () => {
	// console.log(form.value)

	uni.showModal({
		title: '提示',
		content: '车牌预约之后将不能修改，确定提交么?',
		success: async (res) => {
			if (res.confirm) {
				const res = await addVisitorRecordReq(form.value)
				// console.log(res)
				if (res.code == 200) {
					uni.showToast({
						title: '提交成功',
						icon: 'success'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/mySubscribe/mySubscribe'
						})
					}, 1500)
				}
			}
		}
	})
}
</script>

<style lang="scss" scoped>
.container {
	box-sizing: border-box;
	padding-top: 12rpx;
	min-height: 100vh;
	background-color: #f5f9ff;

	.body {
		box-sizing: border-box;
		padding: 30rpx 15rpx;
		background-color: #fff;

		.box {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;

			.box_key {
				width: 216rpx;
				font-size: 28rpx;
				text-align: end;
			}

			.box_value {
				padding: 0 20rpx;
				width: 507rpx;
				height: 74rpx;
				border-radius: 4rpx;
				border: 2rpx solid #cccccc;

				.input {
					height: 100%;
				}

				.select {
					display: flex;
					align-items: center;
					justify-content: space-between;
					height: 74rpx;
					font-size: 28rpx;
					color: #ccc;
				}

				.active {
					color: #000;
				}
			}
		}

		.btn {
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 100rpx auto 50rpx;
			height: 100rpx;
			font-size: 32rpx;
			color: #fff;
			border-radius: 8rpx;
			background-color: #0061ff;
		}
	}
}
</style>
