<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.constants.RoleConstants" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.types.User" %>
<%@ page import="ro.emid.common.utils.MenuConstants" %>
<%@ page import="ro.emid.common.utils.UserUtils" %>
<%@ page import="ro.emid.common.utils.Util" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationData" %>
<%@ page import="ro.emid.common.utils.sort.SortController" %>
<%@ page import="ro.emid.common.utils.sort.SortData" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationController" %>
<%@ page import="ro.emid.common.constants.UserConstants" %>

<%
    /**
     * @see ro.emid.administrare.action.user.ShowUsersAction
     */
    final SortData sd = (SortData) request.getAttribute("sortData");
    final PaginationData pd = (PaginationData) request.getAttribute("paginationData");

    String username = (String)request.getAttribute("username");
    String reqAcces = (String)request.getAttribute("reqAcces");

    User[] listUsers = (User[]) request.getAttribute("users");
%>

<form action="<%=AppConstants.APP_NAME%>?action=show-users" method="post">
<%=SortController.initSort(sd)%>

<table width="100%" border=0>
    <tr><td>
        <table class="gsaptd" width="100%">
          <tr><td>
              Utilizator: <input type="text" name="SHOW_USERI.username" value="<%=Util.format( username)%>">
              &nbsp;
              Acces aplicatie:
              <select name="acces">
                  <option name="acces" <%= reqAcces == null || reqAcces.trim().length() == 0 ? "selected=\"selected\"" : ""%> value="">Selectati</option>
                  <option name="acces" <%= reqAcces != null && reqAcces.equals("ie") ? "selected=\"selected\"" : ""%> value="ie">Incidente</option>
                  <option name="acces" <%= reqAcces != null && reqAcces.equals("cc") ? "selected=\"selected\"" : ""%> value="cc">Callcenter / Deranjamente</option>
                  <option name="acces" <%= reqAcces != null && reqAcces.equals("lp") ? "selected=\"selected\"" : ""%> value="lp">Lucrari programate</option>
                  <option name="acces" <%= reqAcces != null && reqAcces.equals("od") ? "selected=\"selected\"" : ""%> value="od">Operatiuni Dispecer</option>
                  <option name="acces" <%= reqAcces != null && reqAcces.equals("adm") ? "selected=\"selected\"" : ""%> value="adm">Administrare</option>
              </select>

              <input type="submit" value="Caută">
          </td></tr>
        </table>
    </td></tr>
    <tr><td>
        <table cellpadding=2 cellspacing=1 width="100%" class="table">
            <thead>
                <tr>
                    <th height=25 nowrap width="10">Nr.</th>
                    <th nowrap>
                        <%=SortController.printSortableColumn("Utilizator", UserConstants.SQL_USERNAME, sd)%>
                    </th>
                    <th nowrap>
                        <%=SortController.printSortableColumn("Nume prenume", UserConstants.SQL_FULLNAME, sd)%>
                    </th>
                    <th nowrap>Incidente</th>
                    <th>Call center/ Deranjamente</th>
                    <th>Lucrari Programate</th>
                    <th>Operatiuni dispecer</th>
                    <th nowrap>Administrare</th>
                    <th nowrap width="40">&nbsp;</th>
                </tr>
            </thead>
            <tbody>
            <% if( listUsers.length == 0){ %>
                <tr ><td colspan="10" class="no-data">Nu exista date ...</td></tr>
            <%}else for (int i=0; i < listUsers.length; i++) {
                    final User useru = listUsers[i];
            %>

                    <tr ondblclick="window.location = '<%=AppConstants.APP_NAME%>?action=view-user&id=<%=useru.getId()%>';">
                        <td align="center"><%=pd.getStartIndex() + i%></td>
                        <td>
                            <a class="link" href="<%=AppConstants.APP_NAME%>?action=view-user&id=<%=useru.getId()%>"><%=useru.getUsername()%></a>
                        </td>
                        <td><%=useru.getFullName()%></td>

                        <td nowrap>
                        <% if(UserUtils.hasAccessIncidente( useru)){%>
                        <%=RoleConstants.getRoleName(useru.getIeRole())%>
                        <% } else { %>  - <% } %>
                        </td>
                        <td nowrap>
                            <% if(UserUtils.hasAccessCallcenter( useru) || UserUtils.hasAccessDeranjamente(useru))  {%>
                            <%=RoleConstants.getRoleName(useru.getCcRole())%>
                            <% } else { %>  - <% } %>
                        </td>
                        <td nowrap>
                            <% if(UserUtils.hasAccessLucrariProgramate( useru)){%>
                            <%=RoleConstants.getRoleName((int)useru.getLpRole()) %>
                            <% } else { %>  - <% } %>
                        </td>
                        <td nowrap>
                            <% if(UserUtils.hasAccessOperatiuniDispecer( useru)){%>
                            <%=RoleConstants.getRoleName((int)useru.getOdRole())%>
                            <% } else { %>  - <% } %>
                        </td>
                        <td nowrap>
                            <% if(UserUtils.hasAccessAdministrare( useru)){%>
                                x
                            <% } else { %>  - <% } %>
                        </td>

                        <td align="center">
                            <nobr>
                            <%--<a class="link" href="<%=AppConstants.APP_NAME%>?action=view-user&id=<%=useru.getId()%>">Detalii</a>--%>
                            <a class="link" onclick="return confirm('Stergeti utilizatorul \'<%=ro.emid.common.utils.Util.escapeToJs( useru.getUsername())%>\' ?')" href="<%=AppConstants.APP_NAME%>?action=delete-user&id=<%=useru.getId()%>">Şterge</a>
                            </nobr>
                        </td>
                    </tr>
                <% }%>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="20">
                    <div style="float: left">
                        <input type="button" data-href="<%=AppConstants.APP_NAME%>?action=edit-user&id=-1" value="Adaugă utilizator"/>
                    </div>
                    <div style="float: right;">
                        <%=PaginationController.printPaginationData(pd)%>
                    </div>
                    </td>
                </tr>
            </tfoot>
        </table>
    </td>
  </tr>                                 
</table>
</form>
