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

{% block localstyle %}
    #page-wrapper {
        min-height:0px !important;
    }

    .container-fluid {
        padding:0px;
    }

    #explorer {
        margin-right: -15px;
        margin-left: -15px;
    }

    .grid .grid-list .grid-item-logo {
        margin:0px;
    }

    .grid .grid-list .grid-item-title h2 {
        font-size: 14px;
        line-height: 18px;
        height: 18px
    }    

    .grid .grid-list .grid-item-title {
        padding:0px 0px 0px 5px;
        line-height: 10px;
    }

    .grid-item-title h2 {
        text-transform: initial !important;
    }

    a.item-update {
        right: 25px;
    }

    

{% endblock %}

{% block pagewrapper %}

    <div id="explorer">
        <div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups" style="margin-top:5px">
            {% if canupdate and not fgtrash %}
                <div class="btn-group mr-2" role="group" aria-label="First group">
                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#mymodal" onClick="ModalLoad('mymodal','Téléchargez vos Fichiers','{{ path('cadoles_core_'~access~'_file_upload',{'id': directory,'type':'all','subdirectory':subdirectory}) }}');" title="Ajouter des fichiers">
                        <i class="fa fa-upload"></i>
                    </button>
                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#mymodal" onClick="ModalLoad('mymodal','Création Répertoire','{{ path('cadoles_core_'~access~'_file_subdirectory',{'id': directory,'type':'all','subdirectory':subdirectory}) }}');" title="Ajouter un Répertoire">
                        <i class="fa fa-folder-plus"></i>
                    </button>
                </div>
            {% endif %}

            <div class="btn-group mr-2" role="group" aria-label="Second group">
                {% if view=="small" %}
                    {%set action="list" %}
                {% else %}
                    {%set action="small" %}
                {% endif %}
                {% if not subdirectory is empty %}
                    <a href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory,'subdirectory':subdirectory,'view':action }) }}" type="button" class="btn btn-primary">
                {% else %}
                    <a href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory,'clear':true,'view':action }) }}" type="button" class="btn btn-primary">
                {% endif %}
                {% if view=="small" %}
                    <i class="fas fa-list"></i>
                {% else %}
                    <i class="fas fa-th"></i>
                {% endif %}
                </a>
                
                {% set active="" %}
                {% if sort=="name" %}
                    {% set active="active" %}
                {%endif%}

                {% if order=="SORT_ASC" %}
                    {%set action="SORT_DESC" %}
                {% else %}
                    {%set action="SORT_ASC" %}
                {% endif %}
                {% if not subdirectory is empty %}
                    <a href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory,'subdirectory':subdirectory,'sort':'name', 'order':action }) }}" type="button" class="btn btn-primary  {{active}}">
                {% else %}
                    <a href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory,'clear':true,'sort':'name', 'order':action }) }}" type="button" class="btn btn-primary {{active}}">
                {% endif %}
                {% if order=="SORT_ASC" %}
                    <i class="fas fa-long-arrow-alt-down"></i> Nom
                {% else %}
                    <i class="fas fa-long-arrow-alt-up"></i> Nom
                {% endif %}
                </a>

                {% set active="" %}
                {% if sort=="date" %}
                    {% set active="active" %}
                {%endif%}

                {% if order=="SORT_ASC" %}
                    {%set action="SORT_DESC" %}
                {% else %}
                    {%set action="SORT_ASC" %}
                {% endif %}
                {% if not subdirectory is empty %}
                    <a href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory,'subdirectory':subdirectory,'sort':'date', 'order':action }) }}" type="button" class="btn btn-primary  {{active}}">
                {% else %}
                    <a href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory,'clear':true,'sort':'date', 'order':action }) }}" type="button" class="btn btn-primary {{active}}">
                {% endif %}
                {% if order=="SORT_ASC" %}
                    <i class="fas fa-long-arrow-alt-down"></i> Date
                {% else %}
                    <i class="fas fa-long-arrow-alt-up"></i> Date
                {% endif %}
                </a>
            </div>

            {% if canupdate %}
            <div class="btn-group mr-2" role="group" aria-label="Third group">
                <a href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory,'subdirectory':'__trash' }) }}" type="button" class="btn btn-primary"><i class="fas fa-trash"></i></a>
                {% if fgtrash %}
                <a href="{{ path('cadoles_core_'~access~'_file_purgetrash',{'directory':directory }) }}" type="button" class="btn btn-primary" data-method="delete" data-confirm="Souhaitez-vous purger votre poubelle ? Cette action sera irrévocable.">Purger la poubelle</a>
                {% endif %}
            </div>
            {% endif %}
        </div>


        <div aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item directory" data-dir="{{directory}}" data-name=""><a href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory, 'clear':true }) }}">Home</a></li>
                {% set sub = "" %}
                {% set datadir = directory %}
                {% for parent in parents %}
                    {% if loop.index==1 %}
                        {% set sub = parent %}
                    {% else %}
                        {% set datadir = datadir~'/'~sub %}
                        {% set sub = sub~"/"~parent %}
                    {% endif %}

                    {% set tmpdirname = parent|replace({'__trash': 'Poubelle'}) %}
                    <li class="brea.dcrumb-item directory" data-dir="{{datadir}}" data-name="{{ parent }}"><a href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory,'subdirectory':sub }) }}">{{tmpdirname|raw}}</a></li>
                {% endfor %}
                
                {% if not subdirectory is empty %}
                    {% set tmpdirname = dirname|replace({'__trash': 'Poubelle'}) %}
                    <li class="breadcrumb-item directory" data-dir="{{directory}}/{{sub}}" data-name="{{dirname}}"><a href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory,'subdirectory':subdirectory }) }}">{{tmpdirname|raw}}</a></li>
                {% endif %}

            </ol>
        </div>        
        
        <div class="grid clearfix">
            <div class="grid-sizer  grid-{{view}}"></div>
            <div class="grid-gutter-sizer"></div>           



            {% for dir in dirs %}
                <div id="{{dir.name|e('js')|raw }}" class="grid-item grid-{{view}} directory" data-dir="{{directory}}/{{subdirectory}}" data-name="{{dir.name}}">
                    <div class="grid-item-content">
                        {% if canadd %}
                            {% if fgtrash %}
                                <a  style="cursor:pointer;" onClick="restaureFile('{{ directory }}','{{subdirectory}}','{{dir.name|e('js')|raw }}')" class="item-restaure">
                                    <i class="fa fa-undo" title="Restaurer le fichier"></i>
                                </a>

                                <a  style="cursor:pointer;" onClick="delFile('{{ directory }}','{{subdirectory}}','{{dir.name|e('js')|raw }}')" class="item-delete">
                                    <i class="fa fa-trash" title="Supprimer le dossier"></i>
                                </a>
                            {% else %}
                                <a  style="cursor:pointer;" data-toggle="modal" data-target="#mymodal" onClick="ModalLoad('mymodal','Modification Répertoire','{{ path('cadoles_core_'~access~'_file_rename',{'id': directory,'subdirectory':subdirectory,'oldname':dir.name}) }}');" class="item-update">
                                    <i class="fa fa-file" title="Renommer le dossier"></i>
                                </a>

                                <a  style="cursor:pointer;" onClick="trashFile('{{ directory }}','{{subdirectory}}','{{dir.name|e('js')|raw }}')" class="item-delete">
                                    <i class="fa fa-trash" title="Supprimer le dossier"></i>
                                </a>
                            {% endif %}
                        {% endif %}

                        {% if subdirectory is empty %}
                            {% set link = dir.name %}
                        {% else %}
                            {% set link = subdirectory~"/"~dir.name %}
                        {% endif %}
                        <a style="cursor:pointer" href="{{ path('cadoles_core_'~access~'_file_list',{'directory':directory,'subdirectory':link }) }}">
                            <div class="item-link clearfix">
                                <div class="grid-item-logo">
                                    <img class="grid-item-img" height="110" src="/{{ alias }}/bundles/cadolescore/images/files/dir.png">
                                </div>

                                <div class="grid-item-title">
                                    <h2>{{ dir.name }}</h2>{% if view=="list" %}<small>le {{ dir.date }}</small>{%endif%}
                                </div>                    
                            </div>
                        </a>
                    </div>
                </div>
            {% endfor %}

            {% for file in files %}
                <div class="grid-item grid-{{view}} file" data-dir="{{directory}}/{{subdirectory}}" data-name="{{file.name}}">
                    <div class="grid-item-content">
                        {% if canadd %}
                            {% if fgtrash %}
                                <a  style="cursor:pointer;" onClick="restaureFile('{{ directory }}','{{subdirectory}}','{{file.name|e('js')|raw }}')" class="item-restaure">
                                    <i class="fa fa-undo" title="Restaurer le fichier"></i>
                                </a>

                                <a  style="cursor:pointer;" onClick="delFile('{{ directory }}','{{subdirectory}}','{{file.name|e('js')|raw }}')" class="item-delete">
                                    <i class="fa fa-trash" title="Supprimer le fichier"></i>
                                </a>
                            {% else %}
                                <a  style="cursor:pointer;" data-toggle="modal" data-target="#mymodal" onClick="ModalLoad('mymodal','Modification Fichier','{{ path('cadoles_core_'~access~'_file_rename',{'id': directory,'subdirectory':subdirectory,'oldname':file.name}) }}');" class="item-update">
                                    <i class="fa fa-file" title="Renommer le fichier"></i>
                                </a>

                                <a  style="cursor:pointer;" onClick="trashFile('{{ directory }}','{{subdirectory}}','{{file.name|e('js')|raw }}')" class="item-delete">
                                    <i class="fa fa-trash" title="Supprimer le fichier"></i>
                                </a>
                            {% endif %}
                        {% endif %}

                        {% if file.minefamily=="text" or file.minefamily=="image" or file.minetype == "application/pdf" %}
                            <a style="cursor:pointer" onClick="showFrameitem('file-{{iddirectory}}-{{loop.index}}','{{ path('cadoles_core_'~access~'_file_view',{'directory':directory,'subdirectory':subdirectory,'filename':file.name }) }}')">
                        {% else %}
                            <a style="cursor:pointer" href="{{ path('cadoles_core_'~access~'_file_download',{'directory':directory,'subdirectory':subdirectory,'filename':file.name }) }}">
                        {% endif %}
                        <div class="item-link clearfix">
                            <div class="grid-item-logo">
                                {% if file.thumb is not empty %}
                                    <img class="grid-item-img" height="110" src="{{ file.thumb }}">
                                {% else %}
                                    <img class="grid-item-img" height="110" src="/{{ alias }}/bundles/cadolescore/images/files/_blank.png">
                                {% endif %}
                            </div>

                            <div class="grid-item-title">
                                <h2>{{ file.name }}</h2>{% if view=="list" %}<small>le {{ file.date }}</small>{%endif%}
                            </div>                    
                        </div>
                        </a>
                    </div>
                </div>
            {% endfor %}
        </div>
    </div>
{% endblock %}


{% block localjavascript %}
    var optiongrid={columnWidth: '.grid-sizer',itemSelector: '.grid-item', gutter: '.grid-gutter-sizer'};
    var formodalglobal;
    var dirsource;
    var namesource;
    var domsource;

    // Création des grilles d'items
    $('body').imagesLoaded(function() {
        var grid = $('.grid').masonry(optiongrid);        
        ajust();
    });

    $('document').ready(function(){
        if(inIframe()) {
            color=$("#frame-{{directory}}",window.parent.document).data("color");
        }
        else {
            color = "#{{ color["main"]}}";
        }
        $(".item-update").css("color",color);
        $(".item-update > i").css("color",color);
        $(".item-delete").css("color",color);
        $(".item-delete > i").css("color",color);
        $(".item-restaure").css("color",color);
        $(".item-restaure > i").css("color",color);        
        $(".grid-item-title > h2").css("color",color);

        $('#mymodal').on('shown.bs.modal', function() { 
            $("#frame-{{directory}}",window.parent.document).height($("#mymodal").find(".modal-content").height()+80);
        });

        $('#mymodal').on('hide.bs.modal', function() { 
            $("#frame-{{directory}}",window.parent.document).height($("#page-wrapper").height() + 10);
        });

        
        {% if canupdate and not fgtrash %}
        $('.file').on('dragstart', function (s) {
            dirsource=$(this).data("dir");
            namesource=$(this).data("name");
            domsource=$(this);
        });    

        $('.directory').on('dragstart', function (s) {
            dirsource=$(this).data("dir");
            namesource=$(this).data("name");
            domsource=$(this);
        });    


        $('.directory')
            // crucial for the 'drop' event to fire
            .on('dragover', false) 

            .on('drop', function (e) {
                // Récupérer la destination
                dirdestination=$(this).data("dir")+"/"+$(this).data("name");
                namedestination=namesource;
                
                urlsource=dirsource+"/"+namesource;
                urlsource=urlsource.replace("//","/");
                urldestination=dirdestination+"/"+namedestination;
                urldestination=urldestination.replace("//","/");

                // Si la source est la meme que la destination on ne fait rien
                if(urlsource==urldestination) return false;

                // Si la source est dans la destination on ne fait rien
                //if(urldestination.includes(urlsource)) return false;

                // On déplace l'element
                $.ajax({
                    method: "POST",
                    url: "{{ path('cadoles_core_'~access~'_file_move') }}",
                    data: {
                        directory:'{{directory}}',
                        dirsource:dirsource,
                        namesource:namesource,
                        dirdestination:dirdestination,
                        namedestination:namedestination
                    },
                    success: function() {
                        domsource.remove();
                        var grid = $('.grid').masonry(optiongrid);     
                        ajust();                        
                    }            
                });  

                return false;
        });
        {% endif %}
                        
        
    });

    $(window).resize(function() {
        ajust();
    });  


    function ajust(formodal) {
        if(inIframe()) {
            frame=$("#frame-{{directory}}",window.parent.document);
            widgetbody=frame.parent();

            if(!frame.hasClass("notframeajust")&&widgetbody.css("display")=="block")  {
                if($('#mymodal').hasClass('in')) {
                    $("#frame-{{directory}}",window.parent.document).height($("#mymodal").find(".modal-content").height()+80);
                }
                else {
                    $("#frame-{{directory}}",window.parent.document).height($("#page-wrapper").height() + 10);
                }
            }           
        }
    }

    {% if canupdate %}
    // Trasher un fichier 
    function trashFile(directory,subdirectory,filename) {
        var r = confirm("Confirmez-vous la mise à la poubelle de ce fichier ? Vous pourrez le récupèrer si besoin.");
        if (r == true) {        
            $.ajax({
                method: "POST",
                url: "{{ path('cadoles_core_'~access~'_file_trash') }}",
                data: {
                    directory:directory,
                    subdirectory:subdirectory,
                    filename:filename
                },
                success: function() {
                    location.reload();
                }            
            });        
        }
    }

    // Restaurer un fichier 
    function restaureFile(directory,subdirectory,filename) {
        var r = confirm("Confirmez-vous la restauration de ce fichier ?");
        if (r == true) {        
            $.ajax({
                method: "POST",
                url: "{{ path('cadoles_core_'~access~'_file_restaure') }}",
                data: {
                    directory:directory,
                    subdirectory:subdirectory,
                    filename:filename
                },
                success: function() {
                    location.reload();
                }            
            });        
        }
    }

    // Supprimer un fichier 
    function delFile(directory,subdirectory,filename) {
        var r = confirm("Confirmez-vous la suppression définitive de ce fichier ?");
        if (r == true) {        
            $.ajax({
                method: "POST",
                url: "{{ path('cadoles_core_'~access~'_file_delete') }}",
                data: {
                    directory:directory,
                    subdirectory:subdirectory,
                    filename:filename
                },
                success: function() {
                    location.reload();
                }            
            });        
        }
    }
    {% endif %}


    // Affichage des frames associés aux items de bureau
    function showFrameitem(id,url,forcereload) {
        if(inIframe()) {
            parent.showFrameitem(id,url,forcereload);
        }
        else {
            $("#explorer").hide();
            $("body").css("overflow-y","hidden");

            if($("#frameitem-"+id).length) {
                if(forcereload) {
                    $("#frameitem-"+id).attr("src",url);
                }
                $("#frameitem-"+id).show();
            }
            else {
                $("#explorer").before("<iframe id='frameitem-"+id+"' class='frameitem' src='"+url+"'></iframe>");
            }
        }
    }    
{% endblock %}



