{% extends "base.html" %}
{% block title %}{% if task %}Modifier{% else %}Ajouter{% endif %} une tâche — {{ equipment.name }} — 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('equipments.index') }}">Équipements</a></li>
                    <li class="breadcrumb-item"><a href="{{ url_for('equipments.detail', id=equipment.id) }}">{{ equipment.name }}</a></li>
                    <li class="breadcrumb-item"><a href="{{ url_for('equipments_scheduled.scheduled_tasks', id=equipment.id) }}">Tâches planifiées</a></li>
                    <li class="breadcrumb-item active">{% if task %}Modifier{% else %}Ajouter{% endif %}</li>
                </ol>
            </nav>
            <h1><i class="bi bi-calendar-plus me-2"></i>{% if task %}Modifier la tâche{% else %}Ajouter une tâche{% endif %}</h1>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <form method="POST">
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">Tâche</h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <label for="preventive_task_id" class="form-label">Tâche préventive associée</label>
                            <select name="preventive_task_id" id="preventive_task_id" class="form-select">
                                <option value="">-- Tâche manuelle --</option>
                                {% for pt in equipment.preventive_tasks if equipment.preventive_tasks %}
                                <option value="{{ pt.id }}" {% if task and task.preventive_task_id == pt.id %}selected{% endif %}>
                                    {{ pt.name }} ({{ pt.period_days }} jours)
                                </option>
                                {% endfor %}
                            </select>
                            {% if not equipment.preventive_tasks %}
                            <small class="text-muted">Aucune tâche préventive définie pour cet équipement.</small>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="room_id" class="form-label">Salle</label>
                            <select name="room_id" id="room_id" class="form-select">
                                <option value="">-- Héritée de l'équipement --</option>
                                {% for room in rooms %}
                                <option value="{{ room.id }}" {% if task and task.room_id == room.id %}selected{% endif %}>
                                    {{ room.name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>

                        <div class="mb-3">
                            <label for="notes" class="form-label">Notes</label>
                            <textarea name="notes" id="notes" class="form-control" rows="2">{% if task %}{{ task.notes }}{% endif %}</textarea>
                        </div>
                    </div>
                </div>

                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">Planification</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-4 mb-3">
                                <label for="scheduled_date" class="form-label">Date planifiée</label>
                                <input type="date" name="scheduled_date" id="scheduled_date" class="form-control"
                                    value="{% if task and task.scheduled_date %}{{ task.scheduled_date.strftime('%Y-%m-%d') }}{% endif %}">
                            </div>
                            <div class="col-md-4 mb-3">
                                <label for="scheduled_start" class="form-label">Heure début</label>
                                <input type="time" name="scheduled_start" id="scheduled_start" class="form-control"
                                    value="{% if task and task.scheduled_start %}{{ task.scheduled_start.strftime('%H:%M') }}{% endif %}">
                            </div>
                            <div class="col-md-4 mb-3">
                                <label for="scheduled_end" class="form-label">Heure fin</label>
                                <input type="time" name="scheduled_end" id="scheduled_end" class="form-control"
                                    value="{% if task and task.scheduled_end %}{{ task.scheduled_end.strftime('%H:%M') }}{% endif %}">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="estimated_duration" class="form-label">Durée estimée (minutes)</label>
                                <input type="number" name="estimated_duration" id="estimated_duration" class="form-control"
                                    value="{% if task %}{{ task.estimated_duration or 30 }}{% else %}30{% endif %}" min="5" step="5">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="status" class="form-label">Statut</label>
                                <select name="status" id="status" class="form-select">
                                    <option value="planned" {% if task and task.status == 'planned' %}selected{% endif %}>Planifiée</option>
                                    <option value="in_progress" {% if task and task.status == 'in_progress' %}selected{% endif %}>En cours</option>
                                    <option value="completed" {% if task and task.status == 'completed' %}selected{% endif %}>Terminée</option>
                                    <option value="postponed" {% if task and task.status == 'postponed' %}selected{% endif %}>Reportée</option>
                                    <option value="cancelled" {% if task and task.status == 'cancelled' %}selected{% endif %}>Annulée</option>
                                </select>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="assigned_to_id" class="form-label">Technicien</label>
                                <select name="assigned_to_id" id="assigned_to_id" class="form-select">
                                    <option value="">-- Personne assigné --</option>
                                    {% for tech in technicians %}
                                    <option value="{{ tech.id }}" {% if task and task.assigned_to_id == tech.id %}selected{% endif %}>
                                        {{ tech.name or tech.username }}
                                    </option>
                                    {% endfor %}
                                </select>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="company_id" class="form-label">Entreprise</label>
                                <select name="company_id" id="company_id" class="form-select">
                                    <option value="">-- Aucune entreprise --</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>
                    </div>
                </div>

                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-check-circle me-1"></i>Enregistrer
                    </button>
                    <a href="{{ url_for('equipments_scheduled.scheduled_tasks', id=equipment.id) }}" class="btn btn-outline-secondary">
                        Annuler
                    </a>
                </div>
            </form>
        </div>

        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-info-circle me-2"></i>Informations</h5>
                </div>
                <div class="card-body">
                    <p class="text-muted">
                        <strong>Équipement:</strong><br>
                        {{ equipment.name }}<br>
                        {% if equipment.effective_room %}
                        <strong>Salle:</strong> {{ equipment.effective_room.name }}
                        {% endif %}
                    </p>
                    <hr>
                    <p class="text-muted">
                        <strong>Tâches préventives:</strong><br>
                        {% if equipment.preventive_tasks %}
                        {{ equipment.preventive_tasks|length }} tâche(s) définie(s)
                        {% else %}
                        Aucune tâche préventive
                        {% endif %}
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
