package org.openmrs.module.radiologyfhirsupport.web.controller;

import org.openmrs.api.context.Context;
import org.openmrs.module.radiologyfhirsupport.MRRTTemplate;
import org.openmrs.module.radiologyfhirsupport.api.MRRTTemplateService;
import org.openmrs.web.controller.PortletController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by devmaany on 18/10/16.
 */
@Controller
@RequestMapping(PatientDashboardRadiologyFHIRSupportTabPortletController.PATIENT_DASHBOARD_PORTLET_CONTROLLER)
public class PatientDashboardRadiologyFHIRSupportTabPortletController extends PortletController {
    public static final String PATIENT_DASHBOARD_PORTLET_CONTROLLER = "**/patientDashboardRadiologyFHIRSupportTab.portlet";
    private MRRTTemplateService mrrtTemplateService=null;
    @Override
    protected void populateModel(HttpServletRequest request, Map<String, Object> model) {
        if(mrrtTemplateService==null)
            mrrtTemplateService = Context.getService(MRRTTemplateService.class);
        List<MRRTTemplate> mrrtTemplates = mrrtTemplateService.getAll();
        model.put("availableTemplates",mrrtTemplates);


    }
}

@@ -81,87 +81,354 @@
 mrs.
-api.context.Context;%0Aimport org.openmrs.module.radiologyfhirsupport.MRRTTemplat
+Encounter;%0Aimport org.openmrs.Patient;%0Aimport org.openmrs.api.EncounterService;%0Aimport org.openmrs.api.PatientService;%0Aimport org.openmrs.api.context.Context;%0Aimport org.openmrs.module.radiologyfhirsupport.MRRTReport;%0Aimport org.openmrs.module.radiologyfhirsupport.MRRTTemplate;%0Aimport org.openmrs.module.radiologyfhirsupport.api.MRRTReportServic
 e;%0Ai
@@ -707,24 +707,52 @@
 letRequest;%0A
+import java.util.ArrayList;%0A
 import java.
@@ -1240,226 +1240,818 @@
     
-@Override%0A    protected void populateModel(HttpServletRequest request, Map%3CString, Object%3E model) %7B%0A        if(mrrtTemplateService==null)%0A            mrrtTemplateService = Context.getService(MRRTTemplateService.class);
+private MRRTReportService mrrtReportService=null;%0A    private PatientService patientService=null;%0A    private EncounterService encounterService=null;%0A    @Override%0A    protected void populateModel(HttpServletRequest request, Map%3CString, Object%3E model) %7B%0A        initServices();%0A%0A        Integer patientId = Integer.parseInt(request.getParameter(%22patientId%22));%0A        Patient patient = patientService.getPatient(patientId);%0A%0A        List%3CEncounter%3E encountersByPatient = encounterService.getEncountersByPatient(patient);%0A%0A        List%3CMRRTReport%3E existingReports = new ArrayList%3CMRRTReport%3E();%0A        for(Encounter encounter: encountersByPatient)%7B%0A            MRRTReport report = mrrtReportService.getByEncounter(encounter);%0A            if(report!=null)%0A                existingReports.add(report);%0A        %7D%0A
 %0A   
@@ -2116,24 +2116,25 @@
 e.getAll();%0A
+%0A
         mode
@@ -2144,45 +2144,627 @@
 ut(%22
-availableTemplates%22,mrrtTemplates);%0A%0A
+reports%22,existingReports);%0A        model.put(%22templates%22,mrrtTemplates);%0A        model.put(%22patient%22,patient);%0A        model.put(%22encounters%22,encountersByPatient);%0A    %7D%0A    private void initServices()%7B%0A        if(mrrtTemplateService==null)%0A            mrrtTemplateService = Context.getService(MRRTTemplateService.class);%0A        if(mrrtReportService==null)%0A            mrrtReportService = Context.getService(MRRTReportService.class);%0A        if(patientService==null)%0A            patientService= Context.getPatientService();%0A        if(encounterService==null)%0A            encounterService=Context.getEncounterService();
 %0A   

update PortletController a nd populate it with modelMap data