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

{% macro render_issue_row(issue, repo, username, subtitle=True, class="", showproject=False, filterstatus=None, filtermilestone=None) %}
    {% if issue.status == 'Open' %}
        {% set status_color = "success" %}
    {% else %}
        {% set status_color = "danger" %}
    {% endif %}

<div class="issuerow list-group-item list-group-item-action {{class}}">
        <div class="d-flex align-items-start">
            <span class="fa fa-fw text-{{status_color}} fa-exclamation-circle pt-1"></span>
            <div class="w-100">
            <div class="ml-1 d-flex">
                <div>
                    <span class="text-{{status_color}} font-weight-bold">#{{issue.id}}</span>
                    <span class="font-weight-bold">
                        <a class="notblue" href="{{ url_for('ui_ns.view_issue',
                        repo=repo.name,
                        username=repo.username if repo.is_fork else None,
                        namespace=repo.namespace,
                        issueid=issue.id) }}">
                        {{issue.title}}
                        </a>
                    </span>
                    {% if issue.private %}
                    <span title="Private ticket" class="text-danger fa fa-fw fa-lock"></span>
                    {% endif %}
                </div>
                <!--<div class="ml-auto nowrap mr-3">-->
                    <span class="row font-weight-bold text-muted ml-auto nowrap" style="width:150px">

                    <div class="col-6 px-0 text-right">
                        {% if issue.assignee %}
                        <span title="Assigned to {{ issue.assignee.username}}" data-toggle="tooltip">
                            <i class="fa fa-fw fa-user-plus pr-2"></i>
                            {% if filterstatus %}
                            <a href="{{ url_for('ui_ns.view_issues',
                            repo=repo.name,
                            username=username,
                            namespace=repo.namespace,
                            status=filterstatus,
                            milestone=filtermilestone,
                            assignee=issue.assignee.username) }}" class="notblue">
                            {% endif %}
                            {{ issue.assignee.username | avatar(size=20) | safe}}
                            {% if filterstatus %}
                            </a>
                            {% endif %}
                        </span>
                        {% endif %}
                    </div>

                    <div class="col-6">
                        {% if issue.user_comments|count > 0 %}
                        <span title="{{issue.user_comments|count}} comments" class="text-muted ml-2" data-toggle="tooltip">
                        <span class="fa fa-fw fa-comment pr-2"></span>
                            {{issue.user_comments|count}}
                        </span>
                        {% endif %}
                    </div>
                    </span>
                <!--</div>-->
            </div>
                {% if subtitle %}
                <div class="d-flex">
                    <small>Opened <a href="#" data-toggle="tooltip" title="{{
                        issue.date_created | format_datetime
                      }}">{{ issue.date_created | humanize}}</a> by <span title="{{
                      issue.user.html_title }}">{{ issue.user.user }}</span>.
                      Modified <span data-toggle="tooltip" title="{{
                        issue.last_updated | format_datetime
                      }}">{{ issue.last_updated | humanize}}</span>
                      </small>
                      {% 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 issue.tags %}
                            {% if filterstatus %}
                            <a id="tag-{{ tag.tag }}" title="{{ tag.tag_description }}"
                                  style="background-color:{{ tag.tag_color}}; font-size:90%"
                                  class="badge badge-secondary"
                                  href="{{ url_for('ui_ns.view_issues',
                                      repo=repo.name,
                                      username=repo.username if repo.is_fork else None,
                                      namespace=repo.namespace,
                                      tags=tag.tag,
                                      milestone=filtermilestone,
                                      status=filterstatus) }}">
                                {{ tag.tag }}
                            </a>
                            {% else %}
                            <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>
                            {% endif %}
                        {% endfor %}
                        {% if issue.milestone %}
                        <span title="milestone {{issue.milestone}}" class="badge font-weight-bold text-muted font-size-09" data-toggle="tooltip">
                            <i class="fa fa-map-signs"></i>
                            {% if filterstatus %}
                            <a href="{{ url_for('ui_ns.view_issues',
                            repo=repo.name,
                            username=username,
                            namespace=repo.namespace,
                            status=filterstatus,
                            milestone=issue.milestone) }}" class="notblue">
                            {% endif %}
                            {{ issue.milestone }}
                            {% if filterstatus %}
                            </a>
                            {% endif %}
                        </span>
                        {% endif %}
                        {% if issue.priority is not none  %}
                        <span title="Priority" class="badge font-weight-bold text-muted font-size-09" data-toggle="tooltip">
                            <i class="fa fa-bolt"></i>
                            {% if filterstatus %}
                            <a href="{{ url_for('ui_ns.view_issues',
                            repo=repo.name,
                            username=username,
                            namespace=repo.namespace,
                            status=filterstatus,
                            milestone=filtermilestone,
                            priority=issue.priority) }}" class="notblue">
                            {% endif %}
                            {{ repo.priorities[issue.priority | string] }}
                            {% if filterstatus %}
                            </a>
                            {% endif %}
                        </span>
                        {% endif %}
                        {% if issue.related_prs | length == 1  %}
                          <span title="Related to PR#{{ issue.related_prs[0].id }}" class="badge font-weight-bold text-muted font-size-09" data-toggle="tooltip">
                            <i class="fa fa-link"></i>
                            <a href="{{ url_for('ui_ns.request_pull',
                            repo=issue.related_prs[0].project.name,
                            username=issue.related_prs[0].project.user.user if issue.related_prs[0].project.is_fork else None,
                            namespace=issue.related_prs[0].project.namespace,
                            requestid=issue.related_prs[0].id,
                            ) }}" class="notblue">PR#{{ issue.related_prs[0].id }}</a>
                          </span>
                        {% elif issue.related_prs | length > 1  %}
                          <span title="Related to {{issue.related_prs | length }} PRs" class="badge font-weight-bold text-muted font-size-09" data-toggle="tooltip">
                            <i class="fa fa-link"></i>
                          </span>
                        {% endif %}

                </div>
                {% endif %}
                </div>
        </div>
    </div><!-- end issuerow -->
{% endmacro%}
