{% extends "base.html" %}
{% block title %}Cycle de vie des équipements — GMAO{% endblock %}
{% block content %}
<div class="container-fluid">
  <div class="d-flex justify-content-between align-items-center mb-3"><div><h1 class="h3 mb-1"><i class="bi bi-activity"></i> Cycle de vie des équipements</h1><p class="text-muted mb-0">Réparations, décisions de mise au rebut et historique de sortie.</p></div><a class="btn btn-outline-secondary" href="{{ url_for('equipments.index') }}">Inventaire</a></div>
  <div class="row g-3 mb-4">
    <div class="col-md-4"><a class="card text-decoration-none {{ 'border-warning' if status == 'en_reparation' }}" href="{{ url_for('equipments.lifecycle_dashboard', status='en_reparation') }}"><div class="card-body"><div class="text-muted">En réparation</div><div class="display-6 text-warning">{{ counts.en_reparation }}</div></div></a></div>
    <div class="col-md-4"><a class="card text-decoration-none {{ 'border-danger' if status == 'mise_au_rebut_planifiee' }}" href="{{ url_for('equipments.lifecycle_dashboard', status='mise_au_rebut_planifiee') }}"><div class="card-body"><div class="text-muted">Rebut à décider</div><div class="display-6 text-danger">{{ counts.mise_au_rebut_planifiee }}</div></div></a></div>
    <div class="col-md-4"><a class="card text-decoration-none {{ 'border-secondary' if status == 'jete' }}" href="{{ url_for('equipments.lifecycle_dashboard', status='jete') }}"><div class="card-body"><div class="text-muted">Sortis du patrimoine</div><div class="display-6 text-secondary">{{ counts.jete }}</div></div></a></div>
  </div>
  <div class="card"><div class="card-header d-flex justify-content-between"><strong>{{ equipments|length }} équipement(s)</strong><a href="{{ url_for('equipments.lifecycle_dashboard', status='all') }}">Tout afficher</a></div><div class="table-responsive"><table class="table table-hover align-middle mb-0"><thead><tr><th>Équipement</th><th>Localisation d’origine</th><th>Lot / préventif</th><th>État</th><th>Depuis / échéance</th><th>Actions</th></tr></thead><tbody>
  {% for equipment in equipments %}<tr><td><a href="{{ url_for('equipments.detail', id=equipment.id) }}"><strong>{{ equipment.name }}</strong></a>{% if equipment.code %}<div class="small text-muted">{{ equipment.code }}</div>{% endif %}</td><td>{{ equipment.lifecycle_events[0].room_name_snapshot if equipment.lifecycle_events else (equipment.effective_room.name if equipment.effective_room else '—') }}</td><td>{{ equipment.effective_lot.name if equipment.effective_lot else 'Sans lot' }}<div class="small text-muted">{{ equipment.scheduled_tasks.filter_by(status='suspended').count() }} tâche(s) suspendue(s)</div></td><td>{% if equipment.lifecycle_status == 'en_reparation' %}<span class="badge bg-warning text-dark">En réparation</span>{% elif equipment.lifecycle_status == 'mise_au_rebut_planifiee' %}<span class="badge bg-danger">Rebut proposé</span>{% elif equipment.lifecycle_status == 'jete' %}<span class="badge bg-dark">Sorti</span>{% else %}<span class="badge bg-success">En service</span>{% endif %}</td><td>{{ equipment.unavailable_since|datetime_fmt if equipment.unavailable_since else (equipment.planned_disposal_date|date_fmt if equipment.planned_disposal_date else (equipment.disposal_date|date_fmt if equipment.disposal_date else '—')) }}</td><td>
    {% if equipment.lifecycle_status == 'en_reparation' %}<form method="post" action="{{ url_for('equipments.complete_repair', id=equipment.id) }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><button class="btn btn-sm btn-success">Remettre en service</button></form>{% elif equipment.lifecycle_status == 'mise_au_rebut_planifiee' %}<form class="d-inline" method="post" action="{{ url_for('equipments.confirm_trashed', id=equipment.id) }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><button class="btn btn-sm btn-danger" onclick="return confirm('Confirmer la sortie définitive ?')">Confirmer le rebut</button></form> <form class="d-inline" method="post" action="{{ url_for('equipments.cancel_trash', id=equipment.id) }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><button class="btn btn-sm btn-outline-success">Annuler</button></form>{% endif %}
  </td></tr>{% else %}<tr><td colspan="6" class="text-center text-muted py-5">Aucun équipement dans cet état.</td></tr>{% endfor %}
  </tbody></table></div></div>
</div>
{% endblock %}
