import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import '../../../core/api/api_error_mapper.dart'; import '../../../core/ui/async_state_views.dart'; import '../../auth/data/auth_providers.dart'; import '../data/inventory_providers.dart'; import '../domain/inventory_item.dart'; class InventoryScreen extends ConsumerWidget { const InventoryScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final inventoryAsync = ref.watch(inventoryProvider); return inventoryAsync.when( loading: () => const LoadingStateView(label: 'Laddar inventarie...'), error: (e, _) => ErrorStateView( message: e.toString(), onRetry: () => ref.invalidate(inventoryProvider), ), data: (items) { if (items.isEmpty) { return Stack( children: [ const EmptyStateView(title: 'Inventariet är tomt.'), Positioned( right: 16, bottom: 16, child: FloatingActionButton.extended( onPressed: () => context.push('/inventory/create'), icon: const Icon(Icons.add), label: const Text('Lägg till'), ), ), ], ); } return Stack( children: [ ListView.separated( padding: const EdgeInsets.only(bottom: 88), itemCount: items.length, separatorBuilder: (_, __) => const Divider(height: 1), itemBuilder: (context, index) { final item = items[index]; return _InventoryTile(item: item); }, ), Positioned( right: 16, bottom: 16, child: FloatingActionButton.extended( onPressed: () => context.push('/inventory/create'), icon: const Icon(Icons.add), label: const Text('Lägg till'), ), ), ], ); }, ); } } class _InventoryTile extends StatelessWidget { final InventoryItem item; const _InventoryTile({required this.item}); @override Widget build(BuildContext context) { final subtitle = [ '${item.quantity} ${item.unit}', if (item.location != null && item.location!.isNotEmpty) item.location!, if (item.bestBeforeDate != null) 'Bäst före: ${_formatDate(item.bestBeforeDate!)}', ].join(' · '); return ListTile( title: Text(item.productName), subtitle: Text(subtitle), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ if (item.opened) const Padding( padding: EdgeInsets.only(right: 4), child: Chip( label: Text('Oppnad'), padding: EdgeInsets.zero, visualDensity: VisualDensity.compact, ), ), Tooltip( message: 'Konsumera', child: IconButton( icon: const Icon(Icons.remove_circle_outline), onPressed: () => context.push('/inventory/${item.id}/consume'), ), ), Tooltip( message: 'Redigera', child: IconButton( icon: const Icon(Icons.edit_outlined), onPressed: () => context.push('/inventory/${item.id}/edit'), ), ), _DeleteInventoryButton(item: item), ], ), onTap: () => context.push('/inventory/${item.id}'), ); } String _formatDate(String iso) { try { final dt = DateTime.parse(iso); return '${dt.year}-${dt.month.toString().padLeft(2, '0')}-${dt.day.toString().padLeft(2, '0')}'; } catch (_) { return iso; } } } class _DeleteInventoryButton extends ConsumerWidget { final InventoryItem item; const _DeleteInventoryButton({required this.item}); @override Widget build(BuildContext context, WidgetRef ref) { return Tooltip( message: 'Ta bort', child: IconButton( icon: const Icon(Icons.delete_outline, color: Colors.red), onPressed: () async { final confirmed = await showDialog( context: context, builder: (ctx) => AlertDialog( title: const Text('Ta bort inventariepost?'), content: Text('Vill du ta bort "${item.productName}"?'), actions: [ TextButton( onPressed: () => Navigator.pop(ctx, false), child: const Text('Avbryt'), ), FilledButton( onPressed: () => Navigator.pop(ctx, true), child: const Text('Ta bort'), ), ], ), ); if (confirmed != true) return; try { final token = await ref.read(authStateProvider.future); await ref .read(inventoryRepositoryProvider) .deleteInventoryItem(item.id, token: token); ref.invalidate(inventoryProvider); } catch (error) { if (!context.mounted) return; ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(mapErrorToUserMessage(error))), ); } }, ), ); } }