{% extends "userdash_master.html" %}

{% block title %}Home{% endblock %}
{% set tag = "projects" %}
{% from "_browseheader.html" import browse_header %}
{% from "_render_repo.html" import pagination_link %}


{% block header %}
  <link rel="stylesheet" type="text/css" media="screen"
      href="{{ url_for('static', filename='toggle.css') }}?version={{ g.version}}"/>
{% endblock %}


{% from "_render_repo.html" import render_repos_as_card, render_activity_graph %}

{% block userdash %}
<div class="pt-4">
  <div class="container">
    <div class="row">
      <div class="col-md-8">

          {{ render_activity_graph(username) }}

        <div class="card mb-2">
          <div class="card-header">
            My Projects <span class="badge badge-secondary">{{ repos_length }}</span>
            {% if config.get('ENABLE_NEW_PROJECTS', True) and
                  config.get('ENABLE_UI_NEW_PROJECTS', True) and repos %}
            <span class="float-right">
              <a class="btn btn-secondary btn-sm" href="{{
                    url_for('ui_ns.new_project') }}">
                <span class="fa fa-plus" data-toggle="tooltip"
                    title="Create New Project" aria-hidden="true">
                </span>
              </a>
            </span>
          {% endif %}
          </div>
          {% if repos %}
            <div class="list-group list-group-flush">
            {% for repo in repos %}
              <div class="list-group-item">
                {% set url = url_for(
                    'ui_ns.view_repo',
                    repo=repo.name,
                    username=repo.user.username if repo.is_fork else None,
                    namespace=repo.namespace) %}
                <a href="{{ url }}">
                  {% if repo.avatar_email %}
                    <img class="projecticon pull-xs-left" src="{{ repo.avatar_email | avatar_url }}" width=20 height=20 />&nbsp;
                  {% else %}
                    <span class="oi text-muted" data-glyph="document"></span>
                  {% endif %}
                  <strong>{{ repo.namespace + '/' if repo.namespace }}{{ repo.name }}</strong>
                </a>
                {% if repo.private %}
                  <span title="Private project" class="text-danger fa fa-fw fa-lock"></span>
                {% endif %}
                  <div class="float-right">
                    {% if config.get('ENABLE_TICKETS', True) and repo.settings.get('issue_tracker', True) %}
                      {% if repo.open_tickets_public == 0 %}
                        <span class="text-muted" data-toggle="tooltip"
                          title="{{ repo.open_tickets_public }} open issues against {{repo.name}}">
                          <span class="fa fa-exclamation-circle"></span>
                          {{- repo.open_tickets_public }}
                        </span>
                      {% else %}
                        <a class="notblue" data-toggle="tooltip"
                          href="{{ url_for('ui_ns.view_issues', repo=repo.name, namespace=repo.namespace) }}"
                          title="{{ repo.open_tickets_public }} open issues against {{repo.name}}">
                          <span class="fa fa-exclamation-circle"></span>
                          {{- repo.open_tickets_public }}
                        </a>
                      {% endif %}
                    {% endif %}

                    {% if repo.open_requests == 0 %}
                      <span class="text-muted" data-toggle="tooltip"
                        title="{{repo.open_requests}} open pull requests in {{repo.name}}">
                        <span class="fa fa-arrow-circle-down"></span>
                        {{- repo.open_requests}}
                      </span>
                    {% else %}
                      <a class="notblue" data-toggle="tooltip"
                        href="{{ url_for('ui_ns.request_pulls',
                        repo=repo.name,
                        username=repo.user.username if repo.is_fork else None,
                        namespace=repo.namespace) }}"
                        title="{{repo.open_requests}} open pull requests in {{repo.name}}">
                        <span class="fa fa-arrow-circle-down"></span>
                        {{- repo.open_requests}}
                      </a>
                    {% endif %}

                  </div>
              </div>
            {% endfor %}
            {% if repos_length and repos_length > 1 %}
            {{ pagination_link('repopage', repopage, total_repo_page, forkpage=forkpage) }}
            {% endif %}
            </div>
        {% else %}
          <div class="card-body">
            <div class="text-center">You have no projects</div>
            <div class="text-center mt-1">
              {% if config.get('ENABLE_NEW_PROJECTS', True) and
                  config.get('ENABLE_UI_NEW_PROJECTS', True) %}
              <a class="btn btn-success" href="{{ url_for('ui_ns.new_project') }}">
                <span class="fa fa-plus" data-toggle="tooltip"
                    title="Create New Project" aria-hidden="true">
                </span> Create a Project
              </a>
              {% endif %}
            </div>
          </div>
        {% endif %}
        </div>

        <div class="card mb-2">
          <div class="card-header">
            My Forks <span class="badge badge-secondary">{{ forks_length }}</span>
          </div>
          {% if forks %}
            <div class="list-group list-group-flush">
            {% for fork in forks %}
              <div class="list-group-item">
                {% set url = url_for(
                    'ui_ns.view_repo',
                    repo=fork.name,
                    username=fork.user.username if fork.is_fork else None,
                    namespace=fork.namespace) %}

                <a href="{{ url }}">
                  <i class="fa fa-code-fork text-muted"></i>
                  <strong>{{fork.user.username}}/{{ fork.namespace + '/' if fork.namespace }}{{ fork.name }}</strong>
                </a>
                  <div class="float-right">
                  {% if fork.parent %}
                  <span class="text-muted">forked from</span>
                  <a href="{{ url_for(
                    'ui_ns.view_repo', repo=fork.parent.name,
                    namespace=fork.parent.namespace) }}">
                    {{ fork.parent.namespace + '/' if fork.parent.namespace
                    }}{{fork.parent.name}}
                  </a>
                  {% else %}
                  <span class="text-muted">forked from a deleted repository</span>
                  {% endif %}
                  {% if fork.settings.get('issue_tracker', True) %}
                    {% if fork.open_tickets_public == 0 %}
                      <span class="text-muted" data-toggle="tooltip"
                        title="{{ fork.open_tickets_public }} open issues against {{fork.name}}">
                        <span class="fa fa-exclamation-circle"></span>
                        {{- fork.open_tickets_public }}
                      </span>
                    {% else %}
                      <a class="notblue" data-toggle="tooltip"
                        href="{{ url_for(
                          'ui_ns.view_issues', repo=fork.name,
                          namespace=fork.namespace) }}"
                        title="{{ fork.open_tickets_public }} open issues against {{fork.name}}">
                        <span class="fa fa-exclamation-circle"></span>
                        {{- fork.open_tickets_public }}
                      </a>
                    {% endif %}
                  {% endif %}
                  {% if fork.settings.get('pull_requests', True) %}
                    {% if fork.open_requests == 0 %}
                      <span class="text-muted" data-toggle="tooltip"
                        title="{{fork.open_requests}} open pull requests in {{fork.name}}">
                        <span class="fa fa-arrow-circle-down"></span>
                        {{- fork.open_requests}}
                      </span>
                    {% else %}
                      <a class="notblue" data-toggle="tooltip"
                        href="{{ url_for(
                            '.request_pulls', repo=fork.name,
                            namespace=fork.namespace) }}"
                        title="{{fork.open_requests}} open pull requests in {{fork.name}}">
                        <span class="fa fa-arrow-circle-down"></span>
                        {{- fork.open_requests}}
                      </a>
                    {% endif %}
                  {% endif %}
                  </div>
              </div>
            {% endfor %}
            {% if repos_length and repos_length > 1 %}
            {{ pagination_link('forkpage', forkpage, total_fork_page, repopage=repopage) }}
            {% endif %}
            </div>
        {% else %}
          <div class="card-body">
            <p>You have no forks</p>
          </div>
        {% endif %}
        </div>

      </div>
      <div class="col-md-4">
        <div class="card mb-2" id="group_list">
            <div class="card-header">
                My Groups <span class="badge badge-secondary">{{ user.groups }}</span>
                <span class="float-right">
                  {% if config.get('ENABLE_GROUP_MNGT') %}
                    <a class="btn btn-secondary btn-sm" href="{{
                        url_for('ui_ns.add_group') }}">
                      <span class="fa fa-plus" data-toggle="tooltip"
                        title="Create New Group" aria-hidden="true">
                      </span>
                    </a>
                  {% endif %}
                </span>
            </div>
            {% for group in user.groups %}
              <div class="list-group">
                <a class="list-group-item" href="{{ url_for(
                    'ui_ns.view_group', group=group)}}">
                  <div class=""><strong><span class="fa fa-users">
                    </span> &nbsp;{{ group }}</strong>
                  </div>
                </a>
              </div>
            {% else %}
            <div class="card-body">
                <p>No group found</p>
            </div>
            {% endfor %}
        </div>
        <div class="card mb-2" id="watch_list">
            <div class="card-header">
                My Watch List <span class="badge badge-secondary">{{ watch_list | count }}</span>
            </div>
            {% for repo in watch_list %}
              <div class="list-group">
                {% if repo.is_fork %}
                  <a class="list-group-item" href="{{ url_for(
                    'ui_ns.view_repo',
                    repo=repo.name,
                    username=repo.user.username,
                    namespace=repo.namespace) }}">
                    <div class="">
                      <strong>
                        <i class="fa fa-code-fork"></i>
                        &nbsp;{{ repo.user.username }}/{{
                          repo.namespace + '/' if repo.namespace
                              }}{{ repo.name }}
                      </strong>
                    </div>
                  </a>
                {% else %}
                  <a class="list-group-item" href="{{ url_for(
                    'ui_ns.view_repo', repo=repo.name, namespace=repo.namespace) }}">
                    <div class="">
                      <strong>
                        <span class="fa fa-calendar-o fa-rotate-270"></span>
                        &nbsp;{{ repo.namespace + '/' if repo.namespace
                              }}{{ repo.name }}
                      </strong>
                    </div>
                  </a>
                {% endif %}
              </div>
            {% else %}
            <div class="card-body">
                <p>No project in watch list</p>
            </div>
            {% endfor %}
        </div>
      </div>
    </div>
  </div>
</div>
{% endblock %}

{% block jscripts %}
    {{ super() }}
    <script type="text/javascript">
      $(function(){
        $('.show_parts input[type="checkbox"]').change(function(){
          $('#' + $(this).attr('name')).toggle();
        });
      });
    </script>
{% endblock %}
