{% set pipelines = pipelines if pipelines is defined else (state.pipelines if state else []) %}
{% if pipelines %}
{% set running = [] %}{% set failed = [] %}{% set ok = [] %}
{% for p in pipelines %}
    {% if p.status == "running" %}{% if running.append(1) %}{% endif %}
    {% elif p.status == "failed" %}{% if failed.append(1) %}{% endif %}
    {% elif p.status == "success" %}{% if ok.append(1) %}{% endif %}
    {% endif %}
{% endfor %}

<div class="stats-bar">
    <div class="stat-chip">
        <span class="stat-chip-value">{{ state.pipelines | length }}</span>
        <span class="stat-chip-label">total</span>
    </div>
    {% if failed %}
    <div class="stat-chip stat-chip-danger">
        <span class="stat-chip-value">{{ failed | length }}</span>
        <span class="stat-chip-label">failed</span>
    </div>
    {% endif %}
    {% if running %}
    <div class="stat-chip stat-chip-warning">
        <span class="stat-chip-value">{{ running | length }}</span>
        <span class="stat-chip-label">running</span>
    </div>
    {% endif %}
    {% if ok %}
    <div class="stat-chip stat-chip-success">
        <span class="stat-chip-value">{{ ok | length }}</span>
        <span class="stat-chip-label">success</span>
    </div>
    {% endif %}
</div>

<table class="sortable">
    <thead>
        <tr><th>Repo</th><th>Ref</th><th>Status</th><th>Created</th></tr>
    </thead>
    <tbody>
        {% for p in pipelines %}
        <tr>
            <td><a href="/repo/{{ p.repo_name }}?from=pipelines"><code>{{ p.repo_name }}</code></a></td>
            <td><code>{{ p.ref }}</code></td>
            <td>
                <a href="{{ p.web_url }}" target="_blank">
                    <span class="dot dot-{{ p.status }}"></span>
                    <span class="badge-{{ p.status }}">{{ p.status }}</span>
                </a>
            </td>
            <td data-sort="{{ p.created_at }}" title="{{ p.created_at }}">{{ p.created_at | timeago }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% else %}
<p class="text-center-muted">No pipelines.</p>
{% endif %}
