{% extends "base.html" %}
{% block title %}{{ title }} — 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('planning.meters') }}">Compteurs</a></li>
            <li class="breadcrumb-item active">{{ title }}</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>{{ title }}
                </div>
                <div class="card-body">
                    <form method="POST">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        
                        <div class="mb-3">
                            <label for="equipment_id" class="form-label">Équipement *</label>
                            <select class="form-select" id="equipment_id" name="equipment_id" required>
                                <option value="">-- Sélectionner un équipement --</option>
                                {% for eq in equipments %}
                                <option value="{{ eq.id }}" {% if meter and meter.equipment_id == eq.id %}selected{% endif %}>
                                    {{ eq.name }}
                                    {% if eq.room %}({{ eq.room.name }}){% endif %}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        
                        <div class="mb-3">
                            <label for="name" class="form-label">Nom du compteur *</label>
                            <input type="text" class="form-control" id="name" name="name" required
                                   value="{{ meter.name if meter else '' }}"
                                   placeholder="Ex: Compteur heures, Compteur cycles">
                        </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="hours" {% if meter and meter.meter_type == 'hours' %}selected{% endif %}>Heures</option>
                                    <option value="cycles" {% if meter and meter.meter_type == 'cycles' %}selected{% endif %}>Cycles</option>
                                    <option value="distance" {% if meter and meter.meter_type == 'distance' %}selected{% endif %}>Distance</option>
                                    <option value="custom" {% if meter and meter.meter_type == 'custom' %}selected{% endif %}>Personnalisé</option>
                                </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 if meter else 'h' }}"
                                       placeholder="Ex: h, km, cycles, m³">
                            </div>
                        </div>
                        
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="initial_value" class="form-label">Valeur initiale</label>
                                <input type="number" step="0.01" class="form-control" id="initial_value" name="initial_value"
                                       value="{{ meter.initial_value if meter else 0 }}">
                            </div>
                            <div class="col-md-6">
                                <label for="current_value" class="form-label">Valeur actuelle</label>
                                <input type="number" step="0.01" class="form-control" id="current_value" name="current_value"
                                       value="{{ meter.current_value if meter else 0 }}">
                            </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 if meter else '' }}"
                                       placeholder="Ex: 500">
                                <small class="text-muted">Valeur à partir de laquelle une alerte est générée</small>
                            </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 if meter else '' }}"
                                       placeholder="Ex: 1000">
                                <small class="text-muted">Valeur critique nécessitant une intervention</small>
                            </div>
                        </div>
                        
                        <hr>
                        <h6 class="mb-3"><i class="bi bi-tools me-1"></i>Maintenance préventive</h6>
                        
                        <div class="mb-3">
                            <label for="maintenance_interval" class="form-label">Intervalle de maintenance</label>
                            <input type="number" step="0.01" class="form-control" id="maintenance_interval" name="maintenance_interval"
                                   value="{{ meter.maintenance_interval if meter else '' }}"
                                   placeholder="Ex: 500">
                            <small class="text-muted">Tous les X {{ meter.unit if meter else 'unités' }}, une maintenance est requise</small>
                        </div>
                        
                        <div class="mb-3">
                            <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 if meter else '' }}">
                        </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('planning.meters') }}" 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>Aide
                </div>
                <div class="card-body">
                    <h6>Types de compteurs</h6>
                    <ul class="small">
                        <li><strong>Heures</strong> - Compteur d'heures de fonctionnement (motrice, groupe électrogène...)</li>
                        <li><strong>Cycles</strong> - Nombre de cycles d'ouverture/fermeture (porte, portail...)</li>
                        <li><strong>Distance</strong> - Kilométrage (véhicule...)</li>
                        <li><strong>Personnalisé</strong> - Autre unité (m³, litres...)</li>
                    </ul>
                    
                    <h6 class="mt-3">Maintenance préventive</h6>
                    <p class="small text-muted">
                        Définissez un intervalle de maintenance pour que le système vous alerte 
                        automatiquement quand l'équipement nécessite une intervention préventive.
                    </p>
                    <p class="small text-muted">
                        Exemple: vidange tous les 500 heures, remplacement filtre tous les 1000 cycles.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}