{% extends "base.html" %}
{% block title %}{{ meter.name }}{% if meter.equipment %} — {{ meter.equipment.name }}{% endif %} — GMAO{% endblock %}

{% block content %}
<div class="container-fluid">
    <nav aria-label="breadcrumb" class="mb-3">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ url_for('meters.index') }}">Compteurs</a></li>
            <li class="breadcrumb-item">
                <a href="{{ url_for('equipments.detail', id=meter.equipment.id) if meter.equipment else '#' }}">{{ meter.equipment.name if meter.equipment else 'Sans equipement' }}</a>
            </li>
            <li class="breadcrumb-item active">{{ meter.name }}</li>
        </ol>
    </nav>
    
    <div class="row">
        <div class="col-lg-4">
            <div class="card mb-3">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <span><i class="bi bi-speedometer2 me-1"></i>{{ meter.name }}</span>
                    <a href="{{ url_for('meters.edit', meter_id=meter.id) }}" class="btn btn-sm btn-outline-primary">
                        <i class="bi bi-pencil me-1"></i>Modifier
                    </a>
                </div>
                <div class="card-body">
                    <dl class="row mb-0">
                        <dt class="col-sm-5">Équipement</dt>
                        <dd class="col-sm-7">
                            <a href="{{ url_for('equipments.detail', id=meter.equipment.id) if meter.equipment else '#' }}">
                                {{ meter.equipment.name if meter.equipment else 'Sans equipement' }}
                            </a>
                        </dd>
                        
                        <dt class="col-sm-5">Type</dt>
                        <dd class="col-sm-7">{{ meter.meter_type }}</dd>
                        
                        <dt class="col-sm-5">Unité</dt>
                        <dd class="col-sm-7">{{ meter.unit }}</dd>
                        
                        <dt class="col-sm-5">Valeur initiale</dt>
                        <dd class="col-sm-7">{{ meter.initial_value }}</dd>
                        
                        <dt class="col-sm-5">Valeur actuelle</dt>
                        <dd class="col-sm-7">
                            <strong class="fs-4">{{ meter.current_value }}</strong> {{ meter.unit }}
                        </dd>
                        
                        <dt class="col-sm-5">Intervalle relevé</dt>
                        <dd class="col-sm-7">
                            {% if meter.reading_interval_days %}
                                Tous les {{ meter.reading_interval_days }} jours
                            {% else %}
                                —
                            {% endif %}
                        </dd>
                        
                        <dt class="col-sm-5">Dernier relevé</dt>
                        <dd class="col-sm-7">
                            {% if meter.last_reading_date %}
                                {{ meter.last_reading_date.strftime('%d/%m/%Y') }}
                            {% else %}
                                —
                            {% endif %}
                        </dd>
                        
                        <dt class="col-sm-5">Seuil alerte</dt>
                        <dd class="col-sm-7">{{ meter.warning_threshold or '—' }}</dd>
                        
                        <dt class="col-sm-5">Seuil critique</dt>
                        <dd class="col-sm-7">{{ meter.critical_threshold or '—' }}</dd>
                        
                        <dt class="col-sm-5">Intervalle maintenance</dt>
                        <dd class="col-sm-7">
                            {% if meter.maintenance_interval %}
                                {{ meter.maintenance_interval }} {{ meter.unit }}
                            {% else %}
                                —
                            {% endif %}
                        </dd>
                        
                        <dt class="col-sm-5">Dernière maintenance</dt>
                        <dd class="col-sm-7">{{ meter.last_maintenance_value or '—' }}</dd>
                        
                        <dt class="col-sm-5">Prochaine maintenance</dt>
                        <dd class="col-sm-7">
                            {% if meter.next_maintenance_at %}
                                {{ meter.next_maintenance_at }} {{ meter.unit }}
                            {% else %}
                                —
                            {% endif %}
                        </dd>
                        
                        <dt class="col-sm-5">Statut</dt>
                        <dd class="col-sm-7">
                            {% 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 requise</span>
                            {% else %}
                                <span class="badge bg-success">OK</span>
                            {% endif %}
                        </dd>
                    </dl>
                </div>
            </div>
            
            <!-- Formulaire de relevé -->
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-pencil me-1"></i>Nouveau relevé
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ url_for('meters.add_reading', meter_id=meter.id) }}">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        
                        <div class="mb-3">
                            <label for="value" class="form-label">Valeur *</label>
                            <div class="input-group">
                                <input type="number" step="0.01" class="form-control" id="value" name="value" required
                                       value="{{ meter.current_value }}">
                                <span class="input-group-text">{{ meter.unit }}</span>
                            </div>
                        </div>
                        <div class="form-check mb-2"><input class="form-check-input" type="checkbox" value="1" name="is_reset" id="is_reset"><label class="form-check-label" for="is_reset">Remise à zéro / remplacement du compteur</label></div>
                        <div class="mb-3"><input class="form-control" name="reset_reason" placeholder="Motif obligatoire si remise à zéro"></div>
                        
                        <div class="mb-3">
                            <label for="notes" class="form-label">Notes</label>
                            <textarea class="form-control" id="notes" name="notes" rows="2"></textarea>
                        </div>
                        
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="bi bi-check-lg me-1"></i>Enregistrer
                        </button>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-clock-history me-1"></i>Historique des relevés
                </div>
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Valeur</th>
                                <th>Variation</th>
                                <th>Par</th>
                                <th>Notes</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for reading in readings %}
                            <tr>
                                <td>{{ reading.reading_date|datetime_fmt if reading.reading_date else '-' }}</td>
                                <td><strong>{{ reading.value }}</strong> {{ meter.unit }}</td>
                                <td>
                                    {% if loop.index0 < loop.length - 1 %}
                                        {% set diff = reading.value - readings[loop.index0 + 1].value %}
                                        {% if diff > 0 %}
                                            <span class="text-success">+{{ diff | round(2) }}</span>
                                        {% elif diff < 0 %}
                                            <span class="text-danger">{{ diff | round(2) }}</span>
                                        {% else %}
                                            <span class="text-muted">0</span>
                                        {% endif %}
                                    {% endif %}
                                </td>
                                <td>{{ reading.read_by.username if reading.read_by else '—' }}</td>
                                <td>{% if reading.is_reset %}<span class="badge bg-warning text-dark">RAZ</span> {{ reading.reset_reason }}{% else %}{{ reading.notes or '—' }}{% endif %}</td>
                            </tr>
                            {% else %}
                            <tr>
                                <td colspan="5" class="text-center text-muted py-4">
                                    <i class="bi bi-clock-history fs-1"></i>
                                    <p>Aucun relevé enregistré</p>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
