import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:intl/intl.dart'; import '../controller/abfall_controller.dart'; import '../helper/input_validators.dart'; import 'entry_panel.dart'; class AbfallForm extends StatelessWidget { const AbfallForm({super.key, required this.controller}); final AbfallController controller; @override Widget build(BuildContext context) => Obx(() { final date = controller.datumZeit.value; final busy = controller.isSaving.value; final editing = controller.editingId.value != null; return EntryPanel( title: editing ? 'Abfall bearbeiten' : 'Abfall erfassen', subtitle: 'Erfasse zusätzlichen Abfall. Abfall aus gespeicherten Bauteilen erscheint automatisch in der Übersicht.', child: Form( key: controller.formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ EntryFieldGrid( children: [ OutlinedButton.icon( onPressed: busy ? null : () async { final picked = await showDatePicker( context: context, initialDate: date, firstDate: DateTime(1900), lastDate: DateTime(2100, 12, 31), helpText: 'Datum auswählen', cancelText: 'Abbrechen', confirmText: 'Übernehmen', ); if (picked != null && context.mounted) { controller.setDatum(picked); } }, icon: const Icon(Icons.calendar_today_outlined), label: Text( 'Datum: ${DateFormat('dd.MM.yyyy').format(date)}', ), ), OutlinedButton.icon( onPressed: busy ? null : () async { final picked = await showTimePicker( context: context, initialTime: TimeOfDay.fromDateTime(date), helpText: 'Uhrzeit auswählen', cancelText: 'Abbrechen', confirmText: 'Übernehmen', builder: (context, child) => MediaQuery( data: MediaQuery.of(context) .copyWith(alwaysUse24HourFormat: true), child: child!, ), ); if (picked != null && context.mounted) { controller.setUhrzeit(picked); } }, icon: const Icon(Icons.schedule), label: Text('Uhrzeit: ${DateFormat('HH:mm').format(date)}'), ), ], ), const SizedBox(height: 20), const Text( 'Durchschnittlicher Preis pro kg', style: TextStyle(fontWeight: FontWeight.w600), ), const SizedBox(height: 8), EntryNumberField( controller: controller.kilopreisController, label: 'Kilopreis (optional)', unit: '€/kg', validator: (value) => InputValidators.nonNegative(value, optional: true), onChanged: controller.berechneKosten, ), const SizedBox(height: 16), EntryFieldGrid( children: [ EntryNumberField( controller: controller.gewichtController, onChanged: controller.berechneKosten, label: 'Abfallgewicht', unit: 'g', validator: InputValidators.positive, ), EntryNumberField( controller: controller.kostenController, label: 'Abfallkosten', unit: '€', validator: InputValidators.nonNegative, ), ], ), const SizedBox(height: 8), const Text( 'Abfallkosten sind manuell änderbar. Bei Änderung von Gewicht oder Kilopreis werden sie neu berechnet.', style: TextStyle(fontSize: 12), ), const SizedBox(height: 24), FilledButton.icon( onPressed: busy ? null : controller.saveAbfall, icon: Icon(editing ? Icons.check : Icons.add), label: Text( busy ? 'Wird gespeichert …' : editing ? 'Änderungen speichern' : 'Abfall speichern', ), ), if (editing) TextButton.icon( onPressed: busy ? null : controller.resetForm, icon: const Icon(Icons.close), label: const Text('Bearbeitung abbrechen'), ), EntryMessage( message: controller.message.value, hasError: controller.hasError.value, ), ], ), ), ); }); }