<%@ page extends="oneit.servlets.jsp.JSPInclude"%>
<%@ include file="/setuprequest.jsp"%>
<%@ include file="inc/stdimports.jsp"%>
<%@ include file="/editor/stdimports.jsp"%>

<%@ page import="oneit.security.jsp.*" %>

<oneit:dynIncluded>
    
<%
    ORMProcessState     process     =   (ORMProcessState) ProcessDecorator.getDefaultProcess(request);
    ObjectTransaction   objTran     =   process.getTransaction ();
    String              currentPage =   request.getContextPath() + "/editor/home.jsp";
    SecUser             secUser     =   SecUser.getTXUser(objTran);
    UserSortOption      userSortOpt =   (UserSortOption) process.getAttribute("UserSortOption");
    SearchHiringTeam    searchHT    =   (SearchHiringTeam) RunSearchExecutorFP.setupExecutor(request, SearchHiringTeam.REFERENCE_SearchHiringTeam, true);
    HiringTeam[]        hiringTeams =   (HiringTeam[]) process.getAttribute("HiringTeams");

    if(hiringTeams == null)
    {
        hiringTeams =   (HiringTeam[]) searchHT.doSearch();
    }

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

    process.setAttribute("HiringTeams", hiringTeams);

    if( request.getParameter("UserSortOption") != null)
    {
        userSortOpt =   UserSortOption.forName((String) request.getParameter("UserSortOption"));
    }
    
    if(userSortOpt == null)
    {
        userSortOpt =   UserSortOption.ALPHA_A_Z;
    }
    
    process.setAttribute("UserSortOption", userSortOpt);

    Collection<HiringTeam> sortedHiringTeams    =   Utils.getHiringTeamsSorted(Arrays.asList(hiringTeams), userSortOpt);

    request.setAttribute("oneit.pageFormDetails", CollectionUtils.mapEntry("name", "Customers").mapEntry("enctype", "multipart/form-data").toMap());
    request.setAttribute("oneit.pageHeaderTitle", "Customers");
%>
    <oneit:css>
        <oneit:css href="/css/common.css" />
    </oneit:css>
    <style>
        .dashboard-content-area {
            margin-top: 0px ; 
            width: 100%; 
        }
    </style>
    <script type="text/javascript">
       $(function(){
            $('.search-icon-btn').click(function () {   
               $('.search-real-btn').click();
            });
        });
    </script>
    <div class="dashboard-content-area second-part">
            
        <div class="jobs-list-shorting">
            <div class="d-job-title all-jobs-title">Customers</div>
            <div class="shorting-dropdown relative-parent">
                    <div class="order-label">Search Customers</div>
                    <oneit:ormInput obj="<%= searchHT %>"    type="text" attributeName="Details"    cssClass="form-control search-input"    id="searchText" />
                    <span class="search-icon-btn"></span>
                </div>
            <oneit:button value="Search" name="search" cssClass="btn btn-primary search-real-btn hidden"  />
            <div class="shorting-dropdown">
                <span class="order-label">order by</span>
                <select class="form-control"    onChange="location=this.value">
                    <%
                        for (UserSortOption  sortOption : UserSortOption.getUserSortOptionArray())
                        {
                            String  optionLink  =    currentPage + "?UserSortOption=" + sortOption.getName() ;
                    %>
                            <option <%= (userSortOpt != null && userSortOpt == sortOption ? "selected" : "" )%>  value="<%= optionLink %>">
                                <oneit:toString value="<%= sortOption.getDescription() %>"   mode="EscapeHTML"/>
                            </option>
                    <%
                        }
                    %>
                </select>
            </div>
        </div>

        <div class="main-client-list">
        <%
            for(HiringTeam hiringTeam : sortedHiringTeams)
            {
                Job[]   filledJobs  =   Job.SearchByAll().andJobStatus(new EqualsFilter<>(JobStatus.FILLED))
                                                                .andHiringTeam(new EqualsFilter<>(hiringTeam))
                                                                .search(objTran);
                int     filledCount =   filledJobs != null ? filledJobs.length : 0;

                Job[]   openJobs    =   Job.SearchByAll().andJobStatus(new EqualsFilter<>(JobStatus.OPEN))
                                                                .andHiringTeam(new EqualsFilter<>(hiringTeam))
                                                                .search(objTran);
                int     openCount   =   openJobs != null ? openJobs.length : 0;
                int     clientCount =   hiringTeam.pipelineHiringTeam().toClients().uniqueVals().size();
        %>
                <div class="client-list"   id="<%= hiringTeam.getID() %>">
                    <div class="client-row"  >
                        <div class="client-name-cell jl-c" style="width:50%;">
                            <div class="client-name">
                                    <oneit:toString value="<%= hiringTeam.getHiringTeamName() %>" mode="EscapeHTML" />
                            </div>
                        </div>
                    </div>
                    <div class="<%= "application-count jl-c " + (clientCount == 0 ? "opaque-jobcount" : "")%>">
                        <oneit:toString value="<%= clientCount %>" mode="EscapeHTML"   nullValue="0"/>
                        <span class="grey-span">Clients</span>
                    </div>
                    <div class="<%= "application-count jl-c " + (openCount == 0 ? "opaque-jobcount" : "")%>">
                        <oneit:toString value="<%= openCount %>" mode="EscapeHTML"   nullValue="0"/>
                        <span class="grey-span">Open Jobs</span>
                    </div>
                    <div class="<%= "application-count jl-c " + (filledCount == 0 ? "opaque-jobcount" : "")%>">
                        <oneit:toString value="<%= filledCount %>" mode="EscapeHTML"   nullValue="0"/>
                        <span class="grey-span">Jobs Filled</span>
                    </div>
                    <div class="application-count jl-c"  style="width:20%;">
                        <oneit:button   value="Login as Admin" name="assume"  cssClass="btn btn-primary loginAsAdmin"
                                        requestAttribs="<%= CollectionUtils.mapEntry("HiringTeam", hiringTeam)
                                                                            .toMap()%>"/>
                    </div>
                </div>


        <%
            }
        %>
        </div>
    </div>
</oneit:dynIncluded>