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

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1>
        <i class="bi bi-door-open"></i> {{ room.name }}
        {% if room.code %}<small class="text-muted">({{ room.code }})</small>{% endif %}
    </h1>
    <div>
        <a href="{{ url_for('rooms.edit', id=room.id) }}" class="btn btn-outline-primary">
            <i class="bi bi-pencil"></i> Modifier
        </a>
        <a href="{{ url_for('rooms.duplicate', id=room.id) }}" class="btn btn-outline-success">Dupliquer</a>
        <a href="{{ url_for('rooms.index') }}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left"></i> Retour
        </a>
    </div>

    <div class="card mb-4"><div class="card-header">Ouvrages / surfaces (facultatif)</div><div class="card-body"><div class="row g-2 mb-3">{% for surface in surfaces %}<div class="col-md-6"><span class="badge text-bg-light">{{ surface.surface_type }}</span> {{ surface.label or '' }} — {{ surface.material }}{% if surface.finish %} + {{ surface.finish }}{% endif %}</div>{% else %}<div class="col-12 text-muted">Aucune composition renseignée.</div>{% endfor %}</div><form method="post" action="{{ url_for('rooms.add_surface', id=room.id) }}" class="row g-2"><div class="col-md-2"><select name="surface_type" class="form-select" aria-label="Type"><option value="mur">Mur</option><option value="sol">Sol</option><option value="plafond">Plafond</option><option value="autre">Autre</option></select></div><div class="col-md-3"><input name="material" class="form-control" placeholder="Matériau" required></div><div class="col-md-3"><input name="finish" class="form-control" placeholder="Finition"></div><div class="col-md-2"><input name="label" class="form-control" placeholder="Repère"></div><div class="col-md-2"><button class="btn btn-outline-secondary">Ajouter</button></div></form></div></div>
</div>

<div class="row">
    <!-- Informations de la salle -->
    <div class="col-md-4">
        <div class="card mb-4">
            <div class="card-header">
                <i class="bi bi-info-circle"></i> Informations
            </div>
            <div class="card-body">
                <dl class="row mb-0">
                    <dt class="col-sm-5">Bâtiment</dt>
                    <dd class="col-sm-7">{{ room.building.name }}</dd>
                    
                    <dt class="col-sm-5">Étage</dt>
                    <dd class="col-sm-7">{{ room.floor or '—' }}</dd>
                    
                    {% if room.room_type %}
                    <dt class="col-sm-5">Type</dt>
                    <dd class="col-sm-7">{{ room.room_type.name }}</dd>
                    {% endif %}
                    
                    <dt class="col-sm-5">Équipements</dt>
                    <dd class="col-sm-7">{{ equipments|length }}</dd>
                    {% if room.room_profile %}<dt class="col-sm-5">Profil</dt><dd class="col-sm-7">{{ room.room_profile.name }}<br><a href="{{ url_for('room_profiles.preview', id=room.room_profile.id, room_id=room.id) }}">Prévisualiser / appliquer</a></dd>{% endif %}
                </dl>
            </div>
        </div>
        
        <!-- État d'occupation -->
        <div class="card mb-4">
            <div class="card-header">
                <i class="bi bi-clock"></i> Occupation actuelle
            </div>
            <div class="card-body">
                {% if room_status.status == 'libre' %}
                <div class="alert alert-success mb-0">
                    <i class="bi bi-check-circle"></i>
                    <strong>{{ room_status.message }}</strong>
                    {% if room_status.next_occupied %}
                    <br><small class="text-muted">Prochain cours à {{ room_status.next_occupied }}</small>
                    {% endif %}
                </div>
                {% elif room_status.status == 'occupee' %}
                <div class="alert alert-danger mb-0">
                    <i class="bi bi-x-circle"></i>
                    <strong>{{ room_status.message }}</strong>
                    {% if room_status.course %}
                    <br><small>{{ room_status.course.subject }}</small>
                    {% if room_status.course.teacher %}
                    <br><small class="text-muted">{{ room_status.course.teacher }}</small>
                    {% endif %}
                    {% endif %}
                </div>
                {% else %}
                <div class="alert alert-secondary mb-0">
                    <i class="bi bi-question-circle"></i> {{ room_status.message }}
                </div>
                {% endif %}
            </div>
        </div>
    </div>
    
    <!-- Emploi du temps du jour -->
    <div class="col-md-8">
        <div class="card mb-4">
            <div class="card-header">
                <i class="bi bi-calendar-day"></i> Emploi du temps du jour
            </div>
            <div class="card-body">
                {% if courses %}
                <table class="table table-sm">
                    <thead>
                        <tr>
                            <th>Heure</th>
                            <th>Matière</th>
                            <th>Professeur</th>
                            <th>Classe</th>
                        </tr>
                    </thead>
                    <tbody>
                    {% for course in courses %}
                    <tr {% if room_status.course and course.id == room_status.course.id %}class="table-danger"{% endif %}>
                        <td>{{ course.start_time.strftime('%H:%M') }} - {{ course.end_time.strftime('%H:%M') }}</td>
                        <td>{{ course.subject }}</td>
                        <td>{{ course.teacher }}</td>
                        <td>{{ course.class_name }}</td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
                {% elif room_status.status == 'inconnu' %}
                <p class="text-danger mb-0"><i class="bi bi-exclamation-triangle"></i> {{ room_status.message }}</p>
                {% else %}
                <p class="text-success mb-0"><i class="bi bi-check-circle"></i> Aucun cours prévu aujourd'hui</p>
                {% endif %}
            </div>
        </div>
        
        <!-- Liste des équipements -->
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-pc-display"></i> Équipements dans cette salle ({{ equipments|length }})</span>
                <a href="{{ url_for('equipments.create') }}?room_id={{ room.id }}" class="btn btn-sm btn-outline-success">
                    <i class="bi bi-plus"></i> Ajouter
                </a>
            </div>
            <div class="card-body">
                {% if equipments %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Nom</th>
                                <th>Code</th>
                                <th>Type</th>
                                <th>Catégorie</th>
                                <th>Statut</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                        {% for eq in equipments %}
                        <tr>
                            <td>
                                <a href="{{ url_for('equipments.detail', id=eq.id) }}">{{ eq.name }}</a>
                                {% if eq.status == 'a_jeter' %}
                                <span class="badge bg-warning text-dark">À jeter</span>
                                {% elif eq.status == 'jete' %}
                                <span class="badge bg-secondary">Jeté</span>
                                {% endif %}
                            </td>
                            <td><code>{{ eq.code or '—' }}</code></td>
                            <td>
                                {% if eq.is_group %}
                                <span class="badge bg-info">{{ eq.children_count }}/{{ eq.total_children }}</span>
                                {% if eq.tracked_individually %}
                                <span class="badge bg-success">Suivi indiv.</span>
                                {% else %}
                                <span class="badge bg-secondary">Groupé</span>
                                {% endif %}
                                {% elif eq.is_individual %}
                                <span class="badge bg-warning text-dark">n°{{ eq.individual_number }}</span>
                                {% else %}
                                <span class="text-muted">—</span>
                                {% endif %}
                            </td>
                            <td>{{ eq.category.name if eq.category else '—' }}</td>
                            <td>
                                {% if eq.status == 'en_service' %}
                                <span class="badge bg-success">En service</span>
                                {% elif eq.status == 'hors_service' %}
                                <span class="badge bg-danger">Hors service</span>
                                {% elif eq.status == 'en_maintenance' %}
                                <span class="badge bg-warning text-dark">En maintenance</span>
                                {% elif eq.status == 'a_jeter' %}
                                <span class="badge bg-warning text-dark">À jeter</span>
                                {% elif eq.status == 'jete' %}
                                <span class="badge bg-secondary">Jeté</span>
                                {% else %}
                                <span class="badge bg-secondary">{{ eq.status }}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if eq.status != 'jete' %}
                                <a href="{{ url_for('equipments.detail', id=eq.id) }}" class="btn btn-sm btn-outline-primary" title="Voir">
                                    <i class="bi bi-eye"></i>
                                </a>
                                {% if eq.status == 'a_jeter' %}
                                <form action="{{ url_for('equipments.cancel_trash', id=eq.id) }}" method="POST" style="display:inline;">
                                    <button type="submit" class="btn btn-sm btn-outline-success" title="Annuler (remettre en service)">
                                        <i class="bi bi-arrow-counterclockwise"></i>
                                    </button>
                                </form>
                                {% endif %}
                                {% else %}
                                <a href="{{ url_for('equipments.detail', id=eq.id) }}" class="btn btn-sm btn-outline-secondary" title="Voir (archivé)">
                                    <i class="bi bi-eye"></i>
                                </a>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <p class="text-muted mb-0"><i class="bi bi-info-circle"></i> Aucun équipement dans cette salle</p>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}
