<template>
	<view class="container">
		<!-- 选择发车时间区域 -->
		<view class="time">
			<view class="time-title">
				选择发车时间 <span>/ 开启校车之旅</span>
			</view>

			<view class="time-list">
				<view :class="currentIndex1 === index?'active':'time-list-item'" v-for="(item,index) in timeList"
					:key="index" @click="handleChangeCurrentIndex(index,1,item)">
					{{item}}
				</view>
			</view>
		</view>

		<!-- 选择预约车辆区域 -->
		<view class="bus">
			<view class="bus-title">
				选择预约车辆 <span>/ 墨轩湖 - 综合楼</span>
			</view>
			<view class="bus-list">
				<view :class="currentIndex2 === index?'active':'bus-list-item'" v-for="(item,index) in busList"
					:key="index" @click="handleChangeCurrentIndex(index,2,item)">
					{{item}}
				</view>

			</view>
		</view>

		<!-- 立即预约按钮区域 -->
		<view class="btn">
			<view class="btn-clock">
				<img src="../../static/clock.png">
			</view>
			<view class="btn-info">
				<view class="btn-info-title">
					预约截止时间
				</view>
				<view class="btn-info-time">
					2023-10-03 15:15:15
				</view>
			</view>

			<view class="btn-button" @click="handleSub">
				立即预约
			</view>
		</view>

		<!-- 预约截止时间区域 -->
		<view class="info">
			<view class="info-title">
				预约说明
			</view>
			<view class="info-detail">
				预约成功后请提前5分钟到校车地点，若迟到需重新排队或预约；如有疑问请联系后勤。后勤电话：13689545487
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue"
	import {
		onLoad
	} from "@dcloudio/uni-app"

	const wxCode = ref('')
	const access_token = ref('')

	onLoad(() => {
		const busUserInfo = uni.getStorageSync('busUserInfo');
		if (!busUserInfo) {
			wxCode.value = getQueryString('wxcode')
			get_access_token(wxCode.value)
		}
	})

	// 通过wxcode换取access_token
	const get_access_token = (wxCode) => {
		uni.request({
			url: 'open.wecard.qq.com/connect/oauth2/token',
			method: 'POST',
			// header: {
			// 	'content-type': 'application/json'
			// },
			data: {
				"wxcode": wxCode,
				"app_key": "4AA7B3944BDF3739",
				"app_secret": "69E762653EF393C0122278F21754B6CF",
				"grant_type": "authorization_code",
				"redirect_uri": "http://192.168.161.125:5173/#/pages/home/home"
			},
			success: (res) => {
				alert(JSON.stringify(res))
				// access_token.value = res.data.access_token
				// alert(access_token.value)
				// getUserInfo(access_token.value)
			}
		});
	}

	// 通过access_token换取用户信息
	const getUserInfo = (access_token) => {
		uni.request({
			url: 'open.wecard.qq.com/connect/oauth/get-user-info',
			method: "POST",
			data: {
				"access_token": access_token
			},
			header: {
				'content-type': 'application/json'
			},
			success: (res) => {
				alert(JSON.stringify(res))
			}
		});
	}

	//获取当前URL指定参数
	const getQueryString = (name) => {
		// 获取URL
		let url = window.location.href;
		// 正则匹配URL
		let pattern = new RegExp("[?&]" + name + "=([^&]+)", "i");
		let matcher = pattern.exec(url);
		if (matcher == null || matcher.length < 1) {
			return false;
		}
		// 输出指定的参数值
		return decodeURIComponent(matcher[1]);
	}

	const currentIndex1 = ref(null)
	const currentIndex2 = ref(null)
	const currentTime = ref(null)
	const currentBus = ref(null)

	// 发车时间列表
	const timeList = ref([
		'2023-10-10 15:15:15', '2023-10-10 16:15:15', '2023-10-10 18:15:15'
	])

	// 发车车辆列表
	const busList = ref(['车牌尾号：6601（63/63）', '车牌尾号：6661（63/63）', '车牌尾号：6001（63/63）'])

	// 改变选中项回调
	const handleChangeCurrentIndex = (index, type, value) => {
		if (type === 1) {
			currentIndex1.value = index
			currentTime.value = value
		} else {
			currentIndex2.value = index
			currentBus.value = value
		}
	}

	// 立即预约按钮回调
	const handleSub = () => {
		if (!currentTime.value) {
			uni.showToast({
				title: '请先选择发车时间',
				icon: "none"
			})
			return
		}
		if (!currentBus.value) {
			uni.showToast({
				title: '请先选择预约车辆',
				icon: "none"
			})
			return
		}
		uni.showModal({
			title: '提示',
			content: '确定预约吗？',
			success: (res) => {
				if (res.confirm) {
					console.log(123);
				} else if (res.cancel) {

				}
			}
		});
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 0 30rpx;

		.time {
			.time-title {
				height: 128rpx;
				line-height: 128rpx;
				font-size: 30rpx;
				font-weight: bold;

				span {
					font-size: 24rpx;
					font-weight: 400;
					color: #B3B3B3;
				}
			}

			.time-list {
				display: grid;
				grid-template-columns: 1fr 1fr;
				grid-auto-rows: 100rpx;
				grid-gap: 30rpx 20rpx;

				.time-list-item {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					border: 1rpx solid #CCCCCC;
					border-radius: 12rpx;
				}

				.active {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					border: 1rpx solid #CCCCCC;
					border-radius: 12rpx;
					color: #fff;
					background-image: linear-gradient(#8684FF, #3D50E7);
				}

				.disable {
					background: #E6E6E6;
				}
			}
		}

		.bus {
			.bus-title {
				height: 128rpx;
				line-height: 128rpx;
				font-size: 30rpx;
				font-weight: bold;

				span {
					font-size: 24rpx;
					font-weight: 400;
				}
			}

			.bus-list {
				display: flex;
				flex-direction: column;
			}

			.bus-list-item {
				display: flex;
				justify-content: center;
				align-items: center;
				margin-bottom: 30rpx;
				height: 100rpx;
				font-size: 26rpx;
				border: 1rpx solid #CCCCCC;
				border-radius: 12rpx;
			}

			.active {
				display: flex;
				justify-content: center;
				align-items: center;
				margin-bottom: 30rpx;
				height: 100rpx;
				font-size: 26rpx;
				border: 1rpx solid #CCCCCC;
				border-radius: 12rpx;
				color: #fff;
				background-image: linear-gradient(#8684FF, #3D50E7);
			}

			.disable {
				background: #E6E6E6;
			}
		}

		.btn {
			position: relative;
			display: flex;
			margin-top: 50rpx;
			padding: 24rpx 30rpx;
			box-sizing: border-box;
			height: 115rpx;
			border-radius: 15rpx;
			box-shadow: 0px 0px 12rpx rgba(0, 0, 0, .12);

			.btn-clock {
				width: 61rpx;
				height: 61rpx;

				img {
					width: 100%;
				}
			}

			.btn-info {
				margin-left: 18rpx;
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: center;

				.btn-info-title {
					font-size: 28rpx;
					color: #5863F1;
				}

				.btn-info-time {
					font-size: 24rpx;
					color: #B3B3B3;
				}
			}

			.btn-button {
				position: absolute;
				right: 0;
				top: -7rpx;
				width: 257rpx;
				height: 130rpx;
				line-height: 130rpx;
				text-align: center;
				font-size: 30rpx;
				color: #fff;
				background: url(../../static/box.png);
				background-size: 100% 100%;
			}
		}

		.info {
			margin: 40rpx 0;

			.info-title {
				height: 41rpx;
				font-size: 28rpx;
			}

			.info-detail {
				margin-top: 5rpx;
				height: 70rpx;
				color: #B3B3B3;
				font-size: 24rpx;
			}
		}
	}
</style>