{% if error %}
<p class="err">{{ error }}</p>
{% elif not missions %}
<p class="muted">Aucune mission dans state.db.</p>
{% else %}
{% for m in missions %}
<div class="pipeline-mission">
  <div class="pm-header">
    <span class="pm-name">{{ m.name }}</span>
    <span class="pm-status status-{{ m.status }}">{{ m.status }}</span>
    <span class="pm-counts">
      {% if m.counts.get('done') %}<span class="cnt ok">{{ m.counts.done }} done</span>{% endif %}
      {% if m.counts.get('running') %}<span class="cnt busy">{{ m.counts.running }} running</span>{% endif %}
      {% if m.counts.get('queued') %}<span class="cnt muted">{{ m.counts.queued }} queued</span>{% endif %}
      {% if m.counts.get('degraded') %}<span class="cnt warn">{{ m.counts.degraded }} degraded</span>{% endif %}
      {% if m.counts.get('error') %}<span class="cnt err">{{ m.counts.error }} error</span>{% endif %}
    </span>
  </div>
  {% if m.auvs %}
  <div class="pm-auvs">
    <span class="pm-auvs-label">Viser AUV:</span>
    {% for auv_id in m.auvs %}
    <button class="btn-viser-auv"
            data-url="/pipeline/missions/{{ m.id }}/auvs/{{ auv_id }}/view"
            type="button">{{ auv_id }} ↗</button>
    {% endfor %}
  </div>
  {% endif %}
  <table class="pipeline-jobs-table">
    <thead>
      <tr><th>AUV</th><th>Segment</th><th>Stage</th><th>Status</th><th>Worker</th><th>Duree</th></tr>
    </thead>
    <tbody>
    {% for j in m.jobs %}
    <tr class="pj-row status-{{ j.status }}">
      <td>{{ j.auv_id }}</td>
      <td class="muted">{{ j.segment_label or '-' }}</td>
      <td><code>{{ j.stage }}</code></td>
      <td><span class="pj-badge status-{{ j.status }}">{{ j.status }}</span></td>
      <td class="muted">{{ j.worker_host or '-' }}</td>
      <td class="muted">
        {% if j.started_at and j.finished_at %}
          {{ j.finished_at[11:16] if j.finished_at else '' }}
        {% elif j.started_at %}
          {{ j.started_at[11:16] }} →
        {% else %}-{% endif %}
      </td>
    </tr>
    {% if j.error_msg %}
    <tr class="pj-err-row"><td colspan="6" class="err" style="font-size:0.72rem;padding:2px 8px">{{ j.error_msg[:120] }}</td></tr>
    {% endif %}
    {% endfor %}
    </tbody>
  </table>
</div>
{% endfor %}
{% endif %}
