<%@ page import="com.atlassian.jira.ManagerFactory,
                 com.atlassian.seraph.auth.DefaultAuthenticator,
                 com.opensymphony.user.User,
                 com.opensymphony.util.TextUtils,
                 org.ofbiz.core.entity.GenericValue,
                 javax.servlet.http.HttpSession,
                 java.util.Collection,
                 java.util.Iterator"%>
<%@ taglib uri="webwork" prefix="webwork" %>

<%-- if you change something here - make sure you also change it in issuepicker.jsp --%>
<html>
<head><title><webwork:text name="'popups.savedfilters.title'"/></title></head>
<body>
<%
    // TODO: Reuse ProjectActionSupport.getSavedFilters() (JT)
    HttpSession httpSession = request.getSession(false);
    User u;

    // no session or no user
    if (httpSession == null || (u = (User) httpSession.getAttribute(DefaultAuthenticator.LOGGED_IN_KEY)) == null)
    {
%><p><webwork:text name="'popups.notloggedin'"/></p><%
    }
    else
    {
        Collection savedFilters = ManagerFactory.getSearchRequestManager().getVisibleRequests(u);
        if (savedFilters == null || savedFilters.isEmpty())
        {
%><p><webwork:text name="'popups.savedfilters.nosavedfilters'"/></p><%
        }
        else
        {
%><p><webwork:text name="'popups.savedfilters.choose'">
    <webwork:param name="'value0'"><a href="#" onClick="javascript:window.opener.location='<%= request.getContextPath() %>/secure/ManageFilters.jspa'; window.close();"></webwork:param>
    <webwork:param name="'value1'"></a></webwork:param>
</webwork:text></p>
<script language="JavaScript" type="text/javascript">
<!--
// Provide hover and click effect to entire table rows.
// Usage:
// <table class="grid">
//   <tr href="somelink.jsp" onmouseover="rowHover(this)">
//   ...

function rowHover(row)
{
    row.oldClassName = row.className;
    row.className = 'rowHover';
    row.onmouseout = function()
    {
        this.className = this.oldClassName;
    }
}
function openRequestFilter(filterId)
{
   if(window.opener != null)
   {
       window.opener.location='<%= request.getContextPath() %>/secure/IssueNavigator.jspa?mode=hide&requestId=' + filterId;
       window.close();
       return false;
   }
   else
   {
       return true;
   }
}
	//-->

</script>
<table class="grid" width="100%">
    <tr>
        <th nowrap><webwork:text name="'popups.savedfilters.filtername'"/></th>
        <th nowrap><webwork:text name="'popups.savedfilters.filterdesc'"/></th>
    </tr>
<%
            boolean alternate = true;
            for (Iterator it = savedFilters.iterator(); it.hasNext(); alternate = !alternate)
            {
                GenericValue gv = (GenericValue) it.next();
                String name = gv.getString("name");
                String description = TextUtils.noNull(gv.getString("description"));
                if ("null".equals(description))
                {
                    description = "";
                }
                Long requestId = gv.getLong("id");
%>
    <tr onmouseover="rowHover(this)"<%= (alternate ? "" : " class=\"rowAlternate\"") %>>
        <td width="1%" nowrap><a href="<%= request.getContextPath() %>/secure/IssueNavigator.jspa?mode=hide&requestId=<%= requestId %>" title="<%= TextUtils.plainTextToHtml(name) %>" onClick="openRequestFilter('<%= requestId %>');"><%= name%></a> &nbsp;</td>
        <td><a href="<%= request.getContextPath() %>/secure/IssueNavigator.jspa?mode=hide&requestId=<%= requestId %>" title="<%= TextUtils.plainTextToHtml(name) %>" onClick="openRequestFilter('<%= requestId %>');"><%= description%></a> &nbsp;</td>
    </tr>
<%
            }
        }
    }
%>
</table>
</body>
</html>