import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import '../../../providers/auth_provider.dart'; import '../../../providers/customer_provider.dart'; import '../../../database/tables/clienti.dart'; import '../../widgets/search_bar.dart' as sw; import '../../widgets/empty_state.dart'; class CustomerListPage extends ConsumerStatefulWidget { const CustomerListPage({super.key}); @override ConsumerState createState() => _CustomerListPageState(); } class _CustomerListPageState extends ConsumerState { String _filter = ''; final _searchCtrl = TextEditingController(); @override void dispose() { _searchCtrl.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final agente = ref.watch(authProvider).agente; final customersAsync = agente != null ? ref.watch(_filter.isEmpty ? customerListProvider(agente.idAgente) : customerSearchProvider((idAgente: agente.idAgente, filter: _filter))) : null; return Scaffold( appBar: AppBar(title: const Text('Clienti')), body: Column( children: [ sw.SearchBarWidget( controller: _searchCtrl, hint: 'Cerca cliente...', onChanged: (v) => setState(() => _filter = v), onClear: () { _searchCtrl.clear(); setState(() => _filter = ''); }, ), Expanded( child: customersAsync?.when( data: (customers) { if (customers.isEmpty) return const EmptyState(icon: Icons.people_outline, title: 'Nessun cliente'); return ListView.builder( itemCount: customers.length, itemBuilder: (_, i) => ListTile( onTap: () => context.go('/dashboard/customers/${customers[i].idCliente}'), leading: CircleAvatar(child: Text(customers[i].codCliente?.substring(0, 2) ?? '??')), title: Text(customers[i].ragioneSociale ?? ''), subtitle: Text(customers[i].codCliente ?? ''), trailing: const Icon(Icons.chevron_right), ), ); }, loading: () => const Center(child: CircularProgressIndicator()), error: (e, _) => Center(child: Text('Errore: $e')), ) ?? const Center(child: CircularProgressIndicator()), ), ], ), ); } }