{% extends "base.html" %}
{% block title %}Recherche{% endblock %}

{% block content %}

<section class="card">
  <h2>Recherche</h2>
  <form class="search-form" action="{{ url_for('search') }}" method="get">
    <div class="search-row">
      <div class="search-field search-field--grow">
        <label for="q">Nom / contenu</label>
        <input id="q" type="search" name="q" value="{{ q }}" placeholder="ex. logo, favicon…" autofocus>
      </div>
      <div class="search-field">
        <label for="after">Modifié après</label>
        <input id="after" type="date" name="after" value="{{ after }}">
      </div>
      <div class="search-field">
        <label for="before">Modifié avant</label>
        <input id="before" type="date" name="before" value="{{ before }}">
      </div>
    </div>
    <div class="search-options">
      <label class="checkbox-label">
        <input type="checkbox" name="content" value="1" {% if in_content %}checked{% endif %}>
        Rechercher aussi dans le contenu des fichiers texte
      </label>
      <button type="submit" class="btn btn-primary">Rechercher</button>
    </div>
  </form>
</section>

{% if searched %}
<section class="card">
  {% if results %}
  <p class="search-count">
    <strong>{{ results | length }}</strong> résultat{% if results | length != 1 %}s{% endif %}
    {% if q %}pour <em>« {{ q }} »</em>{% endif %}
    {% if after %}· après {{ after }}{% endif %}
    {% if before %}· avant {{ before }}{% endif %}
  </p>
  <table class="file-table">
    <thead>
      <tr>
        <th class="col-icon"></th>
        <th>Fichier</th>
        <th class="col-size">Taille</th>
        <th class="col-date">Modifié le</th>
      </tr>
    </thead>
    <tbody>
      {% for e in results %}
      <tr>
        <td class="col-icon">
          {%- if e.is_image -%}🖼
          {%- elif e.is_pdf -%}📕
          {%- elif e.is_text -%}📄
          {%- else -%}📎
          {%- endif -%}
        </td>
        <td>
          <div class="col-name">
            {% if e.is_image %}
              <img class="thumb-sm" src="{{ url_for('raw_file', subpath=e.path) }}"
                   alt="{{ e.name }}" loading="lazy">
            {% endif %}
            <div>
              <a href="{{ url_for('browse', subpath=e.path) }}">{{ e.name }}</a>
              <span class="file-path">{{ e.path }}</span>
              {% if e.content_match %}
              <span class="content-match">…{{ e.content_match }}…</span>
              {% endif %}
            </div>
          </div>
        </td>
        <td class="col-size">{{ humansize(e.size) if e.size is not none else '—' }}</td>
        <td class="col-date">{{ e.mtime.strftime('%d/%m/%Y %H:%M') }}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
  {% else %}
  <p class="empty">Aucun résultat
    {% if q %}pour <em>« {{ q }} »</em>{% endif %}
    {% if after or before %}dans la plage de dates sélectionnée{% endif %}.
  </p>
  {% endif %}
</section>
{% endif %}

{% endblock %}
