import 'package:flutter/material.dart'; import 'package:get/get.dart'; import '../models/filament_model.dart'; import '../services/appwrite_service.dart'; import '../widgets/add_filament_dialog.dart'; class DetailController extends GetxController { final appwriteService = AppwriteService(); /// Das aktuell angezeigte Filament – wird beim Öffnen der Seite gesetzt. final filament = Rx(FilamentModel.empty); final isLoading = false.obs; @override void onInit() { super.onInit(); // Filament kommt via Get.arguments von der HomeView if (Get.arguments != null && Get.arguments is FilamentModel) { filament.value = Get.arguments as FilamentModel; } } @override void onReady() {} @override void onClose() {} double get remaining => (filament.value.weight - filament.value.weightUsed) .clamp(0, filament.value.weight); double get progress => filament.value.weight > 0 ? remaining / filament.value.weight : 0; /// Öffnet den Bearbeitungs-Dialog und aktualisiert lokal + in Appwrite. Future openEditDialog() async { final updated = await AddFilamentDialog.showEdit(entry: filament.value); if (updated == null) return; isLoading.value = true; try { final success = await appwriteService.updateFilamentDocument( updated.documentId, FilamentModel.toMapForAppwrite(updated), ); if (success) filament.value = updated; // bleibt auf Detail-Seite } catch (e) { print('Fehler beim Aktualisieren: $e'); } finally { isLoading.value = false; } } /// Löscht das Filament und navigiert zurück. Future deleteFilament() async { final confirm = await Get.dialog( AlertDialog( backgroundColor: const Color(0xFF1A2035), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), title: const Text( 'Filament löschen?', style: TextStyle(color: Colors.white), ), content: Text( '${filament.value.name} wird dauerhaft gelöscht.', style: const TextStyle(color: Colors.white70), ), actions: [ TextButton( onPressed: () => Get.back(result: false), child: const Text( 'Abbrechen', style: TextStyle(color: Colors.white60), ), ), TextButton( onPressed: () => Get.back(result: true), child: const Text( 'Löschen', style: TextStyle(color: Color(0xFFFF6B6B)), ), ), ], ), ); if (confirm != true) return; isLoading.value = true; try { await appwriteService.deleteFilamentDocument(filament.value.documentId); } catch (e) { print('Fehler beim Löschen: $e'); } finally { isLoading.value = false; } Get.back(result: 'deleted'); } /// Aktualisiert nur das verbrauchte Gewicht. Future updateWeightUsed(double newWeightUsed) async { final updated = FilamentModel( documentId: filament.value.documentId, name: filament.value.name, type: filament.value.type, color: filament.value.color, weight: filament.value.weight, weightUsed: newWeightUsed, price: filament.value.price, manufacturer: filament.value.manufacturer, purchaseDate: filament.value.purchaseDate, notes: filament.value.notes, printingTemp: filament.value.printingTemp, bedTemp: filament.value.bedTemp, ); isLoading.value = true; try { final success = await appwriteService.updateFilamentDocument( updated.documentId, FilamentModel.toMapForAppwrite(updated), ); if (success) filament.value = updated; } catch (e) { print('Fehler beim Aktualisieren des Gewichts: $e'); } finally { isLoading.value = false; } } }