{% extends "repo_master.html" %}

{% from "_render_repo.html" import pagination_link %}
{% from "_render_pullrequests.html" import render_pullrequest_row %}


{% block title %}Pull requests - {{
    repo.namespace + '/' if repo.namespace }}{{ repo.name }}{% endblock %}
{% set tag = "home" %}

{% block header %}
<link href="{{ url_for('static', filename='vendor/selectize/selectize.bootstrap3.css') }}"
  rel="stylesheet" />
{% endblock %}

{% block repo %}

<h3 class="font-weight-bold">
  <i class="fa fa-arrow-circle-down text-muted"></i>
  Pull Requests
  <div class="btn-group btn-group-sm float-right" role="group">
      <a class="btn btn-sm btn-outline-success font-weight-bold"
          href="{{ url_for('ui_ns.request_pulls',
          repo=repo.name,
          username=username,
          namespace=repo.namespace,
          status=True) }}">
        <span class="fa fa-fw fa-arrow-circle-down"></span> {{total_open}} Open
      </a>
      <a class="btn btn-sm btn-outline-info font-weight-bold"
          href="{{ url_for('ui_ns.request_pulls',
          repo=repo.name,
          username=username,
          namespace=repo.namespace,
          status=False) }}">
          <span class="fa fa-fw fa-arrow-circle-down"></span> {{total_merged}} Merged
      </a>
  </div>
</h3>
<div class="row mt-4">
  <div class="col">
    <div class="list-group">
        <div class="list-group-item bg-light">
          <div class="row">
            <div class="col">
              <div class="btn-group">
                <div class="dropdown">
                {% if status_filter == 'Open' %}
                  <a class="btn btn-sm btn-outline-success border-0 font-weight-bold dropdown-toggle" href="#" data-toggle="dropdown" id="issue-status-dropdown">
                      <span class="fa fa-fw fa-arrow-circle-down"></span> {{open_cnt}} Open PRs
                {% elif status_filter == 'Merged' %}
                    <a class="btn btn-sm btn-outline-info border-0 font-weight-bold dropdown-toggle" href="#" data-toggle="dropdown" id="issue-status-dropdown">
                        <span class="fa fa-fw fa-arrow-circle-down"></span> {{merged_cnt}} Merged PRs
                {% elif status_filter == 'Closed' %}
                    <a class="btn btn-sm btn-outline-danger border-0 font-weight-bold dropdown-toggle" href="#" data-toggle="dropdown" id="issue-status-dropdown">
                        <span class="fa fa-fw fa-arrow-circle-down"></span> {{closed_cnt}} Cancelled PRs
                {% else %}
                  <a class="btn btn-sm btn-outline-secondary border-0 font-weight-bold dropdown-toggle" href="#" data-toggle="dropdown" id="issue-status-dropdown">
                       <span class="fa fa-fw fa-arrow-circle-down"></span> {{open_cnt+closed_cnt+merged_cnt}} All PRs
                {% endif %}
                  </a>
                  <div class="dropdown-menu">
                    <a class="dropdown-item {% if status_filter == 'Open' %}active{%endif%}"
                          href="{{ url_for('ui_ns.request_pulls',
                          repo=repo.name,
                          username=username,
                          namespace=repo.namespace,
                          author=author,
                          search_pattern=search_pattern,
                          assignee=assignee) }}">
                          {{open_cnt}} Open PRs
                    </a>

                    <a class="dropdown-item {% if status_filter == 'Merged' %}active{%endif%}"
                        href="{{ url_for('ui_ns.request_pulls',
                          repo=repo.name,
                          username=username,
                          namespace=repo.namespace,
                          author=author, status='Merged',
                          search_pattern=search_pattern,
                          assignee=assignee) }}">
                          {{merged_cnt}} Merged PRs
                    </a>

                    <a class="dropdown-item {% if status_filter == 'Closed' %}active{%endif%}"
                        href="{{ url_for('ui_ns.request_pulls',
                          repo=repo.name,
                          username=username,
                          namespace=repo.namespace,
                          author=author, status='Closed',
                          search_pattern=search_pattern,
                          assignee=assignee) }}">
                          {{closed_cnt}} Cancelled PRs
                    </a>

                    <a class="dropdown-item {% if status_filter == None %}active{%endif%}"
                          href="{{ url_for('ui_ns.request_pulls',
                          repo=repo.name,
                          username=username,
                          namespace=repo.namespace,
                          author=author, status='all',
                          search_pattern=search_pattern,
                          assignee=assignee) }}">
                          {{open_cnt+closed_cnt+merged_cnt}} All PRs
                    </a>
                  </div>
                </div>
                <div class="btn-group">
                  <button class="btn btn-sm btn-outline-secondary border-0 dropdown-toggle" data-flip="false" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <span class="font-weight-bold">Filter{%if
                        author or
                        assignee or
                        search_pattern %}:{% endif %}
                    </span>
                    {% if search_pattern %}
                    <i class="fa fa-search pl-2 pr-1" title="search"></i>
                    <span>{{ search_pattern }}</span>
                    {% endif %}
                    {% if author %}
                    <i class="fa fa-user pl-2 pr-1" title="reported by"></i>
                      {{ author }}
                    {% endif %}
                    {% if assignee %}
                    <i class="fa fa-user-plus pl-2 pr-1" title="assigned to"></i>
                      {{ assignee }}
                    {% endif %}
                  </button>
                  <div class="dropdown-menu p-4" style="min-width:400px" aria-labelledby="dropdownMenuButton" id="filters-dropdown">
                    <div>
                      <form id="filters_form" action="{{ url_for('ui_ns.request_pulls',
                            username=username,
                            namespace=repo.namespace,
                            repo=repo.name ) }}" method="GET">
                        <input type="hidden" name="status" value="{{ status }}" />

                        <div class="form-group row mb-2">
                          <label for="search_pattern" class="col-auto align-self-center pl-1 pr-0"><i class="text-muted fa fa-fw fa-search"></i></label>
                          <div class="col pl-1">
                            <input type="text" class="form-control"
                                    name="search_pattern" placeholder="Search"
                                    value="{{ search_pattern or '' }}" />
                          </div>
                          <div class="col-auto pl-0 pr-1 pt-1">
                            <i class="fa fa-times fa-fw text-muted" id="search_pattern-selectize-reset"></i>
                          </div>
                        </div>

                        <div class="form-group row mb-1" id="assignee-filter-group">
                          <label for="assignee" class="col-auto align-self-center pl-1 pr-0"><i class="text-muted fa fa-fw fa-user-plus"></i></label>
                          <div class="col pl-1">
                            <select name="assignee" id="assignee-selectize" placeholder="Assignee">
                              {% if assignee %}
                              <option value="{{assignee}}" selected="selected">{{assignee}}</option>
                              {% endif %}
                            </select>
                          </div>
                          <div class="col-auto pl-0 pr-1 pt-1">
                            <i class="fa fa-times fa-fw text-muted" id="assignee-selectize-reset"></i>
                          </div>
                        </div>

                        <div class="form-group row mb-1" id="author-filter-group">
                          <label for="author" class="col-auto align-self-center pl-1 pr-0"><i class="text-muted fa fa-fw fa-user"></i></label>
                          <div class="col pl-1">
                            <select name="author" id="author-selectize" placeholder="Submitted by">
                              {% if author %}
                              <option value="{{author}}" selected="selected">{{author}}</option>
                              {% endif %}
                            </select>
                          </div>
                          <div class="col-auto pl-0 pr-1 pt-1">
                            <i class="fa fa-times fa-fw text-muted" id="author-selectize-reset"></i>
                          </div>
                        </div>

                        <input type="submit" class="btn btn-block btn-primary" value="Apply Filters" />

                        <a href="{{ url_for('ui_ns.request_pulls',
                        repo=repo.name,
                        username=username,
                        namespace=repo.namespace,
                        status=status) }}" class="btn btn-link btn-block">Reset Filters</a>

                      </form>
                    </div>
                  </div>
                </div>
              </div>

              <div class="btn-group float-right">

                <div class="btn-group">
                {% set filters_list = [
                    {"key": "date_created", "display_string": "Open Date", "sort_icon_prefix": "fa-sort-numeric-", "icon":"fa-calendar"},
                    {"key": "last_updated", "display_string": "Last Modified Date", "sort_icon_prefix": "fa-sort-numeric-", "icon":"fa-calendar"},
                ] %}
                  <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" id="dropdownMenuButton" data-flip="false" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <i class="fa fa-fw fa-sort"></i> Sort
                  </button>
                  <div class="dropdown-menu dropdown-menu-right" style="min-width:300px" aria-labelledby="dropdownMenuButton">
                    <div class="list-group list-group-flush">
                    {% for filter in filters_list %}
                      <div class="list-group-item pl-1">
                        <div class="btn btn-outline-dark border-0 font-weight-bold disabled">
                          <span class="fa fa-fw {{filter['icon']}}"></span> {{filter["display_string"]}}
                        </div>
                        <div class="btn-group float-right">
                              <a class="btn {%if filter['key'] == order_key and order == 'asc' %}btn-primary{%else%}btn-outline-primary{%endif%}"
                              href="{{ url_for('ui_ns.request_pulls',
                              repo=repo.name, username=username, namespace=repo.namespace,
                              author=author, assignee=assignee, search_pattern=search_pattern,
                              status=status, order_key=filter['key'],
                              order='asc' )}}"><i class="fa fa-fw {{filter['sort_icon_prefix']}}asc"></i></a>
                              <a class="btn {%if filter['key'] == order_key and order == 'desc' %}btn-primary{%else%}btn-outline-primary{%endif%}"
                              href="{{ url_for('ui_ns.request_pulls',
                              repo=repo.name, username=username, namespace=repo.namespace,
                              author=author, assignee=assignee, search_pattern=search_pattern,
                              status=status, order_key=filter['key'],
                              order='desc' )}}"><i class="fa fa-fw {{filter['sort_icon_prefix']}}desc"></i></a>
                        </div>
                      </div>
                    {% endfor %}
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        {% if requests %}
          {% for request in requests %}
            {{render_pullrequest_row(request, repo, username, filterstatus=status)}}
          {% endfor %}
        {% else %}
        <div class="list-group-item">
            <div class="row">
                <div class="col text-center p-5">
                    <h4 class="text-muted">
                      {% if status_filter == 'Open'%}
                        <span class="font-weight-bold">no open pull requests found</span>
                        {% if merged_cnt %}
                        <div>
                          <small>
                            <a href="{{ url_for('ui_ns.request_pulls',
                                      repo=repo.name,
                                      username=username,
                                      namespace=repo.namespace,
                                      author=author,
                                      search_pattern=search_pattern,
                                      status='Merged',
                                      assignee=assignee) }}">
                              {{merged_cnt}} merged pull requests
                            </a>
                            match this filter
                          </small>
                        </div>
                        {% else %}
                        <div><small>no merged pull requests match this filter either</small></div>
                        {% endif %}
                      {% elif status_filter == 'Merged'%}
                        <span class="font-weight-bold">no merged pull requests found</span>
                        {% if open_cnt %}
                        <div>
                          <small>
                            <a href="{{ url_for('ui_ns.request_pulls',
                                      repo=repo.name,
                                      username=username,
                                      namespace=repo.namespace,
                                      author=author,
                                      search_pattern=search_pattern,
                                      assignee=assignee) }}">
                              {{open_cnt}} open pull requests
                            </a>
                            match this filter
                          </small>
                        </div>
                        {% else %}
                        <div><small>no open pull requests match this filter either</small></div>
                        {% endif %}
                      {% else %}
                        no pull requests found
                      {% endif %}
                    </h4>
                </div>
            </div>
        </div>
        {% endif %}
    </div>
  </div>
</div>
{% if total_page > 1 %}
{{ pagination_link('page', g.page, total_page) }}
{% endif %}
{% endblock %}

{% block jscripts %}
{{ super() }}

<script type="text/javascript"
  src="{{ url_for('static', filename='vendor/selectize/selectize.min.js') }}?version={{ g.version}}"></script>
<script type="text/javascript">
$(document).ready(function() {

    $("#search_pattern-selectize-reset").on('click', function(e){
      $('input[name="search_pattern"]').val('');
    });

    var $assignee_selectize = $('#assignee-selectize').selectize({
      valueField: 'user',
      labelField: 'user',
      searchField: 'user',
      maxItems: 1,
      create: false,
      onInitialize: function(){
      $("#assignee-filter-group .selectize-control").on('click', function(event){
        event.stopPropagation();
      })
      $("#filters-dropdown").on('click', function(event){
        event.stopPropagation();
      })
      },
      load: function(query, callback) {
        if (!query.length) return callback();
        $.getJSON(
          "{{ url_for('api_ns.api_users') }}", {
            pattern: "*"+query+"*"
          },
          function( data ) {
            callback( data.users.map(function(x) { return { user: x }; }) );
          }
        );
      }
    });

    var assignee_selectize_control = $assignee_selectize[0].selectize;

    $("#assignee-selectize-reset").on('click', function(e){
      assignee_selectize_control.clear();
    });

var $author_selectize = $('#author-selectize').selectize({
  valueField: 'user',
  labelField: 'user',
  searchField: 'user',
  maxItems: 1,
  create: false,
  onInitialize: function(){
      $("#author-filter-group .selectize-control").on('click', function(event){
        event.stopPropagation();
      })
      $("#filters-dropdown").on('click', function(event){
        event.stopPropagation();
      })
  },
  load: function(query, callback) {
    if (!query.length) return callback();
    $.getJSON(
      "{{ url_for('api_ns.api_users') }}", {
        pattern: "*"+query+"*"
      },
      function( data ) {
        callback( data.users.map(function(x) { return { user: x }; }) );
      }
    );
  }
});

    var author_selectize_control = $author_selectize[0].selectize;

$("#author-selectize-reset").on('click', function(e){
  author_selectize_control.clear();
});

  $( "#filters_form" ).submit(function( event ) {

   if ($('input[name="search_pattern"]').val() == ''){
    $('input[name="search_pattern"]').prop("disabled", true);
   }

  if ($('select[name="assignee"]').val() == ''){
    $('select[name="assignee"]').prop("disabled", true);
   }

  if ($('select[name="author"]').val() == ''){
    $('select[name="author"]').prop("disabled", true);
   }

  });

});
</script>
{% endblock %}

