{% extends "base.html" %}

{% block title %}{{ category.name }} — GMAO Collège{% endblock %}

{% block content %}
<div class="container-fluid">
    <nav aria-label="breadcrumb" class="mb-3">
        <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.categories') }}">Catégories</a></li>
            <li class="breadcrumb-item active">{{ category.name }}</li>
        </ol>
    </nav>

    <div class="row">
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">Informations</h5>
                </div>
                <div class="card-body">
                    <dl class="row">
                        <dt class="col-sm-4">Nom</dt>
                        <dd class="col-sm-8">{{ category.name }}</dd>
                        
                        {% if category.description %}
                        <dt class="col-sm-4">Description</dt>
                        <dd class="col-sm-8">{{ category.description }}</dd>
                        {% endif %}
                    </dl>
                </div>
            </div>
            <div class="card mb-4"><div class="card-header"><h5 class="mb-0">Gestion et pannes récurrentes</h5></div><div class="card-body"><form method="post" action="{{ url_for('equipments.category_recurrence_policy', id=category.id) }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><div class="mb-3"><label class="form-label">Mode de gestion par défaut</label><select class="form-select" name="default_management_mode">{% for value, label in [('quantitatif','Quantitatif'),('individuel_fixe','Individuel fixe'),('individuel_mobile','Individuel mobile'),('serialise','Sérialisé'),('structure','Élément de bâtiment'),('consommable','Consommable / stock')] %}<option value="{{ value }}" {% if category.default_management_mode == value %}selected{% endif %}>{{ label }}</option>{% endfor %}</select></div><div class="form-check mb-2"><input class="form-check-input" type="checkbox" name="enabled" id="recurrence" {% if category.recurrence_alert_enabled %}checked{% endif %}><label class="form-check-label" for="recurrence">Alerter sur les pannes répétées de cette catégorie</label></div><div class="row g-2"><div class="col"><label class="form-label">Nombre de pannes</label><input type="number" min="1" name="threshold" class="form-control" value="{{ category.recurrence_threshold }}"></div><div class="col"><label class="form-label">Sur une période de (jours)</label><input type="number" min="1" name="window_days" class="form-control" value="{{ category.recurrence_window_days }}"></div></div><button class="btn btn-sm btn-primary mt-3">Enregistrer</button></form></div></div>
        </div>
        
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">Lots associés</h5>
                </div>
                <div class="card-body">
                    {% if category.lots %}
                    <ul class="list-unstyled mb-0">
                        {% for lot in category.lots %}
                        <li>
                            <a href="{{ url_for('lots.detail', id=lot.id) }}">{{ lot.name }}</a>
                        </li>
                        {% endfor %}
                    </ul>
                    {% else %}
                    <span class="text-muted">Aucun lot associé</span>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">Équipements ({{ equipments|length }})</h5>
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>Nom</th>
                        <th>Bâtiment</th>
                        <th>Emplacement</th>
                        <th>Statut</th>
                    </tr>
                </thead>
                <tbody>
                    {% for equipment in equipments %}
                    <tr>
                        <td>
                            <a href="{{ url_for('equipments.detail', id=equipment.id) }}">
                                {{ equipment.name }}
                            </a>
                        </td>
                        <td>{{ equipment.room.building.name if equipment.room and equipment.room.building else '-' }}</td>
                        <td>{{ equipment.room.name if equipment.room else '-' }}</td>
                        <td>
                            <span class="badge {% if equipment.status == 'operational' %}bg-success{% elif equipment.status == 'maintenance' %}bg-warning{% else %}bg-danger{% endif %}">
                                {{ equipment.status_label }}
                            </span>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="4" class="text-center text-muted">Aucun équipement dans cette catégorie</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}
