{{template "top" .}}
<style type="text/css">
    .line { width:100%; text-align:left; border-bottom: 1px solid #8f8f8f; line-height:0.1em; margin:10px 0 20px; color: #8f8f8f; }
    .line span { background:#fff; padding:0 10px; margin-left: 50px; }
</style>
<link rel="stylesheet" href="/public/croppie.css" />
<script src="/public/croppie.js"></script>
<div class="container" style="margin-top: 100px;">
    <div class="col-md-12" style="display: none" id="waiting">
        <h1>New Gallery</h1>
        <p>Please Wait ....</p>
    </div>
    <div class="col-md-12" id="newGallery">
        <h1>New Gallery</h1>
        <p class="line"><span>About your new Gallery</span></p>

        <div class="form-floating mb-3">
            <input type="text" name="username" class="form-control " id="floatingInput" placeholder="Name">
            <label for="floatingInput">Name</label>
        </div>
        <p class="line"><span>Gallery preview Image</span></p>
        <div class="row">
            <div class="col-md-2">
                <button class="btn btn-outline-primary" id="selectImage" onClick="onClickHandler(event)">Select Preview 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>
            </div>
        </div>
        <div>

        </div>
        <!--<p class="line" style="cursor: pointer" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample"><span>Optional Settings <i class="fas fa-caret-down" style="height: 10px;"></i></span></p>
        <div class="collapse" id="collapseExample">
            <div class="form-floating mb-3">
                <input type="text" name="username" class="form-control" id="asd" placeholder="Name">
                <label for="floatingInput">Name</label>
            </div>
        </div>!-->
        <button class="btn btn-success" id="create" style="width: 100%">Create</button>
        <script>
            var el = document.getElementById('vanilla-demo');
            var vanilla = new Croppie(el, {
                viewport: { width: 500, height: 500 },
                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 createGallery() {

                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 ("/gallery/new", {
                    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 = "/file/upload?id="+data.id
                }).catch((response) => {
                    console.error("Error POST REQUEST")
                    console.error(response)
                });
            }

            function validate() {
                r = true;
                if (document.getElementById('floatingInput').value == "") {
                    document.getElementById('floatingInput').className = "form-control is-invalid";
                    r = false;
                }

                return r
            }

            create.onclick = evt => {
                if (validate() == false) {
                    return;
                }
                createGallery();
            }

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