{% extends "base.html" %}

{% block content %}

<div class="row">
  <div class="col m-5">
    <a href="{{ url_for('main.index') }}" class="h1 text-left"> ⇽ </a>
    <p class="h1 text-center">a timeline of inventions</p>
    <p class="mt-2 text-center">{{ num_found }} patents found out of {{ total_number }}</p>
  </div>
</div>

<div class="row p-3">
  <form action="{{ url_for('search.basic_search') }}" method="POST">
    <input type="hidden" name="query" value="{{ query }}">
    <input type="hidden" name="searchopt" value="{{ core }}">
    <input type="hidden" name="country" value="{{ country }}">
    <input type="hidden" name="year" value="{{ year }}">
    sort by:
    <select name="sort" id="sort" onchange="this.form.submit()">
      <option value="relevance" {% if sort == 'relevance' %} selected {% endif %}>relevance</option>
      <option value="year desc" {% if sort == 'year desc' %} selected {% endif %}>year descending</option>
      <option value="year asc" {% if sort == 'year asc' %} selected {% endif %}>year ascending</option>
    </select>
    <noscript>
      <input type="submit" class="btn btn-default" value="Set" />
    </noscript>
  </form>
</div>

<div class="row p-3">
  <form action="{{ url_for('search.basic_search') }}" method="POST">
    <input type="hidden" name="query" value="{{ query }}">
    <input type="hidden" name="searchopt" value="{{ core }}">
    <input type="hidden" name="sort" value="{{ sort }}">
    <input type="hidden" name="year" value="{{ year }}">
    filter by country:
    <select name="country" id="sort" onchange="this.form.submit()">
      {% if country is defined %}
        <option value="None" selected>none</option>
      {% else %}
        <option>country</option>
      {% endif %}
      {% for i in range(0, country_facet|length) %}
        {% if i % 2 == 0 %}
          {% if country_facet[i].name is defined %}
            <option value="{{ country_facet[i].alpha_2 }}" {% if country == country_facet[i].alpha_2 %} selected {% endif %}">{{ country_facet[i].name }} ({{ country_facet[i+1] }})</option>
          {% else %}
            <option value="{{ country_facet[i] }}" {% if country == country_facet[i] %} selected {% endif %}">{{ country_facet[i] }} ({{ country_facet[i+1] }})</option>
          {% endif %}
        {% endif %}
      {% endfor %}
    </select>
    <noscript>
      <input type="submit" class="btn btn-default" value="Set" />
    </noscript>
  </form>
</div>

<div class="row p-3">
  <form action="{{ url_for('search.basic_search') }}" method="POST">
    <input type="hidden" name="query" value="{{ query }}">
    <input type="hidden" name="searchopt" value="{{ core }}">
    <input type="hidden" name="sort" value="{{ sort }}">
    <input type="hidden" name="country" value="{{ country }}">
    filter by year:
    <select name="year" id="sort" onchange="this.form.submit()">
      {% if year is defined %}
        <option value="None" selected>none</option>
      {% else %}
        <option>year</option>
      {% endif %}
      {% for i in range(0, year_facet|length) %}
        {% if i % 2 == 0 %}
          <option value="{{ year_facet[i] }}" {% if year == year_facet[i] %} selected {% endif %}">{{ year_facet[i] }} ({{ year_facet[i+1] }})</option>
        {% endif %}
      {% endfor %}
    </select>
    <noscript>
      <input type="submit" class="btn btn-default" value="Set" />
    </noscript>
  </form>
</div>

{% if results == 'no results found' %}

  {{ results }}

{% else %}

  {% for result in results %}

    <p class="h1">

    Year:

    {{ result['year'] }}

    </p>

    {% if result['title'] is defined %}
    <p class="mb-2 mt-5">
      <span class="emphasis">Title:</span>
      <span class="result-entry">
        {{ result['title'] }}
      </span>
    </p>
    {% endif %}

    {% if result['country'] is defined %}
    <p class="mb-2">
      <span class="emphasis">Country:</span>
      {% if result['country'].name is defined %}
        {{ result['country'].name }} {{ result['country'].flag }}
      {% else %}
        {{ result['country'] }}
      {% endif %}
    </p>
    {% endif %}

    {% if result['abstract'] is defined %}
    <p class="mb-2">
      <span class="emphasis">Abstract:</span>
      <span class="result-entry">
        {{ result['abstract'] }}
      </span>
    </p>
    {% endif %}

    <div class="mt-2 mb-5 search-links">
      <p>
        <span class="emphasis">European Patent Office PDF:</span>
        <a href="{{ url_for('search.id_search', id=result['id'], core='all') }}">
          <span class="result-entry">
            {{ result['application_id'] }}
          </span>
        </a>
      </p>
    </div>

  {% endfor %}

{% endif %}

{% if search is defined %}
<script type="text/javascript">
  highlightSearchTerms({{ search|tojson}})
</script>
{% endif %}

{% endblock %}