<%
   table_limit = params[:table_limit] unless table_limit
   widget_id = params[:widget_id] unless widget_id

   search_options = params.clone unless search_options
   search_options['pageIndex'] = params[:pageIndex] || 1
   search_options['pageSize'] = table_limit.to_i

   if search_options['filter']
     results = Internal.issues.execute(search_options['filter'].to_i, search_options)
   else
     results = Internal.issues.execute(search_options)
   end
   issues = results.issues()
   paging = results.paging()

   user_logins = Set.new
   comments_by_issue = {}
   comments = Internal.issues.findCommentsByIssueKeys(issues.map {|i| i.key()})
   comments.each do |c|
     user_logins << c.userLogin()
     current_comments = comments_by_issue[c.issueKey()]
     current_comments = [] unless current_comments
     current_comments << c
     comments_by_issue[c.issueKey()] = current_comments
   end

   users = Internal.users_api.find({'logins', user_logins})
   users_by_login = {}
   users.each do |u|
     users_by_login[u.login()] = u
   end
%>

<% if paging.total() == 0 %>
  <span class="empty_widget"><%= message('issue_filter.no_issues') -%></span>

<% else %>
  <table id="issues-widget-list-<%= widget_id -%>" class="data width100">
    <thead>
    <tr>
      <th coslpan="3">
      </th>
    </tr>
    </thead>
    <tbody>
    <%
       issues.each do |issue|
         current_comments = comments_by_issue[issue.key]
         last_comment = current_comments[-1] unless current_comments.nil? || current_comments.empty?
    %>
      <tr class="<%= cycle 'even', 'odd', :name => ('issues-widget-list-' + widget_id) -%>">
        <td><i class="icon-severity-<%= issue.severity.downcase -%>"></i></td>
        <td>
          <a class="js-issue-link" href="<%= url_for_component_issues(@project, search_options.merge({:issues => issue.key}).except('pageSize', 'pageIndex', 'table_limit', 'widget_id')) -%>">
            <%= h truncate(issue.message, :length => 100) -%></a>
          <% if last_comment && last_comment.userLogin() %>
            <div class="comment-excerpt">
              <img src="<%= ApplicationController.root_context -%>/images/reviews/comment.png"/>
              <% commentAuthor = users_by_login[last_comment.userLogin()] %>
              &nbsp;<b><%= h( commentAuthor.nil? ? last_comment.userLogin() : commentAuthor.name() ) -%> :</b>
              <%= Internal.text.markdownToHtml(last_comment.markdownText) -%>
            </div>
          <% end %>
        </td>
        <td class="nowrap">
          <%= distance_of_time_in_words_to_now(Api::Utils.java_to_ruby_datetime(issue.updateDate())) -%>
        </td>
      </tr>
    <%
       end
    %>
    </tbody>
    <%
       link_params = search_options.clone
       link_params[:controller] = 'issue'
       link_params[:action] = 'widget_issues_list'
       link_params[:snapshot_id] = @snapshot.id if @snapshot
       link_params[:table_limit] = table_limit
       link_params[:widget_id] = widget_id
       link_params[:period] = params[:period]
    %>
    <%= paginate_java(paging, :colspan => 3, :id => "issue-filter-foot-#{widget_id}", :include_loading_icon => true,
                      :url_results => url_for_component_issues(@project, search_options.except('pageSize', 'pageIndex', 'table_limit', 'widget_id')) ) { |label, page_id|
<<EOF
<a href="#" onclick="$j.ajax({	url:'#{url_for(link_params.merge({:pageIndex => page_id}))}',	type:'post', success:function(response){$j('#issues-widget-#{widget_id}').html(response);}}); return false;">#{label}</a>
EOF
    } -%>
  </table>

<% end %>
