{% from "_macros.html" import icon %}
{#
  What is running on the far side, and what it has printed.

  EVERYTHING here came off somebody else's machine and is untrusted exactly as
  much as model output is: the command was written by a model, the log is
  whatever the command printed. Jinja autoescaping covers the lot, and the log
  is `<pre>` rather than anything that could emit HTML -- services/markdown.py is
  the one path allowed to do that, and this is the last content that should be
  given it.

  Not polled. The chip beside the composer is what refreshes on a timer; this is
  fetched when somebody opens it and re-rendered when they press something,
  because reading a log is an SSH round trip per job and nobody is looking at
  most of them.
#}
{% if not jobs %}
<p class="picker__lede">Nothing is running.</p>
{% else %}
<p class="picker__group">Background jobs</p>

{% for job in jobs %}
<div class="jobs__row{{ ' jobs__row--open' if open_job == job.id }}">
  <div class="jobs__head">
    {# `tone`, not `status`: `done` covers exit 0 and exit 2 alike, and a green
       dot beside the "Failed, exit 2" below would be the dot contradicting the
       sentence. The words stay here; only the colour is decided in Python. #}
    <span class="jobs__dot jobs__dot--{{ job.tone }}"
          title="{{ job.status }}{% if job.exit_status is not none %} ({{ job.exit_status }}){% endif %}"></span>

    {# The whole command in the title, a truncated one on screen. A command line
       is unbounded and this sits in a menu with a fixed width; CSS truncates,
       and the title is how you read the rest. #}
    <button class="jobs__command" type="button"
            title="{{ job.command }}"
            hx-get="/api/chats/{{ chat.id }}/jobs/panel{% if open_job != job.id %}?job={{ job.id }}{% endif %}"
            hx-target="#jobs-panel" hx-swap="innerHTML">
      <code>{{ job.command or "(no command recorded)" }}</code>
    </button>

    {% if job.running %}
    <button class="btn btn--sm btn--danger jobs__stop" type="button"
            hx-post="/api/chats/{{ chat.id }}/jobs/{{ job.id }}/stop"
            hx-target="#jobs-panel" hx-swap="innerHTML"
            data-confirm-button="Stop this job? It and everything it started are killed.">
      Stop
    </button>
    {% endif %}
  </div>

  <p class="jobs__meta">
    {% if job.running %}
      Running
    {% elif job.status == "killed" %}
      Stopped
    {% elif job.status == "lost" %}
      {# The far side has no record of it: the host rebooted, or /tmp was
         cleared. Said plainly rather than shown as a failure, because nothing
         failed -- we simply cannot say how it ended. #}
      No longer traceable
    {% elif job.exit_status %}
      Failed, exit {{ job.exit_status }}
    {% else %}
      Finished
    {% endif %}
    {% if job.started_at %}· started {{ job.started_at.strftime("%H:%M") }}{% endif %}
    {# Only ever on a finished job -- see JobView.duration for why a running one
       says nothing rather than saying something frozen. #}
    {% if job.duration %} · took {{ job.duration }}{% endif %}
  </p>

  {% if open_job == job.id %}
  {% if error %}
  <p class="jobs__error">{{ error }}</p>
  {% else %}
  <pre class="jobs__log">{{ body or "Nothing printed yet." }}</pre>
  {% endif %}
  {% endif %}
</div>
{% endfor %}
{% endif %}
