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

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1><i class="bi bi-speedometer2"></i> Compteurs</h1>
    <a href="{{ url_for('meters.new') }}" class="btn btn-primary">
        <i class="bi bi-plus"></i> Nouveau compteur
    </a>
</div>

<table class="table table-hover">
    <thead class="table-light">
        <tr>
            <th>Rattachement</th>
            <th>Nom</th>
            <th>Type</th>
            <th>Valeur actuelle</th>
            <th>Seuil warning</th>
            <th>Seuil critique</th>
            <th>Maintenance</th>
            <th>Statut</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        {% for meter in meters %}
        <tr>
            <td>{% if meter.equipment %}{{ meter.equipment.name }}{% 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 }}{% else %}—{% endif %}</td>
            <td><strong>{{ meter.name }}</strong></td>
            <td><span class="badge bg-secondary">{{ meter.meter_type }}</span></td>
            <td>{{ meter.current_value }} {{ meter.unit }}</td>
            <td>{{ meter.warning_threshold or '-' }} {{ meter.unit if meter.warning_threshold else '' }}</td>
            <td>{{ meter.critical_threshold or '-' }} {{ meter.unit if meter.critical_threshold else '' }}</td>
            <td>
                {% if meter.maintenance_interval %}
                    Tous les {{ meter.maintenance_interval }} {{ 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">Attention</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">
                Aucun compteur. <a href="{{ url_for('meters.new') }}">Créer le premier</a>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% endblock %}
