<template>
	<view class="container">
		<view class="header">注：请按照自己的真实情况填写</view>

		<view class="form">
			<view class="form_box">
				<view class="form_key">按时报到：</view>
				<switch :checked="studentInfo.registerOntime == 1" color="#0061FF" style="transform: scale(0.9)" @change="switchChange" />
			</view>

			<view class="form_box">
				<view class="form_key">交通方式：</view>
				<view class="form_value">
					<picker @change="bindPickerChange" :value="currtIndex" :range="wayList">
						<view class="select" :class="{ active: currtIndex != null }">
							{{ currtIndex != null ? wayList[currtIndex] : '请选择交通方式' }}
							<uni-icons type="down" size="20" color="#ccc"></uni-icons>
						</view>
					</picker>
				</view>
			</view>

			<view class="form_box">
				<view class="form_key">到站地点：</view>
				<view class="form_value">
					<picker @change="bindPickerChange_place" :value="currtIndex_place" :range="placeList">
						<view class="select" :class="{ active: currtIndex_place != null }">
							{{ currtIndex_place != null ? placeList[currtIndex_place] : '请选择到站地点' }}
							<uni-icons type="down" size="20" color="#ccc"></uni-icons>
						</view>
					</picker>
				</view>
			</view>

			<view class="form_box">
				<view class="form_key">到站日期：</view>
				<view class="form_value">
					<uni-datetime-picker type="date" v-model="studentInfo.arrvieDate">
						<view class="select" :class="{ active: studentInfo.arrvieDate }">
							{{ studentInfo.arrvieDate ? studentInfo.arrvieDate : '请输入到站日期' }}
							<uni-icons type="down" size="20" color="#ccc"></uni-icons>
						</view>
					</uni-datetime-picker>
				</view>
			</view>

			<view class="form_box start noMargin">
				<view class="form_key">到站时间：</view>
				<radio-group @change="radioChange">
					<label v-for="(item, index) in timeList" :key="item">
						<view class="form_time">
							<radio style="transform: scale(0.8)" :value="index.toString()" :checked="index === currentIndex" />
							<view>{{ item.timeStr }}</view>
						</view>
					</label>
				</radio-group>
			</view>
		</view>

		<!-- 陪同人员信息区域 -->
		<view class="family">
			<view class="family_top">
				<uni-icons type="staff-filled" size="30"></uni-icons>
				<view class="top_title">陪同人员信息</view>
				<uni-icons style="margin-left: auto" type="personadd" size="26" color="#0061FF" @click="handleAdd"></uni-icons>
			</view>

			<view class="family_table" v-if="avs.length">
				<view class="table_box top">
					<view class="title">序号</view>
					<view class="name">姓名</view>
					<view class="phone">联系电话</view>
				</view>

				<view class="table_box" v-for="(item, index) in avs" :key="index">
					<view class="title">{{ index + 1 }}</view>
					<view class="name">{{ item.name }}</view>
					<view class="phone">{{ item.phone }}</view>
				</view>
			</view>
		</view>

		<!-- 温馨提示区域 -->
		<view class="tips">
			<view class="tips_box">
				<view class="box_title">温馨提示：</view>
				<view class="box_text">{{ info }}</view>
			</view>
		</view>

		<!-- 按钮区域 -->
		<view class="btns">
			<view v-if="!editType" class="btn up" @click="handleUp">上一步</view>
			<view class="btn down" @click="handleNext">提交</view>
		</view>

		<!-- 添加陪同成员弹窗区域 -->
		<uni-popup ref="popup" :is-mask-click="false">
			<view class="pop">
				<image class="pop_img" src="/static/3.png" mode="aspectFill"></image>
				<!-- 关闭图标 -->
				<uni-icons class="pop_close" type="closeempty" color="#808080" size="20" @click="handleClose"></uni-icons>

				<view class="pop_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="popObj.name" />
					</view>
				</view>

				<view class="pop_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="popObj.phone" />
					</view>
				</view>

				<view class="pop_btn" @click="handleConfirm">确定</view>
			</view>
		</uni-popup>

		<!-- 提示缴费弹窗区域 -->
		<uni-popup ref="popup_tips" :is-mask-click="false">
			<view class="pop_tips">
				<image class="pop_img" src="/static/3.png" mode="aspectFill"></image>
				<!-- 关闭图标 -->
				<uni-icons class="pop_close" type="closeempty" color="#808080" size="20" @click="handleClose_tips"></uni-icons>
				<view class="pop_title">温馨提示</view>
				<view class="pop_text">
					{{ pop_info }}
				</view>
				<view class="pop_btn" @click="handleFinish_tips">去完成缴费</view>
			</view>
		</uni-popup>

		<!-- web弹窗区域 -->
		<uni-popup ref="popup_web" :is-mask-click="false">
			<view class="pop_web">
				<!-- 关闭图标 -->
				<uni-icons class="pop_close" type="closeempty" color="#808080" size="20" @click="handleClose_web"></uni-icons>
				<view class="web">
					<iframe class="iframe" src="https://pay.ncjti.edu.cn" frameborder="0"></iframe>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { submitInfoReq, getArriveTimeListReq, getStudentPayInfo } from '@/api/index.js'

const currtIndex = ref(null)
const wayList = ref(['火车', '飞机', '汽车', '自驾', '其他'])

const currtIndex_place = ref(null)
const placeList = ref(['南昌火车站', '南昌西站', '南昌东站', '昌北机场', '南昌长途汽车西站', '靖安汽车站', '墨轩湖校区', '黄家湖校区'])

// 学生信息
const studentInfo = ref({})

// 到达时间数组
const timeList = ref([])

// 陪同人员数组
const avs = ref([])

// 到站时间索引
const currentIndex = ref(null)

// 弹窗DOM
const popup = ref()

const popup_tips = ref()

const popup_web = ref()

// 弹窗对象数据
const popObj = ref({
	name: '',
	phone: ''
})

const info = ref()

const pop_info = ref()

const editType = ref()

onLoad(() => {
	studentInfo.value = uni.getStorageSync('studentInfo')
	info.value = uni.getStorageSync('settingInfo').arriveSchool
	pop_info.value = uni.getStorageSync('settingInfo').chooseDormitory

	editType.value = uni.getStorageSync('editType')
	// console.log(studentInfo.value)
	// console.log(info.value)
	if (!studentInfo.value.registerOntime) {
		studentInfo.value.registerOntime = 0
	}

	if (studentInfo.value.trafficMethod) {
		// console.log(studentInfo.value.trafficMethod)
		wayList.value.forEach((ele, index) => {
			if (ele == studentInfo.value.trafficMethod) {
				currtIndex.value = index
			}
		})

		if (studentInfo.value.trafficMethod == '自驾') {
			placeList.value = ['墨轩湖校区', '黄家湖校区']
		} else if (studentInfo.value.trafficMethod == '火车') {
			placeList.value = ['南昌火车站', '南昌西站', '南昌东站']
		} else if (studentInfo.value.trafficMethod == '飞机') {
			placeList.value = ['昌北机场']
		} else {
			placeList.value = ['南昌火车站', '南昌西站', '南昌东站', '昌北机场', '南昌长途汽车西站', '靖安汽车站', '墨轩湖校区', '黄家湖校区']
		}
	}

	if (studentInfo.value.arrive) {
		// console.log(studentInfo.value.arrive)
		placeList.value.forEach((ele, index) => {
			if (ele == studentInfo.value.arrive) {
				currtIndex_place.value = index
			}
		})
	}

	avs.value = studentInfo.value.avs || []
	getArriveTimeList()
})

// 获取到达时间数组
const getArriveTimeList = async () => {
	const res = await getArriveTimeListReq()
	// console.log(res)
	if (res.code == 200) {
		timeList.value = res.data
		if (studentInfo.value.arriveTimeId) {
			timeList.value.forEach((ele, index) => {
				if (ele.id == studentInfo.value.arriveTimeId) {
					currentIndex.value = index
				}
			})
			// console.log(currentIndex.value)
		}
	}
}

// 选择交通方式回调
const bindPickerChange = (e) => {
	// console.log(e)
	currtIndex.value = e.detail.value
	studentInfo.value.trafficMethod = wayList.value[currtIndex.value]

	currtIndex_place.value = null

	if (studentInfo.value.trafficMethod == '自驾') {
		placeList.value = ['墨轩湖校区', '黄家湖校区']
	} else if (studentInfo.value.trafficMethod == '火车') {
		placeList.value = ['南昌火车站', '南昌西站', '南昌东站']
	} else if (studentInfo.value.trafficMethod == '飞机') {
		placeList.value = ['昌北机场']
	} else {
		placeList.value = ['南昌火车站', '南昌西站', '南昌东站', '昌北机场', '南昌长途汽车西站', '靖安汽车站', '墨轩湖校区', '黄家湖校区']
	}
}

// 选择到站地点回调
const bindPickerChange_place = (e) => {
	currtIndex_place.value = e.detail.value
	studentInfo.value.arrive = placeList.value[currtIndex_place.value]
}

// 添加陪同成员按钮回调
const handleAdd = () => {
	popup.value.open()
}

// 陪同成员弹窗关闭图标回调
const handleClose = () => {
	popup.value.close()
}

// 点击陪同成员弹窗确定按钮回调
const handleConfirm = () => {
	if (!popObj.value.name) {
		uni.showToast({
			title: '请输入姓名',
			icon: 'none'
		})
		return
	}

	if (!popObj.value.phone) {
		uni.showToast({
			title: '请输入联系电话',
			icon: 'none'
		})
		return
	}

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

	uni.showModal({
		title: '提示',
		content: '确定添加吗？',
		success: (res) => {
			if (res.confirm) {
				avs.value.push(popObj.value)
				popObj.value = {
					name: '',
					phone: ''
				}
				popup.value.close()
			}
		}
	})
}

// 提示缴费弹窗关闭图标回调
const handleClose_tips = () => {
	popup_tips.value.close()
}

// 提示缴费弹窗去完成缴费按钮回调
const handleFinish_tips = () => {
	popup_tips.value.close()
	popup_web.value.open()
}

// web弹窗关闭图标回调
const handleClose_web = async () => {
	// 关闭弹窗时查询用户是否已经支付
	// const res = await getStudentPayInfo({
	// 	admissNum: studentInfo.value.admissNum
	// })
	// console.log(res)
	// if (res.code == 200) {
	// 	if (res.data.isPay == 0) {
	// 		popup_web.value.close()
	// 		popup_tips.value.open()
	// 	} else if (res.data.isPay == 1) {
	// 		studentInfo.value.isPay = 1
	// 		uni.setStorageSync('studentInfo', studentInfo.value)
	// 		uni.reLaunch({
	// 			url: '/pages/payInfo/payInfo'
	// 		})
	// 	}
	// }

	popup_web.value.close()

	setTimeout(() => {
		uni.navigateTo({
			url: '/pages/payInfo/payInfo'
		})
	}, 500)
}

// 切换按时报到
const switchChange = (e) => {
	// console.log(e.detail.value)
	if (e.detail.value) {
		studentInfo.value.registerOntime = 1
	} else {
		studentInfo.value.registerOntime = 0
	}
}

// 选择到达时间
const radioChange = (e) => {
	// console.log(e.detail.value)
	studentInfo.value.arriveTime = timeList.value[e.detail.value].timeStr
	studentInfo.value.arriveTimeId = timeList.value[e.detail.value].id
}

// 点击上一步按钮回调
const handleUp = () => {
	uni.navigateBack()
}

// 点击提交按钮回调
const handleNext = () => {
	if (currtIndex.value == null) {
		uni.showToast({
			title: '请选择交通方式',
			icon: 'none'
		})
		return
	}

	if (currtIndex_place.value == null) {
		uni.showToast({
			title: '请选择到站地点',
			icon: 'none'
		})
		return
	}

	if (!studentInfo.value.arrvieDate) {
		uni.showToast({
			title: '请选择到站日期',
			icon: 'none'
		})
		return
	}

	if (!studentInfo.value.arriveTime) {
		uni.showToast({
			title: '请选择到站时间',
			icon: 'none'
		})
		return
	}

	studentInfo.value.avs = avs.value
	uni.setStorageSync('studentInfo', studentInfo.value)

	const mergedObj = { ...studentInfo.value, arrvieDate: studentInfo.value.arrvieDate + ' 00:00:00' }
	// console.log(mergedObj)
	// console.log(studentInfo.value)
	uni.showModal({
		title: '提示',
		content: '确定提交吗？',
		success: async (res) => {
			if (res.confirm) {
				const res = await submitInfoReq(mergedObj)
				// console.log(res)
				if (res.code == 200) {
					uni.showToast({
						title: res.message,
						icon: 'success'
					})

					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/myMsg/myMsg'
						})
					}, 1500)
				}
			}
		}
	})
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	background-color: #f5f9ff;

	.header {
		padding-left: 20rpx;
		height: 93rpx;
		line-height: 93rpx;
		font-size: 28rpx;
		font-weight: bold;
		background-color: #fff;
	}

	.form {
		padding: 25rpx 0 0;
		margin-top: 10rpx;
		background-color: #fff;

		.form_box {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;
			min-height: 75rpx;

			.form_key {
				width: 170rpx;
				font-size: 28rpx;
				text-align: end;
			}

			.form_value {
				box-sizing: border-box;
				padding: 0 25rpx;
				width: 560rpx;
				height: 75rpx;
				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;
				}
			}

			.form_time {
				display: flex;
				margin-bottom: 20rpx;
				font-size: 28rpx;
			}
		}

		.start {
			align-items: start;
		}

		.noMargin {
			margin-top: 30rpx;
			margin-bottom: 0;
		}
	}

	.family {
		background-color: #fff;

		.family_top {
			display: flex;
			align-items: center;
			padding: 0 20rpx;
			height: 90rpx;

			.top_title {
				margin-left: 20rpx;
				font-size: 30rpx;
				font-weight: bold;
			}
		}

		.family_table {
			padding: 0 20rpx;
			font-size: 24rpx;

			.table_box {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 70rpx;
				border-bottom: 2rpx solid #e6e6e6;

				.title {
					width: 140rpx;
					text-align: center;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}

				.name {
					width: 140rpx;
					text-align: center;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}

				.work {
					width: 200rpx;
					text-align: center;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}

				.phone {
					width: 200rpx;
					text-align: center;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}
			}

			.top {
				background-color: #f0f3f7;
			}
		}
	}

	.tips {
		padding: 25rpx 15rpx;
		font-size: 24rpx;
		background-color: #fff;

		.tips_box {
			padding: 20rpx 0 20rpx 10rpx;
			line-height: 48rpx;
			background-color: #fff2f2;
		}
	}

	.btns {
		display: flex;
		align-items: center;
		justify-content: space-around;
		padding: 10rpx 0 50rpx;
		height: 100rpx;
		background-color: #fff;

		.btn {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 300rpx;
			height: 100rpx;
			font-size: 32rpx;
			border-radius: 8rpx;
		}

		.up {
			border: 2rpx solid #0061ff;
		}

		.down {
			color: #fff;
			background-color: #0061ff;
		}
	}

	.pop {
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
		box-sizing: border-box;
		padding: 150rpx 20rpx 50rpx;
		width: 659rpx;
		height: 528rpx;
		border-radius: 15rpx;
		background: linear-gradient(180deg, #ebf2ff 0%, #ffffff 100%);

		.pop_img {
			position: absolute;
			top: -103rpx;
			left: 227rpx;
			width: 206rpx;
			height: 206rpx;
		}

		.pop_close {
			position: absolute;
			top: 19rpx;
			right: 29rpx;
		}

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

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

			.box_value {
				width: 300rpx;
				height: 70rpx;
				border-radius: 4rpx;
				border: 2rpx solid #cccccc;

				.input {
					padding: 0 20rpx;
					height: 100%;
				}
			}
		}

		.pop_btn {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 30rpx;
			width: 460rpx;
			height: 100rpx;
			color: #fff;
			font-size: 32rpx;
			border-radius: 8rpx;
			background-color: #0061ff;
		}
	}

	.pop_tips {
		position: relative;
		box-sizing: border-box;
		padding: 150rpx 20rpx 50rpx;
		width: 659rpx;
		height: 728rpx;
		border-radius: 15rpx;
		background: linear-gradient(180deg, #ebf2ff 0%, #ffffff 100%);

		.pop_img {
			position: absolute;
			top: -103rpx;
			left: 227rpx;
			width: 206rpx;
			height: 206rpx;
		}

		.pop_close {
			position: absolute;
			top: 19rpx;
			right: 29rpx;
		}

		.pop_title {
			margin-bottom: 28rpx;
			font-size: 32rpx;
			font-weight: bold;
		}

		.pop_text {
			font-size: 28rpx;
			line-height: 50rpx;
		}

		.pop_btn {
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 90rpx auto 0;
			width: 543rpx;
			height: 100rpx;
			color: #fff;
			font-size: 32rpx;
			border-radius: 8rpx;
			background-color: #0061ff;
		}
	}

	.pop_web {
		position: relative;
		box-sizing: border-box;
		padding: 80rpx 20rpx 50rpx;
		width: 90vw;
		height: 90vh;
		border-radius: 15rpx;
		background: linear-gradient(180deg, #ebf2ff 0%, #ffffff 100%);

		.pop_close {
			position: absolute;
			top: 19rpx;
			right: 29rpx;
		}

		.web {
			height: calc(90vh - 90rpx);

			.iframe {
				width: 100%;
				height: 100%;
			}
		}
	}
}
</style>
