{% extends "base.html" %}
{% block title %}Centre documentaire{% endblock %}
{% block content %}
<div class="container-fluid main-content">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <div><h1 class="h3 mb-1">Centre documentaire</h1><p class="text-muted mb-0">Recherchez les documents accessibles depuis les interventions, équipements et produits.</p></div>
    <a class="btn btn-outline-primary" href="{{ url_for('documents.fds') }}"><i class="bi bi-shield-check"></i> Voir les FDS</a>
  </div>
  <form class="card card-body mb-3 filter-bar" method="get">
    <div class="row g-2 align-items-end">
      <div class="col-md-5"><label class="form-label" for="q">Recherche</label><input id="q" name="q" class="form-control" value="{{ filters.get('q','') }}" placeholder="Nom, titre, intervention, équipement ou produit"></div>
      <div class="col-md-2"><label class="form-label" for="source">Source</label><select id="source" name="source" class="form-select"><option value="">Toutes</option>{% for value,label in [('intervention','Interventions'),('equipment','Équipements'),('product','Produits')] %}<option value="{{ value }}" {% if filters.get('source') == value %}selected{% endif %}>{{ label }}</option>{% endfor %}</select></div>
      <div class="col-md-2"><label class="form-label" for="document_type">Type</label><select id="document_type" name="document_type" class="form-select"><option value="">Tous</option>{% for value,label in document_types.items() %}<option value="{{ value }}" {% if filters.get('document_type') == value %}selected{% endif %}>{{ label }}</option>{% endfor %}</select></div>
      <div class="col-md-2"><label class="form-label" for="date_from">Depuis</label><input id="date_from" type="date" name="date_from" class="form-control" value="{{ filters.get('date_from','') }}"></div>
      <div class="col-md-1"><button class="btn btn-primary w-100" type="submit" aria-label="Rechercher"><i class="bi bi-search"></i></button></div>
    </div>
  </form>
  {% if results %}
  <div class="table-responsive table-responsive-stack"><table class="table table-hover align-middle"><thead><tr><th>Document</th><th>Type</th><th>Lié à</th><th>Localisation</th><th>Date</th><th>Actions</th></tr></thead><tbody>
  {% for item in results %}<tr><td data-label="Document"><strong>{{ item.title }}</strong><br><small class="text-muted">{{ item.filename }}</small></td><td data-label="Type">{{ item.document_type_label }}</td><td data-label="Lié à">{% if item.context_url %}<a href="{{ item.context_url }}">{{ item.context_label }}</a>{% else %}{{ item.context_label }}{% endif %}</td><td data-label="Localisation">{{ item.location_summary }}{% if item.location_labels|length > 1 %}<details><summary class="small">Détail</summary><ul class="mb-0">{% for location in item.location_labels %}<li>{{ location }}</li>{% endfor %}</ul></details>{% endif %}</td><td data-label="Date">{{ item.document_date|datetime_fmt if item.document_date else '—' }}</td><td data-label="Actions"><a class="btn btn-sm btn-outline-primary" href="{{ item.download_url }}"><i class="bi bi-download"></i> Télécharger</a></td></tr>{% endfor %}
  </tbody></table></div>
  {% else %}<div class="alert alert-info"><i class="bi bi-info-circle"></i> Aucun document accessible n'a été trouvé.<br><span>Les documents sont ajoutés depuis les fiches des équipements, interventions et produits.</span></div>{% endif %}
  {% if total > per_page %}<nav aria-label="Pagination documentaire"><ul class="pagination"><li class="page-item {% if page <= 1 %}disabled{% endif %}"><a class="page-link" href="{{ url_for('documents.index', page=page-1, **filters.to_dict()) }}">Précédent</a></li><li class="page-item active"><span class="page-link">{{ page }}</span></li><li class="page-item {% if page * per_page >= total %}disabled{% endif %}"><a class="page-link" href="{{ url_for('documents.index', page=page+1, **filters.to_dict()) }}">Suivant</a></li></ul></nav>{% endif %}
</div>
{% endblock %}
