diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/ChildController.java b/src/main/java/com/bib/essensbestellungsverwaltung/ChildController.java new file mode 100644 index 0000000..0d11b50 --- /dev/null +++ b/src/main/java/com/bib/essensbestellungsverwaltung/ChildController.java @@ -0,0 +1,142 @@ +package com.bib.essensbestellungsverwaltung; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.*; +import org.controlsfx.control.CheckComboBox; + +import java.util.ArrayList; +import java.util.List; + +public class ChildController { + public TextField firstName; + public ChoiceBox childChoiceBox; + public TextField lastName; + public Button kindHinzufügenButton; + public CheckComboBox allergienComboBox; + public Button kindLoeschenButton; + + private Child currentChild = null; + + private long selectedChildId; + + @FXML + public void initialize() { + kindLoeschenButton.setDisable(true); + updateChildChoiceBoxItems(); + + List a = Database.getTable("allergy"); + ObservableList allergies = FXCollections.observableArrayList(); + for (String allergie : a) { + allergies.add(allergie.split(":")[0] + ": " + allergie.split(":")[1]); + } + allergienComboBox.getItems().addAll(allergies); + } + + public void onSelectChild(ActionEvent mouseEvent) { + selectedChildId = Integer.parseInt(childChoiceBox.getValue().toString().split(":")[0]); + System.out.println("Selected Child: " + selectedChildId); + + clearInputs(); + + if(selectedChildId == 0) { + kindHinzufügenButton.setText("Kind Hinzufügen"); + kindLoeschenButton.setDisable(true); + return; + } + + // Get selected child and update fields + currentChild = AccountMgr.getChildById(selectedChildId); + firstName.setText(currentChild.getFirstname()); + lastName.setText(currentChild.getName()); + for (AllergySeverity a : currentChild.getAllergies()){ + allergienComboBox.getCheckModel().check((int) a.getAllergy().getId() - 1); + } + kindHinzufügenButton.setText("Kind updaten"); + kindLoeschenButton.setDisable(false); + } + + public void onKindHinzufügen(ActionEvent actionEvent) { + if(childChoiceBox.getValue() == null){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Ungültige Eingabe"); + alert.setHeaderText("Sie müssen ein Kind auswählen"); + alert.showAndWait(); + return; + } + + if(lastName.getText().isBlank() || firstName.getText().isBlank()){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Ungültige Eingabe"); + alert.setHeaderText("Es wurden nicht alle Felder ausgefüllt"); + alert.showAndWait(); + return; + } + + Address adress = AccountMgr.currentUser.getAddress(); + List allergies = new ArrayList<>(); + allergienComboBox.getCheckModel().getCheckedItems().stream().forEach(a -> { + long id = Integer.parseInt(a.toString().split(":")[0]); + String name = a.toString().split(":")[1].trim(); + // TODO: Allergy Severity + allergies.add(new AllergySeverity(new Allergy(id, name, ""), 2, null)); + }); + + if(selectedChildId == 0){ + // TODO: Create child + long id = AccountMgr.createChild(new Child(lastName.getText(), firstName.getText(), adress, allergies)); + System.out.println("Child created: " + id); + + if(id <= 0){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Es ist ein fehler beim erstellen des Kinds aufgetreten"); + alert.showAndWait(); + return; + } + AccountMgr.matchParentChild(String.valueOf(AccountMgr.currentUser.getId()), String.valueOf(id)); + + childChoiceBox.getItems().add(id + ": " + firstName.getText() + " " + lastName.getText()); + childChoiceBox.setValue(childChoiceBox.getItems().get(childChoiceBox.getItems().size() - 1)); + clearInputs(); + onSelectChild(null); + + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Kind erstellt"); + alert.setHeaderText("Es wurde ein Kind erstellt"); + alert.showAndWait(); + }else { + long id = AccountMgr.updateChild(new Child(currentChild.getId(), lastName.getText(), firstName.getText(), adress, allergies)); + System.out.println("Updated Child: " + id); + } + } + + private void clearInputs(){ + firstName.setText(""); + lastName.setText(""); + allergienComboBox.getCheckModel().clearChecks(); + } + + public void updateChildChoiceBoxItems(){ + List childList = AccountMgr.getAllChildrenFromParentWithId(AccountMgr.currentUser.getId()); + + ObservableList childOptions = FXCollections.observableArrayList(childList.stream().map(c -> c.getId() + ": " + c.getFirstname() + " " + c.getName()).toList().toArray(new String[0])); + childOptions.add(0, "0: neues Kind erstellen"); + childChoiceBox.setItems(childOptions); + } + + public void onKindLoeschen(ActionEvent actionEvent) { + AccountMgr.deleteChildWithId(currentChild.getId()); + System.out.println("Deleted Child: " + currentChild.getId()); + + childChoiceBox.getItems().remove(childChoiceBox.getItems().stream().filter(v -> v.equals(currentChild.getId() + ": " + currentChild.getFirstname() + " " + currentChild.getName())).findFirst().get()); + childChoiceBox.setValue(childChoiceBox.getItems().get(1)); + + onSelectChild(null); + + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Kind wurde gelöscht"); + alert.showAndWait(); + } +} diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/WorkerMenuController.java b/src/main/java/com/bib/essensbestellungsverwaltung/WorkerMenuController.java index 42d7bcb..a9a854d 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/WorkerMenuController.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/WorkerMenuController.java @@ -12,7 +12,6 @@ import java.io.IOException; public class WorkerMenuController { @FXML public BorderPane contentView; - @FXML Button tagesbestellungButton; @FXML diff --git a/src/main/resources/com/bib/essensbestellungsverwaltung/child-view.fxml b/src/main/resources/com/bib/essensbestellungsverwaltung/child-view.fxml index dab0830..5c0893d 100644 --- a/src/main/resources/com/bib/essensbestellungsverwaltung/child-view.fxml +++ b/src/main/resources/com/bib/essensbestellungsverwaltung/child-view.fxml @@ -1,48 +1,32 @@ - - - - - - - - - - - + + + + + - + - + - + - + - - - - - - - - - - @@ -50,44 +34,16 @@ - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -95,6 +51,19 @@ - - - - - - + - - + - + - - +
- +