<template>
	<view class="container">
		<!-- 状态栏占位 -->
		<scroll-view class="content" scroll-y>
			<!-- 积分卡片 -->
			<view class="points-card">
				<view class="card-decoration">
					<image class="w194 h166" src="@/static/image/img_bg_card.png" mode=""></image>
				</view>
				<view class="card-content">
					<view class="card-label">积分账户</view>
					<view class="card-balance">{{ pointsData.balance || '0.00' }}</view>
					<view class="x-bc">
						<view class="card-pending">支出待入账: {{ pointsData.pendingExpense || '0.00' }}</view>
						<view class="withdraw-btn x-c" @click="goWithdraw">去提现</view>
					</view>
					
				</view>
			</view>
			
			<!-- 筛选和汇总区域 -->
			<view class="filter-summary-section">
				<view class="filter-row">
					<picker mode="date" fields="month" :value="tempDate" @change="onDateChange">
						<view class="date-picker" @click="showDatePicker = true">
							<text>{{ selectedDate }}</text>
							<text class="arrow-down">▾</text>
						</view>
					</picker>
					<view class="total-amount">
						收入{{ summaryData.totalIncome || '0.00' }}
					</view>
				</view>
				<view class="x-bc">
					<view class="type-buttons">
						<view class="type-btn" :class="{ active: incomeType === 'income' }" @click="switchType('income')">
							收入
						</view>
						<view class="type-btn" :class="{ active: incomeType === 'expense' }" @click="switchType('expense')">
							支出
						</view>
					</view>
					<view class="type-filter" @click="openTypePicker">
						<text>{{ selectedType || '全部类型' }}</text>
						<text class="arrow-down">▾</text>
					</view>
				</view>
				<!-- 交易列表 -->
				<view class="transaction-list">
					<view class="transaction-item" v-for="(item, index) in transactionList" :key="index">
						<view class="item-left">
							<view class="item-title">{{ item.title }}</view>
							<view class="item-time">{{ item.dateTime }}</view>
						</view>
						<view class="item-amount" :class="item.type">
							{{ item.type === 'income' ? '+' : '-' }}{{ item.amount }}
						</view>
					</view>
				</view>
			</view>
			
		
		</scroll-view>
		<!-- 任务类型筛选弹窗 -->
		<view class="type-picker-popup" v-if="showTypePicker" @click="closeTypePicker">
			<view class="picker-content" @click.stop>
				<view class="picker-header">
					<view class="picker-title">任务类型</view>
					<view class="picker-close" @click="closeTypePicker">×</view>
				</view>
				<view class="picker-body">
					<view class="type-grid">
						<view class="type-btn-item" 
							:class="{ active: tempType === item.value }"
							v-for="(item, index) in typeOptions" 
							:key="index"
							@click="tempType = item.value">
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="picker-footer">
					<view class="footer-btn btn-reset" @click="resetType">重置</view>
					<view class="footer-btn btn-confirm" @click="confirmType">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { getPointsList, getPointsInfo } from '@/api-js/points'
export default {
	data() {
		return {
			statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
			showDatePicker: false,
			showTypePicker: false,
			selectedDate: '2025-12',
			tempDate: '2025-12',
			incomeType: 'income', // income: 收入, expense: 支出
			selectedType: '全部类型',
			tempType: '全部类型',
			typeOptions: [
				{ label: '全部类型', value: '全部类型' },
				{ label: '医生坐诊', value: '医生坐诊' },
				{ label: '科普讲座', value: '科普讲座' },
				{ label: '学术研究', value: '学术研究' },
				{ label: '科普文章', value: '科普文章' },
				{ label: '科普短视频', value: '科普短视频' },
				{ label: '科普长视频', value: '科普长视频' },
				{ label: '空中课堂', value: '空中课堂' },
				{ label: '用药调研', value: '用药调研' },
				{ label: '问卷调研', value: '问卷调研' },
				{ label: '社群咨询', value: '社群咨询' },
				{ label: '健康问答', value: '健康问答' },
				{ label: '受试者随访', value: '受试者随访' },
				{ label: '受试者招募', value: '受试者招募' }
			],
			pointsData: {
				balance: '320.00',
				pendingExpense: '30.00'
			},
			summaryData: {
				totalIncome: '600.00'
			},
			transactionList: []
		}
	},
	onLoad() {
		this.loadData()
	},
	onReachBottom() {
		this.loadMore()
	},
	methods: {
		goBack() {
			uni.navigateBack()
		},
		goWithdraw() {
			uni.navigateTo({
				url: '/pages/user/withdraw'
			})
		},
		onDateChange(e) {
			this.selectedDate = e.detail.value
			this.loadData()
		},
		// confirmDate() {
		// 	this.selectedDate = this.tempDate
		// 	this.showDatePicker = false
		// 	this.loadData()
		// },
		closeTypePicker() {
			this.showTypePicker = false
			// 关闭时恢复临时选择
			this.tempType = this.selectedType
		},
		resetType() {
			this.tempType = '全部类型'
		},
		confirmType() {
			this.selectedType = this.tempType
			this.showTypePicker = false
			this.loadData()
		},
		switchType(type) {
			this.incomeType = type
			this.loadData()
		},
		openTypePicker() {
			this.tempType = this.selectedType
			this.showTypePicker = true
		},
		async loadData() {
			try {
				uni.showLoading({ title: '加载中...' })
				const [infoRes, listRes] = await Promise.all([
					getPointsInfo(),
					getPointsList({
						date: this.selectedDate,
						type: this.incomeType,
						category: this.selectedType === '全部类型' ? '' : this.selectedType,
						page: 1,
						pageSize: 20
					})
				])
				uni.hideLoading()
				
				if (infoRes.code === 200 && infoRes.data) {
					this.pointsData = {
						balance: (infoRes.data.balance || 0).toFixed(2),
						pendingExpense: (infoRes.data.pendingExpense || 0).toFixed(2)
					}
				}
				
				if (listRes.code === 200 && listRes.data) {
					this.transactionList = listRes.data.list || this.getDefaultData()
					this.summaryData = {
						totalIncome: (listRes.data.totalIncome || 0).toFixed(2)
					}
				} else {
					this.transactionList = this.getDefaultData()
				}
			} catch (e) {
				uni.hideLoading()
				console.error('加载数据失败', e)
				this.transactionList = this.getDefaultData()
			}
		},
		async loadMore() {
			// 加载更多数据
		},
		getDefaultData() {
			// 默认数据，根据图片中的示例
			return [
				{ title: '医生座诊', dateTime: '2021-07-29 19:55', amount: '300', type: 'income' },
				{ title: '科普讲座', dateTime: '2021-07-29 19:55', amount: '300', type: 'income' }
			]
		}
	}
}
</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;
		}
	}
}

.content {
	flex: 1;
	background: #f5f5f5;
}

.points-card {
	position: relative;
	margin: 24rpx;
	height: 280rpx;
	background: linear-gradient(135deg, #FF9800 0%, #FFC107 100%);
	border-radius: 24rpx;
	padding: 48rpx 40rpx;
	overflow: hidden;
	.card-decoration {
		position: absolute;
		top: 0;
		right: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		
		.star-icon {
			font-size: 60rpx;
		}
	}
	
	.card-content {
		position: relative;
		z-index: 1;
		height: 100%;
		    display: flex;
		    flex-direction: column;
		    justify-content: space-around;
		.card-label {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #583504;
			
		}
		
		.card-balance {
			font-family: PingFang SC, PingFang SC;
			font-weight: 600;
			font-size: 48rpx;
			color: #583504;
			
		}
		
		.card-pending {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #583504;
			// margin-bottom: 32rpx;
		}
		
		.withdraw-btn {
			width: 164rpx;
			height: 64rpx;
			background: rgba(255,255,255,0.4);
			border-radius: 34rpx 34rpx 34rpx 34rpx;
			border: 2rpx solid #FFFFFF;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #583504;
		}
	}
}

.filter-summary-section {
	background: #fff;
	margin: 0 24rpx;
	border-radius: 16rpx;
	padding: 40rpx 32rpx;
	.type-buttons {
		display: flex;
		gap: 16rpx;
		
		.type-btn {
			padding: 12rpx 32rpx;
			border-radius: 30rpx;
			font-size: 26rpx;
			background: #F7F8FA;
			color:#666666;
			
			&.active {
				background: rgba(250,171,12,0.2);
				color:#F08112;
			}
		}
	}
	.filter-row {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 24rpx;
		
		.date-picker {
			display: flex;
			align-items: center;
			gap: 8rpx;
			font-size: 28rpx;
			color: #333;
			
			.arrow-down {
				font-size: 20rpx;
				color: #999;
			}
		}
		
		
		
		.total-amount {
			font-size: 28rpx;
			color: #333;
			font-weight: 500;
		}
	}
	
	.type-filter {
		display: flex;
		align-items: center;
		gap: 8rpx;
		font-size: 28rpx;
		color: #333;
		
		.arrow-down {
			font-size: 20rpx;
			color: #999;
		}
	}
}

.transaction-list {
	background: #fff;
	// margin: 0 24rpx 24rpx;
	border-radius: 16rpx;
	padding: 24rpx;
}

.transaction-item {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 24rpx 0;
	border-bottom: 1rpx solid #f0f0f0;
	
	&:last-child {
		border-bottom: none;
	}
	
	.item-left {
		flex: 1;
		
		.item-title {
			font-size: 30rpx;
			color: #333;
			margin-bottom: 16rpx;
		}
		
		.item-time {
			font-size: 24rpx;
			color: #999;
		}
	}
	
	.item-amount {
		font-size: 32rpx;
		font-weight: 500;
		
		&.income {
			color: #333333;
		}
		
		&.expense {
			color: #333;
		}
	}
}

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

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

.picker-content {
	width: 100%;
	background: #fff;
	border-radius: 24rpx 24rpx 0 0;
	max-height: 80vh;
	display: flex;
	flex-direction: column;
	
	.picker-header {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 32rpx 24rpx;
		border-bottom: 1rpx solid #f0f0f0;
		position: relative;
		
		.picker-cancel {
			font-size: 30rpx;
			color: #666;
		}
		
		.picker-title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
		}
		
		.picker-close {
			position: absolute;
			right: 24rpx;
			top: 50%;
			transform: translateY(-50%);
			font-size: 48rpx;
			color: #999;
			width: 48rpx;
			height: 48rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			line-height: 1;
		}
		
		.picker-confirm {
			font-size: 30rpx;
			color: #FF9800;
		}
	}
	
	.picker-body {
		flex: 1;
		padding: 32rpx 24rpx;
		overflow-y: auto;
		
		.type-grid {
			display: flex;
			flex-wrap: wrap;
			gap: 16rpx;
			
			.type-btn-item {
				padding: 16rpx 32rpx;
				border-radius: 44rpx;
				font-size: 28rpx;
				color: #333;
				background: #f5f5f5;
				border: 2rpx solid transparent;
				min-width: 140rpx;
				text-align: center;
				
				&.active {
					background: #fff;
					border-color: #388BFF;
					color: #388BFF;
				}
			}
		}
		
		.picker-item {
			padding: 24rpx 0;
			font-size: 30rpx;
			color: #333;
			border-bottom: 1rpx solid #f0f0f0;
			
			&:last-child {
				border-bottom: none;
			}
			
			&.active {
				color: #FF9800;
				font-weight: bold;
			}
		}
	}
	
	.picker-footer {
		display: flex;
		gap: 24rpx;
		padding: 24rpx;
		border-top: 1rpx solid #f0f0f0;
		
		.footer-btn {
			flex: 1;
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 8rpx;
			font-size: 32rpx;
			font-weight: 500;
			
			&.btn-reset {
				background: #fff;
				border: 2rpx solid #388BFF;
				color: #388BFF;
			}
			
			&.btn-confirm {
				background: #388BFF;
				color: #fff;
			}
		}
	}
}
</style>
