/** handles client-side requests for facility data(usually from ajax javascript)* */
  @RequestMapping(
      value = "/getfacility",
      method = RequestMethod.GET,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody List<String> getFacilty(@RequestParam(value = "province") String province) {
    List<Facility> allFacilities = facilityService.listAllFacilitiesForProvince(province);
    List<String> facilityNames = new ArrayList<String>();
    for (Facility fac : allFacilities) {
      facilityNames.add(fac.getLocalName() + " " + fac.getFacilityType().getReadable());
    }

    return (facilityNames);
  }
  /**
   * Post request that handles the processing of form object(publicStockoutReport) repeats data for
   * /reportStockouts request if form detects errors valid annotation ensures fields in
   * publicStockoutReport have met requirements before being submitted. stockoutreport is processed
   * and submitted from form data. success message is stored as a redirected attribute and sent to
   * reportStockout request upon completion of stockout report submission
   */
  @RequestMapping(value = "/processform", method = RequestMethod.POST)
  public String reportStockout(
      @Valid @ModelAttribute PublicStockoutReport publicStockoutReport,
      BindingResult result,
      RedirectAttributes redirectAttrs,
      Model model) {

    if (result.hasErrors()) {

      DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
      Date date = new Date();
      List<String> provinces = facilityService.listAllProvinces();
      List<MedicineClass> medicineClasses = medicineClassService.getMedicineClassesEagerFetch();
      model.addAttribute("medicineCategories", medicineClasses);
      model.addAttribute("date", dateFormat.format(date));
      model.addAttribute("provinces", provinces);
      String[] selectedMeds = publicStockoutReport.getSelectedMedicines().split(",");
      model.addAttribute("medicineSelection", selectedMeds);

      return "Report_Page";
    }

    System.out.println(publicStockoutReport.getName());
    System.out.println(publicStockoutReport.getDesignation());
    System.out.println(publicStockoutReport.getDateOfOccurence());
    System.out.println(publicStockoutReport.getCellNumber());
    System.out.println(publicStockoutReport.getEmailAddress());
    System.out.println(publicStockoutReport.getReasonForOccurrence());
    System.out.println(publicStockoutReport.getSelectedMedicines());
    System.out.println(publicStockoutReport.getFacilityName());

    Subject reporter = new Subject();
    reporter.setContactNumber(publicStockoutReport.getCellNumber());
    reporter.setDesignation(publicStockoutReport.getDesignation());
    reporter.setEmail(publicStockoutReport.getEmailAddress());

    String[] names = publicStockoutReport.getName().split(" ");

    for (int k = 0; k < names.length - 1; k++) {
      reporter.setName(names[k] + " ");
    }

    if (names.length > 1) {
      reporter.setSurname(names[names.length - 1]);
    }

    Facility facility = facilityService.getClosestMatch(publicStockoutReport.getFacilityName());

    String[] medicine = publicStockoutReport.getSelectedMedicines().split(",");

    for (String med : medicine) {

      Product product = productService.getClosestMatch(med);
      stockoutReportService.submitStockoutReport(
          product.getUid(),
          facility.getUid(),
          reporter,
          publicStockoutReport.getReasonForOccurrence(),
          true);
    }

    redirectAttrs.addFlashAttribute("message", "Stockout Reported");
    redirectAttrs.addFlashAttribute("Name", publicStockoutReport.getName());
    redirectAttrs.addFlashAttribute("Medicine", publicStockoutReport.getSelectedMedicines());

    return "redirect:/sows/reportstockouts";
  }