{% extends 'base.html.twig' %}

{% block pagewrapper %}
    <h1 class="page-title">
        Gestion des Annonces
    </h1>

    <div class="row">
        <div class="col-md-9">
            <p>
                <a href="{{ path('app_portal_config_alert_submit') }}" class="btn btn-success">Ajouter une Annonce</a>
                <a href="{{ path('app_portal_config_alertcategory_submit') }}" class="btn btn-success">Ajouter une Catégorie</a>                
            </p>
            
            <div id="listpending" class="card">
                <div class="card-header">
                    <h3 style="margin:0px"> <i class="fa fa-clock"></i> En Attente Publication</h2>
                </div>

                <div class="card-body">
                    <div class="list-group list-group-flush">
                        {% for alert in alerts|filter(alert => alert.isPending) %}
                            <div
                                class="
                                    card
                                    list-item
                                    alertcategory-{{ alert.alertcategory.id }}
                                    {{ alert.isOnline ? "alertstatut-online" : ""}}
                                    {{ alert.isPending ? "alertstatut-pending" : ""}}
                                    {{ alert.isArchived ? "alertstatut-archived" : ""}}
                                    {% for group in alert.groups %}{{ ' group-' ~ group.id }}{% endfor %}
                                    {% for role in alert.roles %}{{ ' role-' ~ role }}{% endfor %}
                                    "
                                data-alert-category-id="{{ alert.alertcategory.id }}"
                                data-alert-id="{{ alert.id }}"
                                style="background:  {{ alert.alertcategory.color ? '#'~alert.alertcategory.color : '#'~color['main'] }};"
                            >
                                <div class="card-header" role="tab" style="border-bottom:0px;">
                                    <a href="{{ path('app_portal_config_alert_update', { id: alert.id }) }}" class="card-header-icon pull-left">
                                        {% if alert.alertcategory.icon %}
                                            <img height="25" src="{{ asset(alert.alertcategory.icon.label) }}">
                                        {% else %}
                                            <img height="25" src="{{ asset('uploads/icon/icon_megaphone.png') }}">
                                        {% endif %}                                
                                    </a>
                            
                                    <a href="{{ path('app_portal_config_alert_update', { id: alert.id }) }}">
                                        <i class="fa fa-arrows-v" style="color: rgba(255,255,255,.5);"></i>
                                        {{ alert.title }}
                                    </a>
                            
                                    <a role="button" data-bs-toggle="collapse" data-parent="#accordion" href="#alert-{{ alert.id }}" aria-expanded="true" aria-controls="collapseOne" class="card-header-chevron float-end">
                                        <i class="fa fa-chevron-down"></i>
                                    </a>
                                </div>
                        
                                <div id="alert-{{ alert.id }}" class="card-body card-collapse collapse">
                                    {{ alert.content|raw }}
                                </div>
                                
                                <input type="hidden" name="alert_item[id][]" value="{{ alert.id }}" />
                            </div>
                        {% endfor %}           
                    </div>
                </div>
            </div>

            <div id="listonline" class="card">
                <div class="card-header">
                    <h3 style="margin:0px"><i class="fa fa-eye"></i> En Ligne</h2>
                </div>

                <div class="card-body">
                    <div class="list-group list-group-flush">
                        {% for alert in alerts|filter(alert => alert.isOnline) %}
                            <div
                                class="
                                    card
                                    list-item
                                    alertcategory-{{ alert.alertcategory.id }}
                                    {{ alert.isOnline ? "alertstatut-online" : ""}}
                                    {{ alert.isPending ? "alertstatut-pending" : ""}}
                                    {{ alert.isArchived ? "alertstatut-archived" : ""}}
                                    {% for group in alert.groups %}{{ ' group-' ~ group.id }}{% endfor %}
                                    {% for role in alert.roles %}{{ ' role-' ~ role }}{% endfor %}
                                    "
                                data-alert-category-id="{{ alert.alertcategory.id }}"
                                data-alert-id="{{ alert.id }}"
                                style="background:  {{ alert.alertcategory.color ? '#'~alert.alertcategory.color : '#'~color['main'] }};"
                            >
                                <div class="card-header" role="tab" style="border-bottom:0px;">
                                    <a href="{{ path('app_portal_config_alert_update', { id: alert.id }) }}" class="card-header-icon pull-left">
                                        {% if alert.alertcategory.icon %}
                                            <img height="25" src="{{ asset(alert.alertcategory.icon.label) }}">
                                        {% else %}
                                            <img height="25" src="{{ asset('uploads/icon/icon_megaphone.png') }}">
                                        {% endif %}                                
                                    </a>
                            
                                    <a href="{{ path('app_portal_config_alert_update', { id: alert.id }) }}">
                                        <i class="fa fa-arrows-v" style="color: rgba(255,255,255,.5);"></i>
                                        {{ alert.title }}
                                    </a>
                            
                                    <a role="button" data-bs-toggle="collapse" data-parent="#accordion" href="#alert-{{ alert.id }}" aria-expanded="true" aria-controls="collapseOne" class="card-header-chevron float-end">
                                        <i class="fa fa-chevron-down"></i>
                                    </a>
                                </div>
                        
                                <div id="alert-{{ alert.id }}" class="card-body collapse">
                                    {{ alert.content|raw }}
                                </div>
                                
                                <input type="hidden" name="alert_item[id][]" value="{{ alert.id }}" />
                            </div>
                        {% endfor %}
                    </div>
                </div>
            </div>

            <div id="listarchived" class="card">
                <div class="card-header">
                    <h3 style="margin:0px"><i class="fa fa-archive"></i> Archivées</h2>
                </div>

                <div class="card-body">
                    <div class="list-group list-group-flush">
                        {% for alert in alerts|filter(alert => alert.isArchived)  %}
                            <div
                                class="
                                    card
                                    list-item
                                    alertcategory-{{ alert.alertcategory.id }}
                                    {{ alert.isOnline ? "alertstatut-online" : ""}}
                                    {{ alert.isPending ? "alertstatut-pending" : ""}}
                                    {{ alert.isArchived ? "alertstatut-archived" : ""}}
                                    {% for group in alert.groups %}{{ ' group-' ~ group.id }}{% endfor %}
                                    {% for role in alert.roles %}{{ ' role-' ~ role }}{% endfor %}
                                    "
                                data-alert-category-id="{{ alert.alertcategory.id }}"
                                data-alert-id="{{ alert.id }}"
                                style="background:  {{ alert.alertcategory.color ? '#'~alert.alertcategory.color : '#'~color['main'] }};"
                            >
                                <div class="card-header" role="tab" style="border-bottom:0px;">
                                    <a href="{{ path('app_portal_config_alert_update', { id: alert.id }) }}" class="card-header-icon pull-left">
                                        {% if alert.alertcategory.icon %}
                                            <img height="25" src="{{ asset(alert.alertcategory.icon.label) }}">
                                        {% else %}
                                            <img height="25" src="{{ asset('uploads/icon/icon_megaphone.png') }}">
                                        {% endif %}                                
                                    </a>
                            
                                    <a href="{{ path('app_portal_config_alert_update', { id: alert.id }) }}">
                                        <i class="fa fa-arrows-v" style="color: rgba(255,255,255,.5);"></i>
                                        {{ alert.title }}
                                    </a>
                            
                                    <a role="button" data-bs-toggle="collapse" data-parent="#accordion" href="#alert-{{ alert.id }}" aria-expanded="true" aria-controls="collapseOne" class="card-header-chevron float-end">
                                        <i class="fa fa-chevron-down"></i>
                                    </a>
                                </div>
                        
                                <div id="alert-{{ alert.id }}" class="card-body card-collapse collapse">
                                    {{ alert.content|raw }}
                                </div>
                                
                                <input type="hidden" name="alert_item[id][]" value="{{ alert.id }}" />
                            </div>
                        {% endfor %}
                    </div>            
                </div>
            </div>
        </div>

        <div class="col-md-3">
            <div class="card">
                <div class="card-header">
                    <i class="fa fa-filter"></i>
                    Afficher par Catégorie
                </div>

                <ul id="category-filter" class="list-group list-group-flush">
                    {% for alertcategory in alertcategorys %}
                        <div>
                            <a class="btn btn-primary float-end" href="{{ path('app_portal_config_alertcategory_update', { id: alertcategory.id }) }}" style="height:43.27px; border-radius:0px">
                                <i class="fa fa-edit"></i>
                            </a>
            
                            <a href="#" class="list-group-item active category-alert-item" data-category="{{ alertcategory.id }}" style="border-left: 15px solid {{ alertcategory.color ? '#'~alertcategory.color : '#'~color['main'] }}; position: inherit;">
                                {% if alertcategory.icon %}
                                    <img height="25" src="{{ asset(alertcategory.icon.label) }}">
                                {% else %}
                                    <img height="25" src="{{ asset('uploads/icon/icon_megaphone.png') }}">
                                {% endif %}

                                {{ alertcategory.label }}
                            </a>
                        </div>
                    {% endfor %}
                </ul>
            </div>

            <div class="card">
                <div class="card-header">
                    <i class="fa fa-filter"></i>
                    Afficher par statut
                </div>
        
                <ul id="statut-filter" class="list-group list-group-flush">
                    <a class="list-group-item active filterfgall" data-statut="all" href="#">
                        <i class="fa fa-eye-slash"></i>
                        Toutes
                    </a>
                    
                    <a class="list-group-item" data-statut="online"  href="#">
                        <i class="fa fa-eye"></i>
                        En ligne
                    </a>

                    <a class="list-group-item" data-statut="pending"  href="#">
                        <i class="fa fa-clock"></i>
                        En attente de publication
                    </a>

                    <a class="list-group-item" data-statut="archived"  href="#">
                        <i class="fa fa-archive"></i>
                        Archivées
                    </a>
                </ul>
            </div>

            <div class="card">
                <div class="card-header">
                    <i class="fa fa-filter"></i>
                    Afficher par Rôle
                </div>

                <ul id="role-filter" class="list-group list-group-flush">
                    <a class="active list-group-item filterfgall" id="allrole" style="cursor:pointer">
                        <i class="fa fa-users"></i>
                        Toutes les Rôles
                        <span class="badge bg-secondary float-end">{{ alerts|length }}</span>
                    </a>

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

                    <a class="list-group-item" data-role="ROLE_USER" style="cursor:pointer">
                        <i class="fa fa-user"></i>
                        Utilisateur
                        <span class="badge bg-secondary float-end"></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 bg-secondary float-end"></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 bg-secondary float-end"></span>
                    </a>                        

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

            <div class="card">
                <div class="card-header">
                    <i class="fa fa-filter"></i>
                    Afficher par Groupes
                </div>

                <ul id="group-filter" class="list-group list-group-flush">
                    <a href="#" class="active list-group-item filterfgall">
                        <i class="fa fa-users"></i>
                        Tout les groupes
                        <span class="badge bg-secondary float-end">{{ alerts|length }}</span>
                    </a>
                    
                    {% for group in groups %}
                        <a class="list-group-item" href="#" data-role="{{ group.id }}">
                            <i class="fa fa-user"></i>
                            {{ group.label }}
                            <span class="badge bg-secondary float-end">{{ group.alerts|length }}</span>
                        </a>
                    {% endfor %}
                </ul>
            </div>

        </div>
    </div>
{% endblock %}

{% block localjavascript %}
    $( function() {
        $("a[data-role='ROLE_ANONYME']").children("span").html($('.role-ROLE_ANONYME').length);
        $("a[data-role='ROLE_USER']").children("span").html($('.role-ROLE_USER').length);
        $("a[data-role='ROLE_ANIM']").children("span").html($('.role-ROLE_ANIM').length);
        $("a[data-role='ROLE_MODO']").children("span").html($('.role-ROLE_MODO').length);
        $("a[data-role='ROLE_ADMIN']").children("span").html($('.role-ROLE_ADMIN').length);

        function updateItems() {
            $('.list-item').each(function(i) {
                var itemid = $(this).data('alert-id');
                $(this).find('input[name="alert_item[rowOrder][]"]').val(i);

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

            });
        }

        if($(".alertstatut-pending").length==0) $("#listpending").hide();
        if($(".alertstatut-online").length==0) $("#listonline").hide();
        if($(".alertstatut-archived").length==0) $("#listarchived").hide();

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

        $("#category-filter a.category-alert-item").click(function(){
            // On réactive les auters filtres à tout
            $('#role-filter a').removeClass('active');
            $('#group-filter a').removeClass('active');
            $('#statut-filter a').removeClass('active');
            $(".filterfgall").addClass('active');
            $(this).toggleClass('active');

            $('.category-alert-item').each(function(i) {
                var $alerts = $('.alertcategory-'+$(this).data('category'));
                if($(this).hasClass('active')){
                    $alerts.show();
                } else {
                    $alerts.hide();
                }
            });

            return false;
        });

        $('#role-filter a').click(function(){
            // On réactive les auters filtres à tout
            $(".category-alert-item").addClass('active');
            $('#group-filter a').removeClass('active');
            $('#statut-filter a').removeClass('active');
            $(".filterfgall").addClass('active');
            
            // On rend actif la zone cliquée
            $('#role-filter a').removeClass('active');
            $(this).addClass('active');

            // On cache l'ensemble des elements
            $(".list-item").hide();       

            // On affiche les elements filtrés
            if($(this).hasClass('filterfgall'))
                var $alerts = $('.list-item');
            else 
                var $alerts = $('.role-'+$(this).data('role'));
            
            $alerts.show();

            return false;
        });

        $('#group-filter a').click(function(){
            // On réactive les auters filtres à tout
            $(".category-alert-item").addClass('active');
            $('#role-filter a').removeClass('active');
            $('#statut-filter a').removeClass('active');
            $(".filterfgall").addClass('active');
            
            // On rend actif la zone cliquée
            $('#group-filter a').removeClass('active');
            $(this).addClass('active');

            // On cache l'ensemble des elements
            $(".list-item").hide();       

            // On affiche les elements filtrés
            if($(this).hasClass('filterfgall'))
                var $alerts = $('.list-item');
            else 
                var $alerts = $('.group-'+$(this).data('role'));
            
            $alerts.show();

            return false;
        });

        $("#statut-filter a").click(function(){
            // On réactive les auters filtres à tout
            $(".category-alert-item").addClass('active');
            $('#group-filter a').removeClass('active');
            $("#group-filter .filterfgall").addClass('active');
            
            // On rend actif la zone cliquée
            $('#statut-filter a').removeClass('active');
            $(this).addClass('active');

            // On cache l'ensemble des elements
            $(".list-item").hide();       

            // On affiche les elements filtrés
            if($(this).hasClass('filterfgall'))
                var $alerts = $('.list-item');
            else             
                var $alerts = $('.alertstatut-'+$(this).data('statut'));

            $alerts.show();
            return false;
        });

    } );
{% endblock %}


