<template>
	<view class="grades-container">
		<!-- 项目选择卡片 -->
		<view class="filter-card ">
			<view class="card-title">
				<u-icon name="grid" size="18" color="#1890ff"></u-icon>
				<text>选择项目</text>
			</view>
			<view class="picker-row" @click="showProject = true">
				<text class="picker-text" :class="{ placeholder: !selectProcet }">{{ selectProcet || '请选择项目' }}</text>
				<u-icon name="arrow-right" size="16" color="#bbb"></u-icon>
			</view>
		</view>
		<up-picker v-model:show="showProject" keyName="name" valueName="planId" @confirm="selectProcetFunc" :columns="projectOptions"></up-picker>

		<!-- 排名列表区域 -->
		<view class="ranking-section" v-if="selectProcet && gradeList.length > 0">
			<!-- 统计卡片 -->
			<view class="stats-card">
				<view class="stat-item">
					<text class="stat-label">对比项目</text>
					<text class="stat-value">{{ selectProcet }}</text>
				</view>
				<view class="stat-divider"></view>
				<view class="stat-item">
					<text class="stat-label">参与人数</text>
					<text class="stat-value">{{ gradeList.length }} 人</text>
				</view>
			</view>

			<!-- 排名列表 -->
			<view class="ranking-list">
				<view v-for="(item, index) in gradeList" :key="item.id"
					:class="['ranking-item', { 'top-three': index < 3 }]" @click="handleRankingClick(item)">
					<view class="rank-badge" :class="`rank-${index + 1}`">
						<text class="rank-number">{{ index + 1 }}</text>
					</view>

					<view class="student-info">
						<text class="student-name">{{ item.studentName }}</text>
						<text class="student-speed">最快用时：{{ item.quicklyTime }}s</text>
					</view>

					<view class="speed-detail">
						<text class="detail-text">{{ Service.formatDate(item.addTime,2) }}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 空状态 -->
		<view class="empty-card" v-if="selectProcet && gradeList.length === 0">
			<u-icon name="file-text" size="64" color="#d9d9d9"></u-icon>
			<text class="empty-text">暂无排名数据</text>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { onShow, onLoad } from "@dcloudio/uni-app"
	import { Service } from '@/Service/Service'
	import { ref, computed } from 'vue'
	import { PlanService } from '@/Service/swimming/PlanService'

	// 项目选择
	let showProject = ref(false)
	let selectProcet = ref('')
	let selectId = ref('')

	const projectOptions = ref<Array<any>>([[]])

	interface StudentGrade {
		id: string
		name: string
		bestSpeed: number
		bestTime: number
		recordDate: string
		studentId: string
	}

	const gradeList = ref<StudentGrade[]>([])

	const maxSpeed = computed(() => {
		if (gradeList.value.length === 0) return 0
		return Math.max(...gradeList.value.map(item => item.bestSpeed)).toFixed(2)
	})

	onLoad(() => {
		getProjectData()
	})

	const getProjectData = () => {
		PlanService.GetPlanListNoPage('计时项目').then(res => {
			if (res.code == 0) {
				projectOptions.value[0] = res.data.list
			} else {
				Service.Msg(res.msg)
			}
		})
	}

	const selectProcetFunc = (e: any) => {
		selectProcet.value = e.value[0].name
		selectId.value = e.value[0].planId
		loadProjectGrades(selectId.value)
	}

	const loadProjectGrades = (projectId: string) => {
		PlanService.GetRankData(projectId).then(res => {
			if (res.code == 0) {
				gradeList.value = res.data.list
			} else {
				Service.Msg(res.msg)
			}
		})
	}

	const handleRankingClick = (item: StudentGrade) => {
		console.log('点击了排名项:', item)
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #e8ecf3;
	}

	.grades-container {
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: 40rpx;
	}

	/* 通用卡片 */
	.filter-card {
		background: #ffffff;
		border-radius: 16rpx;
		padding: 24rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
	}

	.card-title {
		display: flex;
		align-items: center;
		gap: 10rpx;
		font-size: 30rpx;
		font-weight: 600;
		color: #333;
		margin-bottom: 16rpx;
	}

	.picker-row {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 22rpx 24rpx;
		background: #f5f7fa;
		border-radius: 12rpx;
		border: 1rpx solid #e4e8ee;
		transition: all 0.2s;

		&:active {
			background: #eef1f6;
		}

		.picker-text {
			font-size: 28rpx;
			color: #333;
			font-weight: 500;

			&.placeholder {
				color: #999;
				font-weight: 400;
			}
		}
	}

	/* 排名区域 */
	.ranking-section {
		margin-top: 4rpx;
	}

	.stats-card {
		background: #ffffff;
		border-radius: 16rpx;
		padding: 24rpx;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);

		.stat-item {
			text-align: center;

			.stat-label {
				font-size: 24rpx;
				color: #666;
				display: block;
				margin-bottom: 8rpx;
				font-weight: 500;
			}

			.stat-value {
				font-size: 32rpx;
				font-weight: 700;
				color: #faad14;
			}
		}

		.stat-divider {
			width: 1rpx;
			height: 50rpx;
			background-color: #f0f0f0;
		}
	}

	.ranking-list {
		.ranking-item {
			background-color: #fff;
			border-radius: 16rpx;
			padding: 24rpx;
			margin-bottom: 16rpx;
			display: flex;
			align-items: center;
			gap: 20rpx;
			box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
			transition: all 0.2s;
			position: relative;
			overflow: hidden;

			&:active {
				transform: scale(0.99);
				box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.08);
			}

			&.top-three {
				border-left-color: #ffd700;
			}

			.rank-badge {
				width: 60rpx;
				height: 60rpx;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-shrink: 0;
				background-color: #f0f0f0;
				box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);

				.rank-number {
					font-size: 24rpx;
					font-weight: 700;
					color: #999;
				}

				&.rank-1 {
					background: linear-gradient(135deg, #ffd700 0%, #ffec3d 100%);
					box-shadow: 0 2rpx 8rpx rgba(255, 215, 0, 0.4);

					.rank-number {
						color: #fff;
						font-size: 28rpx;
					}
				}

				&.rank-2 {
					background: linear-gradient(135deg, #c0c0c0 0%, #d9d9d9 100%);
					box-shadow: 0 2rpx 8rpx rgba(192, 192, 192, 0.4);

					.rank-number {
						color: #fff;
					}
				}

				&.rank-3 {
					background: linear-gradient(135deg, #cd7f32 0%, #e6963d 100%);
					box-shadow: 0 2rpx 8rpx rgba(205, 127, 50, 0.4);

					.rank-number {
						color: #fff;
					}
				}
			}

			.student-info {
				flex: 1;
				min-width: 0;

				.student-name {
					font-size: 32rpx;
					font-weight: 600;
					color: #333;
					display: block;
					margin-bottom: 8rpx;
				}

				.student-speed {
					font-size: 26rpx;
					color: #1890ff;
					font-weight: 500;
				}
			}

			.speed-detail {
				display: flex;
				align-items: center;
				gap: 16rpx;
				flex-shrink: 0;

				.detail-text {
					font-size: 24rpx;
					color: #999;
					background-color: #f5f7fa;
					padding: 6rpx 12rpx;
					border-radius: 6rpx;
				}
			}
		}
	}

	/* 空状态 */
	.empty-card {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 120rpx 40rpx;
		background: #ffffff;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
		margin-top: 4rpx;

		.empty-text {
			margin-top: 24rpx;
			font-size: 28rpx;
			color: #999;
		}
	}
</style>
