<template>
	<view class="paragraph-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">
			<view class="card-title">
				<u-icon name="account" size="18" color="#52c41a"></u-icon>
				<text>选择学生</text>
			</view>
			<view class="picker-row" @click="showStudentPicker = true">
				<text class="picker-text" :class="{ placeholder: selectedStudentIndexes.length === 0 }">{{ selectedStudentDisplay }}</text>
				<u-icon name="arrow-right" size="16" color="#bbb"></u-icon>
			</view>
		</view>

		<!-- 数据表格 -->
		<view v-if="tableData.length > 0" class="data-card">
			<view class="card-title">
				<u-icon name="list" size="18" color="#faad14"></u-icon>
				<text>分段数据</text>
			</view>
			<view class="table-wrap">
				<next-table :show-header="true" :columns="columns" :stripe="true" :fit="false" :show-summary="false" :data="tableData" :showPaging="true" :pageIndex="page" @pageChange="pageChange" @cellClick="cellClick" :pageTotal="pageTotal"></next-table>
			</view>
		</view>

		<!-- 空状态 -->
		<view class="empty-card" v-if="begin && selectedStudentIndexes.length > 0 && tableData.length === 0">
			<u-icon name="file-text" 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="showSegmentPopup" @close="closeSegmentPopup" @open="openSegmentPopup" mode="center" round="16" bgColor="#fff">
			<view class="segment-popup-content">
				<view class="popup-header">
					<text class="popup-title">分段详情</text>
					<u-icon name="close" size="22" color="#999" @click="closeSegmentPopup"></u-icon>
				</view>
				<view class="popup-table">
					<view class="popup-table-header">
						<text class="popup-th">分段数</text>
						<text class="popup-th">距离</text>
						<text class="popup-th">时间</text>
					</view>
					<scroll-view class="popup-scroll" scroll-y>
						<view v-for="(item, index) in row.split('/')" :key="index" class="popup-table-row">
							<text class="popup-td">{{ index+1 }}</text>
							<text class="popup-td">{{ item.split('-')[0] }}</text>
							<text class="popup-td">{{ item.split('-')[1] }}</text>
						</view>
						<view v-if="row.split('/').length === 0" class="popup-empty">
							<text class="popup-empty-text">暂无分段数据</text>
						</view>
					</scroll-view>
				</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'

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

	interface TableDataItem {
		date : string
		name : string
		projectName : string
		segment20 : string
		segment70 : string
		segment120 : string
		segment170 : string
	}

	let row = ref('')

	// 项目选择
	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>>([[]])

	const studentList = ref<Student[]>([])
	const selectedStudentIndexes = ref<number[]>([])
	const selectedStudentIds = ref<string[]>([])
	const showStudentPicker = ref(false)

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

	let page = ref(1)
	let status = ref('loadmore')

	let pageTotal = ref(10)

	const columns = ref([
		{
			label: '训练日期',
			name: 'addTime',
		},
		{
			label: '姓名',
			name: 'name',
		},
		{
			label: '总时长',
			name: 'time',
		},
		{
			label: '详细数据',
			name: 'subsection',
		}
	])

	const tableData = ref<TableDataItem[]>([])

	 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
		begin.value = ''
		end.value = ''
		selectedStudentIndexes.value = []
		selectedStudentIds.value = []
		tableData.value = []
		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
		selectedStudentIndexes.value = []
		selectedStudentIds.value = []
		tableData.value = []
	}

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

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

	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 closeStudentPicker = () => {
		showStudentPicker.value = false
	}

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

	const getRecordList = () => {
		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,
			page: page.value
		}
		PlanService.GetFenduanLog(data).then(res => {
			if (res.code == 0) {
				pageTotal.value = res.data.pageTotal
				tableData.value = res.data.list
			} else {
				Service.Msg(res.msg)
			}
		})
	}



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

	const showSegmentPopup = ref(false)

	interface SegmentItem {
		index: number
		distance: string
		time: string
	}

	const segmentPopupData = ref<SegmentItem[]>([])

	const openSegmentPopup = () => {
		showSegmentPopup.value = true
	}

	const closeSegmentPopup = () => {
		showSegmentPopup.value = false
	}

	const cellClick = (rows: any) => {
		row.value = rows.subsection
		openSegmentPopup()
	}
</script>

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

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

	/* 日期选择 */
	.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;
	}

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

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

	/* 空状态 */
	.empty-card {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 40rpx;
		background: #ffffff;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 16rpx 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;
	}

	/* 分段详情弹窗 */
	.segment-popup-content {
		width: 620rpx;
		max-height: 70vh;
		padding: 32rpx;
		display: flex;
		flex-direction: column;

		.popup-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 24rpx;

			.popup-title {
				font-size: 34rpx;
				font-weight: 700;
				color: #333;
			}
		}

		.popup-table {
			background-color: #fafafa;
			border-radius: 16rpx;
			overflow: hidden;
			display: flex;
			flex-direction: column;

			.popup-table-header {
				display: flex;
				background: linear-gradient(135deg, #faad14 0%, #ffc53d 100%);
				padding: 24rpx 0;

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

			.popup-scroll {
				max-height: 400rpx;

				.popup-table-row {
					display: flex;
					padding: 24rpx 0;
					border-bottom: 1rpx solid #eee;
					background-color: #fff;

					&:nth-child(even) {
						background-color: #fafafa;
					}

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

				.popup-empty {
					padding: 60rpx 0;
					text-align: center;

					.popup-empty-text {
						font-size: 28rpx;
						color: #999;
					}
				}
			}
		}
	}

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