import 'package:flutter/material.dart';
import '../../../app/theme.dart';
import '../../../core/identifiers/uuid.dart';
import '../../../shared/widgets/empty_state.dart';
import '../../../core/sync/master_data_sync_repository.dart';
import '../data/customer_store.dart';
class CustomersScreen extends StatelessWidget {
const CustomersScreen({super.key});
Future<void> createCustomer(
BuildContext context, [
Customer? existing,
]) async {
final name = TextEditingController(text: existing?.name);
final contact = TextEditingController(text: existing?.contact);
final email = TextEditingController(text: existing?.email);
final phone = TextEditingController(text: existing?.phone);
final address = TextEditingController(text: existing?.address);
final saved = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: Text(existing == null ? 'Neuer Kunde' : 'Kunde bearbeiten'),
content: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 520),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_field(name, 'Firma / Name', Icons.business_outlined),
const SizedBox(height: 12),
_field(
contact,
'Ansprechpartner',
Icons.person_outline_rounded,
),
const SizedBox(height: 12),
_field(email, 'E-Mail', Icons.mail_outline_rounded),
const SizedBox(height: 12),
_field(phone, 'Telefon', Icons.phone_outlined),
const SizedBox(height: 12),
_field(address, 'Adresse', Icons.location_on_outlined),
],
),
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Abbrechen'),
),
FilledButton(
onPressed: () =>
name.text.trim().isEmpty ? null : Navigator.pop(context, true),
child: const Text('Kunde speichern'),
),
],
),
);
if (saved == true) {
final customer = Customer(
id: existing?.id ?? newUuid(),
name: name.text.trim(),
contact: contact.text.trim(),
email: email.text.trim(),
phone: phone.text.trim(),
address: address.text.trim(),
);
if (existing == null) {
await CustomerStore.instance.add(customer);
} else {
await CustomerStore.instance.update(customer);
}
}
for (final controller in [name, contact, email, phone, address]) {
controller.dispose();
}
}
static Widget _field(
TextEditingController controller,
String label,
IconData icon,
) => TextField(
controller: controller,
decoration: InputDecoration(
labelText: label,
prefixIcon: Icon(icon),
border: const OutlineInputBorder(),
),
);
Future<void> deleteCustomer(BuildContext context, Customer customer) async {
final confirmed = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Kunde löschen?'),
content: Text(
'${customer.name} wird dauerhaft entfernt. Bestehende Aufträge und Nachweise bleiben erhalten.',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Abbrechen'),
),
FilledButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Löschen'),
),
],
),
);
if (confirmed == true) await CustomerStore.instance.remove(customer.id);
}
@override
Widget build(BuildContext context) => ListenableBuilder(
listenable: CustomerStore.instance,
builder: (context, _) {
final customers = CustomerStore.instance.customers;
if (customers.isEmpty) {
return Padding(
padding: const EdgeInsets.all(24),
child: EmptyState(
icon: Icons.people_outline_rounded,
title: 'Noch keine Kunden',
description:
'Speichere Kundendaten einmal und verwende sie anschließend für Aufträge und Nachweise.',
actionLabel: 'Kunde anlegen',
onPressed: () => createCustomer(context),
),
);
}
return SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 900),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Kunden',
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(height: 6),
Text(
'${customers.length} lokal gespeichert',
style: Theme.of(context).textTheme.bodyLarge,
),
],
),
),
FilledButton.icon(
onPressed: () => createCustomer(context),
icon: const Icon(Icons.person_add_alt_rounded),
label: const Text('Neu'),
),
],
),
const SizedBox(height: 24),
for (final customer in customers) ...[
Card(
child: ListTile(
onTap: () => createCustomer(context, customer),
contentPadding: const EdgeInsets.all(20),
leading: const CircleAvatar(
backgroundColor: AppColors.canvas,
child: Icon(
Icons.business_outlined,
color: AppColors.green,
),
),
title: Text(
customer.name,
style: const TextStyle(fontWeight: FontWeight.w800),
),
subtitle: Padding(
padding: const EdgeInsets.only(top: 6),
child: Text(
'${[customer.contact, customer.email, customer.phone].where((value) => value.isNotEmpty).join(' · ')}\n${customer.syncStatus.label}${customer.syncError == null ? '' : ': ${customer.syncError}'}',
),
),
trailing: PopupMenuButton<String>(
onSelected: (action) => action == 'edit'
? createCustomer(context, customer)
: deleteCustomer(context, customer),
itemBuilder: (_) => const [
PopupMenuItem(
value: 'edit',
child: Text('Bearbeiten'),
),
PopupMenuItem(
value: 'delete',
child: Text('Löschen'),
),
],
),
),
),
const SizedBox(height: 12),
],
],
),
),
),
);
},
);
}
