{% extends "passerelle/manage.html" %}
{% load i18n passerelle %}

{% block breadcrumb %}
  {{ block.super }}
  <a href="{{object.get_absolute_url}}">{{ object.title }}</a>
{% endblock %}

{% block appbar %}
  <h2>{{ view.model.get_verbose_name }} - {{ object.title }}
    {% with status=object.get_availability_status %}
      {% if status %}
        {% if status.down %}<span class="down" title="{{status.message}} {% trans 'since:' %} {{status.start_timestamp|date:"SHORT_DATETIME_FORMAT"}} ">{% trans 'Down' %}</span>{% endif %}
      {% endif %}
    {% endwith %}
  </h2>
  <span class="actions">
    <a class="extra-actions-menu-opener"></a>
    {% if object|can_edit:request.user %}
      <a rel="popup" href="{{ object.get_edit_url }}">{% trans 'Edit' %}</a>
    {% endif %}
    {% block actions %}
    {% endblock %}
  </span>
  <ul class="extra-actions-menu">
    {% if object|can_edit:request.user and has_check_status %}
      <li><a rel="popup" href="{% url 'manage-availability' resource_type=object|resource_type resource_pk=object.id %}">{% trans 'Availability check parameters' %}</a></li>
    {% endif %}
    {% if object|can_edit:request.user %}
      <li><a rel="popup" href="{% url 'logging-parameters' resource_type=object|resource_type resource_pk=object.id %}">{% trans 'Logging parameters' %}</a></li>
    {% endif %}
    {% if request.user.is_authenticated %}
      <li><a download href="{% url 'export-connector' connector=object.get_connector_slug slug=object.slug %}">{% trans 'Export' %}</a></li>
    {% endif %}
    {% if object|can_delete:request.user %}
      <li><a rel="popup" href="{{ object.get_delete_url }}">{% trans 'Delete' %}</a></li>
    {% endif %}
  </ul>
{% endblock %}

{% block content %}

  {% if request.user.is_authenticated %}
    <div id="description">
      {% block description %}
        {% if object.description %}{{object.description|linebreaks}}{% endif %}

        {% for field, value in object.get_description_fields %}
          {% if value or value == False %}
            <p>{{field.verbose_name}}{% trans ":" %} {% spaceless %}
              {% if value == True %}{% trans "yes" %}
              {% elif value == False %}{% trans "no" %}
              {% else %}{{value}}
              {% endif %}
            {% endspaceless %}
            </p>
          {% endif %}
        {% endfor %}
      {% endblock %}
    </div>
  {% endif %}

  <div class="pk-tabs connector-tabs">
    <div class="pk-tabs--tab-list" role="tablist">
      <button role="tab" {% if default_tab_page == 'endpoints' %}aria-selected="true" tabindex="0"{% else %}aria-selected="false" tabindex="-1"{% endif %}aria-controls="panel-endpoints" id="tab-endpoints">{% trans "Endpoints" %}</button>
      {% if object|can_edit:request.user and object.queries is not None %}
        <button role="tab" aria-selected="false" aria-controls="panel-queries" id="tab-queries" tabindex="-1">{% trans "Queries" %}</button>
      {% endif %}
      {% if request.user.is_authenticated %}{% block extra-tab-buttons %}{% endblock %}{% endif %}
      {% if perms.base.see_accessright %}
        <button role="tab" aria-selected="false" aria-controls="panel-security" id="tab-security" tabindex="-1">{% trans "Security" %}</button>
      {% endif %}
      {% if perms.base.see_resourcelog %}
        <button role="tab" {% if default_tab_page == 'logs' %}aria-selected="true" tabindex="0"{% else %}aria-selected="false" tabindex="-1"{% endif %} aria-controls="panel-logs" id="tab-logs">{% trans "Logs" %}</button>
      {% endif %}
      {% if perms.base.view_job and object.jobs_set.all %}
        <button role="tab" aria-selected="false" aria-controls="panel-jobs" id="tab-jobs" tabindex="-1">{% trans "Jobs" %}</button>
      {% endif %}
    </div>
    <div class="pk-tabs--container">

      <div id="panel-endpoints" role="tabpanel" tabindex="0" aria-labelledby="tab-endpoints" {% if default_tab_page != 'endpoints' %}hidden{% endif %}>
        {% if object.documentation_url %}
          <div class="pk-information documentation"><a href="{{ object.documentation_url }}">{% trans "Documentation" %}</a></div>
        {% endif %}
        <div>
          {% if object.api_description %}<p>{{object.api_description|safe}}</p>{% endif %}
          {% block endpoints %}
            {% regroup object.get_endpoints_infos by display_category as endpoints_groups %}
            {% for endpoints in endpoints_groups %}
              {% if endpoints.grouper %}<h4>{{ endpoints.grouper }}</h4>{% elif not forloop.first %}<h4>{% trans "Misc" %}</h4>{% endif %}
              <ul class="endpoints">
                {% for endpoint in endpoints.list %}
                  {% if endpoint.show %}
                    {% include "passerelle/manage/endpoint.html" with endpoint=endpoint %}
                  {% endif %}
                {% endfor %}
              </ul>
            {% endfor %}
          {% endblock %}
        </div>
      </div>

      {% block custom-queries %}
        {% if object|can_edit:request.user and object.queries is not None %}
          <div id="panel-queries" role="tabpanel" tabindex="-1" aria-labelledby="tab-queries" hidden>
            <div>
              {% if object.queries.exists %}
                <ul class="objects-list single-links">
                  {% for query in object.queries.all %}
                    <li>
                      <a href="{{ query.edit_url }}">{{ query.name }}</a>
                      <a rel="popup" class="delete" href="{{ query.delete_url }}">{% trans "delete query" %}</a>
                    </li>
                  {% endfor %}
                </ul>
              {% else %}
                <p>{% trans 'No query are defined.' %}</p>
              {% endif %}
              <p>
                <a class="icon-plus button" href="{{ object.create_query_url }}">{% trans 'New Query' %}</a>
              </p>
            </div>
          </div>
        {% endif %}
      {% endblock %}

      {% if request.user.is_authenticated %}

        {% block extra-sections %}
        {% endblock %}

        {% if perms.base.see_accessright %}
          <div id="panel-security" role="tabpanel" tabindex="-1" aria-labelledby="tab-security" hidden>
            <div>
              {% block security %}
                {% for permission in object.get_connector_permissions %}
                  <p>{{permission.label}}</p>
                  {% access_rights_table resource=object permission=permission.key %}
                {% empty %}
                  {% trans 'Access is open.' %}
                {% endfor %}
              {% endblock %}
            </div>
          </div>
        {% endif %}

        {% if perms.base.see_resourcelog %}
          <div id="panel-logs" role="tabpanel" tabindex="-1" aria-labelledby="tab-logs" {% if not default_tab_page == 'logs' %}hidden{% endif %}>
            <div>
              {% block logs %}
                {% resource_logs_table resource=object %}
              {% endblock %}
              <p><a class="button" href="{% url 'view-logs-connector' connector=object.get_connector_slug slug=object.slug %}">{% trans "Full page & filter" %}</a></p>
            </div>
          </div>
        {% endif %}

        {% if perms.base.view_job and object.jobs_set.all %}
          <div id="panel-jobs" role="tabpanel" tabindex="-1" aria-labelledby="tab-jobs" hidden>
            <div>
              {% block jobs %}
                {% resource_jobs_table resource=object %}
              {% endblock %}
            </div>
            <p><a class="button" href="{% url 'view-jobs-connector' connector=object.get_connector_slug slug=object.slug %}">{% trans "Full page & filter" %}</a></p>
          </div>
        {% endif %}

        {% block extra-tab-panels %}
        {% endblock %}

      {% endif %}
    </div>
  </div>

{% endblock %}
