{{template "top" .}}
<link rel="stylesheet" href="/public/croppie.css" />
<script src="/public/croppie.js"></script>
<div class="container"  style="margin-top: 100px;">
<div class="row">
    <div class="col-md-3" style="display: none">
        <div class="list-group">
            <a href="#" class="list-group-item list-group-item-action active" aria-current="true">
                Profile
            </a>
            <!--<a href="#" class="list-group-item list-group-item-action">Sicherheit</a>
            <a class="list-group-item list-group-item-action disabled">A disabled link item</a>!-->
        </div>
    </div>
    <div class="col-md-12">
        <h1>Change Profile Image</h1>
        <div class="row">
            <div class="col-md-2">
                <button class="btn btn-outline-primary" id="selectImage" onClick="onClickHandler(event)" style="width: 100%">Upload Image</button>
            </div>
            <div class="col-md-10">
                <!--<img class="my-image" style="display: none;" id="galleryImage" src="demo/demo-1.jpg" />!-->

                <div id="vanilla-demo"></div>

                <button class="btn btn-success" id="create" style="width: 100%">Create</button>
            </div>
        </div>
        <script>
            var el = document.getElementById('vanilla-demo');
            var vanilla = new Croppie(el, {
                viewport: { width: 500, height: 500, type: 'circle' },
                boundary: { width: '100%', height: el.offsetWidth / 3 * 2},
                showZoomer: false,
                enableOrientation: true,
                url: '/public/dariusz-sankowski-mj2NwYH3wBA-unsplash.jpg'
            });

            var orginal = null;
            var orginalFile = null;

            function onClickHandler(ev) {
                console.log("Upload new Image")
                var el = window._protected_reference = document.createElement("INPUT");
                el.type = "file";
                el.accept = "image/*";
                el.multiple = "multiple"; // remove to have a single file selection
                el.addEventListener('change', function(ev2) {
                    if (el.files.length) {
                        vanilla.bind({
                            url: URL.createObjectURL(el.files[0])
                        });
                        orginalFile = el.files[0]
                    }
                });
                el.click();
            }

            async function changeProfilePic() {

                console.log(vanilla.get())
                res = await vanilla.result({
                    type: "blob",
                    circle: false,
                    format: "png",
                    size: "original",
                });
                let formData = new FormData();
                //formData.append("name", document.getElementById('floatingInput').value)
                formData.append("orginal", orginalFile);
                formData.append("cropImage", res);
                formData.append("cropData", JSON.stringify(vanilla.get()));
                //document.getElementById("newGallery").style.display = "none"
                //document.getElementById("waiting").style.display="inline"
                fetch ("/settings/profile/image", {
                    method: "POST",
                    body: formData,
                    //contentType: false, //this is requireded please see answers above
                    //processData: false, //this is requireded please see answers above
                    enctype: 'multipart/form-data',
                }).then (response => response.json()).then ((data) => {
                    console.log(data)
                    location.href = "settings/profile"
                }).catch((response) => {
                    console.error("Error POST REQUEST")
                    console.error(response)
                });
            }


            create.onclick = evt => {
                changeProfilePic();
            }

        </script>
    </div>
</div>
</div>
{{template "bottom" .}}