{% macro renderPager(search, pager, max, order) %}
    {% if search == '' %}
        {{
            pagerfanta(
                pager,
                'twitter_bootstrap_translated',
                {
                    'proximity' : 1,
                    'routeName': 'claro_admin_workspaces_management',
                    'routeParams': {'max': max, 'order': order}
                }
            )
        }}
    {% else %}
        {{
            pagerfanta(
                pager,
                'twitter_bootstrap_translated',
                {
                    'proximity' : 1,
                    'routeName': 'claro_admin_workspaces_management_search',
                    'routeParams': {'search': search, 'max': max, 'order': order}
                }
            )
        }}
    {% endif %}
{% endmacro %}
{% macro displayControls() %}
    {% if app.user %}
        <button class="btn btn-default action-button table-control" data-action="remove" disabled>
            <i class="fa fa-trash-o"></i>
            {{ 'delete'|trans({}, 'platform') }}
        </button>
        <a href="{{ path('claro_workspace_creation_form') }}" class="btn btn-default action-button" role="button">
            <i class="fa fa-pencil"></i>
            {{ 'create'|trans({}, 'platform') }}
        </a>
        {#<a href="{{ path('claro_admin_import_form') }}" class="btn btn-default action-button">
            <i class="icon-book"></i>
            {{ 'import'|trans({}, 'platform') }}
        </a>#}
    {% endif %}
{% endmacro %}
{% macro sortUrl(max, search, order, direction) %}
    {% if direction == 'ASC' %}
        {% set newDirection = 'DESC' %}
    {% else %}
        {% set newDirection = 'ASC' %}
    {% endif %}

    {% if search == '' %}
        {{ path('claro_admin_workspaces_management', {'max': max, 'order': order , 'direction': newDirection}) }}
    {% else %}
        {{ path('claro_admin_workspaces_management_search', {'search': search, 'max': max, 'order': order}) }}
    {% endif %}
{% endmacro %}

{% from _self import renderPager %}
{% from _self import displayControls %}
{% from _self import sortUrl %}

{% extends "ClarolineCoreBundle:Administration:layout.html.twig" %}

{% block title %}{{ parent() ~ ' - ' ~ 'workspaces_management' | trans({}, 'platform') | striptags | raw }}{% endblock %}

{% block breadcrumb %}
    {{
        macros.breadcrumbs([
            {
                'icon': 'fa fa-cog',
                'name': 'administration'|trans({}, 'platform'),
                'href': path('claro_admin_parameters_index')
            },
            {
                'name': 'workspaces_management'|trans({}, 'platform'),
                'href': ''
            }
        ])
    }}
{% endblock %}

{% block section_content %}
    <div class="panel-heading">
        <h3 class="panel-title">{{ 'workspaces_management'|trans({}, 'platform') }}</h3>
    </div>
    <div class="panel-body">
        <div>
            {{ macros.flashBox() }}
        </div>
        <div class="search-bar row">
            <div class="col-md-8">
                {{ displayControls() }}
            </div>
            <div class="col-md-4">
                <div class="input-group">
                    <input class="form-control" type="text" id="search-items-txt" placeholder="{{ 'search'|trans({}, 'platform') }}" value={{search}}>
                    <span class="input-group-btn">
                        <button class="filter btn btn-default" type="button" id="search-button">
                            <i class="fa fa-search"></i>
                        </button>
                    </span>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-8">
                {% if pager.getNbPages() > 1 %}
                    {{ renderPager(search, pager, max, order) }}
                {% endif %}
            </div>
            <div class="col-md-4 margin-top-md margin-bottom-md">
                <div class="input-group">
                    <select class="form-control input-md" id="max-select">
                        <option>20</option>
                        <option>50</option>
                        <option>100</option>
                        <option>200</option>
                        <option>500</option>
                    </select>
                    <span class="input-group-addon">{{ 'line_per_page'|trans({}, 'platform') }}</span>
                </div>
            </div>
        </div>

        <div class="table-responsive">
            <table id="workspace-table" class="table table-striped table-bordered table-condensed">
                <thead>
                    <tr>
                        <th><input type="checkbox" name="all" id="check-all-items"></th>
                        <th>{{ 'id'|trans({}, 'platform') }}</th>
                        <th><a href="{{ sortUrl(max, search, 'name', direction) }}">{{ 'name'|trans({}, 'platform') }} </a></th>
                        <th><a href="{{ sortUrl(max, search, 'code', direction) }}"> {{ 'code'|trans({}, 'platform') }} </a></th>
                        <th><a href="{{ sortUrl(max, search, 'creator', direction) }}"> {{ 'creator'|trans({}, 'platform') }} </a></th>
                        <th><a href="{{ sortUrl(max, search, 'displayable', direction) }}"> {{ 'visibility'|trans({}, 'platform') }} </a></th>
                        <th><a href="{{ sortUrl(max, search, 'selfRegistration', direction) }}"> {{ 'public_registration'|trans({}, 'platform') }} </a></th>
                        <th>{{ 'edit'|trans({}, 'platform') }}</th>
                        <th>date </th>
                    </tr>
                </thead>
                <tbody>
                {% for w in pager.currentPageResults %}
                    <tr id="{{ w.getId() }}">
                        <td>
                            <input type="checkbox" value="{{ w.id }}" class="chk-item center-text"/>
                        </td>
                        <td>{{ w.getId() }}</td>
                        <td>
                            <a href="{{ path('claro_workspace_open_tool', {'workspaceId': w.getId(), 'toolName': 'home'}) }}"> {{ w.getName() }} </a>
                        </td>
                        <td>{{ w.code() }}</td>
                        <td>{{ macros.displayUser(w.getCreator()) }}</td>
                        <td class="center-text">
                            {% if w.isDisplayable() == true %}
                                <a href="{{ path('claro_admin_workspaces_management_visibility') }}"
                                   id="{{ w.getId() }}"
                                   class="visible center"
                                   data-visible="1">
                                    <i class="fa fa-eye"></i>
                                </a>
                            {% else %}
                                <a href="{{ path('claro_admin_workspaces_management_visibility') }}"
                                   id="{{ w.getId() }}"
                                   class="visible"
                                   data-visible="0">
                                    <i class="fa fa-eye-slash"></i>
                                </a>
                            {% endif %}
                        </td>
                        <td class="center-text">
                            {% if w.selfRegistration() == true %}
                                <a href="{{ path('claro_admin_workspaces_management_registration') }}"
                                   id="{{ w.getId() }}"
                                   class="registration"
                                   data-registration="unlock">
                                    <i class="fa fa-unlock"></i>
                                </a>
                            {% else %}
                                <a href="{{ path('claro_admin_workspaces_management_registration') }}"
                                   id="{{ w.getId() }}"
                                   class="registration"
                                   data-registration="lock">
                                    <i class="fa fa-lock"></i>
                                </a>
                            {% endif %}
                        </td>
                        <td class="center-text">
                            <a href="{{ path('claro_workspace_edit_form', {'workspace':w.getId() }) }}">
                                <i class="fa fa-pencil"></i>
                            </a>
                        </td>
                        <td>

                            {% if w.getCreationDate() is not null %}
                                {{ w.getCreationDate()|intl_date_format}}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
            {% if pager.getNbPages() > 1 %}
                {{ renderPager(search, pager, max, order) }}
            {% endif %}
        <div>
            {{ displayControls() }}
        </div>
    </div>
{% endblock %}
{% block javascripts %}
    {{ parent() }}
    <script src='{{ asset('bundles/clarolinecore/js/table.js') }}' type="text/javascript"></script>
    <script>
        $('#max-select').val({{ max }});
        $('#max-select').on('change', function () {
            $('#search-button').trigger('click');
        });

        var parameters = {};
        parameters.route = {};
        parameters.route.action = {};
        parameters.route.normal = {
            "route": "claro_admin_workspaces_management",
            "parameters": {'order': '{{ order }}' }
        };
        parameters.route.search = {
            "route": "claro_admin_workspaces_management_search",
            "parameters": {'order': '{{ order }}' }
        };
        parameters.route.action.remove = {
            "route": "claro_admin_delete_workspaces",
            "parameters": {},
            "type": "DELETE",
            "confirmTemplate": removeWorkspaceConfirm,
            "delete": true
        };
        Claroline.Table.initialize(parameters);

        $('.visible').on('click', function (e) {
            e.preventDefault();
            var id = $(e.currentTarget).attr('id');
            var visible = $(e.currentTarget).attr('data-visible');
            var icon = e.target.parentElement.children;

            $.ajax({
                'url': $('.visible').attr('href'),
                'type': 'POST',
                'data': {
                    'id': id,
                    'visible': visible
                },
                'success': function (data) {
                    if (visible === '1') {
                        $(icon[0]).removeClass('fa fa-eye');
                        $(icon[0]).addClass('fa fa-eye-slash');
                        $(e.currentTarget).attr('data-visible','0');
                    } else {
                        $(icon[0]).removeClass('fa fa-eye-slash');
                        $(icon[0]).addClass('fa fa-eye');
                        $(e.currentTarget).attr('data-visible', '1');
                    }

                }
            });
        });

        $('.registration').on('click', function (e) {
            e.preventDefault();
            var id = $(e.currentTarget).attr('id');
            var previousRegistrationStatus = $(e.currentTarget).attr('data-registration');
            var icon = e.target.parentElement.children;

            $.ajax({
                'url': $('.registration').attr('href'),
                'type': 'POST',
                'data': {
                    'id': id,
                    'registration': previousRegistrationStatus
                },
                'success': function (data) {
                    if (previousRegistrationStatus === 'unlock') {
                        $(icon[0]).removeClass('fa fa-unlock');
                        $(icon[0]).addClass('fa fa-lock');
                        $(e.currentTarget).attr('data-registration','lock');
                    } else {
                        $(icon[0]).removeClass('fa fa-lock');
                        $(icon[0]).addClass('fa fa-unlock');
                        $(e.currentTarget).attr('data-registration', 'unlock');
                    }
                }
            });
        });
    </script>
{% endblock %}
