<div class="container paper-page admin-page">
  <div class="paper-crumbs">Admin &nbsp;/&nbsp; <span class="here">Queues</span></div>

  <div class="q-header">
    <h1 class="paper-page-title">Queues</h1>
    <div class="q-header-actions">
      <div class="q-range-btns">
        <button class="btn btn-sm" ng-class="{active: range == '1h'}" ng-click="setRange('1h')">1h</button>
        <button class="btn btn-sm" ng-class="{active: range == '6h'}" ng-click="setRange('6h')">6h</button>
        <button class="btn btn-sm" ng-class="{active: range == '24h'}" ng-click="setRange('24h')">24h</button>
        <button class="btn btn-sm" ng-class="{active: range == '7d'}" ng-click="setRange('7d')">7d</button>
      </div>
      <button class="btn btn-sm" ng-click="pauseAll()">Pause all</button>
      <button class="btn btn-sm btn-dark" ng-click="drainSelected()">Drain {{selectedQueue}}</button>
    </div>
  </div>

  <nav class="admin-nav">
    <a href="/admin/"><i class="fas fa-tachometer-alt"></i> Overview</a>
    <a href="/admin/repositories"><i class="fas fa-code-branch"></i> Repositories</a>
    <a href="/admin/users"><i class="fas fa-users"></i> Users</a>
    <a href="/admin/conferences"><i class="fas fa-chalkboard-teacher"></i> Conferences</a>
    <a href="/admin/queues" class="active"><i class="fas fa-tasks"></i> Queues</a>
    <a href="/admin/errors"><i class="fas fa-bug"></i> Errors</a>
  </nav>

  <!-- Queue overview cards -->
  <div class="q-cards">
    <div class="q-card" ng-repeat="q in queueList" ng-class="{selected: selectedQueue == q.key, paused: q.paused}" ng-click="selectQueue(q.key)">
      <div class="q-card-head">
        <span class="q-dot" ng-class="{'q-dot-red': q.paused || q.counts.failed > 0}"></span>
        <span class="q-card-name" ng-bind="q.label"></span>
      </div>
      <div class="q-card-count" ng-bind="(q.counts.waiting || 0) + (q.counts.active || 0) + (q.counts.delayed || 0)"></div>
      <div class="q-card-sub">
        <span>waiting &middot; {{q.counts.active || 0}} active</span>
        <div class="q-card-bar" ng-if="q.counts.active">
          <div class="q-card-bar-fill" ng-style="{width: (q.counts.active / ((q.counts.waiting || 0) + (q.counts.active || 0) + (q.counts.delayed || 0) || 1) * 100) + '%'}"></div>
        </div>
      </div>
    </div>
  </div>

  <!-- Detail: throughput chart + stats panel -->
  <div class="q-detail" ng-if="selectedStats">
    <div class="q-throughput">
      <div class="q-section-label">{{selectedQueue}}&middot;throughput <span class="q-section-right"><span class="q-legend-completed">&#9679;</span> completed <span class="q-legend-failed">&#9679;</span> failed <span class="q-legend-exec">- -</span> avg time &middot; {{range | uppercase}}</span></div>
      <div class="q-chart-wrap">
        <canvas id="q-throughput-chart" height="180"></canvas>
        <div id="q-chart-tooltip" class="q-chart-tooltip" style="display:none;"></div>
        <div id="q-chart-crosshair" class="q-chart-crosshair" style="display:none;"></div>
      </div>
    </div>
    <div class="q-stats-panel">
      <div class="q-section-label">{{selectedQueue}}&middot;stats</div>
      <div class="q-stats-grid">
        <div class="q-stat">
          <div class="q-stat-label">WAITING</div>
          <div class="q-stat-value" ng-bind="selectedStats.counts.waiting || 0"></div>
        </div>
        <div class="q-stat">
          <div class="q-stat-label">ACTIVE</div>
          <div class="q-stat-value" ng-bind="selectedStats.counts.active || 0"></div>
        </div>
        <div class="q-stat">
          <div class="q-stat-label">COMPLETED (24H)</div>
          <div class="q-stat-value" ng-bind="selectedStats.completed24h | number"></div>
        </div>
        <div class="q-stat">
          <div class="q-stat-label">FAILED (24H)</div>
          <div class="q-stat-value" ng-bind="selectedStats.failed24h || 0"></div>
        </div>
        <div class="q-stat">
          <div class="q-stat-label">DELAYED</div>
          <div class="q-stat-value" ng-bind="selectedStats.counts.delayed || 0"></div>
        </div>
        <div class="q-stat">
          <div class="q-stat-label">WORKERS</div>
          <div class="q-stat-value" ng-bind="selectedStats.workers || 0"></div>
        </div>
      </div>
      <div class="q-stats-actions">
        <button class="btn btn-sm" ng-click="togglePause()">{{selectedStats.paused ? 'Resume' : 'Pause'}}</button>
        <button class="btn btn-sm" ng-click="retryFailed()" ng-disabled="!selectedStats.counts.failed">Retry failed</button>
        <button class="btn btn-sm" ng-click="emptyQueue()">Empty</button>
      </div>
    </div>
  </div>

  <div class="q-toast-error" ng-if="actionError" ng-click="actionError = null">
    <i class="fas fa-exclamation-circle"></i> {{actionError}}
  </div>

  <!-- Jobs table -->
  <div class="q-jobs">
    <div class="q-jobs-header">
      <div class="q-section-label">ALL JOBS &middot; {{selectedQueue | uppercase}}</div>
      <div class="q-state-filters">
        <label class="q-state-toggle" ng-repeat="s in allStates">
          <input type="checkbox" ng-model="stateFilter[s]" />
          <span class="q-state-chip q-state-{{s}}">{{s}}</span>
        </label>
      </div>
    </div>

    <div class="q-search-row">
      <input type="search" class="form-control" placeholder="Search by job/repo id…" ng-model="query.search" autocomplete="off" />
      <label class="q-auto-refresh">
        <input type="checkbox" ng-model="query.autoRefresh" />
        Auto-refresh
      </label>
      <button class="btn btn-sm" type="button" ng-click="refreshNow()" title="Refresh now"><i class="fas fa-sync"></i></button>
    </div>

    <table class="q-table" ng-if="filteredJobs().length > 0">
      <thead>
        <tr>
          <th>STATE</th>
          <th>JOB ID</th>
          <th>PAYLOAD</th>
          <th>ATTEMPTS</th>
          <th>DURATION</th>
          <th>PROGRESS</th>
          <th></th>
        </tr>
      </thead>
      <tbody ng-repeat="job in filteredJobs()">
        <tr ng-class="{'q-row-failed': job._state == 'failed', 'q-row-expanded': expanded[job.id]}" ng-click="toggleJob(job)" style="cursor:pointer;">
          <td class="q-cell-state">
            <span class="q-state-badge q-state-{{job._state}}" ng-bind="job._state"></span>
            <span class="q-delay-hint" ng-if="job._state == 'delayed' && job.delayUntil" ng-bind="delayCountdown(job.delayUntil)"></span>
          </td>
          <td class="q-cell-id">
            <i class="fas fa-chevron-right q-chevron" ng-class="{'q-chevron-open': expanded[job.id]}"></i>
            <a target="_blank" ng-href="/r/{{job.id}}" ng-click="$event.stopPropagation()" ng-bind="'job:' + (job.id | limitTo:6)"></a>
          </td>
          <td class="q-cell-payload">
            <span ng-bind="job.name || 'anonymize'"></span>
            <span class="q-payload-detail" ng-if="job.data.repoId && job.data.repoId !== job.name"> &middot; {{job.data.repoId}}</span>
          </td>
          <td class="q-cell-num" ng-bind="job.attemptsMade || 1"></td>
          <td class="q-cell-num" ng-bind="jobDuration(job)"></td>
          <td class="q-cell-progress">
            <div class="q-progress-wrap" ng-if="jobProgressPct(job) !== null">
              <div class="q-progress-bar" ng-style="{'--pct': jobProgressPct(job) + '%'}"></div>
              <span class="q-progress-label" ng-bind="jobProgressPct(job) + '%'"></span>
            </div>
          </td>
          <td class="q-cell-actions">
            <button class="btn btn-sm" ng-click="retryJob(job); $event.stopPropagation()" title="Retry" ng-if="job._state == 'failed'"><i class="fas fa-sync"></i></button>
            <button class="btn btn-sm" ng-click="removeJob(job); $event.stopPropagation()" title="Remove"><i class="fas fa-trash-alt"></i></button>
          </td>
        </tr>
        <tr class="q-detail-row" ng-if="expanded[job.id]">
          <td colspan="7">
            <div class="q-job-detail">
              <div class="q-job-detail-grid">
                <div class="q-job-detail-item">
                  <span class="q-job-detail-label">JOB ID</span>
                  <span class="q-job-detail-value"><a target="_blank" ng-href="/r/{{job.id}}" ng-bind="job.id"></a></span>
                </div>
                <div class="q-job-detail-item">
                  <span class="q-job-detail-label">STATE</span>
                  <span class="q-job-detail-value"><span class="q-state-badge q-state-{{job._state}}" ng-bind="job._state"></span></span>
                </div>
                <div class="q-job-detail-item" ng-if="job.data.repoId">
                  <span class="q-job-detail-label">REPO ID</span>
                  <span class="q-job-detail-value" ng-bind="job.data.repoId"></span>
                </div>
                <div class="q-job-detail-item" ng-if="job.timestamp">
                  <span class="q-job-detail-label">CREATED</span>
                  <span class="q-job-detail-value" ng-bind="humanTime(job.timestamp)"></span>
                </div>
                <div class="q-job-detail-item" ng-if="job._state == 'delayed' && job.delayUntil">
                  <span class="q-job-detail-label">RETRY AT</span>
                  <span class="q-job-detail-value">{{humanTime(job.delayUntil)}} ({{delayCountdown(job.delayUntil)}})</span>
                </div>
                <div class="q-job-detail-item" ng-if="job.processedOn">
                  <span class="q-job-detail-label">PROCESSED</span>
                  <span class="q-job-detail-value" ng-bind="humanTime(job.processedOn)"></span>
                </div>
                <div class="q-job-detail-item" ng-if="job.finishedOn">
                  <span class="q-job-detail-label">FINISHED</span>
                  <span class="q-job-detail-value" ng-bind="humanTime(job.finishedOn)"></span>
                </div>
                <div class="q-job-detail-item" ng-if="job.attemptsMade">
                  <span class="q-job-detail-label">ATTEMPTS</span>
                  <span class="q-job-detail-value" ng-bind="job.attemptsMade"></span>
                </div>
                <div class="q-job-detail-item" ng-if="job.progress && job.progress.status">
                  <span class="q-job-detail-label">STATUS</span>
                  <span class="q-job-detail-value" ng-bind="job.progress.status"></span>
                </div>
                <div class="q-job-detail-item" ng-if="jobProgressPct(job) !== null">
                  <span class="q-job-detail-label">PROGRESS</span>
                  <span class="q-job-detail-value" ng-bind="jobProgressPct(job) + '%'"></span>
                </div>
              </div>
              <div ng-if="job.failedReason" class="q-job-detail-error">
                <span class="q-job-detail-label">ERROR</span>
                <div class="q-error-reason" ng-bind="job.failedReason"></div>
              </div>
              <div ng-if="job.stacktrace.length">
                <span class="q-job-detail-label">STACKTRACE</span>
                <pre ng-repeat="stack in job.stacktrace track by $index" class="q-error-stack"><code ng-bind="stack"></code></pre>
              </div>
              <div class="q-job-detail-actions">
                <button class="btn btn-sm" ng-click="retryJob(job)" ng-if="job._state == 'failed'"><i class="fas fa-sync"></i> Retry</button>
                <button class="btn btn-sm" ng-click="removeJob(job)"><i class="fas fa-trash-alt"></i> Remove</button>
                <a class="btn btn-sm" target="_blank" ng-href="/r/{{job.id}}"><i class="fas fa-external-link-alt"></i> View repo</a>
              </div>
            </div>
          </td>
        </tr>
      </tbody>
    </table>

    <div class="paper-table-empty" ng-if="filteredJobs().length == 0" style="border:1px solid var(--border-color);border-radius:10px;background:var(--paper-card);">
      <i class="fas fa-check-circle"></i>
      <span ng-if="!query.search">No jobs in the {{selectedQueue}} queue.</span>
      <span ng-if="query.search">No jobs match the current filters.</span>
    </div>
  </div>
</div>
