{% extends 'layout.html' %}
{% from 'include/storage_usage.html' import storage_usage %}
{% from 'include/empty.html' import empty %}
{% set active_page='repo' %}
{% block title %}
  {% trans %}Repositories{% endtrans %}
{% endblock title %}
{% block body %}
  {% macro repo_list_item(repo)%}
    <a {{ attrib(class=['list-group-item', repo.failed and 'disabled', repo.status[0] != 'ok' and 'list-group-item-warning']) }} href="{{ url_for('browse', repo) }}">
      <i class="fa fa fa-hdd-o" aria-hidden="true"></i>
      {% set name = repo.display_name.split('/')[-1] %}
      {{ name }}
      {% if name|length==1 %}:\{% endif %}
      <span class="pull-right text-secondary">
        {% if repo.status[0] != 'ok' %}
          <span data-toggle="tooltip"
                data-placement="left"
                title="{{ repo.status[1] }}">
            <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
          </span>
        {% endif %}
      </span>
    </a>
  {% endmacro %}
  <div class="container-fluid" id="locations">
    {# disk usage/quota #}
    {{ storage_usage(disk_usage, disk_quota) }}
    {# Repositories #}
    <h2 class="mb-2">{% trans %}Repositories{% endtrans %}</h2>
    <div class="row">
      {% if repos|length == 0 %}
        <div class="col-md-12">
          {% call empty('fa fa-archive', _('No repositories')) %}
            <p>
              {% trans %}You don't have any repositories in your user directory. You may need to refresh the page if you just made a new backup.{% endtrans %}
            </p>
          {% endcall %}
        </div>
      {% else %}
        {% for repo in repos|sort(attribute='display_name') %}
          <div class="col-md-6 col-lg-4 col-xl-3 mt-2 mb-4">
            <div class="card {% if repo.status[0] != 'ok' %}border-warning{% endif %} h-100">
              <div class="card-header text-center display-1">
                <a href="{{ url_for('browse', repo) }}"
                   class="{% if repo.status[0] != 'ok' %}text-warning{% endif %}">
                  <i class="fa fa-desktop" aria-hidden="true"></i>
                </a>
              </div>
              <div class="card-body">
                {# Format the name to highligth the significant repository name #}
                {% set name = repo.display_name.split('/')[-1] %}
                {% if name|length == 1 %}
                  {% set name = repo.display_name.split('/')[-2] %}
                  {% set prefix = '/'.join(repo.display_name.split('/')[0:-2]) %}
                  {% set suffix = repo.display_name.split('/')[-1] %}
                {% else %}
                  {% set prefix = '/'.join(repo.display_name.split('/')[0:-1]) %}
                  {% set suffix = '' %}
                {% endif %}
                <h5 class="card-title">
                  <a href="{{ url_for('browse', repo) }}"
                     class="{% if repo.status[0] != 'ok' %}text-warning{% endif %}">
                    {% if prefix %}<small><small>{{ prefix }}/</small></small>{% endif %}
                    {{ name }}
                    {% if suffix %}<small><small>/{{ suffix }}</small></small>{% endif %}
                  </a>
                </h5>
                {# Last backup date #}
                <h6 class="card-subtitle text-muted small">
                  {% if repo.status[0] != 'ok' %}
                    {{ repo.status[1] }}
                  {% elif repo.last_backup_date %}
                    {% trans %}Last backup {% endtrans %}
                    <time datetime="{{ repo.last_backup_date }}">{{ repo.last_backup_date | lastupdated }}</time>
                  {% else %}
                    {% trans %}Initial backup not completed.{% endtrans %}
                  {% endif %}
                </h6>
              </div>
            </div>
          </div>
        {% endfor %}
      {% endif %}
    </div>
  </div>
{% endblock body %}
