{% set theme = app.session.get('theme') %}
{% if theme is not empty %}
    {{ include('@Theme/'~theme~'/function.html.twig') }}
{% endif %}

{% import "@CadolesPortal/Pagewidget/constants.twig" as constants %}

{% set colormain = constants.mycolormain() %}
{% set stylewidget = constants.mystylewidget(entity) %}
{% set stylewidgetmenu = constants.mystylewidgetmenu(entity) %}
{% set stylewidgetheader = constants.mystylewidgetheader(entity) %}
{% set stylewidgetbody = constants.mystylewidgetbody(entity) %}
{% set color = app.session.get('color') %}

{% if modedesktop==0 %}
    {% set stylegrid="" %}
{% elseif modedesktop==1 %}
    {% set stylegrid="grid-medium" %}
{% elseif modedesktop==2 %}
    {% set stylegrid="grid-small" %}
{% elseif modedesktop==3 %}
    {% set stylegrid="grid-list" %}    
{% endif %}

{% set colorbodyfont = "" %}
{% if entity.colorbodyfont is not null %}
    {% set colorbodyfont = "color: #" ~ entity.colorbodyfont %}
{% endif %}

{% if app.user %}
    {% set username = app.user.username %}
{% else %}
    {% set username = "" %}
{% endif %}

{%if mini %}
    <div class="widget widget-mini widget-project" data-id="{{ entity.id }}" loc="{{ entity.loc }}">
        <div class="widgetheader">
            <a style="cursor:pointer" onClick="bigWidget({{entity.id}})">
            {% if entity.icon %}
                <img src="/{{ alias }}/{{ entity.icon.label }}" class="logo" title="{{ entity.name }}"/>
            {% else %}
                <img src="/{{ alias }}/uploads/icon/icon_pin.png" class="logo" title="{{ entity.name }}"/>
            {% endif %}
            </a>
        </div>
    </div>    
{% else %}
    <div class="widget {%if entity.border %} widget-bordered {%else%} widget-notbordered {%endif%} widget-item" data-id="{{ entity.id }}" loc="{{ entity.loc }}" style="{{ stylewidget }}" height="{{ entity.height }}px">
        <div class="widgetmenu">
            {% if canadd %}
            <i class="fas fa-text-height" title="Changer taille des items" onClick="changeWidget({{ entity.id }},'modedesktop',{{modedesktop}})" style="{{ stylewidgetmenu }}"></i>
            {% endif %}

            {% if canupdate %}
                <i class="fa fa-trash fa-fw" title="Supprimer le Widget" onClick="delWidget({{ entity.id }})" style="{{ stylewidgetmenu }}"></i>
                <i class="fa fa-file fa-fw" title="Modifier le Widget" onClick="modWidget({{ entity.id }})" style="{{ stylewidgetmenu }}"></i>
            {% endif %}

            {% if canadd and access!="config"%}
                <i class="fa fa-plus fa-fw" title="Ajouter un Favori" onClick="addBookmark({{ entity.id }},true)" style="{{ stylewidgetmenu }}"></i>
            {% endif %}

            {% if look=="list" %}
            <i class="glyphicon glyphicon-resize-small" title="Réduire le Widget" onClick="smallWidget({{ entity.id }})" style="{{ stylewidgetmenu }}"></i>
            {% else %}
            <i class="glyphicon glyphicon-resize-full" title="Agrandir le Widget" onClick="bigWidget({{ entity.id }})" style="{{ stylewidgetmenu }}"></i>
            {% endif %}               
        </div>

        <div class="widgetheader" style="{{ stylewidgetheader }}">
            {% if entity.icon %}
                <img src="/{{ alias }}/{{ entity.icon.label }}" class="logo"/>
            {% else %}
                <img src="/{{ alias }}/uploads/icon/icon_pin.png" class="logo"/>
            {% endif %}
            <span class="title">{{ entity.name }}</span>
        </div>

        <div class="widgetbody" style="{{ stylewidgetbody }}">
            {% set mycategs = [] %}
            {% for itemcategory in itemcategorys %}
                {% set haveitem=false %}

                {% for item in items if item.itemcategory==itemcategory %}

                    {% if loop.index ==1 %}
                        {% set mycategs = mycategs|merge({ (loop.index) : itemcategory}) %}
                    {% endif %}
                {% endfor %}
            {% endfor %}


            {% if menu %}
                {% if menuall %}
                    <div class="cat-list-feed" onClick="showItemCat({{ entity.id}},'all')" style="color:#{{ color['fontcolorhover'] }}; background-color: #{{ color['main'] }}">Tout</div>
                {% endif %}

                {% if bookmarks is not empty %}
                    <div class="cat-list-feed" onClick="showItemCat({{ entity.id}},'fav')" style="color:#{{ color['fontcolorhover'] }}; background-color: #{{ color['main'] }}"><i class="fa fa-heart"></i></div>
                {% endif %}

                {% for itemcategory in mycategs %}
                    <div class="cat-list-feed" onClick="showItemCat({{ entity.id}},{{itemcategory.id}})" style="color:#{{ color['fontcolorhover'] }}; background-color: #{{ color['main'] }}">{{itemcategory.label}}</div>               
                {% endfor %}                    
            {% endif %}

            {% if search %}
                <div class="input-group" style="margin-top:10px">
                    <span class="input-group-addon" id="basic-addon1"><i class="fa fa-search"></i></span>
                    <input onKeyup="searchItem({{ entity.id}},$(this).val());" type="text" id="searchitems" name="searchitems" class="form-control" placeholder="Recherche" aria-describedby="basic-addon1">
                </div>          
            {% endif %}

            {% if (bookmarks is not empty or (canadd and access!="config")) and withbookmark==0 %}
                <div class="bookmark-container">
                    <h3 class="grid-title bookmark-container" data-idcategory="bookmark" style="{{ colorbodyfont }}">{{app.session.get('labelbookmarkuser')}}</h3>
                    <div class="grid clearfix">
                        <div class="grid-sizer {{ stylegrid }}" data-idcategory="bookmark"></div>
                        <div class="grid-gutter-sizer {{ stylegrid }}-gutter-sizer"></div>
                        {% for bookmark in bookmarks if not bookmark.item %}
                            <div class="grid-item {{ stylegrid }}" data-idcategory="bookmark" data-title="{{ bookmark.title|lower }}">
                                <div class="grid-item-content" style="background-color: {{ bookmark.color ? "#"~bookmark.color : '#'~colormain }};">
                                    <a  style="cursor:pointer" onClick="modBookmark({{ bookmark.id }})" class="item-update"><i style="color: #FFF" class="fa fa-file" title="Modifier le favori"></i></a>
                                    <a  style="cursor:pointer"  onClick="delBookmark({{ bookmark.id }})"  class="item-heart"><i style="color: #FFF" class="fa fa-trash" title="Supprimer le favori"></i></a>

                                    {% if bookmark.target == 'frame' %}
                                        <a style="cursor:pointer" onClick="showFrameitem('bookmark{{ bookmark.id }}','{{ bookmark.url|replace({'#login#': username}) }}')">
                                    {% elseif bookmark.target == "_self" %}
                                        <a href="{{ bookmark.url|replace({'#login#': username}) }}" target="{% if access=="user" %}_top{% else %}{{ bookmark.target }}{% endif %}">
                                    {% else %}
                                        <a href="{{ bookmark.url|replace({'#login#': username}) }}" target="{{ bookmark.target }}">
                                    {% endif %}

                                    <div class="item-link clearfix">
                                        <div class="grid-item-logo" title="{{ bookmark.subtitle|nl2br }}">
                                            {% if bookmark.icon %}
                                                <img class="grid-item-img" src="/{{ alias }}/{{ bookmark.icon.label }}">
                                            {% else %}
                                                <img class="grid-item-img" src="/{{ alias }}/uploads/icon/icon_pin.png">
                                            {% endif %}
                                        </div>

                                        <div class="grid-item-title">
                                            <h2>{{ bookmark.title }}</h2>
                                            <span>{{ bookmark.subtitle|nl2br }}</<span>
                                        </div>                    
                                    </div>
                                    </a>
                                </div>
                            </div>
                        {%endfor%}


                        {% if (canadd and access!="config") %}
                            <div class="grid-item {{ stylegrid }}">
                                <div title="Ajouter un Favori" onClick="addBookmark({{ entity.id }},true)" class="grid-item-content" style="background-color: #{{color['main']}};cursor:pointer;">
                                    <div class="item-link clearfix">
                                        <div class="grid-item-logo">
                                            <img class="grid-item-img imageshadow" height="110" src="/{{ alias }}/uploads/icon/icon_add.png">
                                        </div>
                                        <div class="grid-item-title">
                                            <h2>Ajouter</h2>
                                            <span>Ajouter un Favori</<span>
                                        </div>                              
                                    </div>
                                </div>
                            </div>                    
                        {% endif %}
                    </div>

                    {% set haveressource=false %}
                    {% for bookmark in bookmarks if bookmark.item %}
                        {% if loop.first %}
                            {% set haveressource=true %}
                            <h3 class="grid-title bookmark-container" data-idcategory="bookmark" style="{{ colorbodyfont }}">{{app.session.get('labelbookmarkitem')}}</h3>
                            <div class="grid clearfix">
                                <div class="grid-sizer {{ stylegrid }}" data-idcategory="bookmark"></div>
                                <div class="grid-gutter-sizer {{ stylegrid }}-gutter-sizer"></div>
                        {%endif%}

                        <div class="grid-item {{ stylegrid }}" data-idcategory="bookmark" data-title="{{ bookmark.item.title|lower }}">
                            <div class="grid-item-content" style="background-color: {{ bookmark.item.color ? "#"~bookmark.item.color : '#'~colormain }};">
                                {% if bookmark.item.content %}
                                <a  style="cursor:pointer" class="item-preview"><i style="color: #FFF" class="fa fa-info" title="Informations sur ce service"></i></a>
                                {% endif %}
                                {% if canadd %}
                                <a  style="cursor:pointer"  onClick="delBookmark({{ bookmark.id }})"  class="item-heart"><i style="color: #FFF" class="fa fa-trash" title="Supprimer des favoris"></i></a>
                                {% endif %}

                                {% set datasonde = "" %}
                                {% if bookmark.item and bookmark.item.clicksonde %}
                                    {% set datasonde = "class='linktosonde' data-sondetitle='"~bookmark.item.title~"' data-sondeservice='"~bookmark.item.clicksondeservice~"'" %}
                                {% endif %}

                                {% if bookmark.item.target == 'frame' %}
                                    <a {{datasonde|raw}} style="cursor:pointer" onClick="showFrameitem('bookmark{{ bookmark.item.id }}','{{ bookmark.item.url|replace({'#login#': username}) }}')">
                                {% elseif bookmark.target == "_self" %}
                                    <a {{datasonde|raw}} data-sonde="{{ bookmark.item.title }}" href="{{ bookmark.item.url|replace({'#login#': username}) }}" target="{% if access=="user" %}_top{% else %}{{ bookmark.item.target }}{% endif %}">
                                {% else %}
                                    <a {{datasonde|raw}} href="{{ bookmark.item.url|replace({'#login#': username}) }}" target="{{ bookmark.item.target }}">
                                {% endif %}

                                <div class="item-link clearfix">
                                    <div class="grid-item-logo" title="{{ bookmark.item.subtitle|nl2br }}">
                                        {% if bookmark.item.icon %}
                                            <img class="grid-item-img" src="/{{ alias }}/{{ bookmark.item.icon.label }}">
                                        {% else %}
                                            <img class="grid-item-img" src="/{{ alias }}/uploads/icon/icon_pin.png">
                                        {% endif %}
                                    </div>

                                    <div class="grid-item-title">
                                        <h2>{{ bookmark.item.title }}</h2>
                                        <span>{{ bookmark.item.subtitle|nl2br }}</span>
                                        {% if not bookmark.item.badgeurl is empty %}
                                            <div class="itembadgeurl badge badge-pill" data-url="{{bookmark.item.badgeurl}}" style="background-color: #ffffff; color: {{ bookmark.item.color ? "#"~bookmark.item.color : '#'~colormain }};"></div>
                                        {% endif %}                                         
                                    </div>                    
                                </div>
                                </a>

                                <div class="grid-item-body" style="display:none">
                                {{ bookmark.item.content|raw }}
                                </div>                                    
                            </div>
                        </div>
                    {%endfor%}

                    {% if haveressource %}
                        </div>
                    {%endif%}                    
                </div>
            {% endif %}

            {% if (bookmarks is not empty or (canadd and access!="config")) and withbookmark==2 %}
                <div class="bookmark-container">
                    <div class="grid clearfix">
                        <div class="grid-sizer {{ stylegrid }}" data-idcategory="bookmark"></div>
                        <div class="grid-gutter-sizer {{ stylegrid }}-gutter-sizer"></div>
                        {% for bookmark in bookmarks %}
                            {% if bookmark.item %}
                                <div class="grid-item {{ stylegrid }}" data-idcategory="bookmark" data-title="{{ bookmark.item.title|lower }}">
                                    <div class="grid-item-content" style="background-color: {{ bookmark.item.color ? "#"~bookmark.item.color : '#'~colormain }};">
                                        {% if bookmark.item.content %}
                                        <a  style="cursor:pointer" class="item-preview"><i style="color: #FFF" class="fa fa-info" title="Informations sur ce service"></i></a>
                                        {% endif %}
                                        {% if canadd %}
                                        <a  style="cursor:pointer"  onClick="delBookmark({{ bookmark.id }})"  class="item-heart"><i style="color: #FFF" class="fa fa-trash" title="Supprimer des favoris"></i></a>
                                        {% endif %}

                                        {% set datasonde = "" %}
                                        {% if bookmark.item and bookmark.item.clicksonde %}
                                            {% set datasonde = "class='linktosonde' data-sondetitle='"~bookmark.item.title~"' data-sondeservice='"~bookmark.item.clicksondeservice~"'" %}
                                        {% endif %}

                                        {% if bookmark.item.target == 'frame' %}
                                            <a {{datasonde|raw}} style="cursor:pointer" onClick="showFrameitem('bookmark{{ bookmark.item.id }}','{{ bookmark.item.url|replace({'#login#': username}) }}')">
                                        {% elseif bookmark.target == "_self" %}
                                            <a {{datasonde|raw}} href="{{ bookmark.item.url|replace({'#login#': username}) }}" target="{% if access=="user" %}_top{% else %}{{ bookmark.item.target }}{% endif %}">
                                        {% else %}
                                            <a {{datasonde|raw}} href="{{ bookmark.item.url|replace({'#login#': username}) }}" target="{{ bookmark.item.target }}">
                                        {% endif %}

                                        <div class="item-link clearfix">
                                            <div class="grid-item-logo" title="{{ bookmark.item.subtitle|nl2br }}">
                                                {% if bookmark.item.icon %}
                                                    <img class="grid-item-img" src="/{{ alias }}/{{ bookmark.item.icon.label }}">
                                                {% else %}
                                                    <img class="grid-item-img" src="/{{ alias }}/uploads/icon/icon_pin.png">
                                                {% endif %}
                                            </div>

                                            <div class="grid-item-title">
                                                <h2>{{ bookmark.item.title }}</h2>
                                                <span>{{ bookmark.item.subtitle|nl2br }}</span>
                                                {% if not bookmark.item.badgeurl is empty %}
                                                    <div style="margin-top:5px;text-align:center;{% if stylegrid == "grid-list"%} float: right;position: absolute;top: 10px;right: -50px; {%endif%}">
                                                        <div class="itembadgeurl badge badge-pill" data-url="{{bookmark.item.badgeurl}}" style="background-color: #ffffff; color: {{ bookmark.color ? "#"~bookmark.color : '#'~colormain }};"></div>
                                                    </div>
                                                {% endif %}                                                    
                                            </div>                    
                                        </div>
                                        </a>

                                        <div class="grid-item-body" style="display:none">
                                        {{ bookmark.item.content|raw }}
                                        </div>                                    
                                    </div>
                                </div>
                            {%else%}
                                <div class="grid-item {{ stylegrid }}" data-idcategory="bookmark" data-title="{{ bookmark.title|lower }}">
                                    <div class="grid-item-content" style="background-color: {{ bookmark.color ? "#"~bookmark.color : '#'~colormain }};">
                                        <a  style="cursor:pointer" onClick="modBookmark({{ bookmark.id }})" class="item-update"><i style="color: #FFF" class="fa fa-file" title="Modifier le favori"></i></a>
                                        <a  style="cursor:pointer"  onClick="delBookmark({{ bookmark.id }})"  class="item-heart"><i style="color: #FFF" class="fa fa-trash" title="Supprimer le favori"></i></a>

                                        {% if bookmark.target == 'frame' %}
                                            <a style="cursor:pointer" onClick="showFrameitem('bookmark{{ bookmark.id }}','{{ bookmark.url|replace({'#login#': username}) }}')">
                                        {% elseif bookmark.target == "_self" %}
                                            <a href="{{ bookmark.url|replace({'#login#': username}) }}" target="{% if access=="user" %}_top{% else %}{{ bookmark.target }}{% endif %}">
                                        {% else %}
                                            <a href="{{ bookmark.url|replace({'#login#': username}) }}" target="{{ bookmark.target }}">
                                        {% endif %}

                                        <div class="item-link clearfix">
                                            <div class="grid-item-logo" title="{{ bookmark.subtitle|nl2br }}">
                                                {% if bookmark.icon %}
                                                    <img class="grid-item-img" src="/{{ alias }}/{{ bookmark.icon.label }}">
                                                {% else %}
                                                    <img class="grid-item-img" src="/{{ alias }}/uploads/icon/icon_pin.png">
                                                {% endif %}
                                            </div>

                                            <div class="grid-item-title">
                                                <h2>{{ bookmark.title }}</h2>
                                                <span>{{ bookmark.subtitle|nl2br }}</span>
                                            </div>                    
                                        </div>
                                        </a>
                                    </div>
                                </div>
                            {% endif %}
                        {% endfor %}

                        {% if (canadd and access!="config") %}
                            <div class="grid-item {{ stylegrid }}">
                                <div title="Ajouter un Favori" onClick="addBookmark({{ entity.id }},true)" class="grid-item-content" style="background-color: #{{color['main']}};cursor:pointer;">
                                    <div class="item-link clearfix">
                                        <div class="grid-item-logo">
                                            <img class="grid-item-img imageshadow" height="110" src="/{{ alias }}/uploads/icon/icon_add.png">
                                        </div>
                                        <div class="grid-item-title">
                                            <h2>Ajouter</h2>
                                            <span>Ajouter un Favori</<span>
                                        </div>                              
                                    </div>
                                </div>
                            </div>                    
                        {% endif %}
                    </div>
                </div>
            {% endif %}


            {% set firstcat=true %}
            {% for itemcategory in mycategs %}
                {% set haveitem=false %}

                {% for item in items if item.itemcategory==itemcategory %}

                    {% if loop.index ==1 %}
                        {% set haveitem=true %}
                        
                        {% if (bookmarks is not empty or (canadd and access!="config")) and (withbookmark==0 or withbookmark==2) %}
                            {% set toview=true %}
                            {% if menu and not menuall%}
                                {% set toview=false %}
                            {% endif %}
                        {% else %}
                            {% if menu and not menuall%}
                                {% if firstcat %}
                                    {% set toview=true %}
                                {% else %}
                                    {% set toview=false %}
                                {% endif %}
                            {% else %}
                                {% set toview=true %}
                            {% endif %}
                        {% endif %}
                        {% set firstcat=false %}

                        {% set style="" %}
                        {% if not toview %}
                            {% set style="display:none;" %}
                        {% endif %}

                        {% if mycategs|length > 1 or ((bookmarks is not empty or (canadd and access!="config")) and (withbookmark==0 or withbookmark==2)) %}
                            <h3 class="grid-title itemcategory-container" style="{{ colorbodyfont }} {{ style }}" data-idcategory="{{ itemcategory.id }}">{{ itemcategory.label }}</h3>
                        {% else %}
                            <p></p>
                        {% endif %}

                        <div class="grid clearfix itemcategory-container" data-idcategory="{{ item.itemcategory.id }}" style="{{ style }}">
                            <div class="grid-sizer {{ stylegrid }}" data-idcategory="{{ item.itemcategory.id }}"></div>
                            <div class="grid-gutter-sizer"></div>
                    {% endif %}
                                    
                        <div class="grid-item {{ stylegrid }}" data-idcategory="{{ item.itemcategory.id }}" data-iditem="{{ item.id }}" data-title="{{ item.title|lower }}">
                            <div class="grid-item-content" style="background-color: {{ item.color ? "#"~item.color : '#'~colormain }};">
                                {% if item.content %}
                                <a  style="cursor:pointer" class="item-preview"><i style="color: #FFF" class="fa fa-info" title="Informations sur ce service"></i></a>
                                {% endif %}
                                {% if canadd %}
                                <a  style="cursor:pointer"  onClick="heartBookmark({{ item.id }})"  class="item-heart"><i style="color: #FFF" class="fa fa-heart" title="Ajouter aux Favoris"></i></a>
                                {% endif %}
                                
                                {% if item.protected and not app.user %}
                                    {% if mode_auth == "SAML" %}
                                        <a href="{{ path('lightsaml_sp.login') }}" {% if access=="user" %}target="_top"{% endif %}>
                                    {% elseif mode_auth == "CAS"  %}
                                        <a href="{{ path('cas_sp.login') }}" {% if access=="user" %}target="_top"{% endif %}>
                                    {% elseif mode_auth == "MYSQL"  %}
                                        <a href="{{ path('cnous_portal_user_login') }}" {% if access=="user" %}target="_top"{% endif %}>
                                {% endif %}
                                
                                {% else %}
                                    {% set datasonde = "" %}
                                    {% if item.clicksonde %}
                                        {% set datasonde = "class='linktosonde' data-sondetitle='"~item.title~"' data-sondeservice='"~item.clicksondeservice~"'" %}
                                    {% endif %}

                                    {% if item.target == 'frame' %}
                                        <a {{datasonde|raw}} style="cursor:pointer" onClick="showFrameitem({{ item.id }},'{{ item.url|replace({'#login#': username}) }}')">
                                    {% elseif item.target == "_self" %}
                                        <a {{datasonde|raw}} href="{{ item.url|replace({'#login#': username}) }}" target="{% if access=="user" %}_top{% else %}{{ item.target }}{% endif %}">
                                    {% else %}
                                        <a {{datasonde|raw}} href="{{ item.url|replace({'#login#': username}) }}" target="{{ item.target }}">
                                    {% endif %}
                                {% endif %}

                                <div class="item-link clearfix">
                                    <div class="grid-item-logo" title="{{ item.subtitle|nl2br }}">
                                        {% if item.icon %}
                                            <img class="grid-item-img" height="110" src="/{{ alias }}/{{ item.icon.label }}">
                                        {% else %}
                                            <img class="grid-item-img" height="110" src="/{{ alias }}/uploads/icon/icon_pin.png">
                                        {% endif %}
                                    </div>

                                    <div class="grid-item-title">
                                        <h2>{{ item.title }}</h2>
                                        <span>{{ item.subtitle|nl2br }}</span>
                                        {% if not item.badgeurl is empty %}
                                            <div style="margin-top:5px;text-align:center;{% if stylegrid == "grid-list"%} float: right;position: absolute;top: 10px;right: -50px; {%endif%}">
                                                <div class="itembadgeurl badge badge-pill" data-url="{{item.badgeurl}}" style="background-color: #ffffff; color: {{ item.color ? "#"~item.color : '#'~colormain }};"></div>
                                            </div>
                                        {% endif %}                                          
                                    </div>                    
                                </div>
                                </a>

                                <div class="grid-item-body" style="display:none">
                                {{ item.content|raw }}
                                </div>
                            </div>
                        </div>
                {% endfor %}
                {% if haveitem %}            
                    </div>
                {% endif %}
            {% endfor %}
        </div>
    </div>
{% endif %}


