<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="layout.html" />
  <head>
    <title>$title</title>
    <script type="text/javascript" src="${chrome.htdocs_location}js/folding.js"></script>
    <script type="text/javascript">
      jQuery(document).ready(function($) {
        $("#group").change(function() {
          $("#groupdesc").enable(this.selectedIndex != 0)
        }).change();
        $("fieldset legend.foldable").enableFolding(false);
        /* Hide the filters for saved queries. */
        if ( window.location.href.search(/[?&amp;]report=[0-9]+/) != -1 )
          $("#filters").toggleClass("collapsed");
        /* Hide the columns by default. */
        $("#columns").toggleClass("collapsed");
      });
    </script>
  </head>

  <body>
    <py:def function="num_matches(v)">
      <span class="numrows">(${v or 'No'} match${v != 1 and 'es' or ''})</span>
    </py:def>

    <div id="content" class="query">
      <h1>$title ${num_matches(query.num_items)}</h1>

      <div py:if="description" id="description" xml:space="preserve">
        ${wiki_to_html(context(report_resource), description)}
      </div>

      <form id="query" method="post" action="${href.query()}"
            py:with="field_names = sorted(fields.iterkeys(), key=lambda name: labels[name].lower())">
        <fieldset id="filters">
          <input py:if="'id' in query.constraints" type="hidden" name="id" value="${query.constraints['id']}" />
          <legend class="foldable">Filters</legend>
          <table summary="Query filters">
            <tbody>
              <tr style="height: 1px"><td colspan="4"></td></tr>
            </tbody>
            <py:for each="field_name in field_names" py:with="field = fields[field_name]">
              <py:for each="constraint_name, constraint in constraints.items()">
                <tbody py:if="field_name == constraint_name"
                  py:with="multiline = field.type in ('select', 'text', 'textarea')">
                  <py:for each="constraint_idx, constraint_value in enumerate(constraint['values'])">
                    <tr class="${field_name}" py:if="multiline or constraint_idx == 0">
                      <py:choose test="constraint_idx">
                        <py:when test="0">
                          <th scope="row"><label>$field.label</label></th>
                          <td py:if="field.type not in ('radio', 'checkbox')" class="mode">
                            <select name="${field_name}_mode">
                              <option py:for="mode in modes[field.type]" value="$mode.value"
                                selected="${mode.value == constraint.mode and 'selected' or None}">$mode.name
                              </option>
                            </select>
                          </td>
                        </py:when>
                        <py:otherwise><!--! not the first line of a multiline constraint -->
                          <th colspan="2"><label>or</label></th>
                        </py:otherwise>
                      </py:choose>

                      <td class="filter" colspan="${field.type in ('radio', 'checkbox') and 2 or None}"
                          py:choose="">

                        <py:when test="field.type == 'select'">
                          <select name="${constraint_name}">
                            <option></option>
                            <option py:for="option in field.options"
                              selected="${option == constraint_value and 'selected' or None}">$option
                            </option>
                          </select>
                        </py:when>

                        <py:when test="field.type == 'radio'">
                          <py:for each="option in field.options">
                            <input type="checkbox" id="${field_name}_$option" name="${field_name}"
                              value="$option"
                              checked="${any([(value == option) == (constraint.mode == '')
                                              for value in constraint['values']]) and 'checked' or None}" />
                            <label for="${field_name}_$option">${option or 'none'}</label>
                          </py:for>
                        </py:when>

                        <py:when test="field.type == 'checkbox'">
                          <input type="radio" id="${field_name}_on" name="$field_name" value="1"
                                 checked="${constraint.mode != '!' or constraint_value == '1' or None}" />
                          <label for="${field_name}_on">yes</label>
                          <input type="radio" id="${field_name}_off" name="$field_name" value="0"
                                 checked="${constraint.mode == '!' or constraint_value != '1' or None}" />
                          <label for="${field_name}_off">no</label>
                        </py:when>

                        <py:when test="field.type in ('text', 'textarea')">
                          <input type="text" name="${field_name}" value="$constraint_value" size="42" />
                        </py:when>

                      </td>
                      <td class="actions"
                          py:with="rm_idx = multiline and constraint_idx or len(constraint['values']) - 1"><input
                          type="submit" name="rm_filter_${field_name}${field.type != 'radio' and '_%d' % rm_idx or ''}"
                          value="-" /></td>
                    </tr>
                  </py:for>
                </tbody>
              </py:for>
            </py:for>

            <tbody>
              <tr class="actions">
                <td class="actions" colspan="4" style="text-align: right">
                  <label for="add_filter">Add filter</label>&nbsp;
                  <select name="add_filter" id="add_filter">
                    <option></option>
                    <option py:for="field_name in field_names" py:with="field = fields[field_name]"
                            value="$field_name"
                            disabled="${(field.type == 'radio' and
                                         field_name in constraints and
                                         len(constraints[field_name])) or None}">
                      ${field.label}
                    </option>
                  </select>
                  <input type="submit" name="add" value="+" />
                </td>
              </tr>
            </tbody>
          </table>
        </fieldset>

        <!--! Allow the user to decide what columns to include in the output of the query -->
        <fieldset id="columns">
          <legend class="foldable">Columns</legend>
          <div>
            <py:for each="column in all_columns">
              <label>
                <input type="checkbox" name="col" value="$column"
                       checked="${any([(value == column) for value in col])
                                  and 'checked' or None}" />
                ${labels.get(column, column or 'none')}
              </label>
            </py:for>
          </div>
        </fieldset>

        <p class="option">
          <label for="group">Group results by</label>
          <select name="group" id="group">
            <option></option>
            <py:for each="field_name in field_names" py:with="field = fields[field_name]">
              <option py:if="field.type in ('select', 'radio') or field_name in ('owner', 'reporter')"
                      selected="${field_name == query.group or None}"
                      value="${field_name}">${field.label}</option>
            </py:for>
          </select>
          <input type="checkbox" name="groupdesc" id="groupdesc"
                 checked="${query.groupdesc or None}" />
          <label for="groupdesc">descending</label>
        </p>

        <p class="option">
          <!--! Allow the user to decide what columns to include in the output of the query -->
          Show under each result:
          <py:for each="column in all_textareas">
            <label><input type="checkbox" name="row" value="$column"
                checked="${any([(value == column) for value in row]) and 'checked' or None}" />
            ${labels.get(column, column or 'none')}</label>
          </py:for>
        </p>

        <p class="option">
          <label for="max">Max items per page</label>
          <input type="text" name="max" id="max" size="10" value="${query.max}" />
        </p>

        <div class="buttons">
          <input py:if="report_resource" type="hidden" name="report" value="$report_resource.id" />
          <input type="hidden" name="order" value="$query.order" />
          <input py:if="query.desc" type="hidden" name="desc" value="1" />
          <input type="submit" name="update" value="Update" />
        </div>
        <hr />
      </form>

      <script type="text/javascript">
        var properties={
        <py:for each="(field_name, field), sep in separated(fields.iteritems())">
          $field_name: { type: "${javascript_quote(field.type)}", label: "${javascript_quote(field.label)}"
          <py:if test="'options' in field">, options: [
            <py:for each="option, sep in separated(field.options)">"${javascript_quote(option)}"$sep
            </py:for>]
          </py:if>}$sep
        </py:for>
        };
        var modes = {
        <py:for each="(type_name, type_modes), sep in separated(modes.iteritems())">
          $type_name: [
          <py:for each="mode, sep in separated(type_modes)">
            {text: "${javascript_quote(mode.name)}", value: "${javascript_quote(mode.value)}"}$sep
          </py:for>
            ]$sep
        </py:for>
        };
        initializeFilters();
      </script>

      <xi:include href="query_results.html" />

      <div class="buttons" py:with="edit = report_resource and 'REPORT_MODIFY' in perm(report_resource);
                     new = 'REPORT_CREATE' in perm;
                     delete = report_resource and 'REPORT_DELETE' in perm(report_resource)">
        <form py:if="edit" method="get" action="${url_of(report_resource)}">
          <div>
            <input type="hidden" name="action" value="edit" />
            <input type="submit" title="Edit report {$report_resource.id} corresponding to this query"
                   value="Edit query" />
          </div>
        </form>
        <form py:if="new or edit" method="get" action="${edit and url_of(report_resource) or href.report()}">
          <div>
            <input type="hidden" name="action" value="${edit and 'edit' or 'new'}" />
            <input type="hidden" name="query" value="${query.to_string()}" />
            <input type="submit" value="Save query"
                   title="${edit and _('Save updated query in report {%(id)s}',
                                       id=report_resource.id) or
                            _('Create new report from current query')}" />
          </div>
        </form>
        <form py:if="delete" method="get" action="${url_of(report_resource)}">
          <div>
            <input type="hidden" name="action" value="delete" />
            <input type="submit" title="Delete report {$report_resource.id} corresponding to this query"
                   value="Delete query" />
          </div>
        </form>
      </div>

      <div id="help">
        <strong>Note:</strong> See <a href="${href.wiki('TracQuery')}">TracQuery</a>
        for help on using queries.
      </div>

    </div>
  </body>
</html>

