{#
    Copyright (c) 2012 Mike Green <myatus@gmail.com>

    For the full copyright and license information, please view the LICENSE
    file that was distributed with this source code.
#}

{% set thr = '' %}
{% set total_display = 0 %}
{% set total_optin   = 0 %}
{% set total_optout  = 0 %}
<div style="position: relative; width: 100%;"> {# REL DIV #}
    <form method="post" action="" id="stats_form">
        {{ nonce | raw }}
        <div class="tablenav top">
            <div class="alignleft actions">
                <select name="stat_year">
                    {% for a_year in years %}
                    <option value="{{ a_year }}" {% if a_year == year %}selected="selected"{% endif %}>{{ a_year }}</option>
                    {% endfor %}
                </select>

                <input type="submit" name="select-year" class="button action" value="{{ __('View year') }}" title="{{ __('View the statistics for the selected year') }}" />

                <input type="submit" name="clear-stats" id="clear-stats" class="button action" value="{{ __('Delete year') }}" title="{{ __('Delete the statistics for the selected year') }}" />
            </div>

            <div class="alignleft actions">
                <input type="submit" name="download-stats" class="button action" value="{{ __('Download Statistics') }}" title="{{ __('Download a CSV file with all the statistics') }}" />
            </div>
        </div>

        <h3>{{ __('Results for') }} {{ year }}</h3>
        <table class="widefat fixed">
            <thead>
                <tr>
                    <th scope="col">Country</th>
                    <th scope="col">Alerts Displayed</th>
                    <th scope="col">Opted In</th>
                    <th scope="col">Opted Out</th>
                    <th scope="col">Ignored</th>
                </tr>
            </thead>
            <tbody>
                {% for month_name, month_stat in stats %}
                    {% if thr != month_name %}
                        {% set thr = month_name %}
                        {% if stats|length == loop.index %}
                            {% set is_last = true %}
                        {% else %}
                            {% set is_last = false %}
                        {% endif %}

                        <tr>
                            <th scope="row" colspan="5" class="month-row {% if is_last %}expanded{% endif %}" id="row_{{ month_name }}" title="{{ __('Click to expand') }}">
                                <strong>{{ month_name }}</strong>
                                <span class="hide-if-no-js collapse-btn">
                                    {% if not is_last %}&#9660;{% else %}&#9650;{% endif %}
                                </span>
                            </th>
                        </tr>
                    {% endif %}

                    {% for country_abbr, country_stat in month_stat|sort %}
                        {% set total_display = total_display + country_stat.0 %}
                        {% set total_optin   = total_optin   + country_stat.1 %}
                        {% set total_optout  = total_optout  + country_stat.2 %}

                        {% if country_abbr is empty %}
                            {% set country_name = 'Unknown' %}
                        {% elseif country_abbr == 'EU' %}
                            {% set country_name = 'Europe (country unknown)' %}
                        {% elseif country_abbr == 'AP' %}
                            {% set country_name = 'Asia/Pacific (country unknown)' %}
                        {% else %}
                            {% set country_name  = countries[country_abbr].country %}
                        {% endif %}

                        <tr class="{% if not is_last %}hide-if-js {% endif %}row_{{ month_name }}_details">
                            <td>{{ country_name }}</td>
                            <td>{{ country_stat.0 }}</td>
                            <td>{{ country_stat.1 }}</td>
                            <td>{{ country_stat.2 }}</td>
                            <td>{{ country_stat.0 - (country_stat.1 + country_stat.2) }}</td>
                        </tr>
                    {% endfor %}
                {% endfor %}
            </tbody>
            <tfoot>
                <tr>
                    <th scope="col">{{ __('Total:') }}</th>
                    <th scope="col">{{ total_display }}</th>
                    <th scope="col">{{ total_optin }}</th>
                    <th scope="col">{{ total_optout }}</th>
                    <th scope="col">{{ total_display - (total_optin + total_optout) }}</th>
                </tr>
            </tfoot>
        </table>

        <p><em>{{ __('Note: The statistics do not include visitors using the "Do Not Track" browser option, as they cannot be reliably tracked.') }}</em></p>

    </form>


</div> {# REL DIV #}
