<!DOCTYPE html>
<html lang="en">
<head>
    <title>GoCMS | Post edition</title>
    {{.Head}}
</head>
<body class="text-dark vh-100 d-flex flex-column">
{{.Navbar}}
<form class="container mt-3 text-black-50 d-flex flex-column h-100" action="edit" method="post">
    <div class="d-flex align-items-center justify-content-between">
        <div>
            <h1>Post - edition</h1>
            <label for="postBody">Edition</label>
        </div>
        {{ if .Alert.Message }}
        <div class="alert {{ if .Alert.IsError }} alert-danger {{ else }} alert-success {{ end }} alert-dismissible"
             role="alert">
            {{ .Alert.Message }}
            <button type="button" class="btn-close" aria-label="Close" data-bs-dismiss="alert"></button>
        </div>
        {{ end }}
        <div class="d-flex justify-content-center align-items-center">
            <button class="btn btn-success d-flex justify-content-center align-items-center gap-1" style="fill: white;"
                    type="submit">
                <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24">
                    <path d="M840-680v480q0 33-23.5 56.5T760-120H200q-33 0-56.5-23.5T120-200v-560q0-33 23.5-56.5T200-840h480l160 160Zm-80 34L646-760H200v560h560v-446ZM480-240q50 0 85-35t35-85q0-50-35-85t-85-35q-50 0-85 35t-35 85q0 50 35 85t85 35ZM240-560h360v-160H240v160Zm-40-86v446-560 114Z"/>
                </svg>
                Save
            </button>
        </div>
    </div>
    <div class="text-black-50 d-flex align-items-center justify-content-center h-100 py-3">
        <textarea id="postBody" name="postBody">{{.Post.Body}}</textarea>
    </div>
</form>

<script src="/static/tinymce/js/tinymce/tinymce.min.js"></script>
<script>
    tinymce.init({
        selector: '#postBody',
        promotion: false,
        plugins: 'image',
        width: '100%',
        height: '100%',
        setup: function (editor) {
            editor.on('init', function () {
                console.log("LOADED")
            });
        },
        license_key: 'gpl',
        images_upload_handler: (blobInfo, progress) => new Promise((resolve, reject) => {
            const xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
            xhr.open('POST', `/post/{{.Post.ID}}/image/create`);

            xhr.upload.onprogress = (e) => {
                progress(e.loaded / e.total * 100);
            };

            xhr.onload = () => {
                if (xhr.status === 403) {
                    reject({message: 'HTTP Error: ' + xhr.status, remove: true});
                    return;
                }

                if (xhr.status < 200 || xhr.status >= 300) {
                    reject('HTTP Error: ' + xhr.status);
                    return;
                }

                const json = JSON.parse(xhr.responseText);

                if (!json || typeof json.location != 'string') {
                    reject('Invalid JSON: ' + xhr.responseText);
                    return;
                }

                resolve(`http{{ if .Secured }}s{{ end }}://${window.location.host}${json.location}`);
            };

            xhr.onerror = () => {
                reject('Image upload failed due to a XHR Transport error. Code: ' + xhr.status);
            };

            const formData = new FormData();
            formData.append('file', blobInfo.blob(), blobInfo.filename());

            xhr.send(formData);
        }),
    })
</script>
</body>
</html>
