<%@ page extends="oneit.servlets.jsp.JSPInclude" %>

<%@ include file="/inc/stdimports50.jsp" %><%-- This is in cougar --%>
<%@ include file="/inc/stdcms.jsp" %><%-- This is in cougar --%>
<%@ include file="/extensions/performa/inc/stdimports.jsp" %>
<%@page import="oneit.utils.text.FormatUtils"%>
<%@ page import="oneit.utils.filter.*"%>

<oneit:dynIncluded>
<%   
    Job job =   (Job) process.getAttribute("Job");

    if(job == null && request.getParameter("JobID") != null)
    {
       job  =  Job.getJobByID(transaction, Long.parseLong(request.getParameter("JobID")));
       
       process.setAttribute("Job", job);
    }
    
    Debug.assertion(job != null, "Job is null in admin portal view applicants");
    
    String                  currentPage     =   WebUtils.getSamePageInRenderMode(request, WebUtils.VIEW_APPLICANTS);
    String                  applicationPage =   WebUtils.getSamePageInRenderMode(request, WebUtils.VIEW_APPLICATION);
    AppSortOption           appSortOpt      =   null;
    ApplicationStatus       appStatus       =   null;
    int                     tabNumber       =   process.getAttribute("TabNumber") != null ? (int) process.getAttribute("TabNumber") : 2;
    boolean                 showList        =   process.getAttribute("ShowList") != null ? (boolean) process.getAttribute("ShowList") : true;
    SearchApplicant         searchApplicant =   (SearchApplicant) RunSearchExecutorFP.setupExecutor(request, SearchApplicant.REFERENCE_SearchApplicant, true);
    JobApplication[]        applications    =   null;    
    Map<String, String[]>   parameterMap    =   request.getParameterMap();
    boolean                 isApplicantPage =   tabNumber == 2;

    searchApplicant.setJob(job);

    if(applications == null)
    {
        applications    =   (JobApplication[]) searchApplicant.doSearch();
    }

    if (getData (request, GenericObjSearchDF.GENERIC_SEARCH_RESULTS) != null)
    {
        applications = (JobApplication[])getData (request, GenericObjSearchDF.GENERIC_SEARCH_RESULTS);
    }

    process.setAttribute("JobApplications", applications);

    // handle application sort option
    if( request.getParameter("AppSortOption") != null)
    {
        appSortOpt  =   AppSortOption.forName((String) request.getParameter("AppSortOption"));
    }
    else if(process.getAttribute("AppSortOption") != null)
    {
        appSortOpt  =   (AppSortOption) process.getAttribute("AppSortOption");
    }
    else
    {
        appSortOpt  =    AppSortOption.RANK;
    }

    // handle application status
    if(isApplicantPage &&  parameterMap.containsKey("ApplicationStatus"))
    {
        if(!request.getParameter("ApplicationStatus").isEmpty())
        {
            appStatus  =   ApplicationStatus.forName((String) request.getParameter("ApplicationStatus"));
        }
    }
    else if(process.getAttribute("ApplicationStatus") != null)
    {
        appStatus  =   (ApplicationStatus) process.getAttribute("ApplicationStatus");
    }
                
    List<JobApplication>    applicantList   =   Arrays.asList(applications);
    WorkFlow                workflow        =   job.getWorkFlowBySortOrder(tabNumber);

    // filter applicants by status
    oneit.utils.filter.Filter<JobApplication>  filter           =   JobApplication.SearchByAll().andApplicationStatus(new EqualsFilter<>(isApplicantPage ? appStatus : workflow.getApplicationStatus()));
    oneit.utils.filter.Filter<JobApplication>  applicantFilter  =   JobApplication.SearchByAll().andApplicationStatus(new NotEqualsFilter<>(ApplicationStatus.DRAFT));

    // if Applicant Status, then show all application completed applicants
    applicantList =   new ArrayList<>(CollectionFilter.filter(applicantList, appStatus == null && isApplicantPage ? applicantFilter : filter));

    List<JobApplication>    sortedApplications  =   Utils.getApplicationsSorted(applicantList, appSortOpt);
    
    process.setAttribute("AppSortOption", appSortOpt);
    process.setAttribute("ApplicationStatus", appStatus);
    process.setAttribute("TabNumber", tabNumber);
    process.setAttribute("ShowList", showList);
%>

    <oneit:form name="viewApplicants" method="post"    enctype="multipart/form-data">
        
        <div class="dashboard-content-area">
            <oneit:dynInclude   page="/extensions/adminportal/inc/applicant_view_tabs.jsp"  
                                ApplicationStatus="<%= workflow.getApplicationStatus() %>"   
                                data="<%= CollectionUtils.EMPTY_MAP%>"/>

            
            <div class="second-part view-app-area">
                <oneit:dynInclude   page="/extensions/adminportal/inc/application_sorting_bar.jsp"  
                                    ShowList="<%= showList %>"   
                                    SearchApplicant="<%= searchApplicant %>"   
                                    showOrderBy="<%= sortedApplications.size() > 1 %>"
                                    appStatus="<%= appStatus %>"
                                    workflowStatus="<%= workflow.getApplicationStatus() %>"
                                    sortOption="<%= appSortOpt %>"
                                    data="<%= CollectionUtils.EMPTY_MAP%>"/>

                <%
                    if(showList)
                    {
                %>
                        <oneit:dynInclude   page="/extensions/adminportal/inc/view_applicants_list.jsp"  
                                            applications="<%= sortedApplications %>"
                                            currentPage="<%= currentPage %>"
                                            ApplicationStatus="<%= workflow.getApplicationStatus() %>"   
                                            data="<%= CollectionUtils.EMPTY_MAP%>"/>
                <%
                    }
                    else
                    {   
                        if(workflow.getApplicationStatus() == ApplicationStatus.SHORTLISTED)
                        {
                %>
                            <oneit:dynInclude   page="/extensions/adminportal/inc/view_applicants_shortlist_grid.jsp"  
                                                applications="<%= sortedApplications %>"
                                                currentPage="<%= currentPage %>"
                                                data="<%= CollectionUtils.EMPTY_MAP%>"/>
                <%
                        }
                        else
                        {   
                %>
                            <oneit:dynInclude   page="/extensions/adminportal/inc/view_applicants_grid.jsp"  
                                                applications="<%= sortedApplications %>"
                                                currentPage="<%= currentPage %>"
                                                data="<%= CollectionUtils.EMPTY_MAP%>"/>
                
                <%
                        }
                    }
                %>
            </div>
        </div>
    </oneit:form>
</oneit:dynIncluded>