/**
   * Constructs the Reports Page with all the neccessary Model attributes to pre-load various form
   * elements data*
   */
  @RequestMapping(value = "/reportstockouts", method = RequestMethod.GET)
  public String getReportPage(Model model) {

    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);

    return ("Report_Page");
  }
  /**
   * handles client-side requests for medicine data(usually from ajax javascript) returns a list of
   * product names for populating autocomplete form*
   */
  @RequestMapping(
      value = "/getmedicines",
      method = RequestMethod.GET,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody List<String> getMedicines(
      @RequestParam(value = "medicineClassIndex") int medicineClassIndex) {

    List<String> productDescription = new ArrayList<String>();
    List<MedicineClass> medicineClasses = medicineClassService.getMedicineClassesEagerFetch();
    Set<Medicine> medicineNames = medicineClasses.get(medicineClassIndex).getMedicines();

    for (Medicine med : medicineNames) {
      for (Product prod : med.getProducts()) {

        productDescription.add(prod.getName() + " " + prod.getDescription());
      }
    }

    return (productDescription);
  }
  /**
   * 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";
  }