{% load humanize %}

{# Somewhere to dump chart data as we loop through the goals #}
<script>window.Experiments = {EXPERIMENT_CHART_DATA: {}};</script>

<table id="experiment-results-table" class="experiment-results-table experiment-hide-irrelevant" data-set-state-url="{% url "admin:experiment_admin_set_state" %}" data-experiment-name="{{ experiment.name }}" data-set-alternative-url="{% url "admin:experiment_admin_set_alternative" %}">
    <thead>
        <tr>
            <th id="experiment-toggle-goals" class="experiment-toggle-goals" data-shown="false">Toggle All Goals</th>
            <th data-alternative="control"{% if user_alternative == 'control' %} class="experiment-selected-alternative"{% endif %}>
                control <small>({{ control_participants|intcomma }})</small>
                &dash; <span class="experiment-alternative-enrolled">Enrolled</span><span class="experiment-alternative-join">Click to join</span>
            </th>
            {% for alternative, participants in alternatives %}
                 {% if alternative != 'control' %}
                     <th colspan="3" data-alternative="{{ alternative }}"{% if user_alternative == alternative %} class="experiment-selected-alternative"{% endif %}>
                        {{ alternative }} <small>({{ participants|intcomma }})</small>
                        &dash; <span class="experiment-alternative-enrolled">Enrolled</span><span class="experiment-alternative-join">Click to join</span>
                     </th>
                 {% endif %}
            {% endfor %}
        </tr>
    </thead>

    <tbody>
        {% for goal, data in results.items %}
            <tr{% if not data.relevant %} class="experiment-irrelevant-goal"{% endif %}>
                <td>
                    {% if data.mwu %}
                        <span class="experiment-mwu-goal" data-chart-goal="{{ goal }}">{{ goal }}</span>
                    {% else %}
                        {{ goal }}
                    {% endif %}
                </td>

                <td>
                    {{ data.control.conversions|intcomma }} <small>({{ data.control.conversion_rate|floatformat:2 }}%
                    {% if data.mwu %}
                    - APU {{ data.control.average_goal_actions|floatformat:2 }}
                    {% endif %}
                    )</small></td>

                {% for alternative_name, results in data.alternatives %}
                    {% if alternative_name != 'control' %}
                        <td>
                            {{ results.conversions|intcomma }}<small> ({{ results.conversion_rate|floatformat:2 }}%
                            {% if data.mwu %}
                                - APU {{ results.average_goal_actions|floatformat:2 }}
                            {% endif %}
                            )</small>
                        </td>
                        <td title="Improvement">
                            {% with improvement=results.improvement confidence=results.confidence %}
                                {% if improvement != None %}
                                    <span class="{% if confidence >= 95 %}{% if improvement > 0 %}experiment-positive-improvement{% elif improvement < 0 %}experiment-negative-improvement{% endif %}{% endif %}">
                                        {{ improvement|floatformat:2 }}&nbsp;%
                                    </span>
                                {% else %}
                                    N/A
                                {% endif %}
                            {% endwith %}
                        </td>
                        <td title="Confidence Interval">
                            {% with confidence=results.confidence %}
                                {% if confidence != None %}
                                    <span class="{% if confidence >= 95 %}experiment-high-confidence{% else %}experiment-low-confidence{% endif %}">
                                        {% if confidence >= 99.995 %}~{% endif %}{{ confidence|floatformat:2 }}&nbsp;%
                                    </span>
                                {% else %}
                                    N/A
                                {% endif %}
                            {% endwith %}
                            {% if data.mwu %}
                                MWU: {{ results.mann_whitney_confidence|floatformat:2 }}%
                            {% endif %}
                        </td>
                    {% endif %}
                {% endfor %}
            </tr>
            {% if data.mwu %}
                <tr style="display: none;" id="{{ goal }}_mwu_row">
                    <td colspan="{{ column_count }}" >
                        <div id="{{goal}}_chart"></div>
                        <script>window.Experiments.EXPERIMENT_CHART_DATA['{{goal|escapejs}}'] = {{ data.mwu_histogram|safe }};</script>
                    </td>
                </tr>
            {% endif %}
        {% endfor %}
    </tbody>
</table>
