{# Copyright (C) 2006-2020 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 https://trac.edgewall.org/wiki/TracLicense.

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

## Query view

# extends 'layout.html'

<!DOCTYPE html>
<html>
  <head>
    <title>
    # block title
    ${title} ${ super() }
    # endblock title
    </title>
    ## FIXME: find a way to make that the default...

    # block head

    ${ super() }

    <script>
      jQuery(function($) {
        initializeFilters();
        $("#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(/[?&]report=[0-9]+/) != -1)
          $("#filters").toggleClass("collapsed");
        /* Hide the columns by default. */
        $("#columns").toggleClass("collapsed");
      });
    </script>

    # endblock head
  </head>

  <body>
    # block content
    <div id="content" class="query">
      <h1>${title} <span class="numrows">(${
          ngettext('%(num)s match', '%(num)s matches', query.num_items)
          })</span></h1>

      # if description:
      <div id="description">
        ${wiki_to_html(context.child(report_resource), description)}
      </div>
      # endif

      <form id="query" method="post" action="${href.query()}">
        ${jmacros.form_token_input()}
        <fieldset id="filters">
          <legend class="foldable">${_("Filters")}</legend>
          <table>
            # for constraints in (clauses or [{}]):
            #   set clause_num = loop.index0
            <tbody>
              # set clause_pre = '%d_' % loop.index0
              <tr${{'style': {'display': 'none' if loop.first}}|htmlattr}>
                <td>
                  <div class="trac-clause-lsep">&nbsp;<hr /></div>
                  <div class="trac-clause-msep">${_("Or")}</div>
                  <div class="trac-clause-rsep">&nbsp;<hr /></div>
                </td>
              </tr>
              <tr>
                <td class="trac-clause">
                  <table class="trac-clause">
                    # for field_name in field_names if field_name in constraints:
                    #     set field = fields[field_name]
                    #     set n_field_name = clause_pre + field_name
                    #     set constraint = constraints[field_name]
                    #     set multiline = field.type in ('select', 'text', 'textarea', 'time')
                    <tbody>
                      # for constraint_value in constraint['values']:
                      #   if multiline or loop.first:
                      <tr class="${field_name}">
                        <td>
                          <div class="inlinebuttons">
                            <input type="submit"
                                   name="rm_filter_${n_field_name}_${loop.index0}"
                                   value="&ndash;"/>
                          </div>
                        </td>
                        #   if loop.first:
                        <th scope="row"><label id="label_${n_field_name}">${
                            fields[field_name].label
                            }</label></th>
                        #     if field.type not in ('radio', 'checkbox', 'time'):
                        <td class="mode">
                          <select name="${n_field_name}_mode">
                            #   for mode in modes[field.type]:
                            <option${
                               {'value': mode.value,
                                'selected': mode.value == constraint.mode
                               }|htmlattr}>${mode.name}</option>
                            #   endfor
                          </select>
                        </td>
                        #     endif
                        #   else:
                        ##   not the first line of a multiline constraint
                        <th colspan="${1 if field.type == 'time' else 2
                                     }"><label>${_("or")}</label></th>
                        #   endif

                        <td class="filter"${
                          {'colspan':
                             2 if field.type in ('radio', 'checkbox', 'time')
                          }|htmlattr}>

                          # if field.type == 'select':
                          <select name="${n_field_name}">

                            # macro render_options(field_name, options)
                            #   for option in options:
                            #     if field_name == 'owner':
                            #         set opt_name = option.name
                            #         set opt_value = option.value
                            #     else:
                            #         set opt_name = option
                            #         set opt_value = option
                            #     endif
                            <option${{'value': opt_value,
                                      'selected': opt_value == constraint_value
                                     }|htmlattr}>${opt_name}
                            </option>
                            #   endfor
                            # endmacro

                            <option label="${_('unset')}"></option>
                            ${render_options(field.name, field.options)}
                            # for ogroup in field.optgroups if ogroup.options:
                            <optgroup label="${ogroup.label}">
                              ${render_options(field.name, ogroup.options)}
                            </optgroup>{# jinjacheck: "()" OK #}
                            # endfor
                          </select>

                          # elif field.type == 'radio':
                          #   for option in field.options:
                          <input type="checkbox"
                                 id="_${n_field_name}_${option}"${
                               {'name': n_field_name,
                                'value': option,
                                'checked':
                                    (constraint['mode'] == '') is equalto
                                    (option in constraint['values'])
                               }|htmlattr}/>
                          <label for="_${n_field_name}_${option}"
                                 class="control">${option or _('none')}</label>
                          #   endfor

                          # elif field.type == 'checkbox':
                          <input type="radio" id="_${n_field_name}_on"
                                 value="1"${
                               {'name': n_field_name,
                                'checked': constraint.mode != '!' or
                                           constraint_value == '1'
                               }|htmlattr}/>
                          <label for="_${n_field_name}_on" class="control">${
                            _("yes")}</label>
                          <input type="radio" id="_${n_field_name}_off"
                                 value="0"${
                               {'name': n_field_name,
                                'checked': constraint.mode == '!' or
                                           constraint_value != '1'
                               }|htmlattr}/>
                          <label for="_${n_field_name}_off" class="control">${
                            _("no")}</label>

                          # elif field.type in ('text', 'textarea', 'id'):
                          <input type="text" name="${n_field_name}"
                                 value="${constraint_value}" size="42"/>

                          # elif field.type == 'time':
                          #   set start, end = '..' in constraint_value and constraint_value.split('..', 1) or (constraint_value, '')
                          #   set format = field.get('format', 'datetime')
                          #   set date_or_datetime = 'date' if format == 'relative' else format
                          #   set start_picker
                          <input type="text" name="${n_field_name}"
                                 value="${start}" size="14"
                                 class="trac-${date_or_datetime}picker" />
                          #   endset
                          #   set end_picker
                          <input type="text" name="${n_field_name}_end"
                                 value="${end}" size="14"
                                 class="trac-${date_or_datetime}picker" />
                          #   endset
                          #   trans start=start_picker, end=end_picker

                          <label>between ${start}</label> <label>and ${end}</label>

                          #   endtrans
                          # endif
                        </td>
                      </tr>
                      #   endif
                      # endfor
                    </tbody>
                    # endfor

                    # if loop.last:
                    <tbody>
                      <tr class="actions">
                        <td class="and" colspan="2">
                          &nbsp;<label for="add_filter_${clause_num}">${
                            _("And")
                            }</label>&nbsp;
                          <select name="add_filter_${clause_num}"
                                  id="add_filter_${clause_num}">
                            <option label="${_('field…')}"></option>
                            # for field_name in field_names:
                            #   set field = fields[field_name]
                            <option${
                              {'value': field_name,
                               'disabled':
                                 (field.type in ('radio', 'checkbox', 'id') and
                                 field_name in constraints and
                                 len(constraints[field_name]))
                               }|htmlattr}>${field.label}</option>
                            # endfor
                          </select>
                          <div class="inlinebuttons">
                            <input type="submit" name="add_${clause_num}"
                                   value="+"/>
                          </div>
                        </td>
                        # if loop.last:
                        <td class="or" colspan="2">
                          <label for="add_clause">Or</label>&nbsp;
                          <select name="add_clause_${clause_num + 1}"
                                  id="add_clause">
                            <option label="${_('field…')}"></option>
                            # for field_name in field_names:
                            <option value="${field_name}">${
                              fields[field_name].label
                              }</option>
                            # endfor
                          </select>
                          <div class="inlinebuttons">
                            <input type="submit" name="add_${clause_num + 1}"
                                   value="+" />
                          </div>
                        </td>
                        # endif
                      </tr>
                    </tbody>
                    # endif
                  </table>
                </td>
              </tr>
            </tbody>
            # endfor
          </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>
            # for column in all_columns:
            <label>
              <input type="checkbox" name="col"${
                   {'value': column,
                    'checked': col|select('equalto', column)|list|length
                   }|htmlattr}/>
              ${fields.get(column, {'label': column or 'none'}).label}
            </label>
            # endfor
          </div>
        </fieldset>

        <p class="option">
          <label for="group">${_("Group results by")}</label>
          <select name="group" id="group">
            <option label="${_('field…')}"></option>
            # for field_name in field_names:
            #   set field = fields[field_name]
            #   if field.type in ('select', 'radio') or field_name in ('owner', 'reporter'):
            <option${{'selected': field_name == query.group,
                      'value': field_name
                     }|htmlattr}>${field.label}</option>
            #   endif
            # endfor
          </select>
          <input type="checkbox" name="groupdesc" id="groupdesc"${
               {'checked': query.groupdesc}|htmlattr}/>
          <label for="groupdesc">${_("descending")}</label>
        </p>

        <p class="option">
          ## Allow the user to decide what rows to include in the
          ## output of the query

          ${_("Show under each result:")}
          # for column in all_textareas:
          <label><input type="checkbox" name="row"${
                      {'value': column,
                       'checked': row|select('equalto', column)|list|length
                      }|htmlattr}/>
            ${fields.get(column, {'label': column or 'none'}).label}</label>
          # endfor
        </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">
          # if report_resource:
          <input type="hidden" name="report" value="${report_resource.id}" />
          # endif
          <input type="hidden" name="order" value="${query.order}" />
          # if query.desc:
          <input type="hidden" name="desc" value="1" />
          # endif
          <input type="submit" name="update" value="${_('Update')}" />
        </div>
        <hr />
      </form>

      # include 'query_results.html'
      # if tickets:
      #   include 'batch_modify.html' ignore missing
      # endif

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

      # call(note, page) jmacros.wikihelp('TracQuery'):
      #   trans note, page

      ${note} See ${page} for help on using queries.

      #   endtrans
      # endcall
    </div>
    ${ super() }
    # endblock content
  </body>
</html>
