import 'package:flutter/material.dart'; import '../services/odoo_service.dart'; import '../theme/app_theme.dart'; import 'notifications_screen.dart'; class LoyaltyDashboard extends StatefulWidget { final int partnerId; const LoyaltyDashboard({super.key, required this.partnerId}); @override State createState() => _LoyaltyDashboardState(); } class _LoyaltyDashboardState extends State { List _loyaltyCards = []; bool _isLoading = true; @override void initState() { super.initState(); _fetchLoyaltyData(); } Future _fetchLoyaltyData() async { try { final cards = await OdooService().getLoyaltyCards(widget.partnerId); setState(() { _loyaltyCards = cards; _isLoading = false; }); } catch (e) { if (mounted) { setState(() => _isLoading = false); ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Error loading loyalty cards: $e'))); } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('My Rewards'), actions: [ IconButton( icon: const Icon(Icons.notifications_outlined), onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (_) => const NotificationsScreen())), ), const SizedBox(width: 8), ], ), body: _isLoading ? const Center(child: CircularProgressIndicator()) : RefreshIndicator( onRefresh: _fetchLoyaltyData, child: _loyaltyCards.isEmpty ? Center( child: Text( 'No active rewards yet.', style: Theme.of(context).textTheme.titleLarge, ), ) : ListView.builder( padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 32.0), itemCount: _loyaltyCards.length, itemBuilder: (context, index) { final card = _loyaltyCards[index]; return Container( margin: const EdgeInsets.only(bottom: 32), padding: const EdgeInsets.all(24), decoration: BoxDecoration( color: AppTheme.surfaceContainerLow, borderRadius: BorderRadius.circular(24), boxShadow: [ BoxShadow( color: AppTheme.onSurface.withOpacity(0.06), blurRadius: 24, offset: const Offset(0, 8), ) ] ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Expanded( child: Text( '${card['program_id']?[1] ?? 'Loyalty Program'}', style: Theme.of(context).textTheme.titleLarge, softWrap: true, ), ), const SizedBox(width: 16), Container( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), decoration: BoxDecoration( color: AppTheme.secondaryContainer, borderRadius: BorderRadius.circular(24), ), child: Text( 'Gold Member', style: Theme.of(context).textTheme.labelLarge?.copyWith( color: AppTheme.onSecondaryContainer, fontWeight: FontWeight.bold, ), ), ), ], ), const SizedBox(height: 32), Text('Membership Code', style: Theme.of(context).textTheme.bodyMedium), const SizedBox(height: 4), Text('${card['code'] ?? 'N/A'}', style: Theme.of(context).textTheme.titleMedium), const SizedBox(height: 24), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.end, children: [ Text('Available Points', style: Theme.of(context).textTheme.bodyMedium), Text( '${card['points'] ?? 0}', style: Theme.of(context).textTheme.displayMedium?.copyWith( color: AppTheme.primary, ), ), ], ), ], ), ); }, ), ), ); } }