{% extends 'base.html' %}
{% load i18n br2para dictlookup %}

{% block search %}
  <search id="searchform">
    <form id="form" method="get" action="">
      <div class="form-rows">
        <div class="row">
          <div class="label">
            <label for="q">{% trans "Search term:" %}</label>
          </div>
          <div class="field">
            <input type="search" id="q" name="q" size="35" value="{{ q|escape }}" autofocus />
            <a href="./#" id="toggle_options">{% trans "more options" %}</a>
          </div>
        </div>
        <div id="options" class="hidden">
          {% if selects.sv %}
            <div class="row">
              <div class="label">
                <label for="server">{% trans "DICT server:" %}</label>
              </div>
              <div class="field left">
                <select id="server" name="server">
                  {{ selects.sv.html|safe }}
                </select>
              </div>
            </div>
          {% endif %}
          <div class="row">
            <div class="label">
              <label for="db">{% trans "Database:" %}</label>
            </div>
            <div class="field">
              <select id="db" name="db" class="fw">
                {{ selects.db.html|safe }}
              </select>
            </div>
          </div>
          <div class="row">
            <div class="label">
              <label for="strategy">{% trans "Match strategy:" %}</label>
            </div>
            <div class="field">
              <select id="strategy" name="strategy" class="fw">
                {{ selects.st.html|safe }}
              </select>
            </div>
          </div>
        </div><!--/options-->
        <div class="row action-row">
          <div class="label"></div>
          <div id="action">
            <input type="submit" name="search" value="{% trans "Search" %}"/>
            <input type="submit" name="define" value="{% trans "Define (I'm feeling lucky)" %}"/>
          </div>
        </div>
      </div>
    </form>
  </search>

{% endblock %}
{% block results %}

  {% if result.error %}
    <div class="derror">
      {{ result.msg }}
    </div>
  {% else %}

    {% if result.definitions %}
      <div class="definition results">
        {% if mtc.matches %}
          <a href="#match-results" class="link-lmr">{% trans "last match results" %}</a>
        {% endif %}
        <h3>
          {% blocktrans count result.count as rc %}Found one definition{% plural %}Found {{ rc }} definitions{% endblocktrans %}
        </h3>
        <ol type="I">
          {% for df in result.definitions %}
            <li>
              <pre>
                {% trans "From" %} <a href="?q={{ df.db|urlencode }}&amp;db=dbinfo">{% firstof df.db_fullname df.db %}</a>:
                <br/><br/>
                {% if df.format_html %}
                  {{ df.desc|safe }}
                {% else %}
                  {{ df.desc|br2para|safe }}
                {% endif %}
              </pre>
            </li>
          {% endfor %}
        </ol>
      </div>
    {% else %}
      {% if result.desc %}
        <div class="results">
          <pre>
            {{ result.desc|br2para|safe }}
          </pre>
        </div>
      {% endif %}
    {% endif %}

    {% if mtc.matches %}
      <div class="match results">
        <h3>
          <a name="match-results"></a>{% trans "Last match results" %}
        </h3>
        <ul>
          {% for mt in mtc.matches.items %}
            <li>
              <span class="dbname-short" title="{{ mtc.dbnames|dictlookup:mt.0 }}">{{ mt.0 }}</span>:
              {% for term in mt.1 %}
                <a href="?q={{ term|urlencode }}&amp;db={{ mt.0|urlencode }}&amp;define=1"
                   title="{% blocktrans %}Define {{ term }}{% endblocktrans %}">{{ term }}</a>{% if not forloop.last %},
              {% endif %}{% endfor %}
            </li>
          {% endfor %}
        </ul>
      </div>
    {% endif %}
  {% endif %}

  <script type="text/javascript">
    const gettext_msg = {
      'more options': '{% trans "more options" %}',
      'less options': '{% trans "less options" %}'
    };
  </script>
{% endblock %}
