{% import "macros.html.j2" as macros %}
<h2>Job history</h2>
{% with jobs = ctx.db.list_job_history(uid=None if ctx.is_admin else ctx.uid) %}
{% if jobs %}
{% for job in jobs %}
<div class="job row u-flex">
  <div class="details eight columns u-flex u-flex-wrap u-overflow-elipsis">
    <div class="job-filename u-flex u-flex-break">
      <label for="filename">File</label>
      <span name="filename">{{ctx.db.fetch_file(None if ctx.is_admin else ctx.uid, job.file_id).filename or "it's a secret"}}</span>
    </div>
    {% if job.printer_id %}
    <div class="job-printer u-flex">
      <label for="printer">Printer</label>
      <span name="printer">{{ ctx.db.fetch_printer(job.printer_id).name }}</span>
    </div>
    {% endif %}
    {% if job.finished_at and job.started_at %}
    <div class="job-runtime u-flex">
      <label for="runtime">Runtime</label>
      <span name="Runtime">{{ (datetime.fromisoformat(job.finished_at) - datetime.fromisoformat(job.started_at)) }}</span>
    </div>
    {% endif %}
    <div class="u-flex-break"></div>
    {% if job.user_id != ctx.uid %}
    <div class="job-user u-flex">
      <label>Owner</label>
      {{ ctx.db.fetch_user(uid=job.user_id).name }}
    </div>
    {% endif %}
  </div>
  <div class="two columns">
    <div class="job-status u-flex">
      <label for="state">Status</label>
      <div class="dot {{ macros.job_state(job) }} {{ macros.job_active(job) }} tooltip bottom" data-text="{{ macros.job_state(job) }}" style="--dot-size: 1em;"> </div>
    </div>
  </div>
  <div class="controls u-flex u-ml-auto u-mv-auto">
    {% if ctx.uid %}
    {{ macros.duplicate_job(job.id) }}
    {{ macros.delete_job(job.id) }}
    {% endif %}
  </div>
</div>
{% endfor %}
{% else %}
  <p>No job history to display.</p>
{% endif %}
{% endwith %}
