{% extends "base.html" %}
{% block title %}Tous les compteurs — GMAO{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4><i class="bi bi-speedometer2 me-2"></i>Gestion Globale des Compteurs</h4>
        <a href="{{ url_for('dashboard.index') }}" class="btn btn-outline-secondary">Retour Dashboard</a>
    </div>

    {% if grouped %}
    {% for type_name, meters in grouped.items() %}
    <div class="card mb-4 shadow-sm">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0"><i class="bi bi-tag"></i> {{ type_name }}</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Équipement</th>
                            <th>Unité</th>
                            <th>Dernière Valeur</th>
                            <th>Date</th>
                            <th class="text-center">Alerte</th>
                            <th class="text-end">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in meters %}
                        <tr>
                            <td>
                                <a href="{{ url_for('equipments.detail', id=item.equipment.id) }}" class="text-decoration-none fw-bold">
                                    {{ item.equipment.name }}
                                </a>
                            </td>
                            <td>{{ item.meter.unit }}</td>
                            <td>
                                <span class="badge bg-light text-dark border">
                                    {{ item.last_value }} {{ item.meter.unit }}
                                </span>
                            </td>
                            <td class="small text-muted">
                                {{ item.last_date|datetime_fmt if item.last_date else '—' }}
                            </td>
                            <td class="text-center">
                                <form action="{{ url_for('meters.toggle_alert', meter_id=item.meter.id) }}" method="POST" style="display:inline;">
                                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                <div class="form-check form-switch d-inline-block">
                                    <input class="form-check-input" type="checkbox" {% if item.meter.alert_enabled %}checked{% endif %} onchange="this.form.submit()">
                                </div>
                                </form>
                                <!-- Correction: On utilise un petit hack JS pour le toggle car le checkbox n'est pas lié nativement à l'état initial dans l'attribut 'checked' sans logique -->
                            </td>
                            <td class="text-end">
                                <div class="btn-group">
                                    <a href="{{ url_for('meters.add_reading', meter_id=item.meter.id) }}" class="btn btn-sm btn-success">
                                        <i class="bi bi-pencil-square"></i> Saisir
                                    </a>
                                    <a href="{{ url_for('meters.history', meter_id=item.meter.id) }}" class="btn btn-sm btn-outline-secondary">
                                        <i class="bi bi-clock-history"></i> Historique
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% endfor %}
    {% else %}
    <div class="text-center py-5">
        <i class="bi bi-speedometer2 display-1 text-muted"></i>
        <p class="text-muted mt-3">Aucun compteur enregistré dans le système.</p>
    </div>
    {% endif %}
</div>
{% endblock %}
