import 'package:flutter/material.dart'; import '../../../app/app_services.dart'; import '../../../core/widgets/empty_view.dart'; import '../../../core/widgets/error_view.dart'; import '../../../core/widgets/loading_view.dart'; import '../../../domain/entities/customer.dart'; import '../../../domain/repositories/customer_repository.dart'; class CustomerSearchScreen extends StatefulWidget { const CustomerSearchScreen({required this.services, super.key}); final AppServices services; @override State createState() => _CustomerSearchScreenState(); } class _CustomerSearchScreenState extends State { final _queryController = TextEditingController(); List _customers = []; bool _loading = false; bool _hasSearched = false; String? _errorMessage; @override void dispose() { _queryController.dispose(); super.dispose(); } Future _search() async { final query = _queryController.text.trim(); if (query.isEmpty || _loading) return; setState(() { _loading = true; _hasSearched = true; _errorMessage = null; _customers = []; }); try { final customers = await widget.services.customerRepository.search(query); if (mounted) setState(() => _customers = customers); } on CustomerSearchException catch (error) { if (mounted) setState(() => _errorMessage = error.message); } catch (_) { if (mounted) { setState(() => _errorMessage = 'Não foi possível pesquisar clientes.'); } } finally { if (mounted) setState(() => _loading = false); } } void _select(Customer customer) { Navigator.of(context).pop(customer); } Widget _buildResults() { if (_loading) return const LoadingView(message: 'Pesquisando clientes...'); if (_errorMessage != null) { return ErrorView(message: _errorMessage!, onRetry: _search); } if (!_hasSearched) { return const EmptyView( title: 'Pesquisar cliente', message: 'Informe o nome ou CPF do cliente.', icon: Icons.person_search_outlined, ); } if (_customers.isEmpty) { return const EmptyView( title: 'Nenhum cliente encontrado', message: 'Tente outro nome ou CPF.', icon: Icons.person_off_outlined, ); } return ListView.separated( padding: const EdgeInsets.all(12), itemCount: _customers.length, separatorBuilder: (_, __) => const SizedBox(height: 6), itemBuilder: (context, index) { final customer = _customers[index]; return Card( child: ListTile( key: Key('customer-${customer.id}'), title: Text(customer.name), subtitle: customer.documentMasked.isEmpty ? null : Text(customer.documentMasked), trailing: const Icon(Icons.chevron_right), onTap: () => _select(customer), ), ); }, ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Consulta de cliente')), body: SafeArea( child: Column( children: [ Padding( padding: const EdgeInsets.fromLTRB(16, 12, 16, 8), child: Row( children: [ Expanded( child: TextField( key: const Key('customer-search-query'), controller: _queryController, enabled: !_loading, textInputAction: TextInputAction.search, onSubmitted: (_) => _search(), decoration: const InputDecoration( labelText: 'Nome ou CPF', prefixIcon: Icon(Icons.search), ), ), ), const SizedBox(width: 10), IconButton.filledTonal( key: const Key('customer-search-button'), tooltip: 'Pesquisar cliente', constraints: const BoxConstraints.tightFor(width: 52, height: 52), onPressed: _loading ? null : _search, icon: _loading ? const SizedBox.square( dimension: 20, child: CircularProgressIndicator(strokeWidth: 2), ) : const Icon(Icons.search), ), ], ), ), Expanded(child: _buildResults()), ], ), ), ); } }