{% extends "base.html" %}
{% block title %}Alertes — GMAO Collège{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1><i class="bi bi-bell"></i> Alertes</h1>
        <a href="{{ url_for('dashboard.index') }}" class="btn btn-outline-secondary"><i class="bi bi-arrow-left"></i> Retour</a>
    </div>
    
    <!-- Alertes système non lues -->
    {% if alerts %}
    <div class="card border-danger mb-4">
        <div class="card-header bg-danger text-white">
            <i class="bi bi-exclamation-triangle"></i> Alertes système ({{ alerts|length }})
        </div>
        <div class="table-responsive">
            <table class="table table-sm table-hover mb-0">
                <thead>
                    <tr>
                        <th>Titre</th>
                        <th>Message</th>
                        <th>Date</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                {% for alert in alerts %}
                <tr>
                    <td>{{ alert.title or 'Alerte' }}</td>
                    <td>{{ alert.message[:100] if alert.message else '—' }}</td>
                    <td>{{ alert.created_at|datetime_fmt if alert.created_at else '—' }}</td>
                    <td>
                        <form method="POST" action="{{ url_for('dashboard.mark_alert_read', id=alert.id) }}" style="display:inline;">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <button type="submit" class="btn btn-sm btn-outline-success"><i class="bi bi-check"></i> Lu</button>
                        </form>
                    </td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% endif %}
    
    <!-- Interventions urgentes -->
    {% if urgent_interventions %}
    <div class="card border-warning mb-4">
        <div class="card-header bg-warning text-dark">
            <i class="bi bi-exclamation-triangle-fill"></i> Interventions prioritaires ({{ urgent_interventions|length }})
        </div>
        <div class="table-responsive">
            <table class="table table-sm table-hover mb-0">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Titre</th>
                        <th>Équipement</th>
                        <th>Date planifiée</th>
                        <th>Statut</th>
                    </tr>
                </thead>
                <tbody>
                {% for interv in urgent_interventions %}
                <tr>
                    <td><a href="{{ url_for('interventions.detail', id=interv.id) }}">#{{ interv.id }}</a></td>
                    <td>{{ interv.title[:50] }}{% if interv.title|length > 50 %}…{% endif %}</td>
                    <td>{{ interv.equipment.name[:30] if interv.equipment else '—' }}</td>
                    <td>{{ interv.scheduled_date.strftime('%d/%m/%Y') if interv.scheduled_date else '—' }}</td>
                    <td><span class="badge {{ interv.status|status_badge }}">{{ interv.status|format_status }}</span></td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% endif %}
    
    <!-- Équipements en panne -->
    {% if equipments_panne %}
    <div class="card border-danger mb-4">
        <div class="card-header bg-danger text-white">
            <i class="bi bi-gear-fill"></i> Équipements en panne ({{ equipments_panne|length }})
        </div>
        <div class="table-responsive">
            <table class="table table-sm table-hover mb-0">
                <thead>
                    <tr>
                        <th>Équipement</th>
                        <th>Salle</th>
                        <th>Catégorie</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                {% for eq in equipments_panne %}
                <tr>
                    <td><a href="{{ url_for('equipments.detail', id=eq.id) }}">{{ eq.name }}</a></td>
                    <td>{{ eq.room.name if eq.room else '—' }}</td>
                    <td>{{ eq.category.name if eq.category else '—' }}</td>
                    <td>
                        <a href="{{ url_for('interventions.create', equipment_id=eq.id) }}" class="btn btn-sm btn-outline-primary"><i class="bi bi-plus"></i> Nouvelle intervention</a>
                    </td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% endif %}
    
    <!-- Interprétations en attente -->
    {% if pending_outlook or pending_ent %}
    <div class="card border-info mb-4">
        <div class="card-header bg-info text-white">
            <i class="bi bi-robot"></i> Interprétations en attente
            {% if pending_outlook %}<span class="badge bg-light text-dark">{{ pending_outlook|length }} Outlook</span>{% endif %}
            {% if pending_ent %}<span class="badge bg-light text-dark">{{ pending_ent|length }} ENT</span>{% endif %}
        </div>
        <div class="table-responsive">
            <table class="table table-sm table-hover mb-0">
                <thead>
                    <tr>
                        <th>Source</th>
                        <th>Type</th>
                        <th>Expéditeur</th>
                        <th>Sujet</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                {% for interp in pending_outlook %}
                <tr>
                    <td><span class="badge bg-primary">Outlook</span></td>
                    <td>{{ interp.analysis_type or '—' }}</td>
                    <td>{{ interp.mail.from_name if interp.mail else '—' }}</td>
                    <td>{{ (interp.mail.subject if interp.mail else 'N/A')[:40] }}</td>
                    <td>
                        <a href="{{ url_for('outlook_dashboard.create_intervention_from_interpretation', interp_id=interp.id) }}" class="btn btn-sm btn-outline-success"><i class="bi bi-plus"></i> Créer intervention</a>
                    </td>
                </tr>
                {% endfor %}
                {% for interp in pending_ent %}
                <tr>
                    <td><span class="badge bg-info text-dark">ENT</span></td>
                    <td>{{ interp.analysis_type or '—' }}</td>
                    <td>{{ interp.message.sender_name if interp.message else '—' }}</td>
                    <td>{{ (interp.message.subject if interp.message else 'N/A')[:40] }}</td>
                    <td>
                        <a href="{{ url_for('interventions.create', from_interpretation=interp.id, source='ent') }}" class="btn btn-sm btn-outline-success"><i class="bi bi-plus"></i> Créer intervention</a>
                    </td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% endif %}
    
    <!-- Rien à afficher -->
    {% if not alerts and not urgent_interventions and not equipments_panne and not pending_outlook and not pending_ent %}
    <div class="alert alert-success">
        <i class="bi bi-check-circle"></i> Aucune alerte pour le moment. Tout est au vert !
    </div>
    {% endif %}
</div>
{% endblock %}