<!--!  Copyright (C) 2006-2014 Edgewall Software

  This software is licensed as described in the file COPYING, which
  you should have received as part of this distribution. The terms
  are also available at http://trac.edgewall.com/license.html.

  This software consists of voluntary contributions made by many
  individuals. For the exact contribution history, see the revision
  history and logs, available at http://trac.edgewall.org/.

Arguments:
 - groups    - a dict, where:
                key       - is the value shared by all results in this group
                value     - is the list of corresponding tickets

 - headers   - a sequence of header structure:
                .name     - field name for this header
                .label    - what to display for this header

 - fields    - dict of field name to field structure:
                .label    - field label

 - query     - the actual Query instance used to perform the query
-->
<div xmlns="http://www.w3.org/1999/xhtml"
     xmlns:py="http://genshi.edgewall.org/"
     xmlns:i18n="http://genshi.edgewall.org/i18n"
     xmlns:xi="http://www.w3.org/2001/XInclude">
  <h2 class="report-result" py:if="paginator.has_more_pages">
    Results <span class="numresults">(${paginator.displayed_items()})</span>
  </h2>
  <xi:include py:if="paginator.show_index" href="page_index.html" />
  <py:def function="group_heading(groupname, results)">
    <h2 class="report-result" py:if="groupname is not None"
        i18n:msg="grouplabel, groupname, count"
        py:with="grouplabel = fields[query.group].label;
                 groupname = authorinfo(groupname) if query.group in ['owner', 'reporter'] else (groupname or _('None'));
                 count = ngettext('%(num)s match', '%(num)s matches', len(results))">
      ${grouplabel}: ${groupname} <span class="numrows">(${count})</span>
    </h2>
  </py:def>
  <py:def function="column_headers()">
    <tr class="trac-columns">
      <th py:for="header in headers"
          class="$header.name${(' desc' if query.desc else ' asc') if query.order == header.name else ''}">
        <?python asc = _('(ascending)'); desc = _('(descending)') ?>
        <a title="${_('Sort by %(col)s %(direction)s', col=header.label,
                      direction=(desc if query.order == header.name and not query.desc else asc))}"
           href="$header.href">${header.label}</a>
      </th>
    </tr>
  </py:def>
  ${group_heading(*groups[0]) if groups else None}
  <table class="listing tickets"
         py:with="num_cols = len(headers) + int(batch_modify or 0)">
    <thead py:strip="group_index" class="trac-query-heading">
      ${column_headers()}
    </thead>
    <py:for each="group_index, (groupname, results) in enumerate(groups)">
      <tbody py:if="group_index" class="trac-query-heading">
        <tr py:if="groupname is not None" class="trac-group">
          <th colspan="$num_cols">
            ${group_heading(groupname, results)}
          </th>
        </tr>
        ${column_headers()}
      </tbody>
      <tbody class="trac-query-results">
        <tr py:if="not results" class="even">
          <td colspan="$num_cols">
            No tickets found
          </td>
        </tr>
        <py:for each="idx, result in enumerate(results)">
          <py:with vars="ticket_context = context.child('ticket', result.id)">
            <py:if test="'TICKET_VIEW' in perm(ticket_context.resource)">

              <tr class="${'odd' if idx % 2 else 'even'} prio${result._priority_value}${
                ' added' if 'added' in result else ''}${
                ' changed' if 'changed' in result else ''}${
                ' removed' if 'removed' in result else ''}">
                <py:for each="idx, header in enumerate(headers)" py:choose="">
                  <py:with vars="name = header.name; value = result[name];
                                 wikitext = header.field.type == 'text'
                                            and header.field.format == 'wiki';
                                 wikitextarea = header.field.type == 'textarea'
                                                and header.field.format == 'wiki';">
                    <td py:when="name == 'id'" class="id"><a href="$result.href" title="View ticket"
                        class="${classes(closed=result.status == 'closed')}">#$result.id</a></td>
                    <td py:otherwise="" class="$name"
                        xml:space="${'preserve' if wikitextarea else None}" py:choose="">
                      <a py:when="name == 'summary'" href="$result.href" title="View ticket">$value</a>
                      <py:when test="header.field.type == 'time'">${pretty_dateinfo(value, header.field.format)}</py:when>
                      <py:when test="name == 'reporter'">${authorinfo(value)}</py:when>
                      <py:when test="name == 'cc'">${format_emails(ticket_context, value)}</py:when>
                      <py:when test="name == 'owner' and value">${authorinfo(value)}</py:when>
                      <py:when test="name == 'milestone'" py:if="value">${resource_link(context, Resource('milestone', value), 'compact')}</py:when>
                      <py:when test="wikitext">${wiki_to_oneliner(ticket_context, value)}</py:when>
                      <py:when test="wikitextarea">${wiki_to_html(ticket_context, value)}</py:when>
                      <py:otherwise>$value</py:otherwise>
                    </td>
                  </py:with>
                </py:for>
              </tr>

              <py:with vars="result_rows = [t for t in row if result[t]]">
                <py:choose>
                  <tr py:when="ticket_context.resource in context" class="fullrow">
                    <td colspan="$num_cols">
                      <p class="meta"><em>(this ticket)</em></p>
                    </td>
                  </tr>
                  <tr py:otherwise="" py:for="r in result_rows" class="fullrow"
                      py:with="colspan = num_cols - 1; field = fields.get(r); value = result[r]"
                      py:choose="">
                    <th class="meta">${field.label if field else (r or 'none')}</th>
                    <td py:when="not value" colspan="${colspan}"></td>
                    <td py:when="field.format == 'wiki'" colspan="${colspan}" xml:space="preserve">
                      ${wiki_to_html(ticket_context, value)}
                    </td>
                    <td py:otherwise="" colspan="${colspan}">
                      <py:for each="idx, line in enumerate(value.splitlines())">
                        <br py:if="idx" />${line}
                      </py:for>
                    </td>
                  </tr>
                </py:choose>
              </py:with>
            </py:if>
          </py:with>
        </py:for>
        <tr py:if="group_index == len(groups)-1 and last_group_is_partial">
          <td colspan="$num_cols">
            <i>(more results for this group on next page)</i>
          </td>
        </tr>
      </tbody>
    </py:for>
  </table>
  <xi:include py:if="paginator.show_index" href="page_index.html" />
</div>


