{% from "_projectstring.html" import projectstring, projecticon %}

{% macro render_pullrequest_row(request, repo, username, subtitle=True, class="", showproject=False, filterstatus=None) %}
    {% if request.status|lower == 'open' %}
        {% set status_color = "success" %}
    {% elif request.status|lower == 'merged' %}
        {% set status_color = "info" %}
    {% else %}
        {% set status_color = "danger" %}
    {% endif %}

<div class="request-row list-group-item list-group-item-action {{class}}">
        <div class="d-flex align-items-start">
            <span class="fa fa-fw text-{{status_color}} fa-arrow-circle-down pt-1"></span>
            <div class="w-100">
            <div class="ml-1 d-flex">
                <div>
                    <span class="text-{{status_color}} font-weight-bold">#{{request.id}}</span>
                    <span class="font-weight-bold">
                        <a class="notblue" href="{{ url_for(
                            'ui_ns.request_pull',
                            repo=repo.name,
                            username=repo.username if repo.is_fork else None,
                            namespace=repo.namespace,
                            requestid=request.id) }}">
                        {{request.title}}
                        </a>
                    </span>
                </div>
                <span class="font-weight-bold text-muted ml-auto">

                        {% if request.assignee %}
                        <span title="Assigned to {{ request.assignee.username}}" class="ml-3" data-toggle="tooltip">
                            <i class="fa fa-fw fa-user-plus pr-2"></i>
                            {% if filterstatus %}
                            <a href="{{ url_for('ui_ns.request_pulls',
                                repo=repo.name,
                                username=username,
                                namespace=repo.namespace,
                                status=filterstatus,
                                assignee=request.assignee.username) }}" class="notblue">
                            {% endif %}
                            {{ request.assignee.username | avatar(size=20) | safe}}
                            {% if filterstatus %}
                            </a>
                            {% endif %}
                        </span>
                        {% endif %}

                        <span title="branch to" class="text-muted ml-3" data-toggle="tooltip">
                            <span class="fa fa-fw fa-random pr-2"></span>
                              {{request.branch}}
                          </span>

                        <span title="{{request.user_comments|count}} comments" class="text-muted ml-3" data-toggle="tooltip">
                          <span class="fa fa-fw fa-comment pr-2"></span>
                            {{request.user_comments|count}}
                        </span>

                </span>
            </div>
                {% if subtitle %}
                <div class="d-flex">
                    {% if request.status|lower == 'merged'%}
                        <small>
                            <span class="text-info font-weight-bold">Merged</span>
                            <span data-toggle="tooltip" title="{{
                            request.closed_at | format_datetime
                          }}">{{ request.closed_at | humanize}}</span>.

                          Opened <span data-toggle="tooltip" title="{{
                            request.date_created | format_datetime
                          }}">{{ request.date_created | humanize}}</span> by <span title="{{
                          request.user.html_title }}">{{ request.user.user }}</span>.

                        </small>
                    {% elif request.status|lower == 'closed' %}
                        <small>
                            <span class="text-danger font-weight-bold">Cancelled</span>
                            <span data-toggle="tooltip" title="{{
                            request.closed_at | format_datetime
                          }}">{{ request.closed_at | humanize}}</span>.
                          Opened <span data-toggle="tooltip" title="{{
                            request.date_created | format_datetime
                          }}">{{ request.date_created | humanize}}</span> by <span title="{{
                          request.user.html_title }}">{{ request.user.user }}</span>.
                        </small>
                    {% else %}
                    <small>
                        <span class="text-success font-weight-bold">Opened</span>
                        <span data-toggle="tooltip" title="{{
                        request.date_created | format_datetime
                      }}">{{ request.date_created | humanize}}</span> by <span title="{{
                      request.user.html_title }}">{{ request.user.user }}</span>.
                      Modified <span data-toggle="tooltip" title="{{
                        request.updated_on | format_datetime
                      }}">{{ request.updated_on | humanize}}</span>
                      </small>
                    {% endif %}
                    {% if showproject %}
                      <small class="ml-auto mt-1">
                            {% if repo.is_fork -%}
                            <i class="fa fa-code-fork text-muted"></i>&nbsp;
                            {%- else -%}
                            <i class="fa {{projecticon()}} text-muted"></i> &nbsp;
                            {%- endif -%}
                          {% if repo.is_fork %}
                          <a href="{{ url_for(
                              'ui_ns.view_user', username=repo.user.user)
                              }}">{{ repo.user.user }}</a><span class="mt-1 text-muted"> / </span>
                          {% endif %}
                          {% if repo.namespace %}
                              <a href="{{ url_for(
                                  'ui_ns.view_projects', namespace=repo.namespace, pattern='*')
                              }}">{{ repo.namespace }}</a>/
                          {% endif %}
                          {% if repo.is_fork %}
                          <a href="{{ url_for('ui_ns.view_repo',
                              repo=repo.name,
                              username=repo.user.username,
                              namespace=repo.namespace)
                              }}">
                        {% else %}
                        <a href="{{ url_for('ui_ns.view_repo',
                        repo=repo.name,
                        username=None,
                        namespace=repo.namespace)
                        }}">
                        {% endif %}
                            <strong>{{ repo.name }}</strong></a>
                        </small>
                      {% endif %}
                </div>
                <div>
                  {% for tag in request.tags %}
                      <span id="tag-{{ tag.tag }}" title="{{ tag.tag_description }}"
                            style="background-color:{{ tag.tag_color}}; font-size:90%"
                            class="badge badge-secondary">
                          {{ tag.tag }}
                      </span>
                  {% endfor %}
                </div>

                {% if request.flags %}
                  <div class="d-flex prs_flag_list">
                    {% set flags_stats = request.flags_stats %}
                    {% if flags_stats | length == 1 %}
                      {% for status in flags_stats %}
                        <p>{{ flags_stats[status][0].username }}:
                          <span class="badge {{
                            flags_stats[status][0] | flag2label }} font-size-09">
                              {{ flags_stats[status][0].status }}
                          </span>
                        </p>
                      {% endfor %}
                    {% else %}
                      <p>Flags:
                      {% for status in flags_stats %}
                        <span class="badge {{
                          flags_stats[status][0] | flag2label }} font-size-09"
                          title="{% for f in flags_stats[status] %}{{
                          f.username}}{% if not loop.last %}, {% endif %}{% endfor %}">
                            {{ flags_stats[status] | length }} {{ status }}
                        </span>
                      </p>
                      {% endfor %}
                    {% endif %}
                  </div>
                {% endif %}

                </span>
                {% endif %}
            </div>
        </div>
</div><!--end request-row-->
{% endmacro%}
