<table cellspacing="0">
    <tr class="personalise-logo-row-container">
        <td>
            {{ form_widget(form.logoChoice[0]) }}
            {{ form_label(form.logoChoice[0]) }}
            <div class="image-container">
                <img id="original-image-placeholder" alt="gabari"
                     src="/assets/common/images/logo.png"/>
            </div>

        </td>
        <td>
            {{ form_widget(form.logoChoice[1]) }}
            {{ form_label(form.logoChoice[1]) }}
            <br>
            <span id="help-text">{{ 'admin::setup:personalisation_logo: help text' | trans }}</span>
            <br>
            <a id="download-image-template" href="#">{{ 'admin::setup:personalisation_logo: download gabari' | trans }}</a>
            <br>
            <div class="image-container" id="personalize-image-container">
                {% if app['conf'].get(['registry', 'general', 'personalize-logo-choice', 'personalizeFile']) == 'true' %}
                    {% set extension = app['conf'].get(['registry', 'general', 'personalize-logo-choice', 'fileType']) %}
                    {% set personalise_path = '/custom/minilogos/personalize_logo.' ~ extension %}

                    <img id="personalise-image-placeholder" src="{{ personalise_path }}"/>
                {% endif %}
            </div>
            <div>
                {{ form_widget(form.personalizeLogoInput) }}
                <label for="general_personalize-logo-choice_personalizeLogoInput"
                       id="select-logo-btn">{{ 'admin::setup:personalisation_logo: select file' | trans }}</label>
            </div>
            <span id="error-text"></span>
        </td>
        {{ form_widget(form.personalizeFile) }}
        {{ form_widget(form.fileType) }}
    </tr>
</table>

<script type="text/javascript">
    $(document).ready(function () {
        var fileToUpload = null;

        var errorDimension = '{{ 'admin::setup:personalisation_logo: error text dimension' | trans }}';
        var errorFileType = '{{ 'admin::setup:personalisation_logo: error text file type' | trans }}';

        $('#help-text').show();

        // show personalize logo if exist
        {% if app['conf'].get(['registry', 'general', 'personalize-logo-choice', 'personalizeFile']) == 'true' %}
            $('#personalize-image-container').show();
        {% else %}
            $('#personalize-image-container').hide();
        {% endif %}

        $('#error-text').hide();

        $("#download-image-template").on('click', function (event) {
            event.preventDefault();
            var imageInBase64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAApCAYAAAGJsd0WAAAAAXNSR0IArs4c6QAAB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/F3sJ3hr4bVAy3lrlv+b1mtxuZVaAP4InpMw/MXgOd6fa1UHQv4GvDnkz0RmG3gEHAfPAZUvAuXGqxn77oc6gcf4VORFTp5oHwVX8TH81zTwZZ5w00h7HkyHPwrMxta5xB3oPUD8G3A2j/pP8fz7LWEjIGS76BFUzMM4/x6N+xz0brwtUx4Tv0xFXyXeDErDPSMdAbqWd/Gz8NbTqN3ewcm3Bn4ucjXgh4SXILsbFOoNjiObQyM9i9BYdNmxbyHlzeXDA7n8cAL/PKiHtwmUjuF8u1dpQsmXh95TYExGRoZNGpLZ6i1kpTMPBX+ncy4TWaPA2y46aDZQAdFMbVfozY+h5A1GlYw4ly2kW6mU4fCyoS3Q8Yy4EsKraAgLdAnxCvi9Br4MkZH9DqOtgU61n3CvlgG9AvnbCrathHcL+C8d4ro+nXJrib8ATuGvLheJr8a3XOIy40wTJI+MZkPBudMyVGNEmVE1ELIp2igHB70e17Q46GvCpBUQaWCTNqxyK9LAqiZMSv8DHKUE8H/9oh8AAAAASUVORK5CYII=';
            var mimeInfo = base64MimeType(imageInBase64);
            var ext = mimeInfo.split('/').pop();
            var filename = $('#original-image-placeholder').attr('alt') + "." + ext;
            download(imageInBase64, filename, mimeInfo);
        });

        var base64MimeType = function base64MimeType(encoded) {
            var result = null;
            if (typeof encoded !== 'string') {
                return result;
            }
            var mime = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
            if (mime && mime.length) {
                result = mime[1];
            }
            return result;
        };

        $("#setupForm").fileupload({
            dataType: 'html',
            add: function (e, data) {
                $.each(data.files, function (i, file) {
                    var reader = new FileReader();

                    reader.readAsDataURL(data.files[0]);
                    reader.data = data;
                    reader.file = data.files[0];

                    reader.onload = function (_file) {
                        var image = new Image();

                        fileToUpload = _file.target.result;
                        image.src = _file.target.result;
                        image.file = this.file;
                        image.data = this.data;

                        if (image.file.type != 'image/png' && image.file.type!= 'image/svg+xml') {
                            $('#general_personalize-logo-choice_personalizeLogoInput').val("");
                            $('#error-text').text(errorFileType);
                            $('#error-text').show();
                            /*$('#help-text').hide();*/
                            $('#personalize-image-container').hide();
                            return false;
                        }

                        image.onload = function () {
                            var w = this.width,
                                h = this.height;

                            var ext = this.file.type.split('/').pop().toLowerCase();

                            if (h > 41 || w > 120 ) {
                                $('#error-text').text(errorDimension);
                                $('#error-text').show();
                                /*$('#help-text').hide();*/
                                $('#personalize-image-container').hide();
                                $('#general_personalize-logo-choice_personalizeLogoInput').val("");
                                return false;
                            }

                            require([
                                "blueimp.loadimage"
                            ], function (loadImage) {
                                loadImage(file, function (img) {
                                    $('#personalize-image-container').show();
                                    $('#error-text').hide();
                                    /*$('#help-text').hide();*/
                                    $('#personalize-image-container').empty().append(img);
                                }, {
                                    maxSize: 5242880, // 5MB
                                    maxWidth: 120,
                                    maxHeight: 41,
                                });
                            });

                            var fd = new FormData();
                            fd.append("fileToUpload", fileToUpload);
                            $.ajax({
                                type: 'POST',
                                url: '{{ path('setup_send_personalize_logo') }}',
                                data: fd,
                                processData: false,
                                contentType: false,
                                async: false,
                                success: function (data) {
                                    if(data === 'success'){
                                        $('#general_personalize-logo-choice_personalizeFile').val(true);
                                        if(ext==='svg+xml'){
                                            ext = 'svg';
                                        }
                                        $('#general_personalize-logo-choice_fileType').val(ext);
                                    }else{
                                        $('#general_personalize-logo-choice_personalizeFile').val(false);
                                    }

                                   /* console.log(data);*/
                                }

                            });
                        }
                    }

                });

                return false;
            },
            submit: function (e, data) {
            },
            done: function (e, data) {
            }
        });

    });

</script>



