<template>
	<view class="timing-container">
		<!-- 顶部筛选卡片 -->
		<view class="filter-card">
			<view class="card-title">
				<u-icon name="search" size="18" color="#faad14"></u-icon>
				<text>筛选条件</text>
			</view>

			<view class="filter-item" @click="showProject = true">
				<text class="filter-label">项目</text>
				<view class="filter-value">
					<text class="value-text" :class="{ placeholder: !selectProcet }">{{ selectProcet || '请选择项目' }}</text>
					<u-icon name="arrow-right" size="14" color="#bbb"></u-icon>
				</view>
			</view>

			<view  class="filter-item" @click="openCalendar">
				<text class="filter-label">日期</text>
				<view class="filter-value">
					<text class="value-text" :class="{ placeholder: !begin }">
						{{ dateDisplay }}
					</text>
					<u-icon name="arrow-right" size="14" color="#bbb"></u-icon>
				</view>
			</view>

			<view class="filter-item" @click="showStudentPicker = true">
				<text class="filter-label">学生</text>
				<view class="filter-value">
					<text class="value-text" :class="{ placeholder: selectedStudentIndexes.length === 0 }">
						{{ selectedStudentDisplay }}
					</text>
					<u-icon name="arrow-right" size="14" color="#bbb"></u-icon>
				</view>
			</view>
		</view>

		<up-picker v-model:show="showProject" keyName="name" valueName="planId" @confirm="selectProcetFunc" :columns="projectOptions"></up-picker>

		<!-- 数据表格 -->
		<view  class="data-card">
			<view class="card-title">
				<u-icon name="list" size="18" color="#1890ff"></u-icon>
				<text>训练记录</text>
			</view>
			<view v-for="(item,index) in tableData" :key="index">
				<view class="day-title" @click="toggleDayCollapse(index)">
					<text>{{ item.dayTime }}</text>
					<u-icon 
						name="arrow-down" 
						size="16" 
						color="#666"
						:class="{ 'arrow-rotate': collapsedDays[index] }"
					></u-icon>
				</view>
				<view class="table-wrap" v-if="collapsedDays[index]">
					<next-table :show-header="true" @cellClick="cellClick" :columns="columns" :stripe="true" :fit="false" :show-summary="false" :data="item.data" ></next-table>
				</view>
			</view>
		</view>

		<!-- 空状态 -->
		<view v-if=" tableData.length == 0" class="empty-card">
			<u-icon name="info-circle" size="64" color="#d9d9d9"></u-icon>
			<text class="empty-text"> {{ isLoading?'暂无训练记录':'加载中'  }} </text>
		</view>

		<!-- 提示状态 -->
		<!-- <view v-else-if="selectProcet" class="empty-card">
			<u-icon name="search" size="64" color="#d9d9d9"></u-icon>
			<text class="empty-text">请选择筛选条件查看数据</text>
		</view> -->

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

		<!-- 学生选择弹窗 -->
		<view class="modal-overlay" v-if="showStudentPicker" @click="closeStudentPicker"></view>
		<view class="student-picker-modal" v-if="showStudentPicker">
			<view class="picker-header">
				<text class="header-title">选择学生</text>
				<view class="header-actions">
					<text class="action-btn" @click="selectAllStudents">全选</text>
					<text class="action-btn primary" @click="confirmStudentSelection">确定</text>
				</view>
			</view>
			<scroll-view class="student-list" scroll-y>
				<view v-for="(student, index) in studentList" :key="student.studentId" class="student-item" :class="{ selected: selectedStudentIndexes.includes(index) }" @click="toggleStudentSelection(index)">
					<view class="item-checkbox">
						<view class="checkbox-inner" :class="{ checked: selectedStudentIndexes.includes(index) }">
							<u-icon v-if="selectedStudentIndexes.includes(index)" name="checkmark" size="12" color="#fff"></u-icon>
						</view>
					</view>
					<view class="item-avatar">
						<text class="avatar-text">{{ student.name.charAt(0) }}</text>
					</view>
					<text class="item-name">{{ student.name }}</text>
				</view>
			</scroll-view>
		</view>

		<!-- 记录详情弹窗 -->
		<up-popup :show="showRecordPopup" @close="closeRecordPopup" mode="center" round="16" bgColor="#fff">
			<view class="record-popup-content">
				<view class="record-popup-header">
					<text class="record-popup-title">记录详情</text>
					<u-icon name="close" size="22" color="#999" @click="closeRecordPopup"></u-icon>
				</view>
				<scroll-view class="record-popup-body" scroll-y>
					<view class="record-table">
						<view class="record-table-header">
							<text class="record-th">记录</text>
							<text class="record-th">时间</text>
						</view>
						<view 
							v-for="(item, index) in rowData.split('|')" 
							:key="index" 
							class="record-table-row"
							:class="{ 'record-row-odd': index % 2 === 1 }"
						>
							<text class="record-td">记录{{ index+1  }}</text>
							<text class="record-td">{{ item }}</text>
						</view>
						<view v-if="rowData.split('|').length === 0" class="record-empty">
							<text class="record-empty-text">暂无数据</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</up-popup>
	</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'
	import { studentService } from '@/Service/swimming/studentService'

	interface Student {
		studentId: string
		name: string
		gender: string
		age: number
	}

	interface TableDataItem {
		date: string
		name: string
		projectName: string
		plan: string
		completion: number
		detail: string
	}
	
	// 数据加载
	let isLoading=ref(false)
	
	// 项目选择
	let showProject = ref(false)
	let selectProcet = ref('')
	let selectId = ref('')

	// 日期选择
	const begin = ref<string>('')
	const end = ref<string>('')
	const showCalendar = ref(false)

	// 分页相关
	let page = ref(1)
	let pageTotal = ref(10)

	const columns = ref([
		{ label: '姓名', name: 'studentName' },
		{ label: '项目名称', name: 'planName' },
		{ label: '最快速度', name: 'quicklyTime' },
		{ label: '详细数据', name: 'time', width: '200' }
	])
	
	let rowData=ref('')
	
	const tableData = ref<any[]>([])
	const projectOptions = ref<Array<any>>([[]])
	const studentList = ref<Student[]>([])
	const selectedStudentIndexes = ref<number[]>([])
	const selectedStudentIds = ref<string[]>([])
	const showStudentPicker = ref(false)
	
	// 日期折叠状态
	const collapsedDays = ref<boolean[]>([])
	
	// 切换日期折叠状态
	const toggleDayCollapse = (index: number) => {
		collapsedDays.value[index] = !collapsedDays.value[index]
	}

	const selectedStudentDisplay = computed(() => {
		if (selectedStudentIndexes.value.length === 0) {
			return '请选择学生'
		}
		const names = selectedStudentIndexes.value.map(index => studentList.value[index].name)
		return names.length > 2 ? `${names.slice(0, 2).join(', ')}等${names.length}人` : names.join(', ')
	})

	const dateDisplay = computed(() => {
		if (!begin.value) return '请选择'
		if (begin.value === end.value) return begin.value
		return `${begin.value} 至 ${end.value}`
	})

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

	onShow(() => {})

	const getProjectData = () => {
		PlanService.GetPlanListNoPage('计时项目').then(res => {
			if (res.code == 0) {
				projectOptions.value[0] = res.data.list
			} else {
				Service.Msg(res.msg)
			}
		})
	}
	
	const getStudentList=()=>{
		studentService.GetStudentList().then(res=>{
			if (res.code == 0) {
				studentList.value =res.data
			} else {
				Service.Msg(res.msg)
			}
		})
	}
	
	
	const cellClick=(e:any)=>{
		rowData.value=e.time
		console.log(rowData.value);
		showRecordPopup.value=true
	}
	
	const selectProcetFunc = (e: any) => {
		selectProcet.value = e.value[0].name
		selectId.value = e.value[0].planId
		begin.value = ''
		end.value = ''
		selectedStudentIndexes.value = []
		// studentList.value = []
		tableData.value = []
		// getProjectDetail()
		getRecord()
	}

	const getProjectDetail = () => {
		PlanService.GetPlanInfo(selectId.value).then(res => {
			if (res.code == 0) {
				studentList.value = res.data.plan.users
			} else {
				Service.Msg(res.msg)
			}
		})
	}

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

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

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

	const selectAllStudents = () => {
		selectedStudentIndexes.value = studentList.value.map((_, index) => index)
	}

	const toggleStudentSelection = (index: number) => {
		const idx = selectedStudentIndexes.value.indexOf(index)
		if (idx > -1) {
			selectedStudentIndexes.value.splice(idx, 1)
		} else {
			selectedStudentIndexes.value.push(index)
		}
	}

	const confirmStudentSelection = () => {
		selectedStudentIds.value = selectedStudentIndexes.value.map(index => studentList.value[index].studentId)
		showStudentPicker.value = false
		getRecord()
	}

	const getRecord = () => {
		page.value = 1
		getRecordList()
	}

	const getRecordList = () => {
		Service.Msg('数据加载中...')
		let studentIdList = selectedStudentIndexes.value.map(index => {
			return studentList.value[index].studentId
		})
		
		let data = {
			planId: selectId.value,
			studentId: JSON.stringify(studentIdList),
			sTime: begin.value,
			eTime: end.value
		}
		
		PlanService.GetJishiLog(data).then(res => {
			isLoading.value=true
			Service.LoadClose()
			if (res.code == 0) {
				pageTotal.value = res.data.pageTotal
				tableData.value = res.data.list
				// 初始化折叠状态，默认全部展开
				collapsedDays.value = new Array(res.data.list.length).fill(false)
			} else {
				Service.Msg(res.msg)
			}
		})
	}

	const pageChange = (e: any) => {
		page.value = e
		getRecordList()
	}

	const closeStudentPicker = () => {
		showStudentPicker.value = false
	}
	
	// 记录详情弹窗
	const showRecordPopup = ref(false)
	
	interface RecordItem {
		name: string
		time: string
	}
	
	const recordList = ref<RecordItem[]>([])
	
	// 打开记录详情弹窗
	const openRecordPopup = (data: RecordItem[]) => {
		recordList.value = data
		showRecordPopup.value = true
	}
	
	// 关闭记录详情弹窗
	const closeRecordPopup = () => {
		showRecordPopup.value = false
	}
</script>

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

	.timing-container {
		min-height: 100vh;
		padding: 24rpx;
	}

	/* 通用卡片标题 */
	.card-title {
		display: flex;
		align-items: center;
		gap: 10rpx;
		font-size: 30rpx;
		font-weight: 600;
		color: #333;
		margin-bottom: 20rpx;
		padding-bottom: 16rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

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

	.filter-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 22rpx 0;
		border-bottom: 1rpx solid #f5f5f5;

		&:last-child {
			border-bottom: none;
		}

		&:active {
			opacity: 0.7;
		}
	}

	.filter-label {
		font-size: 28rpx;
		color: #666;
		font-weight: 500;
		width: 100rpx;
	}

	.filter-value {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		gap: 12rpx;
	}

	.value-text {
		font-size: 28rpx;
		color: #333;
		font-weight: 500;
		max-width: 420rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		text-align: right;

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

	/* 数据表格卡片 */
	.data-card {
		background: #ffffff;
		border-radius: 16rpx;
		padding: 24rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.06);
	}

	.day-title {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 0 20rpx;
		font-weight: bold;
		font-size: 28rpx;
		color: #333;
		padding: 16rpx 0 8rpx;
		cursor: pointer;
		
		&:active {
			opacity: 0.7;
		}
		
		.arrow-rotate {
			transform: rotate(180deg);
			transition: transform 0.3s ease;
		}
	}

	.table-wrap {
		border-radius: 12rpx;
		overflow: hidden;
		border: 1rpx solid #f0f0f0;
		margin-bottom: 20rpx;
	}

	/* 空状态 */
	.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 20rpx rgba(0, 0, 0, 0.06);

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

	/* 弹窗遮罩 */
	.modal-overlay {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.55);
		z-index: 998;
	}

	/* 学生选择弹窗 */
	.student-picker-modal {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		height: 60vh;
		background: #ffffff;
		border-radius: 24rpx 24rpx 0 0;
		z-index: 999;
		display: flex;
		flex-direction: column;
		animation: slideUp 0.25s ease;
		box-shadow: 0 -8rpx 32rpx rgba(0, 0, 0, 0.15);
	}

	.picker-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 28rpx 24rpx;
		border-bottom: 1rpx solid #f0f0f0;
		background: #fafafa;
		border-radius: 24rpx 24rpx 0 0;

		.header-title {
			font-size: 32rpx;
			font-weight: 600;
			color: #333;
		}

		.header-actions {
			display: flex;
			align-items: center;
			gap: 16rpx;
		}

		.action-btn {
			font-size: 26rpx;
			color: #666;
			padding: 10rpx 24rpx;
			border-radius: 28rpx;
			background: #eeeeee;
			font-weight: 500;

			&.primary {
				color: #fff;
				background: #faad14;
			}

			&:active {
				opacity: 0.8;
			}
		}
	}

	.student-list {
		flex: 1;
		overflow-y: auto;
		padding: 16rpx 24rpx 40rpx;
	}

	.student-item {
		display: flex;
		align-items: center;
		gap: 20rpx;
		padding: 22rpx 20rpx;
		border-radius: 12rpx;
		margin-bottom: 12rpx;
		background: #f8f9fa;
		transition: background 0.2s;

		&:active {
			background: #eeeeee;
		}

		&.selected {
			background: #fff3e0;
			border: 1rpx solid #ffcc80;
		}
	}

	.item-checkbox {
		flex-shrink: 0;

		.checkbox-inner {
			width: 36rpx;
			height: 36rpx;
			border-radius: 8rpx;
			border: 2rpx solid #bfbfbf;
			background: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
			transition: all 0.2s;

			&.checked {
				border-color: #faad14;
				background: #faad14;
			}
		}
	}

	.item-avatar {
		flex-shrink: 0;
		width: 64rpx;
		height: 64rpx;
		border-radius: 50%;
		background: #1890ff;
		display: flex;
		align-items: center;
		justify-content: center;

		.avatar-text {
			font-size: 28rpx;
			color: #fff;
			font-weight: 600;
		}
	}

	.item-name {
		flex: 1;
		font-size: 30rpx;
		color: #333;
		font-weight: 500;
	}

	/* 表格深度样式 */
	::v-deep .sl-table {
		width: 100%;
	}

	::v-deep .sl-table__header {
		background: #faad14 !important;
	}

	::v-deep .sl-table__header__cell {
		color: #fff !important;
		font-weight: 600;
		font-size: 26rpx;
	}

	::v-deep .sl-table__body__cell {
		font-size: 26rpx;
		color: #333;
		padding: 22rpx 14rpx;
	}

	::v-deep .sl-table__body__row:nth-child(even) {
		background-color: #fafafa !important;
	}

	::v-deep .sl-table__body__row:nth-child(odd) {
		background-color: #fff !important;
	}

	@keyframes slideUp {
		from {
			transform: translateY(100%);
		}
		to {
			transform: translateY(0);
		}
	}

	/* 记录详情弹窗 */
	.record-popup-content {
		width: 600rpx;
		max-height: 70vh;
		display: flex;
		flex-direction: column;
	}

	.record-popup-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 28rpx 32rpx;
		border-bottom: 1rpx solid #f0f0f0;
		background: #fafafa;
		border-radius: 16rpx 16rpx 0 0;

		.record-popup-title {
			font-size: 32rpx;
			font-weight: 600;
			color: #333;
		}
	}

	.record-popup-body {
		flex: 1;
		overflow-y: auto;
		padding: 20rpx 24rpx;
		max-height: 50vh;
	}

	.record-table {
		border-radius: 12rpx;
		overflow: hidden;
		border: 1rpx solid #f0f0f0;
	}

	.record-table-header {
		display: flex;
		padding: 22rpx 20rpx;
		background: #faad14;

		.record-th {
			flex: 1;
			text-align: center;
			font-size: 28rpx;
			font-weight: 600;
			color: #fff;
		}
	}

	.record-table-row {
		display: flex;
		padding: 24rpx 20rpx;
		border-bottom: 1rpx solid #f5f5f5;
		background: #ffffff;

		&:last-child {
			border-bottom: none;
		}

		&.record-row-odd {
			background: #fafafa;
		}
	}

	.record-td {
		flex: 1;
		text-align: center;
		font-size: 28rpx;
		color: #333;
	}

	.record-empty {
		padding: 40rpx;
		text-align: center;

		.record-empty-text {
			font-size: 26rpx;
			color: #999;
		}
	}
</style>
