{% extends "one_column_body.html" %}
{% from "macros.html" import paginator %}
{% from "analytics/macros.html" import format_query_string, sort_icon_class, linked_number_if_positive %}
{% block title %}{% filter trim %}{% trans %}Analytics / Home{% endtrans %}{% endfilter %}{% endblock %}
{% block forestyle %}
  <style>
    table {
      border-collapse: collapse;
      margin-bottom: 1rem;
    }
    th, td {
      padding: 0.5rem;
    }
    th {
      background-color: #f2f2f2;
    }
    tbody tr:nth-child(even) {
      background-color: #f2f2f2;
    }
    tbody td:not(:first-child) {
      text-align: center;
    }
    .title-row {
      align-items: center;
    }
    .analytics-summary {
      margin-bottom: 2rem;
    }
  </style>
{% endblock %}
{% block layout_class %}{{ super() }} analytics-page{% endblock %}
{% block forum_header %}{% endblock %}
{% block content %}
<div class="title-row">
  {% if has_named_segments %}
  <h1><a href="{{ url('analytics_index') }}">{% trans %}Analytics{% endtrans %}</a> / <a href="{{ url('analytics_users',
          dates=dates_url_param, users_segment='all-users') }}">{% trans %}Users{% endtrans %}</a> / {{ default_segment_name }}</h1>
  {% else %}
  <h1><a href="{{ url('analytics_index') }}">{% trans %}Analytics{% endtrans %}</a> / {% trans %}Users{% endtrans %}</h1>
  {% endif %}
  {% include "analytics/date_selector.html" %}
</div>
{% set base_url = url('analytics_users', dates=dates_url_param, users_segment=default_segment_slug) %}
<form
  class="search-form"
  action="{{ base_url }}"
  method="get"
>
  <input
    type="text"
    id="search-input"
    name="query"
    placeholder="Search organizations"
    value="{{ query|escape }}"
  />
  <input type="hidden" name="sort_by" value="{{ sort_by }}">
  <input type="hidden" name="sort_order" value="{{ sort_order }}">
  <button class="btn btn-primary" type="submit">Search</button>
</form>
<table>
  <thead>
    <tr>
      <th>
        <a
          class="{{ sort_icon_class(current_sort_by=sort_by, desired_sort_by='orgname', current_sort_order=sort_order) }}"
          href="{{ format_query_string(query=query, desired_sort_by='orgname', current_sort_by=sort_by, current_sort_order=sort_order) }}">
          {% trans %}Organization{% endtrans %}
        </a>
      </th>
      <th>{% trans %}Num Users{% endtrans %}</th>
      <th>
        <a
          class="{{ sort_icon_class(current_sort_by=sort_by, desired_sort_by='num_users_added', current_sort_order=sort_order) }}"
          href="{{ format_query_string(query=query, desired_sort_by='num_users_added', current_sort_by=sort_by, current_sort_order=sort_order) }}">
          {% trans %}Users Added{% endtrans %}
        </a>
      </th>
      <th>
        <a
          class="{{ sort_icon_class(current_sort_by=sort_by, desired_sort_by='num_questions', current_sort_order=sort_order) }}"
          href="{{ format_query_string(query=query, desired_sort_by='num_questions', current_sort_by=sort_by, current_sort_order=sort_order) }}">
          {% trans %}Questions{% endtrans %}
        </a>
      </th>
      <th>
        <a
          class="{{ sort_icon_class(current_sort_by=sort_by, desired_sort_by='num_answers', current_sort_order=sort_order) }}"
          href="{{ format_query_string(query=query, desired_sort_by='num_answers', current_sort_by=sort_by, current_sort_order=sort_order) }}">
          {% trans %}Answers{% endtrans %}
        </a>
      </th>
      <th>
        <a
          class="{{ sort_icon_class(current_sort_by=sort_by, desired_sort_by='num_upvotes', current_sort_order=sort_order) }}"
          href="{{ format_query_string(query=query, desired_sort_by='num_upvotes', current_sort_by=sort_by, current_sort_order=sort_order) }}">
          {% trans %}Upvotes{% endtrans %}
        </a>
      </th>
      <th>
        <a
          class="{{ sort_icon_class(current_sort_by=sort_by, desired_sort_by='num_downvotes', current_sort_order=sort_order) }}"
          href="{{ format_query_string(query=query, desired_sort_by='num_downvotes', current_sort_by=sort_by, current_sort_order=sort_order) }}">
          {% trans %}Downvotes{% endtrans %}
        </a>
      </th>
      <th>
        {% trans %}All Events{% endtrans %}
      </th>
      <th>
        <a
          class="{{ sort_icon_class(current_sort_by=sort_by, desired_sort_by='time_on_site', current_sort_order=sort_order) }}"
          href="{{ format_query_string(query=query, desired_sort_by='time_on_site', current_sort_by=sort_by, current_sort_order=sort_order) }}">
          {% trans %}Time on Site{% endtrans %}
        </a>
      </th>
    </tr>
  </thead>
  <tbody>
    {% for group_data in groups %}
      <tr>
        <td><a href="{{ url('analytics_users', dates=dates_url_param, users_segment='group:' + group_data["group"].id | string) }}">{{ group_data["group"].name }}</a></td>
        <td>{{ group_data["num_users"] }}</td>
        <td>
          {{ linked_number_if_positive(group_data["num_users_added"],
                                       url('analytics_activity',
                                            content_segment='all-content',
                                            activity_segment=analytics_activity_field.REGISTERED,
                                            dates=dates_url_param,
                                            users_segment='group:' + group_data["group"].id | string))
          }}
        </td>
        <td>
          {{ linked_number_if_positive(group_data["num_questions"],
                                       url('analytics_activity',
                                            content_segment='all-content',
                                            activity_segment=analytics_activity_field.QUESTION_POSTED,
                                            dates=dates_url_param,
                                            users_segment='group:' + group_data["group"].id | string))
          }}
        </td>
        <td>
          {{ linked_number_if_positive(group_data["num_answers"],
                                       url('analytics_activity',
                                            content_segment='all-content',
                                            activity_segment=analytics_activity_field.ANSWER_POSTED,
                                            dates=dates_url_param,
                                            users_segment='group:' + group_data["group"].id | string))
          }}
        </td>
        <td>
          {{ linked_number_if_positive(group_data["num_upvotes"],
                                       url('analytics_activity',  
                                            content_segment='all-content',
                                            activity_segment=analytics_activity_field.CONTENT_UPVOTED,
                                            dates=dates_url_param,
                                            users_segment='group:' + group_data["group"].id | string))
          }}
        </td>
        <td>
          {{ linked_number_if_positive(group_data["num_downvotes"],
                                       url('analytics_activity',  
                                            content_segment='all-content',
                                            activity_segment=analytics_activity_field.CONTENT_DOWNVOTED,
                                            dates=dates_url_param,
                                            users_segment='group:' + group_data["group"].id | string))
          }}
        </td>
        <td>
          <a href={{url('analytics_activity', activity_segment='all-activity', content_segment='all-content', users_segment='group:' + group_data["group"].id | string, dates=dates_url_param)}}>
            {% include "analytics/arrow_right.html" %}
          </a>
        </td>
        <td>
          {{ group_data['time_on_site']|format_time_on_site }}
        </td>
      </tr>
    {% else %}
      <tr class="no-activity-row">
        <td colspan="8">{% trans %}There has been no activity in this time period{% endtrans %}</td>
      </tr>
    {% endfor %}
    {% if paginator_context.is_paginated %}
      <tr class="paginator-row">
        <td colspan="8">
        {{ paginator(paginator_context) }}
        </td>
      </tr>
    {% endif %}
  </tbody>
</table>
{% endblock %}
