<template>
	<view class="container">
		<!-- logo区域 -->
		<image class="logo" src="/static/logo.png" mode="aspectFill"></image>

		<!-- 标题区域 -->
		<view class="title" v-if="pageValue == 1">信息采集迎新系统</view>
		<view class="title" v-if="pageValue == 2">宿舍选取迎新系统</view>
		<view class="title" v-if="pageValue == 3">访客预约迎新系统</view>

		<!-- 录取号区域 -->
		<view class="box">
			<view class="box_text">录取号</view>
			<view class="box_input">
				<input class="input" type="text" placeholder="请输入录取号" placeholder-style="color:#CCCCCC;font-size:28rpx" v-model="admissNum" />
			</view>
		</view>

		<!-- 身份证号区域 -->
		<view class="box">
			<view class="box_text">身份证号</view>
			<view class="box_input">
				<input class="input" type="text" placeholder="请输入身份证号" placeholder-style="color:#CCCCCC;font-size:28rpx" v-model="cardId" />
			</view>
		</view>

		<!-- 登录按钮区域 -->
		<view class="btn" :class="{ active: admissNum && cardId }" @click="handleLogin">授权登录</view>

		<!-- 背景图片区域 -->
		<image class="logo_bg" src="/static/logo-bg.png" mode="aspectFill"></image>

		<!-- 弹窗区域 -->
		<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_title">温馨提示</view>
				<view class="pop_text" v-if="pageValue == 2">{{ tips.chooseDormitory }}</view>
				<view class="pop_text" v-if="pageValue == 3">{{ tips.carOrder }}</view>
				<view class="pop_btn" @click="handleFinish">下一步</view>
			</view>
		</uni-popup>
	</view>
</template>

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

// 页面类型
const pageValue = ref()

// 录取号
const admissNum = ref()

// 身份证号
const cardId = ref()

const popup = ref()

const tips = ref({})

onLoad((options) => {
	pageValue.value = options.pageValue
	// 获取设置数据
	getSettingsInfo()
})

// 获取设置数据
const getSettingsInfo = async () => {
	const res = await getSettingsInfoReq()
	// console.log(res)
	if (res.code == 200) {
		tips.value = res.data
	}
}

// 授权登录按钮回调
const handleLogin = async () => {
	if (!admissNum.value) {
		uni.showToast({
			title: '请输入录取号',
			icon: 'none'
		})
		return
	}

	if (!cardId.value) {
		uni.showToast({
			title: '请输入身份证号',
			icon: 'none'
		})
		return
	}

	let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/

	if (!reg.test(cardId.value)) {
		uni.showToast({
			title: '身份证号格式错误',
			icon: 'none'
		})
		return
	}

	const res = await loginReq({
		admissNum: admissNum.value,
		cardId: cardId.value
	})
	// console.log(res)

	if (res.code == 200) {
		uni.setStorageSync('studentInfo', res.data)
		uni.setStorageSync('TOKEN', res.data.token)

		if (pageValue.value == 1) {
			const studentInfo = uni.getStorageSync('studentInfo')
			if (studentInfo.fillStatus == '已填报') {
				uni.reLaunch({
					url: '/pages/myMsg/myMsg'
				})
			} else {
				uni.reLaunch({
					url: '/pages/read/read'
				})
			}
		} else if (pageValue.value == 2 || pageValue.value == 3) {
			let { isPay } = res.data
			// console.log(isPay)

			if (isPay == 0) {
				popup.value.open()
			} else if (isPay == 1) {
				if (pageValue.value == 2) {
					uni.reLaunch({
						url: '/pages/select/select'
					})
				} else {
					uni.reLaunch({
						url: '/pages/subscribe/subscribe'
					})
				}
			} else {
				uni.showToast({
					title: '暂无缴费信息',
					icon: 'none'
				})
			}
		} else {
			uni.setStorageSync('token', '')
			uni.reLaunch({
				url: '/pages/404/404?message=暂无页面'
			})
		}
	}
}

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

// 去完成缴费回调
const handleFinish = () => {
	window.location.href = `https://pay.ncjti.edu.cn`
}
</script>

<style lang="scss" scoped>
.container {
	position: relative;
	display: flex;
	flex-direction: column;
	align-items: center;
	box-sizing: border-box;
	padding: 0 20rpx;
	width: 100%;
	min-height: 100vh;
	background: linear-gradient(180deg, rgba(242, 247, 255, 1) 0%, rgba(242, 247, 255, 0) 100%);
	overflow: hidden;

	.logo {
		margin-top: 80rpx;
		width: 143rpx;
		height: 143rpx;
	}

	.title {
		margin-top: 28rpx;
		font-size: 40rpx;
		font-weight: bold;
	}

	.box {
		margin-bottom: 40rpx;
		width: 100%;

		.box_text {
			margin-left: 30rpx;
			font-size: 28rpx;
		}

		.box_input {
			padding: 0 40rpx;
			margin-top: 10rpx;
			height: 100rpx;
			border-radius: 8rpx;
			background-color: #fff;

			.input {
				height: 100%;
			}
		}
	}

	.btn {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 260rpx;
		width: 100%;
		height: 100rpx;
		border-radius: 8rpx;
		font-size: 32rpx;
		color: #fff;
		background-color: #ccc;
	}

	.active {
		background-color: #0061ff;
	}

	.logo_bg {
		position: absolute;
		top: -50rpx;
		right: -45rpx;
		width: 589rpx;
		height: 320rpx;
	}

	.pop {
		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;
		}
	}
}
</style>
