{% extends "base.html" %}

{% block title %}Recherche — GMAO{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col">
            <h1 class="h3">
                <i class="fas fa-search me-2"></i>Recherche
            </h1>
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-body">
            <form method="GET" action="{{ url_for('dashboard.search') }}">
                <div class="row">
                    <div class="col-md-8">
                        <input type="text" name="q" class="form-control" 
                               placeholder="Rechercher interventions, équipements, entreprises..."
                               value="{{ query|default('') }}">
                    </div>
                    <div class="col-md-4">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-search me-1"></i>Rechercher
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    {% if query %}
    <div class="row">
        <div class="col-md-4 mb-3">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Interventions ({{ interventions|length }})</h5>
                </div>
                <div class="card-body">
                    {% if interventions %}
                    <ul class="list-unstyled mb-0">
                        {% for i in interventions[:10] %}
                        <li class="mb-2">
                            <a href="{{ url_for('interventions.detail', id=i.id) }}">
                                {{ i.title }}
                            </a>
                            <br><small class="text-muted">{{ i.status }}</small>
                        </li>
                        {% endfor %}
                        {% if interventions|length > 10 %}
                        <li class="text-muted">... et {{ interventions|length - 10 }} autres</li>
                        {% endif %}
                    </ul>
                    {% else %}
                    <p class="text-muted mb-0">Aucun résultat</p>
                    {% endif %}
                </div>
            </div>
        </div>
        
        <div class="col-md-4 mb-3">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Équipements ({{ equipments|length }})</h5>
                </div>
                <div class="card-body">
                    {% if equipments %}
                    <ul class="list-unstyled mb-0">
                        {% for e in equipments[:10] %}
                        <li class="mb-2">
                            <a href="{{ url_for('equipments.detail', id=e.id) }}">
                                {{ e.name }}
                            </a>
                            <br><small class="text-muted">{{ e.code or '—' }}</small>
                        </li>
                        {% endfor %}
                        {% if equipments|length > 10 %}
                        <li class="text-muted">... et {{ equipments|length - 10 }} autres</li>
                        {% endif %}
                    </ul>
                    {% else %}
                    <p class="text-muted mb-0">Aucun résultat</p>
                    {% endif %}
                </div>
            </div>
        </div>
        
        <div class="col-md-4 mb-3">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Entreprises ({{ companies|length }})</h5>
                </div>
                <div class="card-body">
                    {% if companies %}
                    <ul class="list-unstyled mb-0">
                        {% for c in companies[:10] %}
                        <li class="mb-2">
                            <a href="{{ url_for('companies.detail', id=c.id) }}">
                                {{ c.trade_name or c.specialty }}
                            </a>
                        </li>
                        {% endfor %}
                        {% if companies|length > 10 %}
                        <li class="text-muted">... et {{ companies|length - 10 }} autres</li>
                        {% endif %}
                    </ul>
                    {% else %}
                    <p class="text-muted mb-0">Aucun résultat</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}