{% extends "layout/base.html" %}

{# If a user chooses these fields, they carry between different pages. #}
{% set product_args = {
    'product_name': product.name,
    'year': year,
    'month': month,
    'day': day
} %}

{% set additional_page_args=additional_page_args or {} %}

{% set product_args_label -%}
    {%- if year -%}
        {%- if not day %} across
        {%- else %} on
        {%- endif -%}
    {%- if month -%}
        {%- if day %} {{ day | day_ordinal}}
        {%- endif %} {{ month | month_name }}
    {%- endif %} {{ year }}
    {%- else %} whole collection
    {%- endif -%}
{%- endset %}

{% set back_to_product_overview %}
    <div>
        <a href="{{ url_for('pages.product_page', **product_args) }}">
            Return to {{ product.name }} {{ product_args_label }} overview
        </a>
    </div>
{% endset %}

{% block title -%}
    {{ product.name }} {{ product_args_label }}
{%- endblock %}

{% block header %}
    <header id="product-headers">
        {# Go to current page with different product/year/month options#}
        {% set nav_endpoint=request.url_rule.endpoint %}

        <div class="header-option primary product-selection-header ex-menu">
            <h2 class="option-title ex-menu-title" onclick="">
                {% if product.definition.description %}
                    <strong>{{ product.name }}</strong><span class="option-description">: {{ product.definition.description}}</span>
                {% else %}
                    <strong>{{ product.name }}</strong>
                {% endif %}
            </h2>
            <div class="option-menu">
                {% for row in grouped_products | batch(3) %}
                    <ul>
                        {% for group_name, product_summaries in row %}
                            <li>
                                {# We leave a space character in blank headings for consistent line height. #}
                                <h3 class="group-name">{{ group_name or ('&nbsp;'|safe)}}</h3>
                                <ul class="items">
                                    {% for product_opt, summary in product_summaries %}
                                    {% if product_opt.name not in hidden_product_list %}
                                        <li class="{% if (not same_region_products and ((not summary) or summary.dataset_count == 0)) or (same_region_products and product_opt.name not in same_region_products) %}empty{% endif %}">
                                            <a href="{{ url_for(request.url_rule.endpoint, product_name=product_opt.name, **additional_page_args) }}"
                                               title="{{ product_opt.definition.description }}"
                                               class="option-menu-link {% if product_opt.name == product.name %}active{% endif %}">
                                                {{ product_opt.name }}
                                            </a>
                                        </li>
                                    {% endif %}
                                    {% endfor %}
                                </ul>
                            </li>
                        {% endfor %}
                    </ul>
                {% endfor %}
            <div class="footer"> </div>
            </div>
        </div>
        {% if product %}
            <div class="header-option limited ex-menu" onclick="">
                <h2 class="option-title ex-menu-title">{{ year or 'All years' }}</h2>
                <div class="option-menu left">

                    {% set year_range=[] %}
                    {% if year_selector_summary and year_selector_summary.timeline_dataset_counts %}
                    {% for dataset_date, dataset_count in year_selector_summary.timeline_dataset_counts.items() %}
                            {% if dataset_count > 0 %}
                            {% do year_range.append(dataset_date.year) %}
                            {% endif %}
                    {% endfor %}
                    {% endif %}

                    {% for row in year_range | unique | batch(3) %}
                        <ul>
                            {% for year_opt in row %}
                                <li>
                                    <a href="{{ url_for(request.url_rule.endpoint, product_name=product.name, year=year_opt, **additional_page_args) }}"
                                       class="option-menu-link {% if (year_opt) == year %}active{% endif %}">
                                        {{ year_opt }}
                                    </a>
                                </li>
                            {% endfor %}
                        </ul>
                    {% endfor %}
                    <ul>
                        <li class="wildcard">
                            <a href="{{ url_for(nav_endpoint, product_name=product.name, **additional_page_args) }}"
                               class="option-menu-link {% if not year %}active{% endif %}">All</a>
                        </li>
                    </ul>
                </div>
            </div>
        {% endif %}
        {% if year %}
            <div class="header-option limited ex-menu" onclick="">
                <h2 class="option-title ex-menu-title">
                    {% if month %}
                        {{ month | month_name }}
                    {% else %}
                        All months
                    {% endif %}
                </h2>
                <div class="option-menu left">
                    {% set month_range=[] %}
                    {% if time_selector_summary and time_selector_summary.timeline_dataset_counts%}
                        {% for time_selector_dataset_date, time_selector_dataset_count in time_selector_summary.timeline_dataset_counts.items() %}
                            {% if time_selector_dataset_count > 0 and time_selector_dataset_date.year == year %}
                            {% do month_range.append(time_selector_dataset_date.month) %}
                            {% endif %}
                        {% endfor %}
                    {% endif %}
                    {% for row in month_range |unique| batch(3) %}
                        <ul>
                            {% for month_opt in row %}
                                <li>
                                    <a href="{{ url_for(
                                        request.url_rule.endpoint,
                                        product_name=product.name,
                                        year=year,
                                        month=month_opt,
                                        **additional_page_args
                                       ) }}"
                                       class="option-menu-link {% if (month_opt) == month %}active{% endif %}">
                                        {{ month_opt | month_name }}
                                    </a>
                                </li>
                            {% endfor %}
                        </ul>
                    {% endfor %}
                    <ul>
                        <li class="wildcard">
                            <a href="{{ url_for(nav_endpoint, product_name=product.name, year=year, **additional_page_args) }}"
                               class="option-menu-link {% if not month %}active{% endif %}">All</a>
                        </li>
                    </ul>
                </div>
            </div>
        {% endif %}

        {% if month %}
            <div class="header-option limited ex-menu" onclick="">
                <h2 class="option-title ex-menu-title">
                    {% if day %}
                        {{ day | day_ordinal }}
                    {% else %}
                        All days
                    {% endif %}
                </h2>
                <div class="option-menu left">
                    {% set day_range=[] %}
                    {% if time_selector_summary and time_selector_summary.timeline_dataset_counts%}
                        {% for time_selector_dataset_date, time_selector_dataset_count in time_selector_summary.timeline_dataset_counts.items() %}
                            {% if time_selector_dataset_count > 0 and time_selector_dataset_date.year == year and time_selector_dataset_date.month == month %}
                            {% do day_range.append(time_selector_dataset_date.day) %}
                            {% endif %}
                        {% endfor %}
                    {% endif %}
                    {% for row in day_range |  unique | batch(3) %}
                        <ul>
                            {% for day_opt in row %}
                                <li>
                                    <a href="{{ url_for(
                                        request.url_rule.endpoint,
                                        product_name=product.name,
                                        year=year,
                                        month=month,
                                        day=day_opt,
                                        **additional_page_args
                                        ) }}"
                                       class="option-menu-link {% if (day_opt) == day %}active{% endif %}">
                                        {{ day_opt | day_ordinal }}
                                    </a>
                                </li>
                            {% endfor %}
                        </ul>
                    {% endfor %}
                    <ul>
                        <li class="wildcard">
                            <a href="{{ url_for(nav_endpoint, product_name=product.name, year=year, month=month, **additional_page_args) }}"
                               class="option-menu-link {% if not day %}active{% endif %}">
                                All
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        {% endif %}
    </header>
{% endblock %}
