<template>
	<view class="container">
		<view class="wallet-card">
			<view class="balance-section">
				<view class="balance-header">
					<view class="balance-label">账户余额(元)</view>
					<view class="recharge-btn-wrapper">
						<button class="btn-recharge" @tap="showRechargeModal = true">充值</button>
					</view>
				</view>
				<view class="balance-amount">{{ formatMoney(walletDetail.balance) }}</view>
			</view>
			<view class="wallet-footer">
				<view class="footer-item">
					<view class="footer-label">冻结金额</view>
					<view class="footer-value">{{ formatMoney(walletDetail.frozen_balance) }}</view>
				</view>
				<view class="footer-divider"></view>
				<view class="footer-item">
					<view class="footer-label">更新时间</view>
					<view class="footer-value">{{ formatDateTime(walletDetail.updated_at) }}</view>
				</view>
			</view>
		</view>

		<!-- 充值弹窗 -->
		<up-popup v-if="showRechargeModal" :show="showRechargeModal" mode="center" @close="showRechargeModal=false" :customStyle="{ borderRadius: '24rpx' }">
			<view class="recharge-popup" @tap.stop>
				<view class="popup-header">
					<view class="popup-title">账户充值</view>
					<view class="popup-close" @tap="showRechargeModal=false">×</view>
				</view>

				<view class="recharge-amounts">
					<view
						class="amount-item"
						v-for="item in rechargeAmounts"
						:key="item.value"
						:class="{ active: selectedAmount === item.value }"
						@tap="selectAmount(item.value)"
					>
						<text class="amount-value">¥{{ item.label }}</text>
					</view>
					<view
						class="amount-item custom"
						:class="{ active: isCustomAmount }"
						@tap="selectCustomAmount"
					>
						<text class="amount-value">自定义</text>
					</view>
				</view>

				<view class="custom-input" v-if="isCustomAmount">
					<view class="input-label">请输入充值金额（元）</view>
					<up-input
						v-model="customAmount"
						type="number"
						placeholder="最低0.01元"
						border="surround"
					/>
				</view>

				<view class="popup-footer">
					<button class="btn-apple btn-secondary" @tap="showRechargeModal=false">取消</button>
					<button class="btn-apple btn-primary" @tap="confirmRecharge">确认充值</button>
				</view>
			</view>
		</up-popup>

		<view class="tab-section">
			<view class="tab-header">
				<view class="tab-indicator" :style="{ left: currentTab === 0 ? '8rpx' : '50%' }"></view>
				<view class="tab-item" :class="{ active: currentTab === 0 }" @tap="onTabTap(0)">
					<text>充值记录</text>
				</view>
				<view class="tab-item" :class="{ active: currentTab === 1 }" @tap="onTabTap(1)">
					<text>钱包流水</text>
				</view>
			</view>

			<swiper class="tab-swiper" :current="currentTab" @change="onSwiperChange" :circular="false" :acceleration="true">
				<swiper-item class="swiper-item-content">
					<scroll-view scroll-y class="list-content">
						<view v-if="rechargeList.length === 0 && !rechargeLoading" class="empty-state">
							<view class="empty-icon">📋</view>
							<view class="empty-title">暂无充值记录</view>
						</view>

						<view v-else>
							<view class="card record-card" v-for="(item, index) in rechargeList" :key="index">
								<view class="record-header flex-row-sb">
									<view class="record-no">{{ item.recharge_no }}</view>
									<view class="tag-apple" :class="getRechargeStatusClass(item.status)">{{ item.status_name }}</view>
								</view>
								<view class="record-info">
									<view class="info-row flex-row-sb">
										<view class="info-label">充值金额</view>
										<view class="info-value text-danger">+¥{{ formatMoney(item.amount) }}</view>
									</view>
									<view class="info-row flex-row-sb">
										<view class="info-label">支付方式</view>
										<view class="info-value">{{ item.payment_method || '-' }}</view>
									</view>
									<view class="info-row flex-row-sb">
										<view class="info-label">自动购包</view>
										<view class="info-value">{{ item.auto_purchase_status === '1' ? '已开启' : '未开启' }}</view>
									</view>
									<view class="info-row flex-row-sb">
										<view class="info-label">创建时间</view>
										<view class="info-value">{{ item.created_at }}</view>
									</view>
								</view>
							</view>

							<view class="load-more" v-if="rechargeList.length > 0">
								<text v-if="rechargeLoading">加载中...</text>
								<text v-else-if="rechargeNoMore">— 没有更多了 —</text>
								<text v-else @tap="loadMoreRecharge">点击加载更多</text>
							</view>
						</view>
					</scroll-view>
				</swiper-item>

				<swiper-item class="swiper-item-content">
					<scroll-view scroll-y class="list-content">
						<view v-if="transactionList.length === 0 && !transactionLoading" class="empty-state">
							<view class="empty-icon">💰</view>
							<view class="empty-title">暂无钱包流水</view>
						</view>

						<view v-else>
							<view class="card record-card" v-for="(item, index) in transactionList" :key="index">
								<view class="record-header flex-row-sb">
									<view class="record-type">{{ item.type_name }}</view>
									<view class="record-amount" :class="item.amount >= 0 ? 'text-success' : 'text-danger'">
										{{ item.amount >= 0 ? '+' : '' }}{{ formatMoney(item.amount) }}
									</view>
								</view>
								<view class="record-info">
									<view class="info-row flex-row-sb">
										<view class="info-label">流水ID</view>
										<view class="info-value text-ellipsis">{{ item.transaction_id }}</view>
									</view>
									<view class="info-row flex-row-sb">
										<view class="info-label">变动后余额</view>
										<view class="info-value">¥{{ formatMoney(item.balance_after) }}</view>
									</view>
									<view class="info-row flex-row-sb" v-if="item.remark">
										<view class="info-label">备注</view>
										<view class="info-value">{{ item.remark }}</view>
									</view>
									<view class="info-row flex-row-sb">
										<view class="info-label">创建时间</view>
										<view class="info-value">{{ item.created_at }}</view>
									</view>
								</view>
							</view>

							<view class="load-more" v-if="transactionList.length > 0">
								<text v-if="transactionLoading">加载中...</text>
								<text v-else-if="transactionNoMore">— 没有更多了 —</text>
								<text v-else @tap="loadMoreTransaction">点击加载更多</text>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script setup>
	import { ref, reactive, onMounted } from 'vue';
	import { walletApi } from '@/api/index.js';
	import { useUserStore } from '@/store/index.js';

	const userStore = useUserStore();

	let currentTab = ref(0);

	const onTabTap = (index) => {
		currentTab.value = index;
	};

	const onSwiperChange = (e) => {
		currentTab.value = e.detail.current;
	};

	const formatMoney = (amount) => {
		if (!amount && amount !== 0) return '0.00';
		return (amount / 100).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
	};

	const formatDateTime = (dateStr) => {
		if (!dateStr) return '-';
		const date = new Date(dateStr);
		const year = date.getFullYear();
		const month = String(date.getMonth() + 1).padStart(2, '0');
		const day = String(date.getDate()).padStart(2, '0');
		const hour = String(date.getHours()).padStart(2, '0');
		const minute = String(date.getMinutes()).padStart(2, '0');
		return `${year}-${month}-${day} ${hour}:${minute}`;
	};

	let walletDetail = reactive({
		balance: 0,
		frozen_balance: 0,
		updated_at: ''
	});

	let rechargeList = reactive([]);
	let rechargeLoading = ref(false);
	let rechargeNoMore = ref(false);
	let rechargePage = ref(1);
	const rechargePageSize = 5;

	let transactionList = reactive([]);
	let transactionLoading = ref(false);
	let transactionNoMore = ref(false);
	let transactionPage = ref(1);
	const transactionPageSize = 5;

	let showRechargeModal = ref(false);
	let selectedAmount = ref(null);
	let isCustomAmount = ref(false);
	let customAmount = ref('');
	const rechargeAmounts = [
		{ value: 1000, label: '10' },
		{ value: 2000, label: '20' },
		{ value: 5000, label: '50' },
		{ value: 10000, label: '100' },
		{ value: 20000, label: '200' },
		{ value: 50000, label: '500' }
	];

	const selectAmount = (value) => {
		selectedAmount.value = value;
		isCustomAmount.value = false;
		customAmount.value = '';
	};

	const selectCustomAmount = () => {
		isCustomAmount.value = true;
		selectedAmount.value = null;
	};

	const confirmRecharge = async () => {
		let amount = 0;

		if (isCustomAmount.value) {
			// 自定义金额
			const customAmountNum = parseFloat(customAmount.value);
			if (!customAmountNum || customAmountNum < 0.01) {
				uni.showToast({ title: '请输入正确的充值金额', icon: 'none' });
				return;
			}
			amount = Math.round(customAmountNum * 100); // 转换为分
		} else if (selectedAmount.value) {
			// 预设金额
			amount = selectedAmount.value;
		} else {
			uni.showToast({ title: '请选择充值金额', icon: 'none' });
			return;
		}

		uni.showLoading({ title: '处理中...', mask: true });

		try {
			// 第一步：充值前校验
			const checkData = await walletApi.rechargeCheck(userStore.state.identifier);

			// 如果需要强制充值且金额小于强制充值金额
			if (checkData.need_force_recharge && amount < checkData.force_recharge_amount) {
				uni.hideLoading();
				uni.showModal({
					title: '提示',
					content: `${checkData.message || '当前需充值最低金额'} ¥${(checkData.force_recharge_amount / 100).toFixed(2)}`,
					showCancel: false
				});
				return;
			}

			// 检查金额范围
			if (amount < checkData.min_amount || amount > checkData.max_amount) {
				uni.hideLoading();
				uni.showToast({
					title: `充值金额范围：¥${(checkData.min_amount / 100).toFixed(2)} - ¥${(checkData.max_amount / 100).toFixed(2)}`,
					icon: 'none',
					duration: 2500
				});
				return;
			}

			// 第二步：创建充值订单
			const rechargeData = await walletApi.recharge(
				userStore.state.identifier,
				amount,
				'wechat'
			);

			uni.hideLoading();
			showRechargeModal.value = false;

			// 验证支付配置
			if (!rechargeData.pay_config || !rechargeData.pay_config.package) {
				uni.showToast({ title: '支付参数获取失败', icon: 'none' });
				return;
			}

			// 第三步：调起微信支付
			// #ifdef H5
			if (typeof WeixinJSBridge === 'undefined') {
				uni.showToast({ title: '请在微信中打开', icon: 'none' });
				return;
			}

			WeixinJSBridge.invoke('getBrandWCPayRequest', {
				appId: rechargeData.pay_config.app_id,
				timeStamp: rechargeData.pay_config.timestamp,
				nonceStr: rechargeData.pay_config.nonce_str,
				package: rechargeData.pay_config.package,
				signType: rechargeData.pay_config.sign_type,
				paySign: rechargeData.pay_config.pay_sign
			}, function(res) {
				if (res.err_msg === 'get_brand_wcpay_request:ok') {
					console.log('支付成功', res);
					uni.showToast({ title: '充值成功', icon: 'success' });
					setTimeout(() => {
						loadWalletDetail();
						rechargePage.value = 1;
						rechargeNoMore.value = false;
						loadRechargeList();
					}, 1500);
				} else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
					uni.showToast({ title: '已取消支付', icon: 'none' });
				} else {
					console.error('支付失败', res);
					uni.showToast({ title: '支付失败', icon: 'none' });
				}
			});
			// #endif

			// #ifndef H5
			uni.requestPayment({
				timeStamp: rechargeData.pay_config.timestamp,
				nonceStr: rechargeData.pay_config.nonce_str,
				package: rechargeData.pay_config.package,
				signType: rechargeData.pay_config.sign_type,
				paySign: rechargeData.pay_config.pay_sign,
				success: (res) => {
					console.log('支付成功', res);
					uni.showToast({ title: '充值成功', icon: 'success' });
					setTimeout(() => {
						loadWalletDetail();
						rechargePage.value = 1;
						rechargeNoMore.value = false;
						loadRechargeList();
					}, 1500);
				},
				fail: (err) => {
					console.error('支付失败', err);
					if (err.errMsg && (err.errMsg.includes('cancel') || err.errMsg.includes('用户取消'))) {
						uni.showToast({ title: '已取消支付', icon: 'none' });
					} else {
						uni.showToast({ title: '支付失败', icon: 'none' });
					}
				}
			});
			// #endif

		} catch (e) {
			uni.hideLoading();
			console.error('充值失败', e);
			const errorMsg = e.msg || e.message || '充值失败，请稍后重试';
			uni.showToast({ title: errorMsg, icon: 'none' });
		}
	};

	const getRechargeStatusClass = (status) => {
		const classMap = {
			'0': 'tag-warning',
			'1': 'tag-success',
			'2': 'tag-danger'
		};
		return classMap[status] || '';
	};

	const loadWalletDetail = async () => {
		try {
			const data = await walletApi.getDetail(userStore.state.identifier);
			walletDetail.balance = data.balance || 0;
			walletDetail.frozen_balance = data.frozen_balance || 0;
			walletDetail.updated_at = data.updated_at || '';
		} catch (e) {
			console.error('加载钱包详情失败', e);
		}
	};

	const loadRechargeList = async (append = false) => {
		if (rechargeLoading.value || rechargeNoMore.value) return;
		rechargeLoading.value = true;

		try {
			const data = await walletApi.getRecharges(
				userStore.state.identifier,
				rechargePage.value,
				rechargePageSize
			);

			const newData = (data.items || []).map(item => ({
				...item,
				status_name: item.status === 1 ? '已支付' : item.status === 0 ? '待支付' : '已关闭'
			}));

			if (append) {
				rechargeList.push(...newData);
			} else {
				rechargeList.splice(0, rechargeList.length, ...newData);
			}

			if (newData.length < rechargePageSize) {
				rechargeNoMore.value = true;
			} else {
				rechargePage.value++;
			}
		} catch (e) {
			console.error('加载充值记录失败', e);
		}
		rechargeLoading.value = false;
	};

	const loadTransactionList = async (append = false) => {
		if (transactionLoading.value || transactionNoMore.value) return;
		transactionLoading.value = true;

		try {
			const data = await walletApi.getTransactions(
				userStore.state.identifier,
				transactionPage.value,
				transactionPageSize
			);

			const newData = (data.items || []).map(item => ({
				...item,
				type_name: item.type === 'recharge' ? '充值' : item.type === 'purchase' ? '消费' : '其他'
			}));

			if (append) {
				transactionList.push(...newData);
			} else {
				transactionList.splice(0, transactionList.length, ...newData);
			}

			if (newData.length < transactionPageSize) {
				transactionNoMore.value = true;
			} else {
				transactionPage.value++;
			}
		} catch (e) {
			console.error('加载钱包流水失败', e);
		}
		transactionLoading.value = false;
	};

	const loadMoreRecharge = () => {
		if (!rechargeNoMore.value) {
			loadRechargeList(true);
		}
	};

	const loadMoreTransaction = () => {
		if (!transactionNoMore.value) {
			loadTransactionList(true);
		}
	};

	onMounted(() => {
		loadWalletDetail();
		loadRechargeList();
		loadTransactionList();
	});
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.wallet-card {
		background: linear-gradient(135deg, #0A84FF 0%, #5AC8FA 50%, #64D2FF 100%);
		border-radius: 24rpx;
		padding: 36rpx;
		color: #fff;
		box-shadow: 0 8rpx 32rpx rgba(10, 132, 255, 0.3);

		.balance-section {
			margin-bottom: 28rpx;
			.balance-header {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 8rpx;
				.balance-label { font-size: 24rpx; opacity: 0.75; }
				.recharge-btn-wrapper {
					.btn-recharge {
						background: rgba(255, 255, 255, 0.25);
						border: 1rpx solid rgba(255, 255, 255, 0.5);
						color: #fff;
						font-size: 24rpx;
						padding: 8rpx 24rpx;
						border-radius: 40rpx;
						font-weight: 500;
						&::after { border: none; }
					}
				}
			}
			.balance-amount { font-size: 72rpx; font-weight: 700; letter-spacing: -2rpx; }
		}

		.wallet-footer {
			display: flex;
			align-items: center;
			padding-top: 24rpx;
			border-top: 1rpx solid rgba(255, 255, 255, 0.2);
			.footer-divider { width: 1rpx; height: 40rpx; background: rgba(255, 255, 255, 0.3); margin: 0 32rpx; }
			.footer-item {
				flex: 1;
				.footer-label { font-size: 22rpx; opacity: 0.7; margin-bottom: 6rpx; }
				.footer-value { font-size: 26rpx; font-weight: 600; }
			}
		}
	}

	.tab-section {
		margin-top: 32rpx;
		flex: 1;
		min-height: 0;
		display: flex;
		flex-direction: column;
		.tab-header {
			flex-shrink: 0;
			display: flex;
			position: relative;
			background: #fff;
			border-radius: 16rpx;
			padding: 8rpx;
			.tab-indicator {
				position: absolute;
				top: 8rpx;
				width: calc(50% - 16rpx);
				height: calc(100% - 16rpx);
				background: var(--primary);
				border-radius: 12rpx;
				transition: left 0.3s ease;
				z-index: 0;
			}
			.tab-item {
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 20rpx 0;
				font-size: 28rpx;
				font-weight: 500;
				color: var(--text-tertiary);
				position: relative;
				z-index: 1;
				transition: color 0.3s;
				&.active {
					color: #fff;
					font-weight: 600;
				}
			}
		}
	}

	.tab-swiper {
		flex: 1;
		margin-top: 20rpx;
		min-height: 0;
	}

	.swiper-item-content {
		height: 100%;
	}

	.list-content {
		height: 100%;
		padding-top: 16rpx;
		box-sizing: border-box;
	}

	.record-card {
		margin-bottom: 24rpx;
		.record-header {
			margin-bottom: 20rpx;
			.record-no { font-size: 28rpx; font-weight: 600; color: var(--text-primary); }
			.record-type { font-size: 28rpx; font-weight: 600; color: var(--text-primary); }
			.record-amount { font-size: 32rpx; font-weight: 700; }
		}
		.record-info {
			.info-row {
				padding: 10rpx 0;
				.info-label { font-size: 24rpx; color: var(--text-tertiary); }
				.info-value { font-size: 24rpx; color: var(--text-primary); max-width: 380rpx; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
			}
		}
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 120rpx 40rpx;
		.empty-icon { font-size: 100rpx; margin-bottom: 24rpx; opacity: 0.5; }
		.empty-title { font-size: 28rpx; font-weight: 600; color: var(--text-secondary); }
	}

	.load-more {
		text-align: center;
		padding: 32rpx;
		color: var(--text-tertiary);
		font-size: 24rpx;
	}

	.recharge-popup {
		width: 600rpx;
		max-width: 90vw;
		background: var(--bg-primary);
		border-radius: var(--radius-large);
		overflow: hidden;
		box-sizing: border-box;

		.popup-header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 30rpx;
			border-bottom: 1rpx solid var(--border-light);

			.popup-title {
				font-size: 32rpx;
				font-weight: 600;
				color: var(--text-primary);
			}

			.popup-close {
				width: 48rpx;
				height: 48rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 40rpx;
				color: var(--text-tertiary);
				cursor: pointer;
			}
		}

		.recharge-amounts {
			padding: 30rpx;
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 20rpx;

			.amount-item {
				height: 88rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border: 2rpx solid var(--border-light);
				border-radius: var(--radius-medium);
				cursor: pointer;
				transition: all 0.3s;

				&.active {
					border-color: var(--primary);
					background: rgba(0, 122, 255, 0.05);

					.amount-value {
						color: var(--primary);
						font-weight: 600;
					}
				}

				.amount-value {
					font-size: 28rpx;
					color: var(--text-primary);
				}
			}
		}

		.custom-input {
			padding: 0 30rpx 30rpx;

			.input-label {
				font-size: 26rpx;
				color: var(--text-secondary);
				margin-bottom: 16rpx;
			}
		}

		.popup-footer {
			display: flex;
			gap: 20rpx;
			padding: 30rpx;
			border-top: 1rpx solid var(--border-light);

			.btn-apple {
				flex: 1;
				height: 88rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: var(--radius-medium);
				font-size: 28rpx;
				font-weight: 500;
				border: none;

				&.btn-secondary {
					background: var(--gray-200);
					color: var(--text-primary);
				}

				&.btn-primary {
					background: var(--primary);
					color: #fff;
				}
			}
		}
	}
</style>