<!-- 钱包流水页面 -->
<template>
	<view class="walletflow-page">
		<!-- 顶部导航栏 -->
		<navbar title="钱包流水" leftClick @leftClick="$utils.navigateBack" />

		<!-- 账户余额卡片 -->
		<view class="balance-card">
			<view class="balance-label">账户</view>
			<view class="balance-row">
				<text class="balance-amount">{{ accountBalance }}</text>
				<button class="recharge-btn" @click="goRecharge">充值</button>
			</view>
		</view>

		<!-- tab和流水列表卡片 -->
		<view class="flow-card">
			<uv-tabs 
				:list="tabList" 
				:current="activeTab" 
				@change="switchTab"
				:scrollable="false"
				activeColor="#223a7a"
				inactiveColor="#888"
				lineColor="#223a7a"
				lineWidth="44rpx"
				lineHeight="4rpx"
				:itemStyle="{
					height: '88rpx',
					fontSize: '30rpx',
					fontWeight: 'bold'
				}"
			></uv-tabs>
			<scroll-view scroll-y class="flow-list" @scrolltolower="loadMore">
				<view v-if="activeTab === 0">
					<view class="flow-item" v-for="(item, idx) in rechargeList" :key="idx">
						<view class="flow-item-row">
							<view class="flow-item-left">
								<view class="flow-title">{{ item.title || item.type || '充值' }}</view>
								<view class="flow-date">{{ formatDate(item.createTime) }}</view>
							</view>
							<view class="flow-amount plus">+{{ item.money }}</view>
						</view>
					</view>
					<view v-if="rechargeList.length == 0 && !loading" class="empty-tip">暂无记录</view>
				</view>
				<view v-else>
					<view class="flow-item" v-for="(item, idx) in payList" :key="idx">
						<view class="flow-item-row">
							<view class="flow-item-left">
								<view class="flow-title">{{ item.title || item.type || '支付' }}</view>
								<view class="flow-date">{{ formatDate(item.createTime) }}</view>
							</view>
							<view class="flow-amount minus">-{{ item.money }}</view>
						</view>
					</view>
					<view v-if="payList.length == 0 && !loading" class="empty-tip">暂无记录</view>
				</view>
				<view v-if="loading" class="loading-tip">加载中...</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import mixinsList from '@/mixins/list.js'
	
	export default {
		mixins: [mixinsList],
		components: {
		},
		data() {
			return {
				accountBalance: 0,
				activeTab: 0,
				rechargeList: [],
				payList: [],
				loading: false,
				noMore: false,
				pageNo: 1,
				pageSize: 20,
				tabList: [
                    {
                        name: '收入',
                        value: 0
                    },
                    {
                        name: '支出',
                        value: 1
                    }
                ]
			}
		},
		onLoad() {
			this.getAccountBalance();
			this.getFlowList();
		},
		methods: {
			// 获取账户余额
			getAccountBalance() {
				this.$fetch('getMyMoneyNum').then(res => {
					this.accountBalance = res || 0;
				}).catch(err => {
					console.error('获取账户余额失败:', err);
					this.accountBalance = 0;
				});
			},
			
			// 切换tab
			switchTab(tab) {
				// uv-tabs组件传递的可能是对象，需要获取index
				const tabIndex = typeof tab === 'object' ? tab.index : tab;
				
				if (this.activeTab === tabIndex) return;
				
				this.activeTab = tabIndex;
				this.pageNo = 1;
				this.noMore = false;
				
				// 清空对应列表
				if (tabIndex === 0) {
					this.rechargeList = [];
				} else {
					this.payList = [];
				}
				
				this.getFlowList();
			},
			
			// 获取流水列表
			getFlowList() {
				if (this.loading || this.noMore) return;
				
				this.loading = true;
				
				// 根据当前tab确定流水类型：0-充值，1-支付
				const logType = this.activeTab === 0 ? 0 : 1;
				
				this.$fetch('getMyMoneyLogPage', {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					type: logType // 0-充值记录，1-支付记录
				}).then(res => {
					const records = res.records || [];
					
					if (records.length === 0) {
						this.noMore = true;
					} else {
						if (this.activeTab === 0) {
							// 充值记录
							if (this.pageNo === 1) {
								this.rechargeList = records;
							} else {
								this.rechargeList = [...this.rechargeList, ...records];
							}
						} else {
							// 支付记录
							if (this.pageNo === 1) {
								this.payList = records;
							} else {
								this.payList = [...this.payList, ...records];
							}
						}
						
						// 如果返回的记录数少于pageSize，说明没有更多了
						if (records.length < this.pageSize) {
							this.noMore = true;
						}
					}
					
					this.loading = false;
				}).catch(err => {
					console.error('获取流水列表失败:', err);
					this.loading = false;
					uni.showToast({
						title: '获取流水失败',
						icon: 'none'
					});
				});
			},
			
			// 加载更多
			loadMore() {
				if (this.loading || this.noMore) return;
				
				this.pageNo++;
				this.getFlowList();
			},
			
			// 格式化日期
			formatDate(dateString) {
				if (!dateString) return '';
				
				const date = new Date(dateString);
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
				const day = date.getDate().toString().padStart(2, '0');
				
				return `${year}.${month}.${day}`;
			},
			
			goRecharge() {
				uni.navigateTo({
					url: '/pages_order/mine/recharge'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.walletflow-page {
		min-height: 100vh;
		background: linear-gradient(180deg, #f8f8fc 0%, #fff 100%);
		padding-bottom: 30rpx;
	}

	.balance-card {
		background: linear-gradient(90deg, #f7f2fa 0%, #fbeaf2 100%);
		border-radius: 18rpx;
		margin: 24rpx 12rpx 0 12rpx;
		padding: 18rpx 24rpx 14rpx 24rpx;
		box-shadow: none;
		border: 1rpx solid #ede7ef;
		position: relative;
		display: flex;
		flex-direction: column;
		min-height: 130rpx;
		justify-content: center;

		.balance-label {
			color: #bbb;
			font-size: 26rpx;
			margin-bottom: 8rpx;
		}

		.balance-row {
			display: flex;
			align-items: center;
			margin-top: 0;
			position: relative;

			.balance-amount {
				color: #e94f7a;
				font-size: 48rpx;
				font-weight: bold;
			}

			.recharge-btn {
				position: absolute;
				right: 0;
				top: 50%;
				transform: translateY(-50%);
				background: linear-gradient(90deg, #ffb6c1 0%, #fa5a99 100%);
				color: #fff;
				font-size: 28rpx;
				border-radius: 32rpx;
				padding: 0 40rpx;
				height: 56rpx;
				line-height: 56rpx;
				font-weight: 500;
				border: none;
				box-shadow: none;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}

	.flow-card {
		background: #fff;
		border-radius: 20rpx;
		margin: 32rpx 16rpx 0 16rpx;
		box-shadow: 0 4rpx 24rpx 0 rgba(0, 0, 0, 0.06);
		padding-bottom: 8rpx;
		overflow: hidden;
		
		// uv-tabs组件样式调整
		:deep(.uv-tabs) {
			background: #fff;
			border-top-left-radius: 20rpx;
			border-top-right-radius: 20rpx;
		}
		
		:deep(.uv-tabs__wrapper__nav__line) {
			border-radius: 2rpx !important;
		}
	}

	.flow-list {
		margin: 0;
		padding: 0 16rpx;
		max-height: calc(75vh - 88rpx);
		background: #fff;
	}

	.flow-item {
		border-bottom: 1px solid #f5f5f5;
		padding: 18rpx 0 8rpx 0;

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

		.flow-item-row {
			display: flex;
			align-items: flex-start;
			justify-content: space-between;
			padding-right: 45rpx;
			padding-left: 15rpx;
		}

		.flow-item-left {
			display: flex;
			flex-direction: column;
			align-items: flex-start;

			.flow-title {
				font-size: 28rpx;
				color: #222;
				font-weight: 500;
				margin-bottom: 2rpx;

			}

			.flow-date {
				color: #bbb;
				font-size: 22rpx;
				margin-top: 0;
			}
		}

		.flow-amount {
			font-size: 26rpx;
			font-weight: 500;
			margin-left: 24rpx;
			margin-top: 2rpx;

			&.plus {
				color: #223a7a;
			}

			&.minus {
				color: #e94f7a;
			}
		}
	}

	// 提示文字样式
	.empty-tip,
	.loading-tip,
	.no-more-tip {
		text-align: center;
		padding: 40rpx 0;
		color: #999;
		font-size: 26rpx;
	}

	.loading-tip {
		color: #666;
	}
</style>