{% extends 'modal_base.html' %}
{% load i18n widget_tweaks %}

{% block title %}
    {% trans 'Results export' %}
{% endblock %}

{% block body %}
    <form action="{{ trainingsession.get_export_results_url }}" method="post">{% csrf_token %}
        <div class="modal-body">
            {% regroup exportable_activities by type as activity_types %}
            <ul class="list-unstyled">
                {% for type in activity_types %}
                    <li class="checkbox type-choice">
                        <label>
                            <input type="checkbox" class="type-checkbox"> <strong>{{ type.grouper }}</strong>
                        </label>
                        <ul>
                            {% for activity in type.list %}
                                <li class="checkbox">
                                    <label>
                                        <input class="activity-checkbox" type="checkbox" name="activities_to_export" value="{{ activity.id }}">
                                        {{ activity.name }}
                                    </label>
                                </li>
                            {% endfor %}
                        </ul>
                    </li>
                {% endfor %}
            </ul>
        </div>
        <div class="modal-footer clearfix">
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">{% trans 'Cancel' %}</button>
            <input class="btn btn-primary pull-right download-button" type="submit" value="{% trans 'Download' %}" />
        </div>
    </form>
    <script type="text/javascript">
        $(document).ready(function(){
            // Disable the download button if no activities were selected
            var $downloadButton = $('.download-button');
            var enableOrDisableDownloadButton = function(){
                var mustBeEnabled = $('.activity-checkbox:checked').length > 0;
                $downloadButton.prop('disabled', !mustBeEnabled);
            };

            $('.type-checkbox').change(function(){
                var $checkbox = $(this),
                    isChecked = $checkbox.is(':checked');
                $checkbox.closest('li').find('.activity-checkbox').prop('checked', isChecked);
                enableOrDisableDownloadButton();
            });
            $('.activity-checkbox').change(enableOrDisableDownloadButton);
            enableOrDisableDownloadButton();
        });
    </script>
{% endblock %}
