<template>
	<view class="container">
		<!-- 状态栏占位 -->
		<scroll-view class="content" scroll-y>
			<!-- 积分卡片 -->
			<view class="points-card">
				<view class="card-decoration">
					<text class="star-icon">⭐</text>
				</view>
				<view class="card-content">
					<view class="card-label">积分账户</view>
					<view class="card-balance">{{ pointsData.balance || '0.00' }}</view>
					<view class="card-pending">支出待入账: {{ pointsData.pendingExpense || '0.00' }}</view>
					<view class="withdraw-btn" @click="goWithdraw">去提现</view>
				</view>
			</view>
			
			<!-- 筛选和汇总区域 -->
			<view class="filter-summary-section">
				<view class="filter-row">
					<view class="date-picker" @click="showDatePicker = true">
						<text>{{ selectedDate }}</text>
						<text class="arrow-down">▾</text>
					</view>
					<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="total-amount">
						收入{{ summaryData.totalIncome || '0.00' }}
					</view>
				</view>
				<view class="type-filter" @click="showTypePicker = true">
					<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>
		</scroll-view>
		
		<!-- 日期选择弹窗 -->
		<view class="date-picker-popup" v-if="showDatePicker" @click="showDatePicker = false">
			<view class="picker-content" @click.stop>
				<view class="picker-header">
					<view class="picker-cancel" @click="showDatePicker = false">取消</view>
					<view class="picker-title">选择年月</view>
					<view class="picker-confirm" @click="confirmDate">确定</view>
				</view>
				<view class="picker-body">
					<picker mode="date" fields="month" :value="tempDate" @change="onDateChange">
						<view class="picker-item">
							<text>{{ tempDate || '请选择年月' }}</text>
						</view>
					</picker>
				</view>
			</view>
		</view>
		
		<!-- 类型选择弹窗 -->
		<view class="type-picker-popup" v-if="showTypePicker" @click="showTypePicker = false">
			<view class="picker-content" @click.stop>
				<view class="picker-header">
					<view class="picker-cancel" @click="showTypePicker = false">取消</view>
					<view class="picker-title">选择类型</view>
					<view class="picker-confirm" @click="confirmType">确定</view>
				</view>
				<view class="picker-body">
					<view class="picker-item" 
						:class="{ active: tempType === item.value }"
						v-for="(item, index) in typeOptions" 
						:key="index"
						@click="tempType = item.value">
						{{ item.label }}
					</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: '科普短视频' }
			],
			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.tempDate = e.detail.value
		},
		confirmDate() {
			this.selectedDate = this.tempDate
			this.showDatePicker = false
			this.loadData()
		},
		confirmType() {
			this.selectedType = this.tempType
			this.showTypePicker = false
			this.loadData()
		},
		switchType(type) {
			this.incomeType = type
			this.loadData()
		},
		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;
	background: linear-gradient(135deg, #FF9800 0%, #FFC107 100%);
	border-radius: 24rpx;
	padding: 48rpx 32rpx;
	overflow: hidden;
	
	.card-decoration {
		position: absolute;
		top: 20rpx;
		right: 20rpx;
		width: 120rpx;
		height: 120rpx;
		background: rgba(255, 255, 255, 0.2);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		
		.star-icon {
			font-size: 60rpx;
		}
	}
	
	.card-content {
		position: relative;
		z-index: 1;
		
		.card-label {
			font-size: 28rpx;
			color: rgba(255, 255, 255, 0.9);
			margin-bottom: 16rpx;
		}
		
		.card-balance {
			font-size: 72rpx;
			font-weight: bold;
			color: #fff;
			margin-bottom: 16rpx;
		}
		
		.card-pending {
			font-size: 24rpx;
			color: rgba(255, 255, 255, 0.8);
			margin-bottom: 32rpx;
		}
		
		.withdraw-btn {
			display: inline-block;
			padding: 16rpx 48rpx;
			background: #fff;
			border-radius: 44rpx;
			font-size: 28rpx;
			color: #FF9800;
			font-weight: 500;
		}
	}
}

.filter-summary-section {
	background: #fff;
	margin: 0 24rpx 24rpx;
	border-radius: 16rpx;
	padding: 24rpx;
	
	.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;
			}
		}
		
		.type-buttons {
			display: flex;
			gap: 16rpx;
			
			.type-btn {
				padding: 12rpx 32rpx;
				border-radius: 44rpx;
				font-size: 26rpx;
				background: #f5f5f5;
				color: #333;
				
				&.active {
					background: #FF9800;
					color: #fff;
				}
			}
		}
		
		.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: 8rpx;
		}
		
		.item-time {
			font-size: 24rpx;
			color: #999;
		}
	}
	
	.item-amount {
		font-size: 32rpx;
		font-weight: bold;
		
		&.income {
			color: #FF5030;
		}
		
		&.expense {
			color: #333;
		}
	}
}

.date-picker-popup,
.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;
	
	.picker-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 24rpx;
		border-bottom: 1rpx solid #f0f0f0;
		
		.picker-cancel {
			font-size: 30rpx;
			color: #666;
		}
		
		.picker-title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
		}
		
		.picker-confirm {
			font-size: 30rpx;
			color: #FF9800;
		}
	}
	
	.picker-body {
		padding: 24rpx;
		max-height: 600rpx;
		overflow-y: auto;
		
		.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;
			}
		}
	}
}
</style>
