{% set mine_users = (state.usernames | default({})).values() | list %}
{% if mrs %}
{% set mine_count = [] %}
{% set approved_count = [] %}
{% for m in mrs %}
    {% set _involved = m.author in mine_users or (m.assignees | default([]) | select("in", mine_users) | list | length > 0) or (m.reviewers | default([]) | select("in", mine_users) | list | length > 0) %}
    {% if _involved %}{% if mine_count.append(1) %}{% endif %}{% endif %}
    {% if m.approved %}{% if approved_count.append(1) %}{% endif %}{% endif %}
{% endfor %}

<div class="stats-bar">
    <div class="stat-chip">
        <span class="stat-chip-value">{{ mrs | length }}</span>
        <span class="stat-chip-label">total</span>
    </div>
    {% if mine_count %}
    <div class="stat-chip stat-chip-accent">
        <span class="stat-chip-value">{{ mine_count | length }}</span>
        <span class="stat-chip-label">mine</span>
    </div>
    {% endif %}
    {% if approved_count %}
    <div class="stat-chip stat-chip-success">
        <span class="stat-chip-value">{{ approved_count | length }}</span>
        <span class="stat-chip-label">approved</span>
    </div>
    {% endif %}
</div>

<table class="sortable">
    <thead>
        <tr>
            <th>Repo</th>
            <th>Title</th>
            <th>Author</th>
            <th>Branches</th>
            <th>Status</th>
            <th class="no-sort">Info</th>
        </tr>
    </thead>
    <tbody>
        {% for m in mrs %}
        {% set _involved = m.author in mine_users or (m.assignees | default([]) | select("in", mine_users) | list | length > 0) or (m.reviewers | default([]) | select("in", mine_users) | list | length > 0) %}
        <tr class="{% if _involved %}row-mine{% endif %}">
            <td><a href="/repo/{{ m.repo_name }}?from=mrs"><code>{{ m.repo_name }}</code></a></td>
            <td>
                <a href="{{ m.web_url }}" target="_blank">{{ m.title }}</a>
                <span class="mr-iid">!{{ m.iid }}</span>
            </td>
            <td>{{ m.author }}</td>
            <td class="branch-cell">
                <code title="{{ m.source_branch }}">{{ m.source_branch }}</code>
                <span class="branch-arrow">&rarr;</span>
                <code title="{{ m.target_branch }}">{{ m.target_branch }}</code>
            </td>
            <td><span class="badge-{% if m.approved %}approved{% else %}{{ m.state }}{% endif %}">{% if m.approved %}approved{% else %}{{ m.state }}{% endif %}</span></td>
            <td class="info-cell">
                {% if m.user_notes_count %}
                <span class="comment-count" title="{{ m.user_notes_count }} comments">
                    <svg class="inline-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
                    {{ m.user_notes_count }}
                </span>
                {% endif %}
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% else %}
<p class="text-center-muted">No open merge requests.</p>
{% endif %}
