{% extends "base.html" %}
{% block title %}{{ date.strftime('%d/%m/%Y') }} — Planning GMAO{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ url_for('planning.index') }}">Planning</a></li>
                    <li class="breadcrumb-item active">{{ date.strftime('%B %Y') }}</li>
                    <li class="breadcrumb-item active">{{ date.strftime('%d/%m/%Y') }}</li>
                </ol>
            </nav>
            
            <div class="d-flex justify-content-between align-items-center">
                <h1>
                    {% if schedule.is_working %}
                    <i class="bi bi-calendar-check text-success me-2"></i>
                    {% else %}
                    <i class="bi bi-calendar-x text-danger me-2"></i>
                    {% endif %}
                    {{ schedule.day_name }} {{ date.strftime('%d %B %Y') }}
                </h1>
                <div>
                    <a href="{{ url_for('planning.day', date_str=(date - timedelta(days=1)).strftime('%Y-%m-%d')) }}" class="btn btn-outline-secondary">
                        <i class="bi bi-chevron-left"></i> Jour précédent
                    </a>
                    <a href="{{ url_for('planning.day', date_str=(date + timedelta(days=1)).strftime('%Y-%m-%d')) }}" class="btn btn-outline-secondary">
                        Jour suivant <i class="bi bi-chevron-right"></i>
                    </a>
                </div>
            </div>
        </div>
    </div>

    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            {% for category, message in messages %}
            <div class="alert alert-{{ 'danger' if category == 'error' else 'success' if category == 'success' else 'info' }}">
                {{ message }}
            </div>
            {% endfor %}
        {% endif %}
    {% endwith %}

    <div class="row">
        <div class="col-lg-8">
            <!-- État du jour -->
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">
                        {% if schedule.is_working %}
                        <i class="bi bi-briefcase text-success me-2"></i>Jour travaillé
                        {% else %}
                        <i class="bi bi-x-circle text-danger me-2"></i>Jour non travaillé
                        {% endif %}
                    </h5>
                </div>
                <div class="card-body">
                    {% if schedule.is_working %}
                    <p><strong>Horaires:</strong> {{ schedule.working_hours.start }} - {{ schedule.working_hours.end }}</p>
                    {% if schedule.working_hours.lunch_start %}
                    <p><strong>Pause déjeuner:</strong> {{ schedule.working_hours.lunch_start }} - {{ schedule.working_hours.lunch_end }}</p>
                    {% endif %}
                    {% elif schedule.closure %}
                    <p class="text-info"><i class="bi bi-info-circle me-1"></i><strong>{{ schedule.closure }}</strong></p>
                    {% elif schedule.leave %}
                    <p class="text-warning"><i class="bi bi-calendar-x me-1"></i><strong>{{ schedule.leave }}</strong></p>
                    {% elif schedule.holiday %}
                    <p class="text-danger"><i class="bi bi-calendar-x me-1"></i><strong>Jour férié: {{ schedule.holiday }}</strong></p>
                    {% else %}
                    <p class="text-muted"><i class="bi bi-calendar-x me-1"></i>Week-end</p>
                    {% endif %}
                </div>
            </div>

            <!-- Tâches du jour -->
            {% if schedule.is_working and schedule.items %}
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-list-task me-2"></i>Interventions planifiées ({{ schedule.items|length }})</h5>
                </div>
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Heure</th>
                                <th>Type</th>
                                <th>Description</th>
                                <th>Salle</th>
                                <th>Durée</th>
                                <th>Statut</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for item in schedule.items %}
                            <tr>
                                <td>
                                    {% if item.start_time %}
                                    {{ item.start_time }}
                                    {% if item.end_time %}
                                    - {{ item.end_time }}
                                    {% endif %}
                                    {% else %}
                                    —
                                    {% endif %}
                                </td>
                                <td>
                                    {% if item.type == 'admin' %}
                                    <span class="badge bg-success">Admin</span>
                                    {% elif item.type == 'curative' %}
                                    <span class="badge bg-danger">Curative</span>
                                    {% elif item.type == 'preventive' %}
                                    <span class="badge bg-primary">Préventive</span>
                                    {% elif item.type == 'formation' %}
                                    <span class="badge bg-warning text-dark">Formation</span>
                                    {% else %}
                                    <span class="badge bg-secondary">{{ item.type }}</span>
                                    {% endif %}
                                </td>
                                <td>{{ item.title }}</td>
                                <td>
                                    {% if item.room_name %}
                                    <span class="badge bg-info">{{ item.room_name }}</span>
                                    {% else %}
                                    —
                                    {% endif %}
                                </td>
                                <td>{{ item.duration }} min</td>
                                <td>
                                    {% if item.status == 'planned' %}
                                    <span class="badge bg-info">Planifié</span>
                                    {% elif item.status == 'in_progress' %}
                                    <span class="badge bg-warning">En cours</span>
                                    {% elif item.status == 'completed' %}
                                    <span class="badge bg-success">Terminé</span>
                                    {% elif item.status == 'postponed' %}
                                    <span class="badge bg-secondary">Reporté</span>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
            {% elif schedule.is_working %}
            <div class="alert alert-info">
                <i class="bi bi-info-circle me-2"></i>Aucune intervention planifiée pour ce jour.
            </div>
            {% endif %}

            <!-- Tâches préventives -->
            {% if schedule.is_working and tasks %}
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-calendar-check me-2"></i>Tâches préventives ({{ tasks|length }})</h5>
                </div>
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Équipement</th>
                                <th>Tâche</th>
                                <th>Salle</th>
                                <th>Durée</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for task in tasks %}
                            <tr>
                                <td>
                                    {% if task.equipment %}
                                    <a href="{{ url_for('equipments.detail', id=task.equipment.id) }}">
                                        {{ task.equipment.name }}
                                    </a>
                                    {% else %}
                                    —
                                    {% endif %}
                                </td>
                                <td>
                                    {% if task.lot_task %}
                                    {{ task.lot_task.tache }}
                                    {% else %}
                                    Tâche manuelle
                                    {% endif %}
                                </td>
                                <td>
                                    {% if task.room %}
                                    <span class="badge bg-info">{{ task.room.name }}</span>
                                    {% elif task.equipment.effective_room %}
                                    <span class="badge bg-info">{{ task.equipment.effective_room.name }}</span>
                                    {% else %}
                                    —
                                    {% endif %}
                                </td>
                                <td>
                                    {% if task.estimated_duration %}
                                    {{ task.estimated_duration }} min
                                    {% elif task.lot_task and task.lot_task.duree_minutes %}
                                    {{ task.lot_task.duree_minutes }} min
                                    {% else %}
                                    —
                                    {% endif %}
                                </td>
                                <td>
                                    {% if task.equipment %}<a href="{{ url_for('equipments_scheduled.edit_scheduled_task', id=task.equipment.id, task_id=task.id) }}" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i></a>{% else %}<span class="text-muted">—</span>{% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
            {% endif %}
        </div>

        <div class="col-lg-4">
            <!-- Actions rapides -->
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-lightning me-2"></i>Actions rapides</h5>
                </div>
                <div class="card-body">
                    <div class="d-grid gap-2">
                        <a href="{{ url_for('interventions.create') }}" class="btn btn-danger">
                            <i class="bi bi-wrench me-1"></i>Nouvelle intervention
                        </a>
                        <a href="{{ url_for('planning.availability') }}" class="btn btn-outline-primary">
                            <i class="bi bi-calendar-week me-1"></i>Disponibilités
                        </a>
                    </div>
                </div>
            </div>

            <!-- Regroupement par salle -->
            {% if schedule.grouped_items %}
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-geo-alt me-2"></i>Par salle</h5>
                </div>
                <div class="card-body">
                    {% for group in schedule.grouped_items %}
                    {% if group.room_name %}
                    <div class="mb-3">
                        <h6><span class="badge bg-info">{{ group.room_name }}</span></h6>
                        <ul class="list-unstyled ms-3">
                            {% for item in group.task_items %}
                            <li>
                                <small>{{ item.start_time if item.start_time else '—' }}</small>
                                {{ item.title }}
                            </li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                    {% endfor %}
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
