<template>
	<view class="curve-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="filter-card " v-if="selectProcet">
			<view class="card-title">
				<u-icon name="calendar" size="18" color="#13c2c2"></u-icon>
				<text>日期范围</text>
			</view>
			<view class="date-filter" @click="openCalendar">
				<view class="date-item">
					<text class="date-label">开始日期</text>
					<text class="date-value" :class="{ placeholder: !begin }">{{ begin || '请选择' }}</text>
				</view>
				<view class="date-divider"></view>
				<view class="date-item">
					<text class="date-label">结束日期</text>
					<text class="date-value" :class="{ placeholder: !end }">{{ end || '请选择' }}</text>
				</view>
				<u-icon name="arrow-right" size="16" color="#bbb" class="date-arrow"></u-icon>
			</view>
		</view>

		<!-- 学生选择卡片 -->
		<view class="filter-card " v-if="selectProcet && begin && projectStudents.length > 0">
			<view class="section-header">
				<view class="card-title">
					<u-icon name="account" size="18" color="#52c41a"></u-icon>
					<text>选择学员</text>
				</view>
				<text class="select-count">已选 {{ selectedStudentIds.length }}/3</text>
			</view>
			<view class="student-list">
				<view v-for="student in projectStudents" :key="student.studentId"
					:class="['student-item', { selected: selectedStudentIds.includes(student.studentId) }]"
					@click="toggleStudent(student.studentId)">
					<view class="student-avatar">
						<text class="avatar-text">{{ student.name.charAt(0) }}</text>
					</view>
					<text class="student-name">{{ student.name }}</text>
					<view class="check-icon" v-if="selectedStudentIds.includes(student.studentId)">
						<u-icon name="checkmark" size="14" color="#fff"></u-icon>
					</view>
				</view>
			</view>
		</view>

		<!-- 图表区域 -->
		<view class="chart-section" v-if="selectedStudentIds.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">{{ selectedStudentIds.length }} 人</text>
				</view>
			</view>

			<!-- 折线图 -->
			<view class="chart-card">
				<view class="chart-header">
					<text class="chart-title">成绩趋势对比</text>
					<text class="chart-desc">历史训练成绩变化曲线</text>
				</view>
				<scroll-view class="chart-scroll" scroll-x :show-scrollbar="false">
					<view class="chart-box" :style="{ width: chartWidth + 'rpx' }">
						<qiun-data-charts type="line" :opts="lineOpts" :chartData="lineChartData" :ontouch="true" />
					</view>
				</scroll-view>
			</view>
		</view>

		<!-- 日历弹窗 -->
		<up-calendar :show="showCalendar" mode="date" minDate="1776240407000" @confirm="calendarConfirm" @close="calendarClose"></up-calendar>
	</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'

	const MAX_SELECTED_STUDENTS = 3
	const chartColors = ['#52c41a', '#1890ff', '#faad14']

	let showProject = ref(false)
	let selectProcet = ref('')
	let selectId = ref('')

	const begin = ref<string>('')
	const end = ref<string>('')
	const showCalendar = ref(false)

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

	interface Student {
		studentId: string
		name: string
	}

	interface StudentTrainingRecord {
		studentId: string
		studentName: string
		time: number
		recordDate: string
		recordFullDate: string
		round: number
	}

	const projectStudents = ref<Student[]>([])
	const selectedStudentIds = ref<string[]>([])

	const selectedStudents = computed(() => {
		return projectStudents.value.filter(student =>
			selectedStudentIds.value.includes(student.studentId)
		)
	})

	const chartWidth = computed(() => {
		const categoryCount = lineChartData.value.categories.length
		if (categoryCount <= 6) return 700
		return Math.max(700, categoryCount * 100)
	})

	const lineOpts = ref({
		color: chartColors,
		padding: [15, 10, 0, 15],
		legend: {
			show: true,
			position: 'bottom',
			itemGap: 30
		},
		xAxis: {
			disableGrid: true,
			rotateLabel: true,
			scrollShow: true,
			scrollAlign: 'left'
		},
		yAxis: {
			data: [{
				min: 0,
				format: (val: number) => val.toFixed(1) + 's'
			}]
		},
		extra: {
			line: {
				type: 'curve',
				width: 2,
				activeType: 'hollow',
				linearType: 'none',
				onGap: false
			}
		}
	})

	const lineChartData = ref({
		categories: [] as string[],
		series: [] as any[]
	})

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

	onShow(() => {})

	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
		selectedStudentIds.value = []
		begin.value = ''
		end.value = ''
		projectStudents.value = []
		lineChartData.value.categories = []
		lineChartData.value.series = []
		loadProjectStudents(selectId.value)
	}

	const openCalendar = () => {
		showCalendar.value = true
	}

	const calendarConfirm = (e: any) => {
		begin.value = e[0]
		end.value = e[e.length - 1]
		showCalendar.value = false
	}

	const calendarClose = () => {
		showCalendar.value = false
	}

	const loadProjectStudents = (projectId: string) => {
		PlanService.GetPlanInfo(projectId).then(res => {
			if (res.code == 0) {
				projectStudents.value = res.data.plan.users
			} else {
				Service.Msg(res.msg)
			}
		})
	}

	const toggleStudent = (studentId: string) => {
		const index = selectedStudentIds.value.indexOf(studentId)
		if (index !== -1) {
			selectedStudentIds.value.splice(index, 1)
		} else {
			if (selectedStudentIds.value.length >= MAX_SELECTED_STUDENTS) {
				Service.Msg(`最多只能选择 ${MAX_SELECTED_STUDENTS} 名学员`)
				return
			}
			selectedStudentIds.value.push(studentId)
		}
		let studentIdList = selectedStudentIds.value.map((item) => item)
		let data = {
			planId: selectId.value,
			studentId: JSON.stringify(studentIdList),
			sTime: begin.value,
			eTime: end.value
		}
		interface DataItem {
			name: string
			data: any[]
		}

		const xData = ref<DataItem[]>(
			Array.from({ length: studentIdList.length }, () => ({
				name: '',
				data: []
			}))
		)
		PlanService.GetQuxianLog(data).then(res => {
			if (res.code == 0) {
				lineChartData.value.categories = []
				res.data.list.map((item: any) => {
					lineChartData.value.categories.push(item.dayTime)
					item.data.map((content: any, index: any) => {
						xData.value[index].name = content.studentName
						xData.value[index].data.push(content.time)
					})
				})
				lineChartData.value.series = xData.value
			} else {
				Service.Msg(res.msg)
			}
		})
	}
</script>

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

	.curve-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);
		// border-left: 8rpx solid #999;

	}

	.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;
			}
		}
	}

	/* 日期选择 */
	.date-filter {
		display: flex;
		align-items: center;
		background: #f0fafa;
		border-radius: 12rpx;
		padding: 22rpx 24rpx;
		border: 1rpx solid #d6f0f0;
		transition: all 0.2s;

		&:active {
			background: #e6f5f5;
		}
	}

	.date-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 6rpx;
	}

	.date-label {
		font-size: 24rpx;
		color: #666;
		font-weight: 500;
	}

	.date-value {
		font-size: 30rpx;
		color: #333;
		font-weight: 600;

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

	.date-divider {
		width: 2rpx;
		height: 50rpx;
		background: #b2dfdb;
		margin: 0 20rpx;
	}

	.date-arrow {
		margin-left: 12rpx;
	}

	/* 学生选择区域 */
	.section-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;

		.select-count {
			font-size: 24rpx;
			color: #52c41a;
			font-weight: 600;
			background: #f0f9eb;
			padding: 6rpx 16rpx;
			border-radius: 20rpx;
		}
	}

	.student-list {
		display: grid;
		grid-template-columns: repeat(4, 1fr);
		gap: 16rpx;
	}

	.student-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 20rpx 10rpx;
		background: #f5f7fa;
		border-radius: 12rpx;
		border: 2rpx solid transparent;
		transition: all 0.2s;
		position: relative;

		&:active {
			transform: scale(0.96);
		}

		&.selected {
			background: #f0f9eb;
			border-color: #52c41a;
		}

		.student-avatar {
			width: 60rpx;
			height: 60rpx;
			background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 12rpx;
			box-shadow: 0 2rpx 8rpx rgba(82, 196, 26, 0.3);

			.avatar-text {
				font-size: 24rpx;
				font-weight: 700;
				color: #fff;
			}
		}

		.student-name {
			font-size: 24rpx;
			color: #333;
			text-align: center;
			font-weight: 500;
		}

		.check-icon {
			position: absolute;
			top: 6rpx;
			right: 6rpx;
			width: 28rpx;
			height: 28rpx;
			background: #52c41a;
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	/* 图表区域 */
	.chart-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);
		// border-left: 8rpx solid #faad14;

		.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;
		}
	}

	.chart-card {
		background: #ffffff;
		border-radius: 16rpx;
		padding: 24rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
		// border-left: 8rpx solid #52c41a;

		.chart-header {
			margin-bottom: 20rpx;
			padding-left: 12rpx;
			border-left: 6rpx solid #52c41a;

			.chart-title {
				font-size: 30rpx;
				font-weight: 600;
				color: #333;
				display: block;
				margin-bottom: 6rpx;
			}

			.chart-desc {
				font-size: 24rpx;
				color: #999;
			}
		}

		.chart-scroll {
			width: 100%;
			height: 500rpx;
			background-color: #fafafa;
			border-radius: 12rpx;
			overflow: hidden;
			white-space: nowrap;
			border: 1rpx solid #f0f0f0;

			.chart-box {
				height: 100%;
				display: inline-block;
			}
		}
	}
</style>
