diff --git a/android/app/src/main/java/com/example/petstoremobile/adapters/AdoptionAdapter.java b/android/app/src/main/java/com/example/petstoremobile/adapters/AdoptionAdapter.java index d4eddff1..259bde61 100644 --- a/android/app/src/main/java/com/example/petstoremobile/adapters/AdoptionAdapter.java +++ b/android/app/src/main/java/com/example/petstoremobile/adapters/AdoptionAdapter.java @@ -46,6 +46,7 @@ public class AdoptionAdapter extends RecyclerView.Adapter petList = new ArrayList<>(); private List customerList = new ArrayList<>(); private List storeList = new ArrayList<>(); + private List employeeList = new ArrayList<>(); private final String[] STATUSES = {"Pending", "Completed", "Cancelled"}; @@ -48,6 +52,7 @@ public class AdoptionDetailFragment extends Fragment { private PetViewModel petViewModel; private CustomerViewModel customerViewModel; private StoreViewModel storeViewModel; + private UserViewModel userViewModel; @Override public void onCreate(Bundle savedInstanceState) { @@ -56,6 +61,7 @@ public class AdoptionDetailFragment extends Fragment { petViewModel = new ViewModelProvider(this).get(PetViewModel.class); customerViewModel = new ViewModelProvider(this).get(CustomerViewModel.class); storeViewModel = new ViewModelProvider(this).get(StoreViewModel.class); + userViewModel = new ViewModelProvider(this).get(UserViewModel.class); } @Override @@ -113,6 +119,7 @@ public class AdoptionDetailFragment extends Fragment { loadPets(); loadCustomers(); loadStores(); + loadEmployees(); } /** @@ -179,6 +186,27 @@ public class AdoptionDetailFragment extends Fragment { preselectedStoreId, StoreDTO::getStoreId); } + /** + * Loads the list of employees from the API. + */ + private void loadEmployees() { + userViewModel.getUsers("STAFF", 0, 100).observe(getViewLifecycleOwner(), resource -> { + if (resource.status == Resource.Status.SUCCESS && resource.data != null) { + employeeList = resource.data.getContent(); + refreshEmployeeSpinner(); + } + }); + } + + /** + * Populates the employee selection spinner with data. + */ + private void refreshEmployeeSpinner() { + SpinnerUtils.populateSpinner(requireContext(), binding.spinnerAdoptionEmployee, employeeList, + UserDTO::getFullName, "-- Select Staff --", + preselectedEmployeeId, UserDTO::getId); + } + /** * Handles arguments to determine if the fragment is in edit or add mode. */ @@ -210,12 +238,16 @@ public class AdoptionDetailFragment extends Fragment { preselectedPetId = a.getPetId() != null ? a.getPetId() : -1; preselectedCustomerId = a.getCustomerId() != null ? a.getCustomerId() : -1; preselectedStoreId = a.getSourceStoreId() != null ? a.getSourceStoreId() : -1; + preselectedEmployeeId = a.getEmployeeId() != null ? a.getEmployeeId() : -1; + binding.etAdoptionDate.setText(a.getAdoptionDate()); + binding.etAdoptionFee.setText(a.getAdoptionFee() != null ? a.getAdoptionFee().toString() : ""); SpinnerUtils.setSelectionByValue(binding.spinnerAdoptionStatus, a.getAdoptionStatus()); refreshPetSpinner(); refreshCustomerSpinner(); refreshStoreSpinner(); + refreshEmployeeSpinner(); } else if (resource.status == Resource.Status.ERROR) { Toast.makeText(getContext(), "Failed to load adoption: " + resource.message, Toast.LENGTH_SHORT).show(); } @@ -240,17 +272,36 @@ public class AdoptionDetailFragment extends Fragment { Toast.makeText(getContext(), "Select a date", Toast.LENGTH_SHORT).show(); return; } + BigDecimal fee = BigDecimal.ZERO; + String feeStr = binding.etAdoptionFee.getText().toString().trim(); + if (!feeStr.isEmpty()) { + try { + fee = new BigDecimal(feeStr); + } catch (NumberFormatException e) { + Toast.makeText(getContext(), "Invalid fee format", Toast.LENGTH_SHORT).show(); + return; + } + } + CustomerDTO customer = customerList.get(binding.spinnerAdoptionCustomer.getSelectedItemPosition() - 1); PetDTO pet = petList.get(binding.spinnerAdoptionPet.getSelectedItemPosition() - 1); StoreDTO store = storeList.get(binding.spinnerAdoptionStore.getSelectedItemPosition() - 1); + + Long employeeId = null; + if (binding.spinnerAdoptionEmployee.getSelectedItemPosition() > 0) { + employeeId = employeeList.get(binding.spinnerAdoptionEmployee.getSelectedItemPosition() - 1).getId(); + } + String status = STATUSES[binding.spinnerAdoptionStatus.getSelectedItemPosition()]; AdoptionDTO dto = new AdoptionDTO( pet.getPetId(), customer.getCustomerId(), + employeeId, store.getStoreId(), date, - status + status, + fee ); if (isEditing) { diff --git a/android/app/src/main/res/layout/fragment_adoption_detail.xml b/android/app/src/main/res/layout/fragment_adoption_detail.xml index 0ad14fcf..45ac673f 100644 --- a/android/app/src/main/res/layout/fragment_adoption_detail.xml +++ b/android/app/src/main/res/layout/fragment_adoption_detail.xml @@ -95,6 +95,21 @@ android:layout_height="wrap_content" android:layout_marginBottom="16dp"/> + + + + + + + + + + + +