<%@page import="oneit.utils.CollectionUtils"%>
<%@page import="oneit.objstore.rdbms.filters.*"%>
<%@page import="java.util.*"%>
<%@page import="oneit.servlets.orm.*"%>
<%@page import="performa.search.*"%>
<%@page import="performa.orm.types.*"%>
<%@page import="performa.utils.*"%>
<%@page import="oneit.security.SecUser"%>
<%@page import="oneit.utils.Debug"%>
<%@page import="performa.orm.*"%>
<%@ 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");
    
    SecUser                 secUser         =   SecUser.getTXUser(transaction);
    CompanyUser             companyUser     =   secUser.getExtension(CompanyUser.REFERENCE_CompanyUser);
    HiringTeam              hiringTeam      =   companyUser.getSelectedTeam();

    if(hiringTeam != job.getHiringTeam())
    {
        response.sendRedirect(WebUtils.getArticleByShortCut(transaction, WebUtils.ADMIN_HOME).getLink(request));
    }

    String                  currentPage     =   WebUtils.getSamePageInRenderMode(request, WebUtils.VIEW_APPLICANTS);
    String                  applicationPage =   WebUtils.getSamePageInRenderMode(request, WebUtils.VIEW_APPLICATION);
    AppSortOption           appSortOpt      =   null;
    ApplicationFilter       appFilter       =   null;
    WorkFlowStage           workFlowStage   =   null;

    SearchApplicant         searchApplicant =   (SearchApplicant) RunSearchExecutorFP.setupExecutor(request, SearchApplicant.REFERENCE_SearchApplicant, true);
    JobApplication[]        applications    =   null;    
    Map<String, String[]>   parameterMap    =   request.getParameterMap();

    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 workflow status
    if(process.getAttribute("WorkFlowStage") != null)
    {
        workFlowStage  =   (WorkFlowStage) process.getAttribute("WorkFlowStage");
    }

    StageType   stageType   =   workFlowStage != null ? workFlowStage.getStageType() : null;

    // handle application filter (showing dropdown)
    if(parameterMap.containsKey("ApplicationFilter") && !request.getParameter("ApplicationFilter").isEmpty())
    {
        appFilter  =   ApplicationFilter.forName((String) request.getParameter("ApplicationFilter"));
    }
    else if(process.getAttribute("ApplicationFilter") != null)
    {
        appFilter  =   (ApplicationFilter) process.getAttribute("ApplicationFilter");
    }
    else
    {
        appFilter   =   ApplicationFilter.ALL_APPLICATIONS;
    }
                
    List<JobApplication>    applicantList   =   Arrays.asList(applications);

    // filter applicants by status
    oneit.utils.filter.Filter<JobApplication>  filter   =   JobApplication.SearchByAll().andWorkFlowStage(new EqualsFilter<>(workFlowStage));

    if(workFlowStage != null)
    {
        applicantList =   new ArrayList<>(CollectionFilter.filter(applicantList, filter));
    }
    
    if(appFilter != ApplicationFilter.ALL_APPLICATIONS)
    {
        applicantList =   job.filterEssentialRequirements(appFilter == ApplicationFilter.HAS_REQ , applicantList);
    }

    if(process.getAttribute("AppView") != null)
    {
        session.setAttribute("AppView", (AppView) process.getAttribute("AppView"));
    }

    AppView                 appView             =   (session.getAttribute("AppView") != null &&  !CollectionUtils.equals(stageType,StageType.INITIAL))? (AppView) session.getAttribute("AppView") : AppView.LIST;
    List<JobApplication>    sortedApplications  =   Utils.getApplicationsSorted(applicantList, appSortOpt);
    
    process.setAttribute("AppSortOption", appSortOpt);
    process.setAttribute("ApplicationFilter", appFilter);
    process.setAttribute("WorkFlowStage", workFlowStage);
    process.setAttribute("AppView", appView);
%>

    <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"  
                                AppView="<%= appView %>"
                                applications="<%= sortedApplications %>"
                                WorkFlowStage="<%= workFlowStage %>"   
                                data="<%= CollectionUtils.EMPTY_MAP%>"/>

            
            <div class="second-part view-app-area">
                <oneit:dynInclude   page="/extensions/adminportal/inc/application_sorting_bar.jsp"  
                                    AppView="<%= appView %>"   
                                    SearchApplicant="<%= searchApplicant %>"   
                                    ShowOrderBy="<%= sortedApplications.size() > 1 %>"
                                    ApplicationFilter="<%= appFilter %>"
                                    WorkFlowStage="<%= workFlowStage %>" 
                                    StageType="<%= stageType %>"
                                    SortOption="<%= appSortOpt %>"
                                    data="<%= CollectionUtils.EMPTY_MAP%>"/>
                
                <%
                    if(appView == AppView.LIST)
                    {
                %>
                        <oneit:dynInclude   page="/extensions/adminportal/inc/view_applicants_list.jsp"  
                                            applications="<%= sortedApplications %>"
                                            currentPage="<%= currentPage %>"
                                            StageType="<%= stageType %>"   
                                            data="<%= CollectionUtils.EMPTY_MAP%>"/>
                <%
                    }
                    else if(appView == AppView.GRID)
                    {   
                %>
                            <oneit:dynInclude   page="/extensions/adminportal/inc/view_applicants_grid.jsp"  
                                                applications="<%= sortedApplications %>"
                                                currentPage="<%= currentPage %>"
                                                data="<%= CollectionUtils.EMPTY_MAP%>"/>
                <%
                    }
                    else
                    {   
                %>
                            <oneit:dynInclude   page="/extensions/adminportal/inc/view_applicants_shortlist_grid.jsp"  
                                                applications="<%= sortedApplications %>"
                                                currentPage="<%= currentPage %>"
                                                data="<%= CollectionUtils.EMPTY_MAP%>"/>
                
                <%
                    }
                %>
            </div>
        </div>
    </oneit:form>
</oneit:dynIncluded>