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

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1><i class="bi bi-buildings"></i> Bâtiments</h1>
    <a href="{{ url_for('buildings.create') }}" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Nouveau bâtiment</a>
</div>

<div class="card mb-4"><div class="card-header"><i class="bi bi-geo"></i> Sites</div><div class="card-body">
  <div class="d-flex flex-wrap gap-2 mb-3">{% for site in sites %}<span class="badge text-bg-light border">{{ site.name }} · {{ site.site_type|replace('_',' ') }}</span>{% else %}<span class="text-muted">Aucun site défini.</span>{% endfor %}</div>
  <form method="post" action="{{ url_for('buildings.create_site') }}" class="row g-2">
    <div class="col-md-4"><input class="form-control" name="name" placeholder="Ex. Collège principal" required></div>
    <div class="col-md-3"><select class="form-select" name="site_type"><option value="college">Collège</option><option value="logements">Logements de fonction</option><option value="annexe">Annexe</option><option value="autre">Autre</option></select></div>
    <div class="col-md-3"><input class="form-control" name="address" placeholder="Adresse (facultatif)"></div>
    <div class="col-md-2"><button class="btn btn-outline-primary w-100" type="submit">Ajouter</button></div>
  </form>
</div></div>

<div class="row">
{% for building in buildings %}
<div class="col-md-6 col-lg-4 mb-3">
    <div class="card">
        <div class="card-body">
            <h5 class="card-title">{{ building.name }}</h5>
            <div class="small text-muted mb-2"><i class="bi bi-geo-alt"></i> {{ building.site.name if building.site else 'Site non renseigné' }}</div>
            <p class="card-text text-muted">{{ building.description or '' }}</p>
            <p><span class="badge bg-secondary">{{ building.rooms.count() }} salle(s)</span></p>
            <div class="btn-group">
                <a href="{{ url_for('buildings.edit', id=building.id) }}" class="btn btn-sm btn-outline-primary">Modifier</a>
                <a href="{{ url_for('rooms.index', building=building.id) }}" class="btn btn-sm btn-outline-secondary">Salles</a>
            </div>
        </div>
    </div>
</div>
{% endfor %}
</div>
{% endblock %}
