<template>
	<view class="container">
		
		<!-- 筛选标签栏 -->
		<view class="filter-bar">
			<view class="filter-tabs">
				<view class="tab-item" 
					:class="{ active: currentTab === item.value }" 
					v-for="(item, index) in tabs" 
					:key="index"
					@click="switchTab(item.value)">
					{{ item.label }}
				</view>
			</view>
			<view class="filter-divider"></view>
			<view class="filter-btn" @click="showFilter = true">
				<image class="w32 h32" src="@/static/image/icon_select.png" mode=""></image>
			</view>
		</view>
		<!-- 任务列表 -->
		<scroll-view class="content" scroll-y>
			<view class="task-card" v-for="(item, index) in taskList" :key="index" @click="showDetail(item)">
				<view class="card-header">
					<view class="card-title">{{ item.title }}</view>
					<view class="status-tag" :class="item.status">
						{{ item.statusText }}
					</view>
				</view>
				<view class="card-tags">
					<view class="tag-item video-tag" v-if="item.videoType">
						<image class="w28 h28 mr10" src="@/static/image/icon_longvideo.png" mode=""></image>
						<text>{{ item.videoType }}</text>
					</view>
					<view class="tag-item" v-if="item.category">
						{{ item.category }}
					</view>
					<view class="tag-item points-tag">
						{{ item.points }}积分
					</view>
					<view class="tag-item">
						{{ item.count }}个
					</view>
				</view>
				
				<view class="card-dates">
					<view class="date-item">
						<text>开始时间: {{ item.startTime }}</text>
					</view>
					<view class="date-item">
						<text>结束时间: {{ item.endTime }}</text>
					</view>
				</view>
				
				<view class="card-warning" v-if="item.warning">
					<text class="warning-icon">⚠</text>
					<text>{{ item.warning }}</text>
				</view>
				
				<view class="card-rejection" v-if="item.rejectionReason">
					<text class="rejection-icon">❌</text>
					<text>驳回原因: {{ item.rejectionReason }}</text>
				</view>
				
				<view class="card-footer">
					<view class="footer-date">{{ item.createTime }}</view>
					<view class="footer-actions">
						<view class="action-btn" v-if="item.status === 'pending'" @click="goComplete(item)">
							去完成
						</view>
						<view class="action-btn" v-if="item.status === 'rejected'" @click="goEdit(item)">
							编辑
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<!-- 筛选弹窗 -->
		<view class="filter-popup" v-if="showFilter" @click="closeFilter">
			<view class="filter-content" @click.stop>
				<view class="filter-header">
					<view class="filter-title">筛选</view>
					<view class="filter-close-btn" @click="closeFilter">×</view>
				</view>
				
				<!-- 申请时间筛选 -->
				<view class="filter-group">
					<view class="group-label">申请时间</view>
					<view class="date-range-inputs">
						<picker mode="date" :value="tempDateRange.startDate" @change="onStartDateChange">
							<view class="date-input" :class="{ placeholder: !tempDateRange.startDate }">
								{{ tempDateRange.startDate || '开始时间' }}
							</view>
						</picker>
						<text class="date-separator">-</text>
						<picker mode="date" :value="tempDateRange.endDate" @change="onEndDateChange">
							<view class="date-input" :class="{ placeholder: !tempDateRange.endDate }">
								{{ tempDateRange.endDate || '结束时间' }}
							</view>
						</picker>
					</view>
				</view>
				
				<!-- 操作按钮 -->
				<view class="filter-actions">
					<view class="reset-btn" @click="resetFilters">重置</view>
					<view class="confirm-btn" @click="confirmFilters">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { getAirClassroomList } from '@/api-js/airClassroom'
export default {
	data() {
		return {
			statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
			currentTab: 'all',
			showFilter: false,
			dateRange: {
				startDate: '',
				endDate: ''
			},
			tempDateRange: {
				startDate: '',
				endDate: ''
			},
			tabs: [
				{ label: '全部', value: 'all' },
				{ label: '未完成', value: 'pending' },
				{ label: '待审核', value: 'reviewing' },
				{ label: '已通过', value: 'approved' },
				{ label: '已驳回', value: 'rejected' }
			],
			taskList: []
		}
	},
	watch: {
		showFilter(newVal) {
			if (newVal) {
				// 打开弹窗时，同步临时日期范围为当前日期范围
				this.tempDateRange = {
					startDate: this.dateRange.startDate,
					endDate: this.dateRange.endDate
				}
			}
		}
	},
	onLoad() {
		this.loadData()
	},
	onReachBottom() {
		this.loadMore()
	},
	methods: {
		goBack() {
			uni.navigateBack()
		},
		switchTab(value) {
			this.currentTab = value
			this.loadData()
		},
		closeFilter() {
			// 关闭弹窗时，恢复临时日期范围为当前日期范围
			this.tempDateRange = {
				startDate: this.dateRange.startDate,
				endDate: this.dateRange.endDate
			}
			this.showFilter = false
		},
		onStartDateChange(e) {
			this.tempDateRange.startDate = e.detail.value
		},
		onEndDateChange(e) {
			this.tempDateRange.endDate = e.detail.value
		},
		resetFilters() {
			this.tempDateRange = {
				startDate: '',
				endDate: ''
			}
		},
		confirmFilters() {
			// 验证日期范围
			if (this.tempDateRange.startDate && this.tempDateRange.endDate) {
				if (new Date(this.tempDateRange.startDate) > new Date(this.tempDateRange.endDate)) {
					uni.showToast({
						icon: 'none',
						title: '开始时间不能大于结束时间'
					})
					return
				}
			}
			this.dateRange = {
				startDate: this.tempDateRange.startDate,
				endDate: this.tempDateRange.endDate
			}
			this.showFilter = false
			this.loadData()
		},
		goComplete(item) {
			uni.navigateTo({
				url: `/pages_task/completeTask?id=${item.id}`
			})
		},
		goEdit(item) {
			uni.navigateTo({
				url: `/pages_task/completeTask?id=${item.id}&edit=true`
			})
		},
		// 查看详情
		showDetail(item) {
			uni.navigateTo({
				url: `/pages_task/taskDetail?id=${item.id}`
			})
		},
		async loadData() {
			try {
				uni.showLoading({ title: '加载中...' })
				const res = await getAirClassroomList({
					status: this.currentTab,
					startDate: this.dateRange.startDate,
					endDate: this.dateRange.endDate,
					page: 1,
					pageSize: 20
				})
				uni.hideLoading()
				if (res.code === 200 && res.data) {
					this.taskList = res.data.list || this.getDefaultData()
				} else {
					this.taskList = this.getDefaultData()
				}
			} catch (e) {
				uni.hideLoading()
				console.error('加载数据失败', e)
				this.taskList = this.getDefaultData()
			}
		},
		async loadMore() {
			// 加载更多数据
		},
		getDefaultData() {
			return [
				{
					id: 1,
					title: '王小明医生空中任务',
					videoType: '长视频',
					category: '学术',
					points: '10',
					count: '1',
					startTime: '2025-9-20 13:55',
					endTime: '2025-9-20 13:55',
					createTime: '2025-9-20 13:55',
					status: 'pending',
					statusText: '待完成'
				},
				{
					id: 2,
					title: '王小明医生空中任务',
					videoType: '长视频',
					category: '学术',
					points: '10',
					count: '1',
					startTime: '2025-9-20 13:55',
					endTime: '2025-9-20 13:55',
					createTime: '2025-9-20 13:55',
					status: 'pending',
					statusText: '待完成',
					warning: '有效观看不足5人'
				},
				{
					id: 3,
					title: '王小明医生空中任务',
					videoType: '短视频',
					category: '学术',
					points: '10',
					count: '1',
					startTime: '2025-9-20 13:55',
					endTime: '2025-9-20 13:55',
					createTime: '2025-9-20 13:55',
					status: 'reviewing',
					statusText: '待审核'
				},
				{
					id: 4,
					title: '王小明医生空中任务',
					videoType: '文章',
					category: '学术',
					points: '10',
					count: '1',
					startTime: '2025-9-20 13:55',
					endTime: '2025-9-20 13:55',
					createTime: '2025-9-20 13:55',
					status: 'rejected',
					statusText: '已驳回',
					rejectionReason: '交付物无效,请重新编辑'
				},
				{
					id: 5,
					title: '王小明医生空中任务',
					videoType: '长视频',
					category: '学术',
					points: '10',
					count: '1',
					startTime: '2025-9-20 13:55',
					endTime: '2025-9-20 13:55',
					createTime: '2025-9-20 13:55',
					status: 'approved',
					statusText: '已通过'
				}
			]
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	background: #f5f5f5;
	display: flex;
	flex-direction: column;
}

.status-bar {
	width: 100%;
	background: #fff;
}

.header {
	position: relative;
	height: 88rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background: #fff;
	border-bottom: 1rpx solid #f0f0f0;
	
	.back-btn {
		position: absolute;
		left: 24rpx;
		width: 40rpx;
		height: 40rpx;
		
		image {
			width: 100%;
			height: 100%;
		}
	}
	
	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}
	
	.header-right {
		position: absolute;
		right: 24rpx;
		display: flex;
		align-items: center;
		gap: 16rpx;
		
		.more-icon {
			font-size: 32rpx;
			color: #333;
		}
	}
}

.filter-bar {
	display: flex;
	align-items: center;
	background: #fff;
	padding: 0 24rpx;
	border-bottom: 1rpx solid #f0f0f0;
	
	.filter-tabs {
		flex: 1;
		display: flex;
		align-items: center;
		gap: 32rpx;
		overflow-x: auto;
		
		.tab-item {
			padding: 24rpx 0;
			font-size: 28rpx;
			color: #666;
			white-space: nowrap;
			position: relative;
			
			&.active {
				color: #388BFF;
				font-weight: bold;
				
				&::after {
					content: '';
					position: absolute;
					bottom: 0;
					left: 0;
					right: 0;
					height: 4rpx;
					background: #388BFF;
				}
			}
		}
	}
	
	.filter-divider {
		width: 1rpx;
		height: 40rpx;
		background: #e0e0e0;
		margin: 0 16rpx;
	}
	
	.filter-btn {
		padding: 24rpx 0;
		
		.filter-icon {
			font-size: 32rpx;
			color: #333;
		}
	}
}

.content {
	flex: 1;
	padding: 24rpx;
	box-sizing: border-box;
}

.task-card {
	background: #fff;
	border-radius: 16rpx;
	padding: 24rpx;
	margin-bottom: 24rpx;
	
	.card-header {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		margin-bottom: 16rpx;
		
		.card-title {
			flex: 1;
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
		}
		
		.status-tag {
			padding: 8rpx 16rpx;
			border-radius: 20rpx;
			font-size: 24rpx;
			
			&.pending {
				background: #E3F2FD;
				color: #2196F3;
			}
			
			&.reviewing {
				background: #FFF3E0;
				color: #FF9800;
			}
			
			&.approved {
				background: #E8F5E9;
				color: #4CAF50;
			}
			
			&.rejected {
				background: #FFEBEE;
				color: #F44336;
			}
		}
	}
	
	.card-tags {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		gap: 12rpx;
		margin-bottom: 16rpx;
		
		.tag-item {
			padding: 8rpx 16rpx;
			background:#FFFAF4;
			border-radius: 8rpx;
			font-size: 24rpx;
			color: #5D410F;
			display: flex;
			align-items: center;
			&.video-tag {
				background: linear-gradient( 90deg, #FFE9C7 0%, #F3D091 100%);
				
				.tag-icon {
					margin-right: 4rpx;
				}
			}
			
			&.points-tag {
				border: 1rpx solid #388BFF;
				color: #388BFF;
				background: transparent;
			}
		}
	}
	
	.card-dates {
		margin-bottom: 16rpx;
		
		.date-item {
			font-size: 26rpx;
			color: #999;
			margin-bottom: 8rpx;
		}
	}
	
	.card-warning {
		display: flex;
		align-items: center;
		gap: 8rpx;
		padding: 12rpx;
		background: #FFF3E0;
		border-radius: 8rpx;
		margin-bottom: 16rpx;
		font-size: 26rpx;
		color: #FF9800;
		
		.warning-icon {
			font-size: 28rpx;
		}
	}
	
	.card-rejection {
		display: flex;
		align-items: center;
		gap: 8rpx;
		padding: 12rpx;
		background: #FFEBEE;
		border-radius: 8rpx;
		margin-bottom: 16rpx;
		font-size: 26rpx;
		color: #F44336;
		
		.rejection-icon {
			font-size: 28rpx;
		}
	}
	
	.card-footer {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-top: 16rpx;
		border-top: 1rpx solid #f0f0f0;
		
		.footer-date {
			font-size: 24rpx;
			color: #999;
		}
		
		.footer-actions {
			.action-btn {
				padding: 12rpx 32rpx;
				background: #388BFF;
				border-radius: 8rpx;
				font-size: 28rpx;
				color: #fff;
			}
		}
	}
}

.filter-popup {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: rgba(0, 0, 0, 0.5);
	z-index: 999;
	display: flex;
	align-items: flex-end;
}

.filter-content {
	width: 100%;
	background: #fff;
	border-radius: 24rpx 24rpx 0 0;
	padding: 24rpx;
	
	.filter-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 32rpx;
		
		.filter-title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
		}
		
		.filter-close-btn {
			width: 48rpx;
			height: 48rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 40rpx;
			color: #999;
		}
	}
	
	.filter-group {
		margin-bottom: 32rpx;
		
		.group-label {
			font-size: 28rpx;
			font-weight: bold;
			color: #333;
			margin-bottom: 20rpx;
		}
		
		.date-range-inputs {
			display: flex;
			align-items: center;
			gap: 16rpx;
			
			.date-input {
				flex: 1;
				height: 80rpx;
				line-height: 80rpx;
				padding: 0 24rpx;
				background: #f5f5f5;
				border-radius: 8rpx;
				font-size: 28rpx;
				color: #333;
				text-align: center;
				
				&.placeholder {
					color: #999;
				}
			}
			
			.date-separator {
				font-size: 28rpx;
				color: #666;
			}
		}
	}
	
	.filter-actions {
		display: flex;
		gap: 24rpx;
		margin-top: 40rpx;
		padding-top: 24rpx;
		border-top: 1rpx solid #f0f0f0;
		
		.reset-btn,
		.confirm-btn {
			flex: 1;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			border-radius: 8rpx;
			font-size: 30rpx;
		}
		
		.reset-btn {
			background: #fff;
			color: #666;
			border: 1rpx solid #e0e0e0;
		}
		
		.confirm-btn {
			background: #388BFF;
			color: #fff;
		}
	}
}
</style>

