{% extends "base.html" %}

{% block title %}Tasks - Admin - {{ config.APP_NAME }}{% endblock %}

{% block content %}
<main class="container">
  <header style="display: flex; justify-content: space-between; align-items: center;">
    <h1>Task Queue</h1>
    <a href="{{ url_for('admin.index') }}" role="button" class="secondary outline">← Dashboard</a>
  </header>
  
  <!-- Failed Tasks -->
  {% if failed_tasks %}
  <section>
    <h2 style="color: var(--del-color);">Failed Tasks ({{ failed_tasks | length }})</h2>
    <article style="border-color: var(--del-color);">
      <table>
        <thead>
          <tr>
            <th>ID</th>
            <th>Task</th>
            <th>Error</th>
            <th>Retries</th>
            <th>Created</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          {% for task in failed_tasks %}
          <tr>
            <td>{{ task.id }}</td>
            <td><code>{{ task.task_name }}</code></td>
            <td>
              <details>
                <summary>{{ task.error[:40] if task.error else 'No error' }}...</summary>
                <pre style="font-size: 0.75rem; white-space: pre-wrap;">{{ task.error }}</pre>
              </details>
            </td>
            <td>{{ task.retries }}</td>
            <td>{{ task.created_at[:16] }}</td>
            <td>
              <div style="display: flex; gap: 0.5rem;">
                <form method="post" action="{{ url_for('admin.task_retry', task_id=task.id) }}" style="margin: 0;">
                  <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                  <button type="submit" class="outline" style="padding: 0.25rem 0.5rem; margin: 0;">Retry</button>
                </form>
                <form method="post" action="{{ url_for('admin.task_delete', task_id=task.id) }}" style="margin: 0;">
                  <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                  <button type="submit" class="outline secondary" style="padding: 0.25rem 0.5rem; margin: 0;">Delete</button>
                </form>
              </div>
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </article>
  </section>
  {% endif %}
  
  <!-- All Tasks -->
  <section>
    <h2>Recent Tasks</h2>
    <article>
      {% if tasks %}
      <table>
        <thead>
          <tr>
            <th>ID</th>
            <th>Task</th>
            <th>Status</th>
            <th>Run At</th>
            <th>Created</th>
            <th>Completed</th>
          </tr>
        </thead>
        <tbody>
          {% for task in tasks %}
          <tr>
            <td>{{ task.id }}</td>
            <td><code>{{ task.task_name }}</code></td>
            <td>
              {% if task.status == 'complete' %}
              <span style="color: var(--ins-color);">✓ complete</span>
              {% elif task.status == 'failed' %}
              <span style="color: var(--del-color);">✗ failed</span>
              {% elif task.status == 'pending' %}
              <span style="color: var(--mark-background-color);">○ pending</span>
              {% else %}
              {{ task.status }}
              {% endif %}
            </td>
            <td>{{ task.run_at[:16] if task.run_at else '-' }}</td>
            <td>{{ task.created_at[:16] }}</td>
            <td>{{ task.completed_at[:16] if task.completed_at else '-' }}</td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
      {% else %}
      <p>No tasks in queue.</p>
      {% endif %}
    </article>
  </section>
</main>
{% endblock %}
