import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flux/features/customers/blocs/customer_form_cubit.dart'; class QuickCustomerDialog extends StatefulWidget { final String initialQuery; const QuickCustomerDialog({super.key, required this.initialQuery}); @override State createState() => _QuickCustomerDialogState(); } class _QuickCustomerDialogState extends State { late final TextEditingController _nameCtrl; final _phoneCtrl = TextEditingController(); final _emailCtrl = TextEditingController(); final _noteCtrl = TextEditingController(); bool _isBusiness = false; // Aggiungiamo un campo per il tipo di cliente bool _isLoading = false; @override void initState() { super.initState(); // Prendiamo tutta la stringa nuda e cruda! _nameCtrl = TextEditingController(text: widget.initialQuery.trim()); } @override void dispose() { _nameCtrl.dispose(); _phoneCtrl.dispose(); _emailCtrl.dispose(); _noteCtrl.dispose(); super.dispose(); } Future _save() async { final NavigatorState navigator = Navigator.of(context); if (_nameCtrl.text.isEmpty) return; setState(() => _isLoading = true); // Chiamata al Cubit (aggiorna i parametri in base a come li hai definiti) final newCustomer = await context .read() .quickCreateCustomer( isBusiness: _isBusiness, name: _nameCtrl.text.trim(), phone: _phoneCtrl.text.trim(), email: _emailCtrl.text.trim(), ); setState(() => _isLoading = false); if (context.mounted) { navigator.pop(newCustomer); // Restituiamo il cliente creato } } @override Widget build(BuildContext context) { return AlertDialog( title: const Text("Nuovo Cliente Rapido"), content: SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, children: [ SingleChildScrollView( scrollDirection: Axis.horizontal, child: Row( children: [ ChoiceChip( label: const Text('Privato (Domestico)'), selected: _isBusiness == false, selectedColor: Colors.blue.withValues(alpha: 0.2), checkmarkColor: Colors.blue.shade700, onSelected: (selected) { if (selected) { setState(() { _isBusiness = false; }); } }, ), const SizedBox(width: 12), ChoiceChip( label: const Text('Business (P.IVA)'), selected: _isBusiness == true, selectedColor: Colors.orange.withValues(alpha: 0.2), checkmarkColor: Colors.orange.shade700, onSelected: (selected) { if (selected) { setState(() { _isBusiness = true; }); } }, ), ], ), ), const Divider(height: 32), TextField( controller: _nameCtrl, autofocus: true, // Focus immediato! decoration: const InputDecoration( labelText: "Nome / Ragione Sociale *", ), textInputAction: TextInputAction.next, ), const SizedBox(height: 8), TextField( controller: _phoneCtrl, decoration: const InputDecoration(labelText: "Telefono"), keyboardType: TextInputType.phone, textInputAction: TextInputAction.next, ), const SizedBox(height: 8), TextField( controller: _emailCtrl, decoration: const InputDecoration(labelText: "Email"), keyboardType: TextInputType.emailAddress, textInputAction: TextInputAction.next, ), const SizedBox(height: 8), TextField( controller: _noteCtrl, decoration: const InputDecoration(labelText: "Note rapide"), maxLines: 2, ), ], ), ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text("Annulla"), ), ElevatedButton( onPressed: _isLoading ? null : _save, child: _isLoading ? const SizedBox( width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2), ) : const Text("Salva e Usa"), ), ], ); } }