{% extends "views/platform-admin/_base_template.html" %}
{% from "components/page-footer.html" import page_footer %}
{% from "components/big-number.html" import big_number %}
{% from "components/table.html" import mapping_table, field, stats_fields, row_group, row, right_aligned_field_heading, hidden_field_heading, text_field %}
{% from "components/form.html" import form_wrapper %}
{% from "components/components/button/macro.njk" import usaButton %}
{% from "components/components/details/macro.njk" import usaDetails %}

{% macro services_table(services, caption) %}
  {% call(item, row_number) mapping_table(
    caption=caption,
    caption_visible=False,
    field_headings=[
    right_aligned_field_heading('Emails'),
    right_aligned_field_heading('Text messages'),
    ],
    field_headings_visible=False,
  ) %}

    {% for service in services %}

      {% call row_group() %}

        {% call row() %}
          {% call field(border=False, colspan=3) %}
            <a href="{{ url_for('main.service_dashboard', service_id=service['id']) }}" class="file-list-filename-large usa-link">{{ service['name'] }}</a>
            {% if not service['active'] %}
              <span class="heading-medium hint">&ensp;Archived</span>
            {% endif %}
          {% endcall %}

        {% endcall %}

        {% call row() %}
          {% for channel in ('email', 'sms') %}
            {% call field(border=False) %}
              {{ big_number(
                service['stats'][channel]['requested'],
                smallest=True,
                label=service['stats'][channel]['requested']|message_count_label(channel)
              ) }}
            {% endcall %}
          {% endfor %}
        {% endcall %}

      {% endcall %}

    {% endfor %}

  {% endcall %}
{% endmacro %}


{% block per_page_title %}
  {{ page_title|capitalize }}
{% endblock %}

{% block platform_admin_content %}

  <h1 class="font-body-2xl">
    {{ page_title|capitalize }}
  </h1>


  {% set details_content %}
    {% call form_wrapper(method="get") %}
      {{ form.start_date(param_extensions={"hint": {"text": "Enter start date in format YYYY-MM-DD"}}) }}
      {{ form.end_date(param_extensions={"hint": {"text": "Enter end date in format YYYY-MM-DD"}}) }}
      {{ form.include_from_test_key }}
      {{ usaButton({ "text": "Filter", "classes": "margin-y-2" }) }}
    {% endcall %}
  {% endset %}

  {{ usaDetails({
    "summaryText": "Apply filters",
    "html": details_content
  }) }}

  {% include "views/platform-admin/_global_stats.html" %}

  {{ services_table(services, page_title|capitalize) }}

{% endblock %}
