{% extends "base.html" %}
{% block title %}Compteurs — GMAO{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1><i class="bi bi-speedometer2"></i> Compteurs</h1>
            <p class="text-muted mb-0">Suivi des heures de fonctionnement, cycles, et autres métriques</p>
        </div>
        <div>
            <a href="{{ url_for('meters.new') }}" class="btn btn-primary">
                <i class="bi bi-plus-circle me-1"></i>Nouveau compteur
            </a>
        </div>
    </div>
    
    <!-- Statistiques -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card">
                <div class="card-body text-center">
                    <h2 class="mb-0">{{ total_count }}</h2>
                    <small class="text-muted">Total</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-warning">
                <div class="card-body text-center">
                    <h2 class="mb-0 text-warning">{{ warning_count }}</h2>
                    <small class="text-muted">Alertes</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-danger">
                <div class="card-body text-center">
                    <h2 class="mb-0 text-danger">{{ critical_count }}</h2>
                    <small class="text-muted">Critiques</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-info">
                <div class="card-body text-center">
                    <h2 class="mb-0 text-info">{{ maintenance_count }}</h2>
                    <small class="text-muted">Maintenance requise</small>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Liste des compteurs -->
    <div class="card">
        <div class="card-header">
            <i class="bi bi-list-ul me-1"></i>Tous les compteurs
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>Rattachement</th>
                        <th>Compteur</th>
                        <th>Type</th>
                        <th>Valeur</th>
                        <th>Seuil alerte</th>
                        <th>Seuil critique</th>
                        <th>Prochaine maintenance</th>
                        <th>Statut</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for meter in meters %}
                    <tr class="{{ 'table-warning' if meter.warning_level == 'warning' else '' }}{{ 'table-danger' if meter.warning_level == 'critical' else '' }}">
                        <td>
                            {% if meter.equipment %}<a href="{{ url_for('equipments.detail', id=meter.equipment.id) }}">{{ meter.equipment.name }}</a>
                            {% elif meter.building %}Bâtiment — {{ meter.building.name }}
                            {% elif meter.zone %}Zone — {{ meter.zone.name }}
                            {% elif meter.room %}Local — {{ meter.room.name }}
                            {% elif meter.housing_unit %}Logement — {{ meter.housing_unit.name }}{% endif %}
                        </td>
                        <td>
                            <a href="{{ url_for('meters.detail', meter_id=meter.id) }}">
                                {{ meter.name }}
                            </a>
                        </td>
                        <td>
                            <span class="badge bg-secondary">{{ meter.meter_type }}</span>
                        </td>
                        <td>
                            <strong>{{ meter.current_value }}</strong> {{ meter.unit }}
                        </td>
                        <td>{{ meter.warning_threshold or '—' }}</td>
                        <td>{{ meter.critical_threshold or '—' }}</td>
                        <td>
                            {% if meter.next_maintenance_at %}
                                {{ meter.next_maintenance_at }} {{ meter.unit }}
                            {% else %}
                                —
                            {% endif %}
                        </td>
                        <td>
                            {% if meter.warning_level == 'critical' %}
                                <span class="badge bg-danger">Critique</span>
                            {% elif meter.warning_level == 'warning' %}
                                <span class="badge bg-warning text-dark">Alerte</span>
                            {% elif meter.warning_level == 'maintenance' %}
                                <span class="badge bg-info">Maintenance</span>
                            {% else %}
                                <span class="badge bg-success">OK</span>
                            {% endif %}
                        </td>
                        <td>
                            <a href="{{ url_for('meters.detail', meter_id=meter.id) }}" class="btn btn-sm btn-outline-primary me-1">
                                <i class="bi bi-eye"></i>
                            </a>
                            <a href="{{ url_for('meters.edit', meter_id=meter.id) }}" class="btn btn-sm btn-outline-secondary">
                                <i class="bi bi-pencil"></i>
                            </a>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="9" class="text-center text-muted py-4">
                            <i class="bi bi-speedometer2 fs-1"></i>
                            <p>Aucun compteur enregistré</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}
