<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>

  <div class="col-xs-2 line-height-1"></div>
  <div id="orphan-section" class="pt-3">
      <div class="col-xs-2 line-height-1"></div>
      {% if repo.user.user == "orphan" %}
      <p>Orphaned for: {{ repo.orphan_reason.reason }}
      {% if repo.orphan_reason.reason_info %}
      -- {{ repo.orphan_reason.reason_info }}
      {% endif %}
      </p>

      <a href="#" title="Adopt this orphaned package"
         {% if g.authenticated %}
         class="btn btn-sm btn-outline-primary"
         {% else %}
         class="btn btn-sm btn-outline-primary disabled"
         {% endif %}
         id="take_orphan_button">
           <i id="take_orphan_icon" class="fa fa-fw fa-hand-paper-o"></i>
           Take
      </a>
      {% else %}

      {% if g.authenticated %}
        <div class="btn-group">
          <a href="#" title="Orphan this package"
             class="btn btn-sm btn-outline-primary"
             data-toggle="modal" data-target="#modal_orphan">
            <i id="orphan_icon-icon" class="fa fa-fw fa-hand-paper-o"></i>
            Orphan
          </a>
        </div>
      {% endif %}
    {% endif %}
  </div>

  <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 g.repo_admin) %}
        <div class="btn-group">
          <a href="#" title="Update the bugzilla 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>
            Edit
          </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="button" 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>

  <div class="modal fade" id="modal_orphan" tabindex="-1"
          role="dialog" aria-labelledby="Orphan this package" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">Orphan package</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>
          <div class="modal-body">
            <div class="form-group">
              <label for="reason_menu">Reason</label>
              <select id="reason_menu" class="form-control">
              {% for id, text in [('lack_of_time_option_button', 'Lack of time'),
                                 ('do_not_use_it_option_button', 'Do not use it anymore'),
                                 ('unmaintained_option_button', 'Unmaintained upstream'),
                                 ('fails_to_build_option_button', 'Fails to build from source'),
                                 ('not_fixed_option_button', 'Important bug not fixed'),
                                 ('other_option_button', 'Other')] %}

                <option id="{{ id }}">{{ text }}</option>
                {% endfor %}
              </select>
            </div>
          <label for="orphan_reason_info">Additional info</label>
          <input title="Additional info for orphaning reason"
                  class="form-control" name="orphan_reason_info" id="orphan_reason_info" value=""/>
          </div>
          <div class="modal-footer">
            <button class="btn btn-primary" type="submit" title="Orphan this package" id="orphan_button">
               Update
            </button>
          </div>
      </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) {
            $("#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');
            console.log("Successfully changed the bugzilla assignees");
            return false;
          },
          error: function(res) {
            var msg = '';
            if(res.responseJSON.errors){
              msg = ': ' + res.responseJSON.errors.join(', ');
            }
            alert("Unable to update the bugzilla assignee(s)" + msg);
            $('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 %}
      $("#take_orphan_button").click(function(){
        $("#take_orphan_button").attr("disabled", true);

        $.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_div").html(`
              Maintained by
              <a href="{{ url_for('ui_ns.view_user', username=repo.user.user)}}"
              id="point_of_contact">
              {{- g.fas_user.username -}}
              </a>
            `);
            $("#orphan-section").html('');
            $("#orphan_button").attr("class", "btn btn-sm btn-outline-primary");
            $("#take_orphan_button").attr("class", "btn btn-sm btn-outline-primary d-none");
            $("#take_orphan_button").attr("disabled", false);
          },
          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);
            }
            $("#take_orphan_button").attr("disabled", false);
          }
        });
      });
      {% endif %}

      $.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}}"
                    + "&amp;template=package_unretiremet");
            });
            _btn.prop( "title", "Package retired - Open a releng ticket to adopt it" );
            _btn.html("Retired");
          }
          _btn.removeClass('disabled');
        }
      });

      $("#orphan_button").click(function(){
        $("#orphan_button").attr("disabled", true);
        $.ajax({
          url: "{{ url_for('distgit_ns.orphan_endpoint', repo=repo.name, namespace=repo.namespace) }}",
          type: 'POST',
          dataType: 'json',
          data: {
            'orphan_reason': $('#reason_menu').val(),
            'orphan_reason_info': $('#orphan_reason_info').val()
          },
          success: function(res) {
            $("#point_of_contact_div").html("Package is currently unmaintained");
            $("#orphan_button").attr("disabled", false);
            $('#modal_orphan').modal('hide');
            $('#orphan-section').html('');
          },
          error: function(res) {
            if (res.responseJSON.errors) {
              alert('Unable to orphan the package: ' + res.responseJSON.errors);
            } else {
              alert('Unable to orphan the package: ' + res.responseJSON.error);
            }
            $("#orphan_button").attr("disabled", false);
          }
        });
      });
    });
  </script>
  {% endif %}

</nav>
