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

{% block content %}
<h1 class="mb-4">{{ title }}</h1>

<form method="POST">
    <div class="row">
        <div class="col-md-6 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5><i class="bi bi-info-circle"></i> Informations générales</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label class="form-label">Lot *</label>
                        <select name="lot_id" class="form-select" required>
                            <option value="">-- Sélectionner un lot --</option>
                            {% for lot in lots %}
                            <option value="{{ lot.id }}" {% if task and task.lot_id == lot.id %}selected{% endif %}>{{ lot.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Numéro</label>
                        <input type="text" name="task_number" class="form-control" placeholder="Ex: 201">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Nom *</label>
                        <input type="text" name="name" class="form-control" required placeholder="Ex: Vérification fenêtres">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Description</label>
                        <textarea name="periodicite" class="form-control" rows="2" placeholder="Ex. tous les ans avant l'hiver"></textarea>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Emplacement (optionnel)</label>
                        <input type="text" name="location_pattern" class="form-control" value="{{ task.location_pattern if task else '' }}" placeholder="Ex: Local chaufferie">
                        <small class="text-muted">Filtre par emplacement (correspondance partielle)</small>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-6 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5><i class="bi bi-calendar"></i> Planification</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label class="form-label">Périodicité (jours) *</label>
                        <input type="number" name="period_days" class="form-control" value="365" min="1">
                        <small class="text-muted">365 = annuel, 30 = mensuel, 7 = hebdomadaire, 1 = quotidien</small>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Durée estimée (minutes) *</label>
                        <input type="number" name="duration_minutes" class="form-control" value="{{ task.duration_minutes if task else '30' }}" required min="1">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Déclenchement</label>
                        <select name="trigger_type" class="form-select">
                            <option value="calendar">Calendrier</option><option value="meter">Compteur</option><option value="event">Événement</option><option value="weather">Météo (orage, gel…)</option><option value="season">Saison</option>
                        </select>
                    </div>
                    
                    <div class="mb-3" id="meterOptions" style="display: {% if task and task.trigger_type == 'meter' %}block{% else %}none{% endif %};">
                        <label class="form-label">Type de compteur</label>
                        <select name="meter_type_required" class="form-select">
                            <option value="hours" {% if task and task.meter_type_required == 'hours' %}selected{% endif %}>Heures</option>
                            <option value="cycles" {% if task and task.meter_type_required == 'cycles' %}selected{% endif %}>Cycles</option>
                            <option value="distance" {% if task and task.meter_type_required == 'distance' %}selected{% endif %}>Distance</option>
                            <option value="energy" {% if task and task.meter_type_required == 'energy' %}selected{% endif %}>Énergie</option>
                        </select>
                        
                        <label class="form-label mt-2">Seuil de déclenchement</label>
                        <input type="number" name="meter_threshold" class="form-control" value="{{ task.meter_threshold if task else '' }}" step="0.1">
                        <small class="text-muted">Déclencher la maintenance à cette valeur</small>
                    </div>
                    <div class="row g-2"><div class="col-md-6"><label class="form-label">Événement déclencheur</label><input name="trigger_event" class="form-control" placeholder="orage, gel, rentrée…"></div><div class="col-md-3"><label class="form-label">Mois début</label><input type="number" min="1" max="12" name="season_start_month" class="form-control"></div><div class="col-md-3"><label class="form-label">Mois fin</label><input type="number" min="1" max="12" name="season_end_month" class="form-control"></div></div>
                </div>
            </div>
            
            <div class="card mt-3">
                <div class="card-header">
                    <h5><i class="bi bi-building"></i> Gestion</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label class="form-label">Gestionnaire</label>
                        <select name="manager" class="form-select">
                            <option value="">-- Non défini --</option>
                            <option value="collège" {% if task and task.manager == 'collège' %}selected{% endif %}>Collège</option>
                            <option value="département" {% if task and task.manager == 'département' %}selected{% endif %}>Département</option>
                            <option value="externe" {% if task and task.manager == 'externe' %}selected{% endif %}>Externe</option>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Entreprise (optionnel)</label>
                        <select name="company_id" class="form-select">
                            <option value="">-- Aucune --</option>
                            {% for company in companies %}
                            <option value="{{ company.id }}" {% if task and task.company_id == company.id %}selected{% endif %}>{{ company.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <div class="form-check mb-3">
                        <input type="checkbox" name="has_contract" class="form-check-input" id="hasContract" {% if task and task.has_contract %}checked{% endif %}>
                        <label class="form-check-label" for="hasContract">Sous contrat</label>
                    </div>
                </div>
            </div>
            
            <div class="card mt-3">
                <div class="card-header">
                    <h5><i class="bi bi-shield"></i> Contraintes d'accès</h5>
                </div>
                <div class="card-body">
                    <div class="form-check mb-3">
                        <input type="checkbox" name="requires_room_access" class="form-check-input" id="requiresRoom" {% if (not task) or task.requires_room_access %}checked{% endif %}>
                        <label class="form-check-label" for="requiresRoom">Nécessite l'accès à une salle</label>
                        <small class="text-muted d-block">Vérifier la disponibilité de la salle avant planification</small>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Créneaux autorisés</label>
                        <select name="allowed_time_slots" class="form-select">
                            <option value="always" {% if (not task) or task.allowed_time_slots == 'always' %}selected{% endif %}>Toujours</option>
                            <option value="morning" {% if task and task.allowed_time_slots == 'morning' %}selected{% endif %}>Matin uniquement</option>
                            <option value="afternoon" {% if task and task.allowed_time_slots == 'afternoon' %}selected{% endif %}>Après-midi uniquement</option>
                            <option value="holiday" {% if task and task.allowed_time_slots == 'holiday' %}selected{% endif %}>Vacances uniquement</option>
                        </select>
                    </div>
                </div>
            </div>
            
            {% if task %}
            <div class="form-check mt-3">
                <input type="checkbox" name="is_active" class="form-check-input" id="isActive" {% if task.is_active %}checked{% endif %}>
                <label class="form-check-label" for="isActive">Tâche active</label>
            </div>
            {% else %}
            <input type="hidden" name="is_active" value="on">
            {% endif %}
        </div>
    </div>
    
    <div class="d-flex justify-content-between">
        <a href="{{ url_for('planning.tasks') }}" class="btn btn-secondary">
            <i class="bi bi-arrow-left"></i> Annuler
        </a>
        <button type="submit" class="btn btn-primary">
            <i class="bi bi-check"></i> Enregistrer
        </button>
    </div>
</form>

<script>
document.querySelector('select[name="trigger_type"]').addEventListener('change', function() {
    document.getElementById('meterOptions').style.display = this.value === 'meter' ? 'block' : 'none';
});
</script>
{% endblock %}
