{% extends "base.html" %}
{% block title %}Plannings — GMAO{% endblock %}

{% block content %}

<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1><i class="bi bi-calendar3"></i> Plannings des salles</h1>
        <div>
            <a href="{{ url_for('pronote.connect') }}" class="btn btn-outline-primary">
                <i class="bi bi-qr-code"></i> Connexion Pronote
            </a>
            <form method="post" action="{{ url_for('pronote.sync_salles') }}" class="d-inline"><button type="submit" class="btn btn-outline-success ms-2">
                <i class="bi bi-download"></i> Synchroniser salles
            </button></form>
        </div>
    </div>

    {% if rooms %}
    <div class="row">
        {% for room in rooms %}
        <div class="col-md-4 col-lg-3 mb-3">
            <div class="card h-100">
                <div class="card-body">
                    <h5 class="card-title">
                        <i class="bi bi-door-open"></i> {{ room.name }}
                    </h5>
                    <p class="text-muted">
                        {% if room.building %}
                        <small>📍 {{ room.building.name }}</small><br>
                        {% endif %}
                        {% if room.room_type %}
                        <span class="badge" style="background-color: {{ room.room_type.color|hex_color }}; color: {{ room.room_type.color|text_color }};">{{ room.room_type.name }}</span>
                        {% endif %}
                    </p>
                    
                    {% if room.room_type and room.room_type.is_teaching %}
                    <div class="mt-3">
                        <a href="{{ url_for('pronote.room_planning', room_id=room.id) }}" 
                           class="btn btn-primary btn-sm">
                            <i class="bi bi-calendar-week"></i> Voir planning
                        </a>
                    </div>
                    {% else %}
                    <small class="text-muted">Salle non d'enseignement</small>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
    {% else %}
    <div class="alert alert-info">
        <i class="bi bi-info-circle"></i> Aucune salle d'enseignement disponible.
        <div class="mt-2">
            <form method="post" action="{{ url_for('pronote.sync_salles') }}"><button type="submit" class="btn btn-primary btn-sm">
                <i class="bi bi-download"></i> Synchroniser les salles depuis Pronote
            </button></form>
        </div>
    </div>
    {% endif %}

    {% if current_user.is_admin() %}
    <div class="mt-4 p-3 bg-light rounded">
        <h6>📊 Statistiques</h6>
        <div class="row text-center">
            <div class="col-md-3">
                <div class="stat-card bg-primary text-white p-3 rounded">
                    <h4>{{ rooms|length }}</h4>
                    <small>Salles totales</small>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card bg-success text-white p-3 rounded">
                    <h4>{{ rooms|selectattr('room_type.is_teaching')|list|length }}</h4>
                    <small>Salles enseignement</small>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card bg-info text-white p-3 rounded">
                    <h4>{{ rooms|selectattr('room_type.is_teaching')|list|length * 35 }}</h4>
                    <small>Cours/semaine</small>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card bg-warning text-white p-3 rounded">
                    <h4>{{ rooms|selectattr('room_type.is_teaching')|list|length * 5 }}</h4>
                    <small>Créneaux/semaine</small>
                </div>
            </div>
        </div>
    </div>
    {% endif %}
</div>

{% endblock %}
