{% extends "base.html" %}
{% block title %}Restrictions — {{ equipment.name }} — GMAO Collège{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col">
            <nav aria-label="breadcrumb">
                <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.detail', id=equipment.id) }}">{{ equipment.name }}</a></li>
                    <li class="breadcrumb-item active">Restrictions</li>
                </ol>
            </nav>
            <div class="d-flex justify-content-between align-items-center">
                <h1><i class="bi bi-shield-exclamation me-2"></i>Restrictions d'intervention</h1>
                <a href="{{ url_for('equipments_restrictions.new_restriction', id=equipment.id) }}" class="btn btn-primary">
                    <i class="bi bi-plus-circle me-1"></i>Ajouter une restriction
                </a>
            </div>
            <p class="text-muted">Définissez quand les interventions sur cet équipement sont restreintes (horaires ou occupation de salles).</p>
        </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-12">
            {% if restrictions %}
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-list-ul me-2"></i>Restrictions actives</h5>
                </div>
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Type</th>
                                <th>Détails</th>
                                <th>Message</th>
                                <th>Actif</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for r in restrictions %}
                            <tr>
                                <td>
                                    {% if r.restriction_type == 'time' %}
                                    <span class="badge bg-primary"><i class="bi bi-clock me-1"></i>Horaires</span>
                                    {% else %}
                                    <span class="badge bg-warning text-dark"><i class="bi bi-door-open me-1"></i>Occupation salle</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if r.restriction_type == 'time' %}
                                        <strong>Jours:</strong> {{ r.days_list()|join(', ') }}<br>
                                        <strong>Horaires:</strong> {{ r.start_time.strftime('%H:%M') if r.start_time else '—' }} - {{ r.end_time.strftime('%H:%M') if r.end_time else '—' }}
                                    {% else %}
                                        <strong>Salle:</strong> {{ r.room.name if r.room else '—' }}
                                    {% endif %}
                                </td>
                                <td>{{ r.message if r.message else '—' }}</td>
                                <td>
                                    {% if r.is_active %}
                                    <span class="badge bg-success">Actif</span>
                                    {% else %}
                                    <span class="badge bg-secondary">Inactif</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <form method="POST" action="{{ url_for('equipments_restrictions.delete_restriction', id=equipment.id, restriction_id=r.id) }}" style="display:inline;">
                                        <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Supprimer cette restriction ?')">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
            {% else %}
            <div class="alert alert-info">
                <i class="bi bi-info-circle me-2"></i>Aucune restriction définie pour cet équipement.
            </div>
            {% endif %}

            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-lightbulb me-2"></i>Types de restrictions</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <h6><i class="bi bi-clock me-2"></i>Restriction par horaires</h6>
                            <p class="text-muted">
                                Interdit les interventions pendant certaines tranches horaires.
                                <br>Exemple: "Intervention interdite entre 8h et 17h du lundi au vendredi"
                            </p>
                        </div>
                        <div class="col-md-6">
                            <h6><i class="bi bi-door-open me-2"></i>Restriction par occupation de salle</h6>
                            <p class="text-muted">
                                Interdit les interventions quand les salles sélectionnées sont occupées (selon le planning PRONOTE).
                                <br>Exemple: "Intervention impossible quand la salle B107 est en cours"
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}