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

{% block content %}
<h1 class="mb-4">{{ title }}</h1>
<div class="card mb-4">
    <div class="card-body">
        <form method="POST">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <div class="mb-3">
                <label class="form-label">Nom *</label>
                <input type="text" name="name" class="form-control" value="{{ building.name if building else '' }}" required>
            </div>
            <div class="mb-3">
                <label class="form-label" for="site_id">Site</label>
                <select id="site_id" name="site_id" class="form-select"><option value="">— Non renseigné —</option>{% for site in sites %}<option value="{{ site.id }}" {% if building and building.site_id == site.id %}selected{% endif %}>{{ site.name }}</option>{% endfor %}</select>
            </div>
            <div class="mb-3">
                <label class="form-label">Description</label>
                <textarea name="description" class="form-control" rows="3">{{ building.description if building else '' }}</textarea>
            </div>
            <div class="mt-3">
                <button type="submit" class="btn btn-primary"><i class="bi bi-check-lg"></i> Enregistrer</button>
                <a href="{{ url_for('buildings.index') }}" class="btn btn-outline-secondary">Annuler</a>
            </div>
        </form>
    </div>
</div>

{% if building and building.id %}
<div class="card">
    <div class="card-header">
        <h5 class="mb-0"><i class="bi bi-grid-3x3-gap"></i> Zones du bâtiment</h5>
    </div>
    <div class="card-body">
        {% if zones %}
        <ul class="list-group mb-3">
            {% for zone in zones %}
            <li class="list-group-item d-flex justify-content-between align-items-center">
                {{ zone.name }}
                <a href="{{ url_for('zones.edit', id=zone.id) }}" class="btn btn-sm btn-outline-primary">Modifier</a>
            </li>
            {% endfor %}
        </ul>
        {% else %}
        <p class="text-muted">Aucune zone dans ce bâtiment.</p>
        {% endif %}

        <form method="POST" action="{{ url_for('buildings.add_zone', id=building.id) }}">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <div class="input-group">
                <input type="text" name="name" class="form-control" placeholder="Nouvelle zone" required>
                <button type="submit" class="btn btn-success"><i class="bi bi-plus-lg"></i> Ajouter une zone</button>
            </div>
        </form>
    </div>
</div>
{% endif %}
{% endblock %}
