<nav class="nav nav-tabs nav-sidetabs flex-column">
  <a class=
      "nav-link nowrap
      {%if select == 'overview' %} active{% endif %}"
      href="{{ url_for(
        'ui_ns.view_repo',
        repo=repo.name,
        username=username,
        namespace=repo.namespace) }}">
      <i class="fa fa-home text-muted fa-fw"></i>&nbsp;<span class="d-none d-md-inline">Overview</span>
  </a>
  <a class=
    "nav-link nowrap
    {%if g.repo_obj and g.repo_obj.is_empty %} disabled{% endif %}
    {%if select == 'tree' %} active{% endif %}"
    href="{{ url_for(
    'ui_ns.view_tree',
    repo=repo.name,
    username=username,
    namespace=repo.namespace,
    identifier=branchname) }}">
    <i class="fa fa-file-code-o text-muted fa-fw"></i>&nbsp;Files
  </a>
  <a class=
    "nav-link nowrap
    {%if g.repo_obj and g.repo_obj.is_empty %} disabled{% endif %}
    {%if select == 'commits' %} active{% endif %}"
    href="{{ url_for(
    'ui_ns.view_commits',
    repo=repo.name,
    username=username,
    namespace=repo.namespace,
    branchname=branchname) }}">
    <i class="fa fa-list-alt text-muted fa-fw" data-glyph="spreadsheet"></i>&nbsp;Commits
  </a>
  <a class=
    "nav-link nowrap
    {%if select == 'branches' %} active{% endif %}"
    href="{{ url_for(
    'ui_ns.view_branches',
    repo=repo.name,
    username=username,
    namespace=repo.namespace,
    branchname=branchname) }}">
    <i class="fa fa-random text-muted fa-fw"></i>&nbsp;Branches
  </a>
  <a class=
    "nav-link nowrap
    {%if select == 'forks' %} active{% endif %}"
    href="{{ url_for(
    'ui_ns.view_forks',
    repo=repo.name,
    username=username,
    namespace=repo.namespace) }}">
    <i class="fa fa-code-fork text-muted fa-fw"></i>&nbsp;Forks
  </a>
  <a class=
    "nav-link nowrap
    {%if select == 'tags' %} active{% endif %}"
    href="{{ url_for(
    'ui_ns.view_tags',
    repo=repo.name,
    username=username,
    namespace=repo.namespace) }}">
    <i class="fa fa-tags text-muted fa-fw"></i>&nbsp;Releases
  </a>

  {% if 'distgit_ns' in g.main_app.blueprints and not repo.is_fork and repo.namespace != 'tests'%}
  <div class="col-xs-2 line-height-1"></div>
  <h6>Monitoring status:</h6>
  <div class="btn-group">
    {% if g.authenticated %}
    <a href="#" title="Monitoring status"
        class="btn btn-sm dropdown-toggle btn-outline-primary"
        data-toggle="dropdown" id="monitoring-button">
      <i id="monitoring-icon" class="fa fa-fw fa-eye"></i>
      <span id="monitoring-label" class="fa fa-circle-o-notch fa-spin fa-1x fa-fw"></span>
    </a>
    <div class="dropdown-menu dropdown-menu-right monitoring-menu">
      <a class="dropdown-item pl-2" id="no_monitoring_option_button">
        <div class="media">
          <div class="align-self-center check-icon pr-2">
            <span class="fa fa-fw"></span>
          </div>
          <div class="media-body">
              No-Monitoring
          </div>
        </div>
      </a>
      <a class="dropdown-item pl-2" id="monitoring_option_button">
        <div class="media">
            <div class="align-self-center check-icon pr-2">
              <span class="fa fa-fw"></span>
            </div>
            <div class="media-body">
              Monitoring
            </div>
          </div>
      </a>
      <a class="dropdown-item pl-2" id="monitoring_and_scratch_option_button">
        <div class="media">
          <div class="align-self-center check-icon pr-2">
            <span class="fa fa-fw"></span>
          </div>
          <div class="media-body">
            Monitoring and scratch builds
          </div>
        </div>
      </a>
    </div>
    <div id="monitoring_feedback"></div>
    {% else %}
    <button title="Monitoring status" class="btn btn-sm btn-outline-primary disabled"
        id="monitoring-button">
      <i id="monitoring-icon" class="fa fa-fw fa-eye"></i>
      <span id="monitoring-label" class="fa fa-circle-o-notch fa-spin fa-1x fa-fw"></span>
    </button>
    {% endif %}
  </div>

  {% if g.authenticated and repo.user.user == "orphan" %}
  <div id="take-orphan-section" class="pt-3">
    <div class="col-xs-2 line-height-1"></div>
    <h6>Orphaned:</h6>
    <div class="btn-group">
      <a href="#" title="Adopt this orphaned package"
          class="btn btn-sm btn-outline-primary disabled"
          id="take-orphan-button">
        <i id="take-orphan-icon" class="fa fa-fw fa-hand-paper-o"></i>
        Take
      </a>
    </div>
  </div>
  {% elif not g.authenticated and repo.user.user == "orphan" %}
    <div id="take-orphan-section">
    <div class="col-xs-2 line-height-1"></div>
    <h6>Orphaned:</h6>
    <div class="btn-group">
      <a href="#" title="Adopt this orphaned package"
          class="btn btn-sm btn-outline-primary disabled"
          id="take-orphan-button">
        <i id="take-orphan-icon" class="fa fa-fw fa-hand-paper-o"></i>
        Take
      </a>
    </div>
  </div>
  {% endif %}

  <div class="pt-3">
    <div class="col-xs-2 line-height-1">
    <h6>Bugzilla Assignee:</h6>
      <dl>
        <dt>Fedora: </dt>
        <dd id="fedora_assignee_txt">
        {{ repo.bzoverride.fedora_assignee if repo.bzoverride.fedora_assignee else repo.user.user }}
        </dd>
        {% if repo.namespace == 'rpms' %}
        <dt>EPEL: </dt>
        <dd id="epel_assignee_txt">
        {{ repo.bzoverride.epel_assignee if repo.bzoverride.epel_assignee else repo.user.user }}
        </dd>
        {% endif %}
      </dl>
      {% if g.authenticated and repo.user.user == g.fas_user.username or pagure_admin %}
        <div class="btn-group">
          <a href="#" title="Update the bugzill assignee(s)"
              class="btn btn-sm btn-outline-primary"
              data-toggle="modal" data-target="#modal_assignee">
            <i id="update-bz-icon" class="fa fa-fw fa-refresh"></i>
            Update
          </a>
        </div>
      {% endif %}
    </div>
  </div>

  <div class="modal fade" id="modal_assignee" tabindex="-1"
          role="dialog" aria-labelledby="Bugzilla assignee" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">Bugzilla Assignee</h4>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
          </button>
        </div>
        <form id="change_assignees">
          <div class="modal-body">
            <label for="fedora_assignee">Fedora</label>
            <input title="Default assignee for Fedora in bugzilla - Empty input resets to default"
              class="form-control" name="fedora_assignee" id="fedora_assignee" value="{{
                repo.bzoverride.fedora_assignee if repo.bzoverride.fedora_assignee else repo.user.user }}"/>
            {% if repo.namespace == 'rpms' %}
            <label for="epel_assignee">EPEL</label>
            <input title="Default assignee for EPEL in bugzilla (if applicable) - Empty input resets to default"
              class="form-control" name="epel_assignee" id="epel_assignee" value="{{
                repo.bzoverride.epel_assignee if repo.bzoverride.epel_assignee else repo.user.user }}" />
            {% endif %}
            <p class="pt-2">
              These two fields allow to specify a different default assignee for ticket opened against
              this package in bugzilla. {% if repo.namespace == 'rpms' %}Note: The EPEL field is
              always displayed for packages in the 'rpms' namespace regardless of whether it
              is used in bugzilla or not.{% endif %}
            </p>
          </div>
          <div class="modal-footer">
            <button class="btn btn-secondary" type="submit" title="Update bugzilla overrides" id="reset_assignees">
               Reset to defaults
            </button>
            <button class="btn btn-primary" type="submit" title="Update bugzilla overrides" id="update_assignees">
               Update
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>

  <script type="text/javascript" nonce="{{ g.nonce }}">
    window.addEventListener('load', function() {
      set_up_monitoring = function(status){
        var _label = "Disabled"
        if (status === "monitoring") {
          _label = "Monitoring";
          $("#monitoring-icon").attr("class", "fa fa-fw fa-eye")
        } else if (status === "monitoring-with-scratch") {
          _label = "Scratch builds"
          $("#monitoring-icon").attr("class", "fa fa-fw fa-eye")
        } else {
          $("#monitoring-icon").attr("class", "fa fa-fw fa-eye-slash")
        }

        $("#monitoring-label").text(_label);
        $("#monitoring-label").removeClass("fa fa-circle-o-notch fa-spin fa-1x fa-fw");
      }

      $.ajax({
        url: "{{ url_for('distgit_ns.anitya_patch_endpoint', repo=repo.name, namespace=repo.namespace) }}",
        type: 'GET',
        dataType: 'json',
        success: function(res) {
          console.log(res);
          set_up_monitoring(res.monitoring)
        }
      });

      {% if g.authenticated %}
      $(".monitoring-menu a").click(function(){
        $("#monitoring_feedback").attr('class', '');
        var selectedValue = $(this).attr('id');
        var _status = "no-monitoring";
        if (selectedValue === "monitoring_option_button") {
            _status = "monitoring";
        } else if (selectedValue === "monitoring_and_scratch_option_button") {
            _status = "monitoring-with-scratch"
        }

        $.ajax({
          url: "{{ url_for('distgit_ns.anitya_patch_endpoint', repo=repo.name, namespace=repo.namespace) }}",
          type: 'POST',
          data: {
            anitya_status: _status,
          },
          dataType: 'json',
          success: function(res) {
            set_up_monitoring(res.monitoring)
            $("#monitoring_feedback").attr("class", "fa fa-fw fa-check")
          },
          error: function() {
            alert('Unable to change the monitoring status!');
          }
        });
      });
      {% endif %}

      $("#reset_assignees").on('click', function(){
        $('#fedora_assignee').val('');
        $('#epel_assignee').val('');
        $("#change_assignees").submit();
        return false;
      });

      $("#change_assignees").on('submit',  function(){
        $('html').css('cursor', 'progress');
        $('#reset_assignees').attr('disabled', true);
        $('#update_assignees').attr('disabled', true);
        $('#update_assignees').text('Updating...');
        $.ajax({
          url: "{{ url_for('distgit_ns.bzoverride_patch_endpoint', repo=repo.name, namespace=repo.namespace) }}",
          type: 'POST',
          dataType: 'json',
          data: {
             'epel_assignee': $('#epel_assignee').val(),
             'fedora_assignee': $('#fedora_assignee').val()
          },
          success: function(res) {
            console.log("Successfully changed the bugzilla assignees");
            $("#fedora_assignee_txt").text(res.fedora_assignee);
            $("#epel_assignee_txt").text(res.epel_assignee);
            $('#modal_assignee').modal('hide');
            $('#reset_assignees').attr('disabled', false);
            $('#update_assignees').attr('disabled', false);
            $('#update_assignees').text('Update');
            $('html').css('cursor', 'default');
            return false;
          },
          error: function(res) {
            alert("Unable to update the bugzilla assignee(s)");
            $('html').css('cursor', 'default');
            $('#reset_assignees').attr('disabled', false);
            $('#update_assignees').attr('disabled', false);
            $('#update_assignees').text('Update');
            return false;
          }
        })
        return false;
      });

      {% if g.authenticated and repo.user.user == "orphan" %}
      $("#take-orphan-button").click(function(){

        $.ajax({
          url: "{{ url_for('distgit_ns.take_orphan_endpoint', repo=repo.name, namespace=repo.namespace) }}",
          type: 'POST',
          dataType: 'json',
          success: function(res) {
            $("#point_of_contact").html("{{ g.fas_user.username }}");
            $("#take-orphan-section").hide();
          },
          error: function(res) {
            if (res.responseJSON.errors) {
              alert('Unable to change the point of contact: ' + res.responseJSON.errors);
            } else {
              alert('Unable to change the point of contact: ' + res.responseJSON.error);
            }
          }
        });
      });
      {% endif %}
      {% if repo.user.user == "orphan" %}
      $.ajax({
        url: "{{ url_for('distgit_ns.get_actived_status', repo=repo.name, namespace=repo.namespace) }}",
        type: 'GET',
        dataType: 'json',
        success: function(res) {
          var _btn = $("#take-orphan-button");
          if (!res.active){
            _btn.off("click");
            _btn.click(function(){
                window.open(
                    "https://pagure.io/releng/new_issue?title="
                    + "Unretire {{repo.namespace}}/{{repo.name}}");
            });
            _btn.prop( "title", "Package retired - Open a releng ticket to adopt it" );
            _btn.html("Retired");
          } else {
            _btn.removeClass('disabled');
          }
        }
      });
      {% endif %}

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

</nav>
