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

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ url_for('equipments_meters.lots') }}">Lots</a></li>
                    <li class="breadcrumb-item active">{{ lot.name }}</li>
                </ol>
            </nav>
            <h1 class="h3 mb-0"><i class="bi bi-box-se me-2"></i>Modifier le lot</h1>
        </div>
    </div>
    
    <div class="card">
        <div class="card-body">
            <form method="POST">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <div class="mb-3">
                    <label for="name" class="form-label">Nom du lot *</label>
                    <input type="text" class="form-control" id="name" name="name" value="{{ lot.name }}" required>
                </div>
                <div class="mb-3">
                    <label for="category_id" class="form-label">Catégorie</label>
                    <select id="category_id" name="category_id" class="form-select">
                        <option value="">-- Sans catégorie --</option>
                        {% for cat in categories %}
                        <option value="{{ cat.id }}" {% if lot.category_id == cat.id %}selected{% endif %}>
                            {{ cat.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="mt-3">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-check-lg"></i> Enregistrer
                    </button>
                    <a href="{{ url_for('equipments_meters.lots') }}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-lg"></i> Annuler
                    </a>
                </div>
            </form>
        </div>
    </div>
    
    {% if lot.tasks %}
    <div class="card mt-4">
        <div class="card-header">
            <h5><i class="bi bi-list-task me-2"></i>Tâches de maintenance préventive</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-sm table-hover">
                    <thead>
                        <tr>
                            <th>N°</th>
                            <th>Tâche</th>
                            <th>Périodicité</th>
                            <th>Contrat</th>
                            <th>Gestionnaire</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for task in lot.tasks %}
                        <tr>
                            <td>{{ task.num_tache or '-' }}</td>
                            <td>{{ task.tache or '-' }}</td>
                            <td>{{ task.periodicite or '-' }}</td>
                            <td>
                                {% if task.contrat %}
                                <i class="bi bi-check-circle text-success"></i>
                                {% else %}
                                <i class="bi bi-dash-circle text-muted"></i>
                                {% endif %}
                            </td>
                            <td>{{ task.gestionnaire or '-' }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% else %}
    <div class="card mt-4">
        <div class="card-body text-muted">
            <i class="bi bi-info-circle me-2"></i>Aucune tâche de maintenance définie pour ce lot.
        </div>
    </div>
    {% endif %}
    
    {# Équipements du lot #}
    {% if equipments %}
    <div class="card mt-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0"><i class="bi bi-hdd-stack me-2"></i>Équipements ({{ equipments|length }})</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-sm table-hover mb-0">
                    <thead>
                        <tr>
                            <th>Nom</th>
                            <th>Localisation</th>
                            <th>Statut</th>
                            <th>Compteurs</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for eq in equipments %}
                        <tr>
                            <td>
                                <a href="{{ url_for('equipments.detail', id=eq.id) }}">
                                    {{ eq.name }}
                                </a>
                            </td>
                            <td>
                                {% if eq.room %}
                                    <a href="{{ url_for('rooms.detail', id=eq.room.id) }}">
                                        {{ eq.room.name }}
                                    </a>
                                {% elif eq.location %}
                                    {{ eq.location }}
                                {% else %}
                                    <span class="text-muted">—</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if eq.status == 'active' %}
                                    <span class="badge bg-success">En service</span>
                                {% elif eq.status == 'inactive' %}
                                    <span class="badge bg-secondary">Inactif</span>
                                {% elif eq.status == 'maintenance' %}
                                    <span class="badge bg-warning text-dark">Maintenance</span>
                                {% elif eq.status == 'retired' %}
                                    <span class="badge bg-danger">Retiré</span>
                                {% else %}
                                    <span class="badge bg-secondary">{{ eq.status }}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if eq.meters %}
                                    {% for meter in eq.meters %}
                                    <a href="{{ url_for('meters.detail', meter_id=meter.id) }}" 
                                       class="badge bg-info text-dark text-decoration-none me-1">
                                        {{ meter.name }}: {{ meter.current_value }} {{ meter.unit }}
                                    </a>
                                    {% endfor %}
                                {% else %}
                                    <span class="text-muted">—</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{{ url_for('equipments.detail', id=eq.id) }}" 
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% else %}
    <div class="card mt-4">
        <div class="card-body text-muted">
            <i class="bi bi-info-circle me-2"></i>Aucun équipement dans ce lot. 
            <a href="{{ url_for('equipments.index') }}">Rechercher un équipement</a> pour l'ajouter à ce lot.
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
