{% extends "base.html" %}
{% block title %}Planning {{ room.name }} — GMAO{% endblock %}

{% block content %}

<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1><i class="bi bi-calendar-week"></i> Planning : {{ room.name }}</h1>
            <p class="text-muted mb-0">
                {% if room.room_type %}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 %}
                {% if room.building %} | 📍 {{ room.building.name }}{% endif %}
            </p>
        </div>
        <div>
            {% if room.room_type and room.room_type.is_teaching %}
            <form method="POST" action="{{ url_for('pronote.import_room_planning', room_id=room.id) }}" style="display:inline;">
                <button type="submit" class="btn btn-outline-success">
                    <i class="bi bi-download"></i> Importer 3 semaines depuis Pronote
                </button>
            </form>
            {% endif %}
            <a href="{{ url_for('pronote.planning') }}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left"></i> Retour aux plannings
            </a>
        </div>
    </div>

    <!-- Navigation par semaine -->
    <div class="row mb-3">
        <div class="col">
            <div class="d-flex align-items-center justify-content-between">
                <div>
                    <h5 class="mb-0">
                        <i class="bi bi-calendar-range"></i> 
                        Semaine du {{ week_start.strftime('%d/%m/%Y') }} 
                        au {{ week_end.strftime('%d/%m/%Y') }}
                    </h5>
                </div>
                <div class="btn-group" role="group">
                    <a href="{{ url_for('pronote.room_planning', room_id=room.id, week=prev_week.isoformat()) }}" 
                       class="btn btn-outline-primary btn-sm">
                        <i class="bi bi-chevron-left"></i> Semaine précédente
                    </a>
                    <a href="{{ url_for('pronote.room_planning', room_id=room.id, week=today.isoformat()) }}" 
                       class="btn btn-outline-secondary btn-sm">
                        Aujourd'hui
                    </a>
                    <a href="{{ url_for('pronote.room_planning', room_id=room.id, week=next_week.isoformat()) }}" 
                       class="btn btn-outline-primary btn-sm">
                        Semaine suivante <i class="bi bi-chevron-right"></i>
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Comparaison planning -->
    <div class="row">
        <!-- Planning GMAO (importé par watchdog) -->
        <div class="col-md-6">
            <div class="card">
                <div class="card-header bg-light">
                    <h5 class="mb-0">
                        <i class="bi bi-database"></i> Planning importé (Watchdog)
                    </h5>
                </div>
                <div class="card-body p-0">
                    {% if db_by_day %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            {% set days = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche'] %}
                            
                            {% for day_num in [0, 1, 2, 3, 4, 5, 6] %}
                                {% set day_date = week_start + timedelta(days=day_num) %}
                            <thead class="table-success">
                                <tr>
                                    <th colspan="4" style="background-color: #d1e7dd;">
                                        <strong>{{ days[day_num] }} {{ day_date.strftime('%d/%m') }}</strong>
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                {% if day_num in db_by_day %}
                                    {% for s in db_by_day[day_num] %}
                                <tr>
                                    <td style="width: 100px;"><strong>{{ s.start_time }}</strong></td>
                                    <td>{{ s.subject or '-' }}</td>
                                    <td>{{ s.teacher or '-' }}</td>
                                    <td><span class="badge bg-secondary">{{ s.class_name or '-' }}</span></td>
                                </tr>
                                    {% endfor %}
                                {% else %}
                                <tr class="table-light">
                                    <td colspan="4" class="text-center text-muted">
                                        <em>Pas de cours</em>
                                    </td>
                                </tr>
                                {% endif %}
                            </tbody>
                            {% endfor %}
                        </table>
                    </div>
                    {% else %}
                    <div class="p-3 text-center text-muted">
                        <i class="bi bi-inbox"></i><br>
                        Aucun planning importé pour cette semaine.<br>
                        <small>Le watchdog PRONOTE synchronise automatiquement les plannings.</small>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
        
        <!-- Planning PRONOTE (direct) -->
        <div class="col-md-6">
            <div class="card">
                <div class="card-header bg-light d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">
                        <i class="bi bi-wifi"></i> Planning PRONOTE (direct)
                    </h5>
                    {% if pronote_connected %}
                    <span class="badge bg-success">Connecté</span>
                    {% else %}
                    <span class="badge bg-secondary">Hors ligne</span>
                    {% endif %}
                </div>
                <div class="card-body p-0">
                    {% if pronote_connected %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            {% set days = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche'] %}
                            
                            {% for day_num in [0, 1, 2, 3, 4, 5, 6] %}
                                {% set day_date = week_start + timedelta(days=day_num) %}
                            <thead class="table-info">
                                <tr>
                                    <th colspan="4" style="background-color: #cff4fc;">
                                        <strong>{{ days[day_num] }} {{ day_date.strftime('%d/%m') }}</strong>
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                {% if day_num in pronote_by_day %}
                                    {% for p in pronote_by_day[day_num] %}
                                <tr>
                                    <td style="width: 100px;"><strong>{{ p.heure }}</strong></td>
                                    <td>{{ p.matiere }}</td>
                                    <td>{{ p.prof }}</td>
                                    <td><span class="badge bg-info">{{ p.classe }}</span></td>
                                </tr>
                                    {% endfor %}
                                {% else %}
                                <tr class="table-light">
                                    <td colspan="4" class="text-center text-muted">
                                        <em>Pas de cours</em>
                                    </td>
                                </tr>
                                {% endif %}
                            </tbody>
                            {% endfor %}
                        </table>
                    </div>
                    {% else %}
                    <div class="p-3 text-center text-muted">
                        <i class="bi bi-exclamation-triangle"></i><br>
                        Connexion à PRONOTE impossible.
                        <div class="mt-2">
                            <small>Vérifiez la configuration du client PRONOTE.</small>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- État de synchronisation -->
    {% if room.room_type and room.room_type.is_teaching %}
    <div class="alert alert-info mt-3">
        <div class="d-flex align-items-center">
            <i class="bi bi-info-circle me-2"></i>
            <div>
                <strong>Synchronisation PRONOTE</strong>
                <p class="mb-0 small">
                    Le watchdog importe automatiquement les plannings PRONOTE toutes les minutes.
                    Les données sont stockées localement pour consultation même hors connexion.
                </p>
            </div>
        </div>
    </div>
    {% endif %}
</div>

{% endblock %}