{% extends "base.html" %}
{% block title %}Builds - circus{% endblock %}
{% block auth %}
    {% if !auth_name.is_empty() %}
        <span class="auth-user">{{ auth_name }}</span>
        <form method="POST" action="/logout">
            <button class="btn-ghost" type="submit">Logout</button>
        </form>
    {% else %}
        <a class="btn btn-secondary" href="/login">Login</a>
    {% endif %}
{% endblock %}
{% block content %}
    <h1>Builds</h1>

    <form method="get" action="/builds" class="filter-form">
        <label>
            Status:
            <select name="status">
                <option value="">All</option>
                <option value="pending" {% if filter_status == "pending" %}selected{% endif %}>Pending</option>
                <option value="running" {% if filter_status == "running" %}selected{% endif %}>Running</option>
                <option value="succeeded" {% if filter_status == "succeeded" %}selected{% endif %}>Completed</option>
                <option value="failed" {% if filter_status == "failed" %}selected{% endif %}>Failed</option>
            </select>
        </label>
        <label>
            System: <input type="text" name="system" value="{{ filter_system }}" placeholder="e.g. x86_64-linux">
        </label>
        <label>
            Job: <input type="text" name="job_name" value="{{ filter_job }}" placeholder="job name">
        </label>
        <button type="submit" class="btn btn-secondary">Filter</button>
    </form>

    {% if builds.is_empty() %}
        <div class="empty">
            <div class="empty-title">No builds match filters</div>
            <div class="empty-hint">Try adjusting the filters above or wait for builds to be queued.</div>
        </div>
    {% else %}
        <div class="table-wrap">
            <table class="data-table dense-table">
                <thead>
                    <tr>
                        <th>Build</th>
                        <th>Project</th>
                        <th>Jobset</th>
                        <th>Job</th>
                        <th>System</th>
                        <th>Status</th>
                        <th>Duration</th>
                        <th>Created</th>
                        <th>Log</th>
                    </tr>
                </thead>
                <tbody>
                    {% for b in builds %}
                        <tr>
                            <td class="mono">
                                <a href="/build/{{ b.id }}"
                                   title="{{ b.id }}"
                                   aria-label="Build {{ b.id }}">#{{ b.id_short }}</a>
                            </td>
                            <td class="truncate" title="{{ b.project_name }}">
                                {% match b.project_id %}
                                    {% when Some with (project_id) %}
                                        <a href="/project/{{ project_id }}">{{ b.project_name }}</a>
                                    {% when None %}
                                        -
                                {% endmatch %}
                            </td>
                            <td class="mono truncate">
                                {% match b.jobset_id %}
                                    {% when Some with (jobset_id) %}
                                        <a href="/jobset/{{ jobset_id }}">{{ b.jobset_name }}</a>
                                    {% when None %}
                                        -
                                {% endmatch %}
                            </td>
                            <td class="mono truncate" title="{{ b.job_name }}">
                                <a href="/build/{{ b.id }}">{{ b.job_name }}</a>
                            </td>
                            <td class="mono nowrap">{{ b.system }}</td>
                            <td>
                                <span class="status status-{{ b.status_class }}">{{ b.status_text }}</span>
                            </td>
                            <td class="mono">{{ b.duration }}</td>
                            <td class="mono nowrap">{{ b.created_at }}</td>
                            <td>
                                {% if b.has_log %}
                                    <a href="/build/{{ b.id }}/log">log</a>
                                {% else %}
                                    -
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% if total_pages > 1 %}
            <nav class="pagination">
                {% if has_prev %}
                    <a href="/builds?offset={{ prev_offset }}&limit={{ limit }}&status={{ filter_status }}&system={{ filter_system }}&job_name={{ filter_job }}"
                       class="btn btn-small btn-secondary">
                        &laquo; Previous
                    </a>
                {% endif %}
                <span class="text-muted">Page {{ page }} of {{ total_pages }}</span>
                {% if has_next %}
                    <a href="/builds?offset={{ next_offset }}&limit={{ limit }}&status={{ filter_status }}&system={{ filter_system }}&job_name={{ filter_job }}"
                       class="btn btn-small btn-secondary">
                        Next &raquo;
                    </a>
                {% endif %}
            </nav>
        {% endif %}
    {% endif %}
{% endblock %}
