{% extends '@CadolesCore/base.html.twig' %}


{% block pagewrapper %}
    <h1>
        Gestion des Items
    </h1>
    
    <div class="row">
        <div class="col-md-9">
            <p>
                <a href="{{ path('cadoles_portal_config_item_submit') }}" class="btn btn-success">Ajouter un Item</a>
                <a href="{{ path('cadoles_portal_config_itemcategory_submit') }}" class="btn btn-success">Ajouter une Categorie</a>

                <a href="{{ path('cadoles_portal_config_item_forceorder') }}" class="btn btn-success pull-right">Ordonner les items en alpha</a>
            </p>

            <div class="category-list">
                {% for itemcategory in itemcategorys %}
                    <div class="category-list-item" data-itemcategory-id="{{ itemcategory.id }}">
                        <h2 class="grid-title">
                            <a href="{{ path('cadoles_portal_config_itemcategory_update', { id: itemcategory.id }) }}">
                                <small><i class="fa fa-arrows-v" style="display: inline-block; margin-top: -26px;"></i></small>
                                {{ itemcategory.label }}
                            </a>
                            <input type="hidden" name="itemcategory[id][]" value="{{ itemcategory.id }}" />
                            <input type="hidden" name="itemcategory[rowOrder][]" value="{{ itemcategory.rowOrder }}" />
                        </h2>

                        <div class="grid clearfix">
                            {% for item in itemcategory.items %}
                                <div class="grid-item {% for group in item.groups %}{{ ' group-' ~ group.id }}{% endfor %}{% for role in item.roles %}{{ ' group-' ~ role }}{% endfor %}" data-item-id="{{ item.id }}">
                                    <div class="grid-item-content" style="background-color: {{ item.color ? '#'~item.color : '#'~color['main'] }};">
                                        <span class="item-drag"><i class="fa fa-arrows"></i></span>
                                        <a class="item-link" href="{{ path('cadoles_portal_config_item_update', {"id": item.id}) }}">
                                            <div class="grid-item-logo">
                                                {% if item.icon %}
                                                    <img class="grid-item-img" height="110" src="/{{ alias }}/{{ item.icon.label }}">
                                                {% else %}
                                                    <img class="grid-item-img" src="/{{ alias }}/uploads/icon/icon_pin.png">
                                                {% endif %}
                                            </div>         
                            
                                            <div class="grid-item-title">
                                                <h2>{{ item.title }}</h2>  
                                                {{ item.subtitle|nl2br }}
                                                
                                            </div>                    
                                        </a>
                                    </div>
                        
                                    <input type="hidden" name="item[id][]" value="{{ item.id }}" />
                                    <input type="hidden" name="item[rowOrder][]" value="{{ item.rowOrder }}" />
                                    <input type="hidden" name="item[itemcategory][]" value="{{ item.itemcategory.id }}" />
                                </div>
                            {% endfor %}
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>


        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <i class="fa fa-filter"></i>
                    Afficher par Rôle
                </div>
                <div class="panel-body">
                    <ul id="group-filter" class="list-group">
                        <a class="active list-group-item" id="allrole" style="cursor:pointer">
                            <i class="fa fa-users"></i>
                            Toutes les Rôles
                            <span class="badge"></span>
                        </a>

                        <a class="list-group-item" data-role="ROLE_ANONYME" style="cursor:pointer">
                            <i class="fa fa-user"></i>
                            Visiteur
                            <span class="badge"></span>
                        </a>

                        <a class="list-group-item" data-role="ROLE_USER" style="cursor:pointer">
                            <i class="fa fa-user"></i>
                            Utilisateur
                            <span class="badge"></span>
                        </a>                        

                        <a class="list-group-item" data-role="ROLE_ANIM" style="cursor:pointer">
                            <i class="fa fa-user"></i>
                            Animateur de Groupe
                            <span class="badge"></span>
                        </a>  

                        <a class="list-group-item" data-role="ROLE_MODO" style="cursor:pointer">
                            <i class="fa fa-user"></i>
                            Modérateur
                            <span class="badge"></span>
                        </a>                        

                        <a class="list-group-item" data-role="ROLE_ADMIN" style="cursor:pointer">
                            <i class="fa fa-user"></i>
                            Administrateur
                            <span class="badge"></span>
                        </a>                        
                    </ul>
                </div>
            </div>


            <div class="panel panel-default">
                <div class="panel-heading">
                    <i class="fa fa-filter"></i>
                    Afficher par Groupe
                </div>
                <div class="panel-body">
                    <ul id="group-filter" class="list-group">
                        <a class="active list-group-item" style="cursor:pointer" id="allgroup">
                            <i class="fa fa-users"></i>
                            Toutes les Groupes
                            <span class="badge"></span>
                        </a>
                
                        {% for group in groups %}
                        <a class="list-group-item" data-role="{{ group.id }}" style="cursor:pointer">
                            <i class="fa fa-user"></i>
                            {{ group.label }}
                            <span class="badge">{{ group.items|length }}</span>
                        </a>
                    {% endfor %}
                    </ul>
                </div>
            </div>
        </div>
    </div>

{% endblock %}

{% block localjavascript %}
    $( function() {
        var changes = false;

        $("#allrole").children("span").html($('.grid-item').length);
        $("#allgroup").children("span").html($('.grid-item').length);

        $("a[data-role='ROLE_ANONYME']").children("span").html($('.group-ROLE_ANONYME').length);
        $("a[data-role='ROLE_USER']").children("span").html($('.group-ROLE_USER').length);
        $("a[data-role='ROLE_ANIM']").children("span").html($('.group-ROLE_ANIM').length);
        $("a[data-role='ROLE_MODO']").children("span").html($('.group-ROLE_MODO').length);
        $("a[data-role='ROLE_ADMIN']").children("span").html($('.group-ROLE_ADMIN').length);

        function updateItems() {
            // Pour chaque catégorie
            $('.category-list-item').each(function(i) {
                // On récupère id et order
                var categoryid = $(this).data('itemcategory-id');
                $(this).find('input[name="itemcategory[rowOrder][]"]').val(i);

                // Mise à jour en base de l'order
                $.ajax({
                    method: "POST",
                    url: "{{ path('cadoles_portal_config_itemcategory_order') }}",
                    data: {
                        id:categoryid,
                        order:i
                    }                   
                });

                // Pour chaque item de la catégorie
                $(this).find('.grid-item').each(function(j) {
                    // On récupère les id
                    var itemid = $(this).data('item-id');
                    $(this).find('input[name="item[rowOrder][]"]').val(j);
                    $(this).find('input[name="item[itemcategory][]"]').val(categoryid);

                    // Mise à jour en base de l'order
                    $.ajax({
                        method: "POST",
                        url: "{{ path('cadoles_portal_config_item_order') }}",
                        data: {
                            id:itemid,
                            categoryid:categoryid,
                            order:j
                        }                   
                    });                    
                });
            });
        }

        $( ".grid" ).sortable({
            connectWith: ".grid",
            placeholder: "grid-item placeholder",
            update: updateItems
        });

        $( ".category-list" ).sortable({
            axis: "y",
            placeholder: "category-list-item placeholder",
            update: updateItems
        });

        $('a.list-group-item').click(function(){
            $('#group-filter a.list-group-item').removeClass('active');
            $(this).addClass('active');
            console.log('.group-'+$(this).attr('data-role'));
            if($(this).attr('data-role')){
                $('.grid-item').hide();
                $('.group-'+$(this).attr('data-role')).show();
            } else {
                $('.grid-item').show();
            }
        });
    });
{% endblock %}

