2026-04-10 11:11:55 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flux/core/widgets/flux_text_field.dart';
|
|
|
|
|
import 'package:flux/features/customers/models/customer_model.dart'; // Uso il tuo widget!
|
|
|
|
|
|
|
|
|
|
class CustomerForm extends StatefulWidget {
|
|
|
|
|
final CustomerModel? customer; // Se presente, siamo in modalità "Modifica"
|
|
|
|
|
final Function(CustomerModel customer) onSave;
|
|
|
|
|
|
|
|
|
|
const CustomerForm({
|
|
|
|
|
super.key,
|
|
|
|
|
this.customer, // Opzionale
|
|
|
|
|
required this.onSave,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<CustomerForm> createState() => _CustomerFormState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _CustomerFormState extends State<CustomerForm> {
|
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
|
|
|
|
|
|
// Controller inizializzati con i dati del cliente (se presenti)
|
|
|
|
|
late final TextEditingController _nomeController;
|
|
|
|
|
late final TextEditingController _telefonoController;
|
|
|
|
|
late final TextEditingController _emailController;
|
|
|
|
|
late final TextEditingController _noteController;
|
|
|
|
|
late bool _nonDisturbare;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
// Se widget.customer è null, i campi saranno vuoti
|
2026-05-04 15:36:42 +02:00
|
|
|
_nomeController = TextEditingController(text: widget.customer?.name ?? '');
|
2026-04-10 11:11:55 +02:00
|
|
|
_telefonoController = TextEditingController(
|
2026-05-04 15:36:42 +02:00
|
|
|
text: widget.customer?.phoneNumber ?? '',
|
2026-04-10 11:11:55 +02:00
|
|
|
);
|
|
|
|
|
_emailController = TextEditingController(
|
|
|
|
|
text: widget.customer?.email ?? '',
|
|
|
|
|
);
|
|
|
|
|
_noteController = TextEditingController(text: widget.customer?.note ?? '');
|
2026-05-04 15:36:42 +02:00
|
|
|
_nonDisturbare = widget.customer?.doNotDisturb ?? false;
|
2026-04-10 11:11:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_nomeController.dispose();
|
|
|
|
|
_telefonoController.dispose();
|
|
|
|
|
_emailController.dispose();
|
|
|
|
|
_noteController.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _submit() {
|
|
|
|
|
if (_formKey.currentState!.validate()) {
|
|
|
|
|
// Creiamo un nuovo modello partendo da quello esistente (se c'è)
|
|
|
|
|
// o creandone uno da zero, preservando l'ID in caso di modifica.
|
|
|
|
|
final updatedCustomer =
|
|
|
|
|
widget.customer?.copyWith(
|
2026-05-04 15:36:42 +02:00
|
|
|
name: _nomeController.text.trim(),
|
|
|
|
|
phoneNumber: _telefonoController.text.trim(),
|
2026-04-10 11:11:55 +02:00
|
|
|
email: _emailController.text.trim(),
|
|
|
|
|
note: _noteController.text.trim(),
|
2026-05-04 15:36:42 +02:00
|
|
|
doNotDisturb: _nonDisturbare,
|
2026-04-10 11:11:55 +02:00
|
|
|
) ??
|
|
|
|
|
CustomerModel(
|
|
|
|
|
// Caso nuovo cliente
|
2026-05-04 15:36:42 +02:00
|
|
|
name: _nomeController.text.trim(),
|
|
|
|
|
phoneNumber: _telefonoController.text.trim(),
|
2026-04-10 11:11:55 +02:00
|
|
|
email: _emailController.text.trim(),
|
|
|
|
|
note: _noteController.text.trim(),
|
2026-05-04 15:36:42 +02:00
|
|
|
doNotDisturb: _nonDisturbare,
|
2026-04-10 11:11:55 +02:00
|
|
|
companyId: '', // Verrà iniettato dal Bloc o dal chiamante
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
widget.onSave(updatedCustomer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return Form(
|
|
|
|
|
key: _formKey,
|
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
widget.customer == null ? 'Nuovo Cliente' : 'Modifica Cliente',
|
|
|
|
|
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
|
FluxTextField(
|
|
|
|
|
label: 'Nome Completo',
|
2026-04-10 11:34:11 +02:00
|
|
|
autoFocus: true,
|
2026-04-10 11:11:55 +02:00
|
|
|
icon: Icons.person_outline,
|
|
|
|
|
controller: _nomeController,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
FluxTextField(
|
|
|
|
|
label: 'Telefono',
|
|
|
|
|
icon: Icons.phone_android_outlined,
|
|
|
|
|
controller: _telefonoController,
|
|
|
|
|
keyboardType: TextInputType.phone,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
FluxTextField(
|
|
|
|
|
label: 'Email',
|
|
|
|
|
icon: Icons.alternate_email_outlined,
|
|
|
|
|
controller: _emailController,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
FluxTextField(
|
|
|
|
|
label: 'Note',
|
|
|
|
|
icon: Icons.notes_outlined,
|
|
|
|
|
controller: _noteController,
|
2026-04-10 11:34:11 +02:00
|
|
|
minLines: 3,
|
2026-04-10 11:11:55 +02:00
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
SwitchListTile(
|
|
|
|
|
title: const Text('Non disturbare'),
|
|
|
|
|
value: _nonDisturbare,
|
|
|
|
|
onChanged: (v) => setState(() => _nonDisturbare = v),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
|
SizedBox(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
height: 50,
|
|
|
|
|
child: ElevatedButton(
|
|
|
|
|
onPressed: _submit,
|
|
|
|
|
child: Text(widget.customer == null ? 'SALVA' : 'AGGIORNA'),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|