{% extends "layout/2-columns-left.html" %}
{% load i18n template_tools %}

{% block head %}
    <script type="text/javascript" src="{{ STATIC_URL }}common/js/ajax-modal.js"></script>
{% endblock %}

{% block subtitle %}
    {% trans 'User list' %}
{% endblock %}

{% block actions %}
    {% block extra_actions_before %}{% endblock %}
    {% if user.is_staff %}
        {% if user.is_superuser %}
            {% comment %}
            <a href="{% url 'accounts:user-merge-list' %}" class="btn btn-primary">
                <i class="fa fa-code-fork"></i>
                {% trans 'Merge users' %}
            </a>
            {% endcomment %}
            <div class="btn-group activate_deactivate_all_users" style="display: none;">
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                    {% trans "Actions" %}
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu pull-right">
                    <li style="display: none;" class="activate_all_users">
                        <a href="{% url 'accounts:user-bulk-undelete' %}" id="activate_all_users">
                            <i class="fa fa-check"></i>
                            {% trans 'Activate' %}
                        </a>
                    </li>
                    <li style="display: none;" class="deactivate_all_users">
                        <a href="{% url 'accounts:user-bulk-delete' %}" id="deactivate_all_users">
                            <i class="fa fa-times"></i>
                            {% trans 'Deactivate' %}
                        </a>
                    </li>
                </ul>
            </div>
        {% endif %}
        <div class="btn-group">
            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                {% trans "Add" %}
                <span class="caret"></span>
            </button>
            <ul class="dropdown-menu">
                <li>
                    <a href="{% url 'accounts:user-create' %}">
                        <i class="fa fa-plus"></i>
                        {% trans 'Add trainee' %}
                    </a>
                </li>
                {% if user.is_superuser %}
                    <li>
                        <a href="{% url 'accounts:user-create' %}?trainer=true">
                            <i class="fa fa-plus"></i>
                            {% trans 'Add trainer' %}
                        </a>
                    </li>
                {% endif %}
            </ul>
        </div>
        <div class="btn-group">
            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                {% trans "Import" %}
                <span class="caret"></span>
            </button>
            <ul class="dropdown-menu pull-right">
                <li>
                    <a href="{% url 'accounts:user-import' %}">
                        <i class="fa fa-file"></i>
                        {% trans 'Import users' %}
                    </a>
                </li>
                {% block extra_actions_after %}{% endblock %}
            </ul>
        </div>
    {% endif %}
{% endblock %}

{% block column_left %}
    <form action="" method="get">
        {% include 'extras/form.html' with form=filter.form %}
        <div class="clearfix">
            <a href="{% url 'accounts:user-list' %}" class="pull-left btn btn-warning">
                {% trans 'Reset' %}
            </a>
            <input type="submit" class="btn btn-primary pull-right" value="{% trans 'Apply' %}" />
        </div>
    </form>
{% endblock %}

{% block column_main %}
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th><input type="checkbox" id="select_all_users"></input></th>
                            <th>{% trans 'Username'%}</th>
                            <th>{% trans 'First name'%}</th>
                            <th>{% trans 'Last name'%}</th>
                            <th>{% trans 'Email'%}</th>
                            <th>{% trans 'Active / Inactive'%}</th>
                            {% if user.is_superuser %}
                                <th>{% trans 'Actions'%}</th>
                            {% endif %}
                            {% comment 'Deactivate this until we revamp the email templates' %}
                                {% if user.is_staff %}
                                    <th>{% trans 'Resend email'%}</th>
                                {% endif %}
                            {% endcomment %}
                        </tr>
                    </thead>
                    <tbody>
                        {% for mtpuser in mtpuser_list %}
                            <tr>
                                <td>{% if not mtpuser.is_superuser %}<input type="checkbox" class="select_row" data-inactive="{{ mtpuser.is_deleted }}" value="{{ mtpuser.id }}"></input>{% endif %}</td>
                                <td>{{ mtpuser.username }}</td>
                                <td>{{ mtpuser.first_name }}</td>
                                <td>{{ mtpuser.last_name }}</td>
                                <td>{{ mtpuser.email }}</td>
                                <td>
                                    {% if mtpuser.is_deleted %}
                                        {% trans "Inactive" %}
                                    {% else %}
                                        {% trans "Active" %}
                                    {% endif %}
                                </td>
                                {% if user.is_superuser and not mtpuser.is_superuser %}
                                    <td>
                                        <div class="btn-group">
                                            <button type="button"
                                                    class="btn btn-default dropdown-toggle"
                                                    data-toggle="dropdown"
                                                    aria-expanded="false"
                                            >
                                                {% trans "Actions" %}
                                                <span class="caret"></span>
                                            </button>
                                            <ul class="dropdown-menu pull-right">
                                                {% if mtpuser.is_deleted %}
                                                    <li>
                                                        <a class="dialog-box" href="{{ mtpuser.get_undelete_url }}">
                                                            <i class="fa fa-check"></i> {% trans 'Activate' %}
                                                        </a>
                                                    </li>
                                                {% else %}
                                                    <li>
                                                        <a href="{{ mtpuser.get_edit_url }}">
                                                            <i class="fa fa-pencil"></i> {% trans 'Edit' %}
                                                        </a>
                                                    </li>
                                                    <li>
                                                        <a class="dialog-box" href="{{ mtpuser.get_delete_url }}">
                                                            <i class="fa fa-times"></i> {% trans 'Deactivate' %}
                                                        </a>
                                                    </li>
                                                {% endif %}
                                            </ul>
                                        </div>
                                    </td>
                                {% endif %}
                                {% comment 'Deactivate this until we revamp the email templates' %}
                                    {% if user.is_staff %}
                                        <td>
                                            <a class="btn btn-default btn-sm" href="{{ mtpuser.get_welcome_url }}">
                                                <i class="fa fa-envelope"></i>
                                            </a>
                                        </td>
                                    {% endif %}
                                {% endcomment %}
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
                <div class="panel-footer">
                    {% pagination %}
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="modal-bulk" tabindex="-1" role="dialog"  aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
            </div>
        </div>
    </div>

    <script type="text/javascript">
    $(document).ready(function (){

        var $deactivateAllUsersItem = $('.deactivate_all_users');
        var $activateAllUsersItem = $('.activate_all_users');
        var $activateDeactivateAllUsersMenu = $('.activate_deactivate_all_users');

        // show or hide action buttons according to status
        // (inactive /active) of selected users in rows
        function showOrHideActions() {
            var hasInactive = false;
            var hasActive = false;
            // what distinct (in)active statuses do we have in selected users?
            $('.select_row:checked').each(function (idx, elt) {
                $(elt).data('inactive') === 'False' ? hasActive = true : hasInactive = true;
                if (hasActive && hasInactive) {
                    return;
                }
            });
            // hide or show actions buttons / list accordingly
            $deactivateAllUsersItem.toggle(hasActive);
            $activateAllUsersItem.toggle(hasInactive);
            $activateDeactivateAllUsersMenu.toggle(hasInactive || hasActive);
        }

        // check or uncheck all rows checkbox
        // hide or unhide action buttons accordingly
        $('#select_all_users').change(function () {
            $('.select_row').prop('checked', this.checked);
            showOrHideActions();
        });

        // recompute action buttons visibility on each row select
        $('.select_row').change(showOrHideActions);

        // shows a confirmation modal (for bulk deactivation or activation)
        function _showModal($actionButton) {
            $modal = $('#modal-bulk');
            // inject selected user ids into modal form
            // no django form in modal, see why in modal template
            var fieldsHtml = $.map($('.select_row:checked'), function (row) {
                return '<input type="hidden" value="' + row.value + '" name="users">';
            });
            var url = $actionButton.attr('href');
            $.get(url, function (data) {
                $modal.find('.modal-content').html(data);
                $modal.find('.fields').html(fieldsHtml.join(''));
                $modal.modal('show');
            }).fail(function (err) {
                console.log('Error fetching modal-bulk template', err);
            });
        }

        // launch the bulk activation process
        var $activateAllUsersButton = $('#activate_all_users');
        $activateAllUsersButton.click(function (e) {
            e.preventDefault();
            _showModal($activateAllUsersButton);
        });

        // launch the bulk deactivation process
        var $deactivateAllUsersButton = $('#deactivate_all_users');
        $deactivateAllUsersButton.click(function (e) {
            e.preventDefault();
            _showModal($deactivateAllUsersButton);
        });
    });
    </script>
{% endblock %}
