{% extends "base.html" %}
{% block title %}Tâches planifiées — {{ 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 active">Tâches planifiées</li>
                </ol>
            </nav>
            <div class="d-flex justify-content-between align-items-center">
                <h1><i class="bi bi-calendar-check me-2"></i>Tâches planifiées</h1>
                <div>
                    {% if equipment.effective_lot or equipment.effective_category %}
                    <form action="{{ url_for('equipments.init_scheduled_tasks', id=equipment.id) }}" method="POST" style="display: inline;">
                        <button type="submit" class="btn btn-outline-success me-2" title="Créer les tâches depuis le lot/catégorie hérités">
                            <i class="bi bi-magic me-1"></i>Initialiser depuis lot/catégorie
                        </button>
                    </form>
                    {% endif %}
                    <a href="{{ url_for('equipments_scheduled.new_scheduled_task', id=equipment.id) }}" class="btn btn-primary">
                        <i class="bi bi-plus-circle me-1"></i>Ajouter une tâche
                    </a>
                </div>
            </div>
        </div>
    </div>

    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            {% for category, message in messages %}
            <div class="alert alert-{{ 'danger' if category == 'error' else 'success' if category == 'success' else 'info' }}">
                {{ message }}
            </div>
            {% endfor %}
        {% endif %}
    {% endwith %}

    <!-- Informations équipement -->
    <div class="card mb-4">
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <strong>{{ equipment.name }}</strong>
                    {% if equipment.effective_lot %}
                    <br><span class="text-muted">Lot: {{ equipment.effective_lot.name }}</span>
                    {% if equipment.lot %}
                        <small class="text-muted">(propre)</small>
                    {% elif equipment.parent %}
                        <small class="text-muted">(hérité)</small>
                    {% endif %}
                    {% endif %}
                </div>
                <div class="col-md-6">
                    {% if equipment.effective_room %}
                    <span class="badge bg-info">{{ equipment.effective_room.name }}</span>
                    {% endif %}
                    <span class="badge {{ 'bg-success' if equipment.status == 'en_service' else 'bg-warning' if equipment.status == 'en_panne' else 'bg-secondary' }}">
                        {{ equipment.status|format_status }}
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Liste des tâches -->
    {% if tasks %}
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0"><i class="bi bi-list-ul me-2"></i>Tâches ({{ tasks|length }})</h5>
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>N° Tâche</th>
                        <th>Description</th>
                        <th>Périodicité</th>
                        <th>Date planifiée</th>
                        <th>Durée</th>
                        <th>Statut</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for task in tasks %}
                    <tr>
                        <td>
                            {% if task.lot_task %}
                            <strong>{{ task.lot_task.num_tache }}</strong>
                            {% else %}
                            <span class="text-muted">—</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if task.lot_task %}
                            {{ task.lot_task.tache }}
                            {% elif task.notes %}
                            {{ task.notes[:50] }}...
                            {% else %}
                            <span class="text-muted">Tâche manuelle</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if task.lot_task %}
                            <span class="badge bg-secondary">{{ task.lot_task.periodicite }}</span>
                            {% else %}
                            —
                            {% endif %}
                        </td>
                        <td>
                            {% if task.scheduled_date %}
                            {{ task.scheduled_date.strftime('%d/%m/%Y') }}
                            {% if task.scheduled_start %}
                            <br><small class="text-muted">{{ task.scheduled_start.strftime('%H:%M') }}
                            {% if task.scheduled_end %}
                            - {{ task.scheduled_end.strftime('%H:%M') }}</small>
                            {% endif %}
                            {% endif %}
                            {% else %}
                            <span class="text-muted">Non planifiée</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if task.estimated_duration %}
                            {{ task.estimated_duration }} min
                            {% elif task.lot_task and task.lot_task.duree_minutes %}
                            {{ task.lot_task.duree_minutes }} min
                            {% else %}
                            —
                            {% endif %}
                        </td>
                        <td>
                            {% if task.status == 'planned' %}
                            <span class="badge bg-info">Planifiée</span>
                            {% elif task.status == 'in_progress' %}
                            <span class="badge bg-warning">En cours</span>
                            {% elif task.status == 'completed' %}
                            <span class="badge bg-success">Terminée</span>
                            {% elif task.status == 'postponed' %}
                            <span class="badge bg-secondary">Reportée</span>
                            {% elif task.status == 'cancelled' %}
                            <span class="badge bg-danger">Annulée</span>
                            {% elif task.status == 'suspended' %}
                            <span class="badge bg-warning text-dark" title="{{ task.suspension_reason or '' }}">Suspendue</span>
                            {% else %}
                            <span class="badge bg-secondary">{{ task.status }}</span>
                            {% endif %}
                        </td>
                        <td>
                            <a href="{{ url_for('equipments_scheduled.edit_scheduled_task', id=equipment.id, task_id=task.id) }}" class="btn btn-sm btn-outline-primary">
                                <i class="bi bi-pencil"></i>
                            </a>
                            <a href="{{ url_for('interventions.create', equipment_id=equipment.id, scheduled_task_id=task.id) }}" class="btn btn-sm btn-success" title="Créer une intervention">
                                <i class="bi bi-play-fill"></i>
                            </a>
                            <form method="POST" action="{{ url_for('equipments_scheduled.delete_scheduled_task', id=equipment.id, task_id=task.id) }}" style="display:inline;" onsubmit="return confirm('Supprimer cette tâche ?')">
                                <button type="submit" class="btn btn-sm btn-outline-danger">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </form>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% else %}
    <div class="alert alert-info">
        <i class="bi bi-info-circle me-2"></i>Aucune tâche planifiée pour cet équipement.
        {% if equipment.lot_id %}
        <br>Les tâches du lot "{{ equipment.lot.name }}" apparaîtront ici une fois planifiées.
        {% else %}
        <br>Associez un lot à cet équipement pour hériter de ses tâches préventives.
        {% endif %}
    </div>
    {% endif %}

    <div class="mt-4">
        <a href="{{ url_for('equipments.detail', id=equipment.id) }}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left me-1"></i>Retour à l'équipement
        </a>
    </div>
</div>
{% endblock %}
