<script language="JavaScript" src="../scripts/ESR-Script.js"></script>
<%@ taglib uri="/tags/esr-core" prefix="esrcore" %>
<%@ taglib uri="/tags/fw" prefix="fw" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html-el" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean-el" prefix="bean-el" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.ArrayList"%>
<%@ page import="gov.va.med.esr.ui.admin.action.UserSessionsAction"%>

<c:set var="resultsSize"><%= ((ArrayList)request.getAttribute(UserSessionsAction.USER_SESSIONS_REQUEST_KEY)).size() %></c:set>
<c:set var="idleUser" scope="page"><bean-el:message key="label.userSessions.idleUser"/></c:set>
<c:set var="currentUser" scope="page"><bean-el:message key="label.currentUser"/></c:set>
<c:set var="contextPath" scope="page"><%=request.getContextPath()%></c:set>
<c:set var="expandableSectionCollapsed" scope="page"><bean-el:message key="label.expandableSectionCollapsed"/></c:set>

<script language="javascript">
// Displays a confirmation message for the Invalidate and Invalidate All buttons
function invalidateConfirmMessage()
{
    return confirmMessage('<bean-el:message key="message.userSessions.confirmInvalidate"/>');
}
function invalidateAllConfirmMessage()
{
    return confirmMessage('<bean-el:message key="message.userSessions.confirmInvalidateAll"/>');
}
</script>

<table width="100%" border="1" cellpadding="0" cellspacing="0" class="moduleShell">
<html-el:form action="/userSessions" method="post">
  <%@ include file="/admin/csrfgentoken.jsp" %>
<tr>
  <td>
    <table width="100%" border="0" cellpadding="0" cellspacing="0" class="moduleShellTable">
      <tr>
        <td>
          <table width="100%" border="0" cellpadding="0" cellspacing="0" class="moduleShellTableHead">
            <tr>
              <td class="moduleBlueBox"><img alt=""  src="../images/spacer.gif" width="28" height="23"/></td>
              <td nowrap style="padding-left: 7"><bean-el:message key="label.userSessions.activeUserSessions"/>: <c:out value="${resultsSize}"/></td>
              <td width="100%"><img alt=""  src="../images/spacer.gif"></td>
              <%--
              ***** TODO *****
              Add a place for broadcast messages to be entered
    					<td nowrap class="moduleBlueLink" style="padding: 1 8 1 8">
                <fw:link href="." onclick="expandOrCollapse(smBlock_All, smImage_All, smHiddenId_All); return false">
                  <html-el:img alt="${expandableSectionCollapsed}" align="center" border="0" styleId="smImage_All" src="../images/redArrowRight.gif"/><bean-el:message key="label.userSessions.boradcastMessage"/>
                </fw:link>
              </td>
              <td><img alt=""  src="../images/spacer.gif" width="5"/></td>
              --%>
		    			<td nowrap class="moduleBlueLink" style="padding: 1 8 1 8"><fw:link onclick="return(invalidateAllConfirmMessage())" action="userSessions.do?methodName=INVALIDATE+ALL"><bean-el:message key="button.invalidateAll"/></fw:link></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td>
          <table width="100%" border="0" cellspacing="0" cellpadding="0" class="moduleShellTableBody">
            <c:choose>
              <c:when test="${resultsSize == 0}">
                <tr>
                  <td class="moduleLabel" style="padding:10 0 10 10; text-align: left"><bean-el:message key="label.noDataAvailable"/></td>
                </tr>
              </c:when>
              <c:otherwise>
                <tr>
                  <td style="text-align:left; padding: 7 0 7 5; vertical-align: bottom" class="darkLabel"><u>#</u></td>
                  <td style="text-align:left; padding: 7 0 7 5; vertical-align: bottom" class="darkLabel"><u><bean-el:message key="label.userSessions.username"/></u></td>
                  <td style="text-align:left; padding: 7 0 7 5; vertical-align: bottom" class="darkLabel"><u><bean-el:message key="label.userSessions.remoteIpAddress"/></u></td>
                  <td style="text-align:left; padding: 7 0 7 5; vertical-align: bottom" class="darkLabel"><u><bean-el:message key="label.userSessions.createTime"/></u></td>
                  <td style="text-align:left; padding: 7 0 7 5; vertical-align: bottom" class="darkLabel"><u><bean-el:message key="label.userSessions.lastAccessedTime"/></u></td>
                  <td style="text-align:left; padding: 7 0 7 5; vertical-align: bottom" class="darkLabel"><u><bean-el:message key="label.userSessions.maxInactiveInterval"/></u></td>
                  <td style="text-align:left; padding: 7 0 7 5; vertical-align: bottom" class="darkLabel"><u><bean-el:message key="label.userSessions.secondsUntilExpire"/></u></td>
                  <td style="text-align:left; padding: 7 0 7 5; vertical-align: bottom" class="darkLabel"><u><bean-el:message key="label.userSessions.idleSeconds"/></u></td>
                  <td style="text-align:left; padding: 7 0 7 5; vertical-align: bottom" class="darkLabel"><u><bean-el:message key="label.actions"/></u></td>
                </tr>

                <c:set var="currentUser" value="${currentUserRequestKey}" />
                <c:set var="rowColor" value="darkValue" />
                <c:forEach var="userSession" items="${userSessionsRequestKey}" varStatus="status">
                <c:set var="counter" value="${status.count - 1}" />

                <c:choose>
                  <c:when test="${rowColor == 'darkValue'}">
                    <c:set var="rowColor" value="value" />
                  </c:when>
                <c:otherwise>
                  <c:set var="rowColor" value="darkValue" />
                </c:otherwise>
                </c:choose>

                <tr>
                  <td valign="top" style="padding-top: 7; padding-bottom: 7" class="<c:out value="${rowColor}"/>">
                    <c:out value="${status.count}"/>.
                  </td>
                  <td valign="top" style="padding-top: 7; padding-bottom: 7" class="<c:out value="${rowColor}"/>">
                    <esrcore:displayValue name="userSession" property="username"/>
                    <c:if test="${userSession.currentSession == true}">
                      &nbsp;<html-el:img alt="${currentUser}" style="vertical-align: top" border="0" src="${contextPath}/images/home.gif" width="32" height="32"/>
                    </c:if>
                  </td>
                  <td valign="top" style="padding-top: 7; padding-bottom: 7" class="<c:out value="${rowColor}"/>">
                    <esrcore:displayValue name="userSession" property="remoteIpAddress"/>
                  </td>
                  <td valign="top" style="padding-top: 7; padding-bottom: 7" class="<c:out value="${rowColor}"/>" nowrap>
                    <esrcore:displayDate name="userSession" dateProperty="creationTime" displayTime="true"/>
                  </td>
                  <td valign="top" style="padding-top: 7; padding-bottom: 7" class="<c:out value="${rowColor}"/>" nowrap>
                    <esrcore:displayDate name="userSession" dateProperty="lastAccessedTime" displayTime="true"/>
                  </td>
                  <td valign="top" style="padding-top: 7; padding-bottom: 7" class="<c:out value="${rowColor}"/>">
                    <esrcore:displayValue name="userSession" property="maxInactiveInterval"/>
                  </td>
                  <td valign="top" style="padding-top: 7; padding-bottom: 7" class="<c:out value="${rowColor}"/>">
                    <esrcore:displayValue name="userSession" property="secondsUntilExpire"/>
                  </td>
                  <td valign="top" style="padding-top: 7; padding-bottom: 7" class="<c:out value="${rowColor}"/>">
                    <esrcore:displayValue name="userSession" property="idleSeconds"/>
                    <c:if test="${userSession.idle == true}">
                      &nbsp;<html-el:img alt="${idleUser}" style="vertical-align: top" border="0" src="${contextPath}/images/idleClock.gif" width="32" height="32"/>
                    </c:if>
                  </td>
                  <td valign="top" style="padding-top: 7; padding-bottom: 7" class="<c:out value="${rowColor}"/>" nowrap>
                    <c:if test="${userSession.currentSession != true}">
                      <fw:link href="." onclick="expandOrCollapse(smBlock_${counter}, smImage_${counter}, smHiddenId_${counter}); return false">
                        <html-el:img alt="${expandableSectionCollapsed}" align="center" border="0" styleId="smImage_${counter}" src="../images/redArrowRight.gif"/><bean-el:message key="label.userSessions.sendMessage"/>
                      </fw:link> |
                      <input name="smHiddenId_<c:out value='${counter}'/>" id="smHiddenId_<c:out value='${counter}'/>" type="hidden" value="false"/>
                      <fw:link action="userSessions.do?methodName=INVALIDATE&amp;id=${userSession.id}" onclick="return(invalidateConfirmMessage())">
                        <bean-el:message key="label.userSessions.invalidate"/>
                      </fw:link> |
                    </c:if>
                    <fw:link action="editUserAccount.do?methodName=SELECT+PERSON&amp;name=${userSession.username}"><bean-el:message key="label.userSessions.editUser"/></fw:link>
                  </td>
                </tr>

                <tr id="smBlock_<c:out value='${counter}'/>" style="display:none">
                  <td colspan="9" valign="top" style="padding-top: 7; padding-bottom: 7" class="<c:out value="${rowColor}"/>">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                      	<td class="<c:out value="${rowColor}"/>"><bean-el:message key="label.message" />:&nbsp;</td>
                      	<td class="<c:out value="${rowColor}"/>">
                      		<input id="userMessage_<c:out value='${counter}'/>" type="text" class="moduleEdit" size="120" maxlength="255"/>
                      	</td>
                        <td>&nbsp;</td>
                        <td><img alt=""  src="../images/buttonEndLeft.gif"/></td>
                        <td><html-el:button onclick="sendAsyncMessage('${userSession.id}', '${userSession.username}', '${currentUser}', userMessage_${counter})" property="methodName" style="width:50; vertical-align: top;" styleClass="buttonText"><bean-el:message key="button.send"/></html-el:button></td>
                        <td><img alt=""  src="../images/buttonEndRight.gif"/></td>
                        <td width="99%"><img alt=""  src="../images/spacer.gif"/></td>
                      </tr>
                    </table>
                  </td>
                </tr>
                </c:forEach>
              </c:otherwise>
            </c:choose>

            <%-- Button Row --%>
            <tr>
              <td colspan="9" height="50">
                <table width="100%" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td width="50%"><img alt=""  src="../images/spacer.gif"/></td>
                    <td><img alt=""  src="../images/buttonEndLeft.gif"/></td>
                    <td><html-el:submit property="methodName" style="width:50; vertical-align: top;" styleClass="buttonText"><bean-el:message key="button.refresh"/></html-el:submit></td>
                    <td><img alt=""  src="../images/buttonEndRight.gif"/></td>
                    <td width="50%"><img alt=""  src="../images/spacer.gif"/></td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </td>
</tr>
</html-el:form>
</table>
