{% extends "base.html" %}{% block title %}{{ unit.name }} — GMAO{% endblock %}{% block content %}
<div class="d-flex flex-wrap justify-content-between gap-2 mb-4"><div><h1 class="h3"><i class="bi bi-house-door"></i> {{ unit.name }}</h1><div class="text-muted">{{ unit.building.site.name if unit.building.site else '' }} · {{ unit.building.name }}</div></div><div><a href="{{ url_for('housing.edit', unit_id=unit.id) }}" class="btn btn-outline-primary">Modifier</a> <a href="{{ url_for('wizard.equipment_wizard', housing_id=unit.id) }}" class="btn btn-success"><i class="bi bi-magic"></i> Inventorier</a></div></div>
<div class="row g-3"><div class="col-lg-4">
  <div class="card mb-3"><div class="card-header">Occupation</div><div class="card-body"><p><strong>État :</strong> {{ unit.occupancy_status|replace('_',' ')|title }}</p>{% if has_permission('housing.private') %}<p><strong>Occupant :</strong> {{ unit.occupant_name or 'Aucun' }}</p><p><strong>Contact :</strong> {{ unit.occupant_contact or '—' }}</p>{% else %}<p class="text-muted"><i class="bi bi-lock"></i> Données personnelles masquées.</p>{% endif %}<p><strong>Période :</strong> {{ unit.occupancy_start|date_fmt if unit.occupancy_start else '—' }} → {{ unit.occupancy_end|date_fmt if unit.occupancy_end else '—' }}</p></div></div>
  {% if has_permission('housing.private') %}<div class="card"><div class="card-header">Historique d’occupation</div><div class="list-group list-group-flush">{% for entry in unit.occupancies %}<div class="list-group-item"><strong>{{ entry.occupant_name }}</strong><div class="small text-muted">{{ entry.start_date|date_fmt }} → {{ entry.end_date|date_fmt if entry.end_date else 'en cours' }} · {{ entry.key_count }} clé(s)</div>{% if entry.entry_condition %}<div class="small mt-1">Entrée : {{ entry.entry_condition }}</div>{% endif %}{% if entry.exit_condition %}<div class="small">Sortie : {{ entry.exit_condition }}</div>{% endif %}</div>{% else %}<div class="list-group-item text-muted">Aucun historique.</div>{% endfor %}</div></div>{% endif %}
</div><div class="col-lg-8">
  <div class="card mb-3"><div class="card-header">Pièces</div><div class="card-body"><div class="list-group mb-3">{% for room in unit.rooms %}<div class="list-group-item d-flex justify-content-between align-items-center"><a href="{{ url_for('rooms.detail', id=room.id) }}">{{ room.name }}</a><div><span class="badge text-bg-secondary">{{ room.equipments|length }} équipement(s)</span><form method="post" action="{{ url_for('housing.unlink_room', unit_id=unit.id, room_id=room.id) }}" class="d-inline"><button class="btn btn-sm btn-outline-danger" title="Détacher sans supprimer"><i class="bi bi-x"></i></button></form></div></div>{% else %}<div class="text-muted">Aucune pièce.</div>{% endfor %}</div>
    <form method="post" action="{{ url_for('housing.add_room', unit_id=unit.id) }}" class="row g-2 mb-3"><div class="col"><input name="name" class="form-control" placeholder="Nouvelle pièce : cuisine, séjour…" required></div><div class="col-sm-3"><input name="code" class="form-control" placeholder="Code"></div><div class="col-auto"><button class="btn btn-primary">Créer</button></div></form>
    {% if available_rooms %}<form method="post" action="{{ url_for('housing.link_rooms', unit_id=unit.id) }}"><label class="form-label">Rattacher des pièces existantes du bâtiment</label><div class="input-group"><select class="form-select" name="room_ids" multiple>{% for room in available_rooms %}<option value="{{ room.id }}">{{ room.name }}</option>{% endfor %}</select><button class="btn btn-outline-primary">Rattacher</button></div></form>{% endif %}
  </div></div>
  <div class="card"><div class="card-header">Patrimoine du logement</div><div class="list-group list-group-flush">{% for equipment in equipments %}<a href="{{ url_for('equipments.detail', id=equipment.id) }}" class="list-group-item list-group-item-action d-flex justify-content-between"><span>{{ equipment.name }} <small class="text-muted">· {{ equipment.room.name }}</small></span><span>{{ equipment.quantity }}</span></a>{% else %}<div class="list-group-item text-muted">Aucun équipement.</div>{% endfor %}</div></div>
</div></div>
{% endblock %}
