<template>
	<view class="page">
		<view class="bg-gradient"></view>
		<van-nav-bar title="选择录入类型" left-arrow @click-left="onBack" fixed :z-index="999" />
		<view class="content">
			<view class="student-info">
				<view class="info-row">
					<text class="label">学校</text>
					<text class="value">{{ schoolName }}</text>
				</view>
				<view class="info-row">
					<text class="label">班级</text>
					<text class="value">{{ gradeName }} {{ className }}</text>
				</view>
				<view class="info-row">
					<text class="label">姓名</text>
					<text class="value">{{ studentName }}</text>
				</view>
				<view class="info-row">
					<text class="label">身份证号</text>
					<text class="value">{{ studentIdCard }}</text>
				</view>
			</view>

			<view class="type-list">
				<view class="type-card" @click="selectType('vision')" v-if="visionStatus === 0">
					<view class="type-name">视力数据录入</view>
					<van-icon name="arrow" />
				</view>
				<view class="type-card" @click="selectType('refraction')" v-if="refractionStatus === 0">
					<view class="type-name">屈光度数据录入</view>
					<van-icon name="arrow" />
				</view>
				<view class="all-done" v-if="visionStatus === 1 && refractionStatus === 1">
					<van-icon name="checked" color="#07c160" size="48rpx" />
					<text class="all-done-text">该学生数据已全部录入</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { getStudentInfo, getStudentVisionData } from '@/services/common'

const studentName = ref('')
const studentIdCard = ref('')
const schoolName = ref('')
const gradeName = ref('')
const className = ref('')
const visionStatus = ref(0)
const refractionStatus = ref(0)
const studentId = ref('')
const params = ref({})

const fetchVisionStatus = async () => {
	if (!studentId.value) return
	try {
		const res = await getStudentVisionData(studentId.value)
		if (res && res.code === 200 && res.data) {
			visionStatus.value = res.data.vision_input_status || 0
			refractionStatus.value = res.data.refraction_input_status || 0
			if (res.data.id) {
				params.value.visionDataId = res.data.id
			}
		}
	} catch (e) {
		visionStatus.value = 0
		refractionStatus.value = 0
	}
}

onLoad((options) => {
	if (options.studentId && !options.studentName) {
		studentId.value = options.studentId
		getStudentInfo(options.studentId).then((res) => {
			studentName.value = res.data.name
			studentIdCard.value = res.data.id_card
			schoolName.value = res.data.school_name || ''
			gradeName.value = res.data.grade || ''
			className.value = res.data.class_name || ''
			studentId.value = res.data.id

			params.value = {
				studentId: res.data.id,
				studentName: res.data.name,
				studentGender: res.data.gender,
				studentIdCard: res.data.id_card,
				classId: res.data.class_id,
				className: res.data.class_name,
				grade: res.data.grade,
				visionDataId: res.data.vision_data_id || '',
				schoolName: res.data.school_name || '',
				userName: res.data.name || '',
				userPhone: options.userPhone || '',
			}

			fetchVisionStatus()
		})
	}

	if (options.studentName) {
		studentId.value = options.studentId
		studentName.value = decodeURIComponent(options.studentName || '')
		studentIdCard.value = decodeURIComponent(options.studentIdCard || '')
		schoolName.value = decodeURIComponent(options.schoolName || '')
		gradeName.value = decodeURIComponent(options.grade || '')
		className.value = decodeURIComponent(options.className || '')

		params.value = {
			studentId: options.studentId,
			studentName: options.studentName,
			studentGender: options.studentGender,
			studentIdCard: options.studentIdCard,
			classId: options.classId,
			className: options.className,
			grade: options.grade,
			visionDataId: options.visionDataId || '',
			schoolName: options.schoolName,
			userName: options.userName,
			userPhone: options.userPhone,
			userOrg: options.userOrg
		}

		fetchVisionStatus()
	}
})

onShow(() => {
	fetchVisionStatus()
})

const selectType = (type) => {
	const url = `/pages/index/vision-input?${Object.keys(params.value).map(k => `${k}=${encodeURIComponent(params.value[k])}`).join('&')}&type=${type}`
	uni.navigateTo({ url })
}

const onBack = () => {
	uni.navigateBack({
		delta: 1,
		success: () => {},
		fail: () => {
			uni.switchTab({ url: '/pages/index/index' })
		}
	})
}
</script>

<style lang="scss" scoped>
.page {
	min-height: 100vh;
	position: relative;
}

.bg-gradient {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: linear-gradient(180deg, #4A9FF5 0%, #FFFFFF 100%);
	z-index: 0;
}

.content {
	padding: 20rpx;
	padding-top: calc(20rpx + 46px);
	position: relative;
	z-index: 1;
}

.student-info {
	background: #fff;
	border-radius: 20rpx;
	padding: 30rpx;
	margin-bottom: 20rpx;
}

.info-row {
	display: flex;
	align-items: center;
	font-size: 28rpx;
	margin-bottom: 20rpx;
}

.info-row:last-child {
	margin-bottom: 0;
}

.label {
	width: 160rpx;
	color: #999;
}

.value {
	flex: 1;
	color: #333;
}

.type-list {
	display: flex;
	flex-direction: column;
	gap: 20rpx;
}

.type-card {
	background: #fff;
	border-radius: 20rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 50rpx 30rpx;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	position: relative;
}

.type-name {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
	flex: 1;
	text-align: center;
}

.type-card :deep(.van-icon) {
	position: absolute;
	right: 30rpx;
}

.all-done {
	background: #fff;
	border-radius: 20rpx;
	padding: 50rpx 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 16rpx;
}

.all-done-text {
	font-size: 30rpx;
	color: #07c160;
	font-weight: bold;
}
</style>
