<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ltsNinja - Raccourcisseur d'URL</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link rel="icon" href="/static/img/logo.svg">

    <script>
        tailwind.config = {
            theme: {
                extend: {
                    boxShadow: {
                        'rainbow': '0 0 10px rgba(255, 0, 0, 0.2), 0 0 20px rgba(255, 165, 0, 0.2), 0 0 30px rgba(255, 255, 0, 0.2), 0 0 40px rgba(0, 255, 0, 0.2), 0 0 50px rgba(0, 0, 255, 0.2), 0 0 60px rgba(75, 0, 130, 0.2), 0 0 70px rgba(238, 130, 238, 0.2)',
                        'color': '0 0 15px rgba(99, 102, 241, 0.4)'
                    }
                }
            }   
        }
    </script>
</head>

<body class="min-h-screen flex flex-col items-center justify-center p-4 bg-[url('/static/img/hero.svg')] bg-cover">
    <img src="/static/img/logo.svg" width="128" height="128" class="mb-4" alt="Links Ninja">

    
    <div class="bg-white/0 ring-1 ring-black/5 rounded-3xl p-8 w-full max-w-2xl backdrop-blur-md">
        <div class="bg-white rounded-2xl p-6 shadow mb-6">
            <form id="shorten-form" class="space-y-4">
                <input type="url" id="url" name="url" placeholder="📋 Paste here your long link" required
                       class="w-full px-4 py-2 rounded-full border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blue-300">
                
                <div class="flex space-x-4">
                    <input type="text" id="custom-name" name="custom_name" placeholder="{{if .loggedIn}} my-custom-name {{else}} Login to enable custom names ✨ {{end}}" {{if not .loggedIn}}disabled{{end}}
                           class="flex-grow px-4 py-2 rounded-full border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blue-300" >
                    <button type="submit"
                            class="px-6 py-2 bg-gradient-to-r from-pink-400 to-red-400 text-white rounded-full shadow hover:from-pink-500 hover:to-red-500 focus:outline-none focus:ring-2 focus:ring-red-300 transition duration-300 ease-in-out transform hover:scale-105">
                        Generate 📬
                    </button>
                </div>
            </form>
        </div>
        
        <div id="result" class="hidden p-4 mb-4 text-sm text-green-800 rounded-lg bg-green-50 dark:bg-gray-800 dark:text-green-400" role="alert">
            <span class="font-medium">📨 URL Generated!</span>
            <span id="result-link"></span>
            <button id="copy-btn" class="ml-2 px-2 py-1 bg-blue-500 text-white rounded hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-300">Copy</button>
        </div>
        <div id="error" class="hidden p-4 mb-4 text-sm text-red-800 rounded-lg bg-red-50 dark:bg-gray-800 dark:text-red-400" role="alert">
            <span class="font-medium">⚠️ An error occured!</span>
            <span id="error-content"></span>
        </div>
        
        <div class="mt-6 flex justify-between items-center">
            {{if .loggedIn}}
                <a href="/dashboard"
                   class="px-6 py-2 bg-blue-400 text-white rounded-full focus:outline-none focus:ring-2 focus:ring-green-300 transition duration-300 ease-in-out transform hover:scale-105">
                    Dashboard
                </a>
                <a href="/logout" class="px-6 py-2 bg-purple-400 text-white rounded-full focus:outline-none focus:ring-2 focus:ring-purple-300 transition duration-300 ease-in-out transform hover:scale-105">
                    Logout
                </a>
            {{else}}
                <a href="/login"
                   class="px-6 py-2 bg-blue-400 text-white rounded-full focus:outline-none focus:ring-2 focus:ring-blue-300 transition duration-300 ease-in-out transform hover:scale-105">
                    <i class="fab fa-github"></i> Login using GitHub
                </a>
            {{end}}
        </div>
    </div>

    <footer class="w-full text-center py-4 mt-8">
        <p class="text-sm text-gray-600">
            &copy; 2024 ltsNinja. Made with ❤️ - 
            <a href="https://github.com/itsmrval/ltsninja" target="_blank" rel="noopener noreferrer" class="text-blue-500 hover:text-blue-700 transition duration-300">
                Source code
            </a>
        </p>
    </footer>

    <script>
        const errorBox = document.getElementById('error');
        const errorContent = document.getElementById('error-content');
        const resultBox = document.getElementById('result');
        const resultLink = document.getElementById('result-link');
        document.getElementById('shorten-form').addEventListener('submit', function(e) {
            e.preventDefault();
            fetch('/', {
                method: 'POST',
                body: new FormData(this)
            })
            .then(response => response.json())
            .then(data => {
                this.reset();
                errorBox.classList.add('hidden');
                resultBox.classList.add('hidden');
                if (data.error) { 
                    errorContent.textContent = data.error;
                    errorBox.classList.remove('hidden');
                    return;
                }

                const shortURL = window.location.origin + '/' + data.shortURL;
                resultLink.textContent = shortURL;
                resultLink.setAttribute('data-url', shortURL);
                resultBox.classList.remove('hidden');
            });
        });

        document.getElementById('copy-btn').addEventListener('click', function() {
            const resultLink = document.getElementById('result-link');
            const url = resultLink.getAttribute('data-url');
            navigator.clipboard.writeText(url).then(() => {
                this.textContent = 'Copied!';
                setTimeout(() => {
                    this.textContent = 'Copy';
                }, 1000);
            })
        });
    </script>
</body>
</html>
