{% extends "base.html" %}
{% block title %}Modifier la tâche — GMAO Collège{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ url_for('planning.tasks') }}">Tâches préventives</a></li>
                    <li class="breadcrumb-item active">Modifier</li>
                </ol>
            </nav>
            <h1 class="h3">
                <i class="bi bi-pencil me-2"></i>
                Modifier la tâche #{{ task.num_tache or task.id }}
            </h1>
        </div>
    </div>
    
    <div class="row">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-info-circle me-1"></i> Informations
                </div>
                <div class="card-body">
                    <form method="POST">
                        <div class="mb-3">
                            <label class="form-label">N° Tâche</label>
                            <input type="text" class="form-control" value="{{ task.num_tache or '' }}" disabled>
                            <small class="text-muted">Non modifiable (importé du lot)</small>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">Description</label>
                            <textarea class="form-control" rows="3" disabled>{{ task.tache or '' }}</textarea>
                            <small class="text-muted">Non modifiable (importé du lot)</small>
                        </div>
                        
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="duree_minutes" class="form-label">Durée estimée (minutes)</label>
                                <input type="number" class="form-control" id="duree_minutes" name="duree_minutes" 
                                       value="{{ task.duree_minutes or '' }}" min="1" max="480">
                                <small class="text-muted">Durée en minutes pour réaliser cette tâche</small>
                            </div>
                            <div class="col-md-6">
                                <label for="jours_entre_interventions" class="form-label">Jours entre interventions</label>
                                <input type="number" class="form-control" id="jours_entre_interventions" name="jours_entre_interventions" 
                                       value="{{ task.jours_entre_interventions or '' }}" min="1">
                                <small class="text-muted">Ou laissez vide si périodicité textuelle</small>
                            </div>
                        </div>
                        <div class="card bg-light mb-3"><div class="card-body"><h6>Déclenchement</h6><div class="row g-2">
                          <div class="col-md-4"><label class="form-label">Type</label><select name="trigger_type" class="form-select">{% for value,label in [('calendar','Calendrier'),('meter','Compteur'),('event','Événement'),('weather','Météo'),('season','Saison')] %}<option value="{{ value }}" {% if task.trigger_type == value %}selected{% endif %}>{{ label }}</option>{% endfor %}</select></div>
                          <div class="col-md-4"><label class="form-label">Événement</label><input name="trigger_event" class="form-control" value="{{ task.trigger_event or '' }}" placeholder="orage, gel…"></div>
                          <div class="col-md-4"><label class="form-label">Seuil compteur</label><input type="number" step="0.01" name="meter_threshold" class="form-control" value="{{ task.meter_threshold or '' }}"></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" value="{{ task.season_start_month or '' }}"></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" value="{{ task.season_end_month or '' }}"></div>
                          <div class="col-md-3"><label class="form-label">Anticipation (jours)</label><input type="number" min="0" name="advance_days" class="form-control" value="{{ task.advance_days or 0 }}"></div>
                          <div class="col-md-3 d-flex align-items-end"><div class="form-check mb-2"><input class="form-check-input" type="checkbox" name="is_active" id="active" {% if task.is_active %}checked{% endif %}><label class="form-check-label" for="active">Active</label></div></div>
                        </div></div></div>
                        
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="periodicite" class="form-label">Périodicité</label>
                                <select class="form-select" id="periodicite" name="periodicite">
                                    <option value="">-- Sélectionner --</option>
                                    <option value="Quotidien" {% if task.periodicite == 'Quotidien' %}selected{% endif %}>Quotidien</option>
                                    <option value="Hebdomadaire" {% if task.periodicite == 'Hebdomadaire' %}selected{% endif %}>Hebdomadaire</option>
                                    <option value="Mensuel" {% if task.periodicite == 'Mensuel' %}selected{% endif %}>Mensuel</option>
                                    <option value="Trimestriel" {% if task.periodicite == 'Trimestriel' %}selected{% endif %}>Trimestriel</option>
                                    <option value="Semestriel" {% if task.periodicite == 'Semestriel' %}selected{% endif %}>Semestriel</option>
                                    <option value="Annuel" {% if task.periodicite == 'Annuel' %}selected{% endif %}>Annuel</option>
                                    <option value="Selon besoin" {% if task.periodicite == 'Selon besoin' %}selected{% endif %}>Selon besoin</option>
                                    <option value="Selon préconisation constructeur" {% if task.periodicite == 'Selon préconisation constructeur' %}selected{% endif %}>Selon préconisation constructeur</option>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label for="type_maintenance" class="form-label">Type de maintenance</label>
                                <select class="form-select" id="type_maintenance" name="type_maintenance">
                                    <option value="">-- Sélectionner --</option>
                                    <option value="préventive" {% if task.type_maintenance == 'préventive' %}selected{% endif %}>Préventive</option>
                                    <option value="curative" {% if task.type_maintenance == 'curative' %}selected{% endif %}>Curative</option>
                                    <option value="corrective" {% if task.type_maintenance == 'corrective' %}selected{% endif %}>Corrective</option>
                                </select>
                            </div>
                        </div>
                        
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="gestionnaire" class="form-label">Gestionnaire</label>
                                <input type="text" class="form-control" id="gestionnaire" name="gestionnaire" 
                                       value="{{ task.gestionnaire or '' }}">
                            </div>
                            <div class="col-md-6">
                                <div class="form-check mt-4">
                                    <input type="checkbox" class="form-check-input" id="contrat" name="contrat" 
                                           {% if task.contrat %}checked{% endif %}>
                                    <label class="form-check-label" for="contrat">
                                        Couvert par un contrat
                                    </label>
                                </div>
                            </div>
                        </div>
                        
                        <div class="d-flex gap-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-check me-1"></i> Enregistrer
                            </button>
                            <a href="{{ url_for('planning.tasks') }}" class="btn btn-outline-secondary">
                                <i class="bi bi-x me-1"></i> Annuler
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-info-circle me-1"></i> Lot associé
                </div>
                <div class="card-body">
                    {% if task.lot %}
                    <p><strong>Lot:</strong> <a href="{{ url_for('lots.detail', id=task.lot.id) }}">{{ task.lot.name }}</a></p>
                    {% if task.lot.category %}
                    <p><strong>Catégorie:</strong> {{ task.lot.category.name }}</p>
                    {% endif %}
                    {% endif %}
                    <p class="text-muted small">
                        La modification de la durée sera appliquée aux nouvelles tâches planifiées.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
