<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Eversync</title>
    
    {% load static %}
    <link rel="icon" href="{% static 'favicon.ico' %}">
    <link rel="stylesheet" href="{% static 'index-style.css' %}">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
    <script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>

    <style>
        html, body {
            margin: 0;
            padding: 0;
            height: 100%;
            overflow: hidden; /* prevent scrolling */
        }

        body {
            display: flex;
            flex-direction: column;
        }

        .header {
            height: 80px;
            flex-shrink: 0;
        }

        #editor-container {
            flex: 1;
            margin: 20px;
            background-color: white;
            color: black;
        }

        .editor-controls {
            margin: 0 20px 20px;
        }

        .editor-controls button {
            margin-right: 10px;
            padding: 6px 12px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            background-color: #3a3a3a;
            color: white;
        }

        .editor-controls button:hover {
            background-color: #555;
        }

        .dropdown-menu {
            z-index: 100;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <a href="/"><img src="{% static 'eversync2.png' %}" alt="Eversync Logo" style="height: 80px; margin-right: 10px; display: flex; align-items: center; gap: 5px;"></a>
            <a href="/" class="logo">eversync</a>
            <div class="nav-links" style="position: relative;">
                <div class="dropdown">
                    <button class="dropdown-toggle" style="background: none; border: none; color: white; font-size: 16px; cursor: pointer;">
                        Welcome, {{ user.username }} <i class="fas fa-caret-down"></i>
                    </button>
                    <div class="dropdown-menu" style="display: none; position: absolute; right: 0; background-color: #333; border: 1px solid #444; border-radius: 4px; padding: 10px; width: 184px;">
                        <form action="{% url 'manage' %}" method="post" style="margin: 0;">
                            {% csrf_token %}
                            <button type="submit" class="logout-button" style="background-color: transparent; color: white; border: none; cursor: pointer;">Manage Account</button>
                        </form>
                        <form action="{% url 'logoutz' %}" method="post" style="margin: 0;">
                            {% csrf_token %}
                            <button type="submit" class="logout-button" style="background-color: transparent; color: white; border: none; cursor: pointer;">Log Out</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div id="editor-container"></div>
    <div class="editor-controls">
        <button onclick="copyToClipboard()">Copy</button>
        <button onclick="downloadNote()">Download</button>
        <button onclick="saveDocument()">Save</button>

    </div>

    <script>
        let quill;

        document.addEventListener('DOMContentLoaded', function () {
            quill = new Quill('#editor-container', {
                theme: 'snow',
                placeholder: 'Start writing something beautiful...'
            });

            const toggle = document.querySelector('.dropdown-toggle');
            const menu = document.querySelector('.dropdown-menu');
            toggle.addEventListener('click', function () {
                menu.style.display = menu.style.display === 'block' ? 'none' : 'block';
            });
        });

        function copyToClipboard() {
            const temp = document.createElement('textarea');
            temp.value = quill.root.innerHTML;
            document.body.appendChild(temp);
            temp.select();
            document.execCommand('copy');
            document.body.removeChild(temp);
            alert('Copied to clipboard!');
        }

        function downloadNote() {
            const blob = new Blob([quill.root.innerHTML], { type: 'text/html' });
            const link = document.createElement('a');
            link.href = URL.createObjectURL(blob);
            link.download = 'note.html';
            link.click();
        }
        
        function saveDocument() {
        const title = prompt("Enter document title:");
        const content = quill.root.innerHTML;

        fetch('/save-document/', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': '{{ csrf_token }}'
            },
            body: JSON.stringify({ title: title, content: content })
        })
        .then(response => response.json())
        .then(data => {
            alert("Saved successfully! Document ID: " + data.id);
        })
        .catch(error => {
            alert("Error saving document.");
            console.error(error);
        });
    }
    </script>
</body>
</html>