import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:ristoris/providers/auth_provider.dart'; import 'package:ristoris/providers/database_provider.dart'; class AgentSwitchPage extends ConsumerWidget { const AgentSwitchPage({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final agentiAsync = ref.watch(agentiListProvider); final currentAgent = ref.watch(authProvider).agente; final theme = Theme.of(context); return Scaffold( appBar: AppBar(title: const Text('Cambia agente')), body: agentiAsync.when( data: (agenti) { final filtered = agenti.where((a) => a.idAgente != currentAgent?.idAgente).toList(); if (filtered.isEmpty) { return const Center(child: Text('Nessun altro agente disponibile')); } return ListView.builder( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), itemCount: filtered.length, itemBuilder: (_, i) { final a = filtered[i]; return Card( margin: const EdgeInsets.only(bottom: 8), child: ListTile( leading: CircleAvatar( backgroundColor: theme.colorScheme.primaryContainer, child: Text( a.ragioneSociale?.substring(0, 1).toUpperCase() ?? '?', style: TextStyle(color: theme.colorScheme.onPrimaryContainer), ), ), title: Text(a.ragioneSociale ?? ''), subtitle: Text('Cod. ${a.codAgente ?? ''}'), trailing: const Icon(Icons.chevron_right), onTap: () async { await ref.read(authProvider.notifier).switchAgent(a); if (context.mounted) { context.go('/dashboard'); } }, ), ); }, ); }, loading: () => const Center(child: CircularProgressIndicator()), error: (e, _) => Center(child: Text('Errore: $e')), ), ); } }