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

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

{% block pagewrapper %}
    <a class="btn btn-default" onClick="closeModal();">Annuler</a>

    <form action="{{ oneup_uploader_endpoint('file') }}" name="formfile" class="dropzone" id="MyDropZone" style="margin-top:10px">
    </form>
{% endblock %}



{% block localjavascript %}
    Dropzone.options.MyDropZone = {
        {% if type=="image" %}
        acceptedMimeTypes: 'image/*',
        {% endif %}
        init: function() {
            this.on("sending", function(file, xhr, formData) {
                formData.append("directory", "{{ id }}");
                formData.append("subdirectory", "{{ subdirectory|raw }}");
            });

            var totalFiles = 0;
            var completeFiles = 0;
            this.on("addedfile", function (file) {
                totalFiles += 1;
            });

            this.on("removed file", function (file) {
                totalFiles -= 1;
            });
            this.on("complete", function (file) {
                completeFiles += 1;
                if (completeFiles === totalFiles) {
                    {% if forcereload %}
                        parent.document.location.reload();
                    {% else %}
                        parent.refreshPJ();
                        closeModal();
                    {% endif %}
                }
            });            
        },
        success: function( file, response ){
        }
    }

    function closeModal() {
        //window.parent.$("#mymodal").removeClass("in");
        //window.parent.$(".modal-backdrop").remove();        
        window.parent.$("#mymodal").modal('hide');
    }

    $(window).load(function () {
        // On vérifie que l'execution se fait bien dans le cadre d'une modal
        if(!window.parent.$("#mymodal #framemodal").length) {
             $(location).attr('href',"{{ path('cadoles_core_home') }}");
        }

        parent.$("#framemodal").height($("#page-wrapper").height() + 10);
    });

    $(window).resize(function () {
        parent.$("#framemodal").height($("#page-wrapper").height() + 10);
    });
{% endblock %}
