import 'package:flutter/material.dart';
import '../../../../app/theme/app_colors.dart';
import '../../../../app/theme/app_typography.dart';
import '../../../../app/theme/app_spacing.dart';

/// 交易记录页面
///
/// 所有交易明细：购买、出售、转赠、充值、提现
/// 按时间/类型筛选
class TransactionRecordsPage extends StatelessWidget {
  const TransactionRecordsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('交易记录'),
          bottom: const TabBar(
            isScrollable: true,
            tabs: [
              Tab(text: '全部'),
              Tab(text: '购买'),
              Tab(text: '出售'),
              Tab(text: '转赠'),
            ],
          ),
        ),
        body: TabBarView(
          children: [
            _buildList(_allRecords),
            _buildList(_allRecords.where((r) => r.type == '购买').toList()),
            _buildList(_allRecords.where((r) => r.type == '出售').toList()),
            _buildList(_allRecords.where((r) => r.type == '转赠').toList()),
          ],
        ),
      ),
    );
  }

  Widget _buildList(List<_TxRecord> records) {
    if (records.isEmpty) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(Icons.receipt_long_rounded, size: 48, color: AppColors.textTertiary),
            const SizedBox(height: 12),
            Text('暂无记录', style: AppTypography.bodyMedium.copyWith(color: AppColors.textTertiary)),
          ],
        ),
      );
    }

    return ListView.separated(
      padding: const EdgeInsets.all(20),
      itemCount: records.length,
      separatorBuilder: (_, __) => const Divider(height: 1),
      itemBuilder: (context, index) {
        final r = records[index];
        return Padding(
          padding: const EdgeInsets.symmetric(vertical: 12),
          child: Row(
            children: [
              Container(
                width: 40,
                height: 40,
                decoration: BoxDecoration(
                  color: r.color.withValues(alpha: 0.1),
                  borderRadius: AppSpacing.borderRadiusSm,
                ),
                child: Icon(r.icon, color: r.color, size: 20),
              ),
              const SizedBox(width: 12),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(r.title, style: AppTypography.labelMedium),
                    const SizedBox(height: 2),
                    Text(r.subtitle, style: AppTypography.caption),
                  ],
                ),
              ),
              Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Text(
                    r.amount,
                    style: AppTypography.labelMedium.copyWith(
                      color: r.amount.startsWith('+') ? AppColors.success : AppColors.textPrimary,
                    ),
                  ),
                  Text(r.time, style: AppTypography.caption),
                ],
              ),
            ],
          ),
        );
      },
    );
  }
}

class _TxRecord {
  final String type;
  final String title;
  final String subtitle;
  final String amount;
  final String time;
  final IconData icon;
  final Color color;

  const _TxRecord({
    required this.type,
    required this.title,
    required this.subtitle,
    required this.amount,
    required this.time,
    required this.icon,
    required this.color,
  });
}

const _allRecords = [
  _TxRecord(type: '购买', title: '购买 星巴克 \$25 礼品卡', subtitle: '订单号 GNX20260210001', amount: '-\$21.25', time: '今天 14:32', icon: Icons.shopping_cart_rounded, color: AppColors.primary),
  _TxRecord(type: '出售', title: '出售 Amazon \$100 购物券', subtitle: '订单号 GNX20260210002', amount: '+\$92.00', time: '今天 12:15', icon: Icons.sell_rounded, color: AppColors.success),
  _TxRecord(type: '转赠', title: '转赠给 Alice', subtitle: 'Nike \$80 运动券', amount: '\$0', time: '昨天 18:45', icon: Icons.card_giftcard_rounded, color: AppColors.info),
  _TxRecord(type: '购买', title: '购买 Target \$30 折扣券', subtitle: '订单号 GNX20260209001', amount: '-\$24.00', time: '昨天 10:20', icon: Icons.shopping_cart_rounded, color: AppColors.primary),
  _TxRecord(type: '出售', title: '出售 Walmart \$50 生活券', subtitle: '订单号 GNX20260208003', amount: '+\$46.50', time: '2天前', icon: Icons.sell_rounded, color: AppColors.success),
];
