{% extends "base.html" %}
{% block title %}{{ meter.name }} — Modifier — 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('meters.detail', meter_id=meter.id) }}">{{ meter.name }}</a></li>
            <li class="breadcrumb-item active">Modifier</li>
        </ol>
    </nav>
    
    <div class="row">
        <div class="col-lg-6">
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-speedometer2 me-1"></i>Modifier le compteur
                </div>
                <div class="card-body">
                    <form method="POST">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        
                        <div class="mb-3">
                            <label for="scope_type" class="form-label">Type de rattachement</label>
                            <select class="form-select" id="scope_type" name="scope_type">
                                <option value="equipment" {% if meter.equipment %}selected{% endif %}>Équipement</option>
                                <option value="building" {% if meter.building %}selected{% endif %}>Bâtiment</option>
                                <option value="zone" {% if meter.zone %}selected{% endif %}>Zone</option>
                                <option value="room" {% if meter.room %}selected{% endif %}>Local</option>
                                <option value="housing_unit" {% if meter.housing_unit %}selected{% endif %}>Logement</option>
                            </select>
                            <label for="scope_id" class="form-label mt-2">Élément patrimoine</label>
                            <select class="form-select" id="scope_id" name="scope_id">
                                {% for eq in equipments %}<option data-scope="equipment" value="{{ eq.id }}" {% if meter.equipment_id == eq.id %}selected{% endif %}>Équipement — {{ eq.name }}</option>{% endfor %}
                                {% for item in buildings %}<option data-scope="building" value="{{ item.id }}" {% if meter.building_id == item.id %}selected{% endif %}>Bâtiment — {{ item.name }}</option>{% endfor %}
                                {% for item in zones %}<option data-scope="zone" value="{{ item.id }}" {% if meter.zone_id == item.id %}selected{% endif %}>Zone — {{ item.name }}</option>{% endfor %}
                                {% for item in rooms %}<option data-scope="room" value="{{ item.id }}" {% if meter.room_id == item.id %}selected{% endif %}>Local — {{ item.name }}</option>{% endfor %}
                                {% for item in housing_units %}<option data-scope="housing_unit" value="{{ item.id }}" {% if meter.housing_unit_id == item.id %}selected{% endif %}>Logement — {{ item.name }}</option>{% endfor %}
                            </select>
                        </div>
                        
                        <div class="mb-3">
                            <label for="name" class="form-label">Nom *</label>
                            <input type="text" class="form-control" id="name" name="name" required
                                   value="{{ meter.name }}">
                        </div>

                        <div class="mb-3">
                            <label for="parent_id" class="form-label">Compteur parent</label>
                            <select class="form-select" id="parent_id" name="parent_id">
                                <option value="">Aucun — compteur racine</option>
                                {% for parent in meters %}
                                    {% if parent.id != meter.id %}
                                    <option value="{{ parent.id }}" {% if meter.parent_id == parent.id %}selected{% endif %}>
                                        {{ parent.name }}{% if parent.unit %} ({{ parent.unit }}){% endif %}
                                    </option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                            <small class="text-muted">Le compteur ne peut pas être son propre parent ni appartenir à un cycle.</small>
                        </div>
                        
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="meter_type" class="form-label">Type</label>
                                <select class="form-select" id="meter_type" name="meter_type">
                                    <option value="eau" {% if meter.meter_type == 'eau' %}selected{% endif %}>Eau</option>
                                    <option value="gaz" {% if meter.meter_type == 'gaz' %}selected{% endif %}>Gaz</option>
                                    <option value="electricite" {% if meter.meter_type == 'electricite' %}selected{% endif %}>Électricité</option>
                                    <option value="photocopieur" {% if meter.meter_type == 'photocopieur' %}selected{% endif %}>Photocopieur</option>
                                    <option value="hours" {% if meter.meter_type == 'hours' %}selected{% endif %}>Heures</option>
                                    <option value="cycles" {% if meter.meter_type == 'cycles' %}selected{% endif %}>Cycles</option>
                                    <option value="distance" {% if meter.meter_type == 'distance' %}selected{% endif %}>Distance</option>
                                    <option value="custom" {% if meter.meter_type == 'custom' %}selected{% endif %}>Personnalisé</option>
                                    {% if meter.meter_type not in ['eau', 'gaz', 'electricite', 'photocopieur', 'hours', 'cycles', 'distance', 'custom'] %}
                                    <option value="{{ meter.meter_type }}" selected>Autre — {{ meter.meter_type }}</option>
                                    {% endif %}
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label for="unit" class="form-label">Unité</label>
                                <input type="text" class="form-control" id="unit" name="unit"
                                       value="{{ meter.unit }}" placeholder="Ex: h, km, m³">
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="contract_role" class="form-label">Rôle contractuel photocopieur</label>
                            <select class="form-select" id="contract_role" name="contract_role">
                                <option value="NONE" {% if meter.contract_role == 'NONE' %}selected{% endif %}>Aucun</option>
                                <option value="COPIER_BW" {% if meter.contract_role == 'COPIER_BW' %}selected{% endif %}>Photocopieur N&amp;B</option>
                                <option value="COPIER_COLOR" {% if meter.contract_role == 'COPIER_COLOR' %}selected{% endif %}>Photocopieur couleur</option>
                            </select>
                            <small class="text-muted">Rôle explicite utilisé par les quotas contractuels. Les compteurs ambigus restent « Aucun ».</small>
                        </div>
                        
                        <div class="mb-3">
                            <label for="current_value" class="form-label">Valeur actuelle</label>
                            <div class="input-group">
                                <input type="number" step="0.01" class="form-control" id="current_value"
                                       value="{{ meter.current_value }}" readonly>
                                <span class="input-group-text">{{ meter.unit }}</span>
                            </div>
                        </div>
                        
                        <hr>
                        <h6 class="mb-3"><i class="bi bi-calendar-check me-1"></i>Intervalle de relevé</h6>
                        
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="reading_interval_days" class="form-label">Tous les (jours)</label>
                                <input type="number" class="form-control" id="reading_interval_days" name="reading_interval_days"
                                       value="{{ meter.reading_interval_days or '' }}" placeholder="Ex: 30">
                                <small class="text-muted">Fréquence de relevé recommandée</small>
                            </div>
                            <div class="col-md-6">
                                <label for="last_reading_date" class="form-label">Dernier relevé</label>
                                <input type="date" class="form-control" id="last_reading_date" name="last_reading_date"
                                       value="{{ meter.last_reading_date.strftime('%Y-%m-%d') if meter.last_reading_date else '' }}">
                            </div>
                        </div>
                        
                        <hr>
                        <h6 class="mb-3"><i class="bi bi-exclamation-triangle me-1"></i>Seuils d'alerte</h6>
                        
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="warning_threshold" class="form-label">Seuil alerte</label>
                                <input type="number" step="0.01" class="form-control" id="warning_threshold" name="warning_threshold"
                                       value="{{ meter.warning_threshold or '' }}">
                            </div>
                            <div class="col-md-6">
                                <label for="critical_threshold" class="form-label">Seuil critique</label>
                                <input type="number" step="0.01" class="form-control" id="critical_threshold" name="critical_threshold"
                                       value="{{ meter.critical_threshold or '' }}">
                            </div>
                        </div>
                        
                        <hr>
                        <h6 class="mb-3"><i class="bi bi-tools me-1"></i>Maintenance préventive</h6>
                        
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="maintenance_interval" class="form-label">Intervalle maintenance</label>
                                <input type="number" step="0.01" class="form-control" id="maintenance_interval" name="maintenance_interval"
                                       value="{{ meter.maintenance_interval or '' }}" placeholder="Ex: 500">
                                <small class="text-muted">Tous les X {{ meter.unit }}</small>
                            </div>
                            <div class="col-md-6">
                                <label for="last_maintenance_value" class="form-label">Valeur dernière maintenance</label>
                                <input type="number" step="0.01" class="form-control" id="last_maintenance_value" name="last_maintenance_value"
                                       value="{{ meter.last_maintenance_value or '' }}">
                            </div>
                        </div>
                        
                        <div class="d-flex gap-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-check-lg me-1"></i>Enregistrer
                            </button>
                            <a href="{{ url_for('meters.detail', meter_id=meter.id) }}" class="btn btn-outline-secondary">
                                <i class="bi bi-x-lg me-1"></i>Annuler
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="col-lg-6">
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-info-circle me-1"></i>Informations
                </div>
                <div class="card-body">
                    <h6>Intervalle de relevé</h6>
                    <p class="text-muted small">
                        Définissez la fréquence à laquelle vous souhaitez effectuer les relevés.
                        Il s'agit d'une fréquence indicative. La planification automatique des échéances sera gérée par le module de relevés planifiés.
                    </p>
                    
                    <h6 class="mt-3">Prochain relevé</h6>
                    {% if meter.reading_interval_days and meter.last_reading_date %}
                        {% set next_date = meter.last_reading_date + timedelta(days=meter.reading_interval_days) %}
                        <p class="{% if next_date < today %}text-danger{% else %}text-success{% endif %}">
                            <strong>{{ next_date.strftime('%d/%m/%Y') }}</strong>
                            {% if next_date < today %}
                                <span class="badge bg-danger">Indication calculée : date dépassée</span>
                            {% endif %}
                        </p>
                    {% else %}
                        <p class="text-muted">Non configuré</p>
                    {% endif %}
                    
                    <h6 class="mt-3">Types de compteurs</h6>
                    <ul class="small">
                        <li><strong>Eau</strong> — index physique en m³</li>
                        <li><strong>Gaz</strong> — index physique en m³</li>
                        <li><strong>Électricité</strong> — unité selon le compteur</li>
                        <li><strong>Photocopieur</strong> — pages N&amp;B ou couleur</li>
                        <li><strong>Heures</strong> - Compteur d'heures (motrice, groupe électrogène...)</li>
                        <li><strong>Cycles</strong> - Nombre d'ouvertures/fermetures</li>
                        <li><strong>Distance</strong> - Kilométrage (véhicule...)</li>
                        <li><strong>Personnalisé / autre</strong> - Autre unité (m³, litres...)</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
const type = document.getElementById('scope_type'), target = document.getElementById('scope_id');
function filterScopes() { const value = type.value; [...target.options].forEach(o => { o.hidden = o.dataset.scope !== value; }); }
type.addEventListener('change', filterScopes); filterScopes();
</script>
{% endblock %}
