{% extends "base.html" %}
{% block content %}

<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1><i class="bi bi-boxes"></i> Gestion des Lots</h1>
        <div class="d-flex gap-2">
            <a href="{{ url_for('lots.create') }}" class="btn btn-primary">
                <i class="bi bi-plus-circle"></i> Ajouter un lot
            </a>
            <a href="{{ url_for('wizard.equipment_wizard') }}" class="btn btn-outline-primary">
                <i class="bi bi-hdd-stack"></i> Ajouter un équipement
            </a>
            <a href="{{ url_for('equipments.index') }}" class="btn btn-secondary">
                <i class="bi bi-arrow-left"></i> Retour aux équipements
            </a>
        </div>
    </div>

    <div class="alert alert-info" role="note">
        Les lots regroupent les équipements suivis ensemble et leurs actions préventives.
        Vous pouvez créer un lot seul, puis y associer des équipements et des tâches.
    </div>

    <!-- Filtres -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <input type="text" name="search" class="form-control" placeholder="Rechercher un lot..." value="{{ request.args.get('search', '') }}">
                </div>
                <div class="col-md-3">
                    <select name="company_id" class="form-select">
                        <option value="">Toutes les entreprises</option>
                        {% for company in companies %}
                        <option value="{{ company.id }}" {% if request.args.get('company_id')|int == company.id %}selected{% endif %}>
                            {{ company.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <select name="presence" class="form-select">
                        <option value="">Tous les statuts</option>
                        <option value="present" {% if request.args.get('presence') == 'present' %}selected{% endif %}>Présents</option>
                        <option value="absent" {% if request.args.get('presence') == 'absent' %}selected{% endif %}>Non présents</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">Filtrer</button>
                </div>
            </form>
        </div>
    </div>

    <!-- Liste des lots -->
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">Liste des Lots ({{ lots.total }})</h5>
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>Lot</th>
                        <th>Présence</th>
                        <th>Équipements</th>
                        <th>Tâches</th>
                        <th>Entreprise</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for lot in lots %}
                    <tr class="{% if lot.needs_equipment_attention %}table-warning{% elif not lot.is_present %}table-secondary{% endif %}">
                        <td><strong>{{ lot.name }}</strong></td>
                        <td>
                            {% if lot.is_present %}
                            <span class="badge bg-success">Présent</span>
                            {% else %}
                            <span class="badge bg-secondary">Non présent</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if lot.active_equipment_count %}
                            <span class="badge bg-primary">{{ lot.active_equipment_count }} équipement(s)</span>
                            {% elif lot.is_present %}
                            <span class="badge bg-warning text-dark"><i class="bi bi-exclamation-triangle me-1"></i>Aucun équipement</span>
                            {% else %}
                            <span class="text-muted">Sans objet</span>
                            {% endif %}
                        </td>
                        <td><span class="badge bg-info text-dark">{{ lot.tasks|length }} tâche(s)</span></td>
                        <td>
                            {% if lot.company %}
                                <span class="badge bg-success">{{ lot.company.name }}</span>
                            {% else %}
                                <span class="text-muted">Non assigné</span>
                            {% endif %}
                        </td>
                        <td>
                            <a href="{{ url_for('lots.detail', id=lot.id) }}" class="btn btn-sm btn-outline-primary">
                                <i class="bi bi-eye"></i> Voir
                            </a>
                            <a href="{{ url_for('lots.edit', id=lot.id) }}" class="btn btn-sm btn-outline-warning">
                                <i class="bi bi-pencil"></i> Modifier
                            </a>
                            {% if not lot.is_present %}
                            <form method="POST" action="{{ url_for('lots.set_presence', id=lot.id) }}" class="d-inline">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                <input type="hidden" name="action" value="present">
                                <button class="btn btn-sm btn-outline-success"><i class="bi bi-check-circle"></i> Marquer présent</button>
                            </form>
                            {% elif not lot.active_equipment_count %}
                            <form method="POST" action="{{ url_for('lots.set_presence', id=lot.id) }}" class="d-inline">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                <input type="hidden" name="action" value="absent">
                                <button class="btn btn-sm btn-outline-secondary"><i class="bi bi-dash-circle"></i> Non présent</button>
                            </form>
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% if lots.pages > 1 %}
    <nav class="mt-3" aria-label="Pagination des lots">
        <ul class="pagination justify-content-center">
            <li class="page-item {% if not lots.has_prev %}disabled{% endif %}">
                <a class="page-link" href="{{ url_for('lots.index', page=lots.prev_num, search=request.args.get('search'), company_id=request.args.get('company_id'), presence=request.args.get('presence')) }}">Précédent</a>
            </li>
            <li class="page-item disabled"><span class="page-link">Page {{ lots.page }} / {{ lots.pages }}</span></li>
            <li class="page-item {% if not lots.has_next %}disabled{% endif %}">
                <a class="page-link" href="{{ url_for('lots.index', page=lots.next_num, search=request.args.get('search'), company_id=request.args.get('company_id'), presence=request.args.get('presence')) }}">Suivant</a>
            </li>
        </ul>
    </nav>
    {% endif %}
</div>

{% endblock %}
