{%- set oc = {state.query_args.get('order', [config.prefs.default_order])[0]: ' selected'} %}
{%- set ou = {
    'rev-freshness': U(add_state_query_string(state.command_url, state, {
                           'url_args_remove': [['order', '']],
                           'url_args_add': [['order', 'rev-freshness']]
                       })),
    'rev-date':      U(add_state_query_string(state.command_url, state, {
                           'url_args_remove': [['order', '']],
                           'url_args_add': [['order', 'rev-date']]
                       })),
    'date':          U(add_state_query_string(state.command_url, state, {
                           'url_args_remove': [['order', '']],
                           'url_args_add': [['order', 'date']]
                       })),
    'rev-flat-date': U(add_state_query_string(state.command_url, state, {
                           'url_args_remove': [['order', '']],
                           'url_args_add': [['order', 'rev-flat-date']]
                       })),
    'rev-index':     U(add_state_query_string(state.command_url, state, {
                           'url_args_remove': [['order', '']],
                           'url_args_add': [['order', 'rev-index']]
                       })),
}%}
{%- set dm = {config.web.display_density: ' selected'} %}
{%- set display_things = [] -%}
{%- if result.index_capabilities.can_sort -%}
  {%- do display_things.append('
    <li class="dropdown">
      <a class="dropdown-toggle" data-toggle="dropdown" href="#">
        <span class="navigation-icon icon-list"></span>
        <span class="navigation-text">' + _("Order") + '</span>
      </a>
      <ul id="menu1" class="dropdown-menu dropdown-menu-right"
          role="menu" aria-labelledby="display-density">
        <li role="presentation">
          <a class="change-search-order' + oc.get('rev-freshness', '') + '"
             data-order="rev-freshness" data-keep-selection=1
             href="' + ou['rev-freshness'] + '">' +  _("Freshness") + '</a>
        </li>
        <li role="presentation">
          <a class="change-search-order' + oc.get('rev-date', '') + '"
             data-order="rev-date" data-keep-selection=1
             href="' + ou['rev-date'] + '">' + _("Newest First") + '</a>
        </li>
        <li role="presentation">
          <a class="change-search-order' + oc.get('date', '') + '"
             data-order="date" data-keep-selection=1
             href="' + ou['date'] + '">' + _("Oldest First") + '</a>
        </li>
        <li role="presentation">
          <a class="change-search-order' + oc.get('rev-flat-date', '') + '"
             data-order="rev-flat-date" data-keep-selection=1
             href="' + ou['rev-flat-date'] + '">' + _("Messages") + '</a>
        </li>
        <li role="presentation">
          <a class="change-search-order' + oc.get('rev-index', '') + '"
             data-order="rev-index" data-keep-selection=1
             href="' + ou['rev-index'] + '">' + _("Unsorted") + '</a>
        </li>
      </ul>
    </li>
') -%}
{%- endif %}
{%- do display_things.append('
    <li class="dropdown mobile-hide" id="display-density-selector">
      <a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="navigation-icon icon-eye"></span> ' + _("Display") + '</a>
      <ul id="menu1" class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="display-density">
        <li role="presentation">
          <a class="change-view-size' + dm.get('snug', '') + '"
             data-view_size="snug" href="#">' + _("Snug") + '</a>
        </li>
        <li role="presentation">
          <a class="change-view-size' + dm.get('cozy', '') + '"
             data-view_size="cozy" href="#">' + _("Cozy") + '</a>
        </li>
        <li role="presentation">
          <a class="change-view-size' + dm.get('comfy', '') + '"
             data-view_size="comfy" href="#">' + _("Comfy") + '</a>
        </li>
      </ul>
    </li>
') -%}

{%- set search_actions = [] -%}

{%- if result.index_capabilities.has_tags -%}
{%-   for tid in config.tags -%}
{%-     set tag = config.tags[tid] -%}
{%-     if tag.toolbar or tag.type in ('unread', ) -%}
{%-       if tag.type in ('attribute', 'unread') -%}
{%-         set msg = _("Toggle %(tag_name)s", tag_name=tag.name) -%}
{%-         set act = "toggle" -%}
{%-       else -%}
{%-         set msg = _("Move to %(tag_name)s", tag_name=tag.name) -%}
{%-         set act = "move" -%}
{%-       endif -%}
{%-       set tag_color = theme_settings().colors.get(tag.label_color, tag.label_color) -%}
{%-       do search_actions.append('
  <li class="hide">
    <a class="bulk-action-tag-op" href="#" title="'+ msg +'"
       data-op="'+ act +'" data-tag="'+ tid +'" data-tagname="'+ tag.name +'">
      <span class="icon '+ tag.icon +'" style="color: ' + tag_color +';"></span>
    </a>
  </li>') -%}
{%-     endif -%}
{%-   endfor -%}

{# FIXME, Issue #879  %-   do search_actions.append('
  <li class="hide">
    <a class="bulk-action-tag" href="#" title="' + _("Assign Tags") + '">
      <span class="icon icon-tags"></span>
    </a>
  </li>') -% #}
{%-   if "in:spam" not in result.search_terms %}
{%-     do search_actions.append('
  <li class="hide">
    <a class="bulk-action-tag-op" href="#" title="' + _("Move to Spam") + '"
       data-ui="spam" data-op="tag" data-untag="type:unread" data-tag="spam">
      <span class="icon-spam"></span>
    </a>
  </li>') -%}
{%-   endif %}
{%-   if "in:trash" not in result.search_terms %}
{%-     do search_actions.append('
  <li class="hide">
    <a class="bulk-action-tag-op" href="#" title="' + _("Move to Trash") + '"
       data-ui="trash" data-op="tag" data-untag="type:unread" data-tag="trash">
      <span class="icon icon-trash"></span>
    </a>
  </li>') -%}
{%-   endif %}
{%-   if result.tag_capabilities.allow_del -%}
{%-     do search_actions.append('
  <li class="hide">
    <a class="bulk-action-tag-op" href="#" title="' + _("Untag Selection") + '"
       data-op="untag">
      <span class="icon icon-circle-x"></span>
    </a>
  </li>') -%}
{%-   endif %}
{%-   do search_actions.append('
  <li class="hide">
    <a class="bulk-action-tag-op" href="#" data-op="archive"
       title="' + _("Archive Selection") +': '+ _("untag completely") +'">
      <span class="icon icon-archive">
    </a>
  </li>') -%}
{%- endif -%}

{%- set hard_coded_display_modes = safe(display_things|join('')) -%}
{%- set hard_coded_selection_actions = safe(search_actions|join('')) -%}

{%- set display_refiners = [] -%}
{%- if result.index_capabilities.has_unread -%}
{%-   do display_refiners.append({
        'name': 'unread',
        'icon': 'new',
        'url': '/search/',
        'url_args_remove': [['qr', ''], ['context', ''], ['start', ''], ['end', ''], ['view', ''], ['order', '']],
        'url_args_add': [['qr', 'is:unread']],
        'text': _("New"),
        'description': _("Unread messages")})
-%}
{%- endif -%}
{%- if result.index_capabilities.has_atts %}
{%-   do display_refiners.append({
        'name': 'has_photos',
        'icon': 'photos',
        'url': '/search/photos.html',
        'url_args_remove': [['qr', ''], ['context', ''], ['start', ''], ['end', ''], ['view', ''], ['order', '']],
        'url_args_add': [['qr', 'has:image'], ['order', 'rev-flat-date']],
        'text': _("Images"),
        'description': _("Photos and images in mail matching this search")})
-%}
{%-   do display_refiners.append({
        'name': 'has_attachment',
        'icon': 'attachment',
        'url': '/search/atts.html',
        'url_args_remove': [['qr', ''], ['context', ''], ['start', ''], ['end', ''], ['view', ''], ['order', '']],
        'url_args_add': [['qr', 'has:attachment'], ['order', 'rev-flat-date']],
        'text': _("Attachments"),
        'description': _("Attachments in mail matching this search")})
-%}
{%- endif %}
{%- if display_refiners %}
{%-   do display_refiners.append({
        'name': 'display_all',
        'icon': 'logo',
        'url': '/search/',
        'url_args_remove': [['qr', ''], ['context', ''], ['start', ''], ['end', ''], ['view', ''], ['order', '']],
        'text': _("All"),
        'description': _("All messages")})
-%}
{%- endif %}

{%- set display_modes = [{
    'name': 'display_list',
    'icon': 'list',
    'url': '/search/',
    'text': _("List"),
    'description': _("List view")}]
-%}

{%- set search_terms_in = (result.search_terms or [''])[0][3:] -%}
{%- set activities = [] -%}
{%- set parent = state.query_args.get('parent', [''])[0] -%}
{%- if parent %}
{%-   do activities.append({
        'name': 'parent',
        'icon': 'upload',
        'url': parent,
        'text': _("Parent"),
        'description': _("Open Parent Folder")})
-%}
{%- endif %}
{%- if 'in:' == (result.search_terms or [''])[0][:3] -%}
{%-   do activities.append({
        'name': 'edit_tag',
        'icon': 'settings',
        'aclass': 'auto-modal auto-modal-reload',
        'url': '/tags/edit.html?only=' + search_terms_in,
        'text': _("Edit"),
        'description': _("Edit") + ': '  + search_terms_in})
-%}
{%- elif result.search_terms not in ([], [''], ['all:mail']) %}
{%-   do activities.append({
        'name': 'save_search',
        'icon': 'star',
        'aclass': '',
        'url': '#save_search',
        'text': _("Save"),
        'description': _("Save the results of this search to a new tag")})
-%}
{%- else %}
{%-   do activities.append({
        'name': 'home',
        'icon': 'home',
        'aclass': '',
        'url': '/',
        'text': _("Home"),
        'description': _("E-mail Accounts")})
-%}
{%- endif %}
{%- include('partials/tools_default.html') -%}
