<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Password Protected URL - URL Slicer</title>
    <link rel="stylesheet" href="/styles.css">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
    <div class="app-container">
        <main class="content">
            <div class="panel">
                <h1>Password Protected URL</h1>
                <p>This URL is password protected. Please enter the password to access the content.</p>
                <form id="password-form">
                    <div class="form-group">
                        <label for="password">Password:</label>
                        <input type="password" id="password" name="password" required>
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
                <p id="error-message" class="error" style="display: none;"></p>
            </div>
        </main>
    </div>

    <script>
        $(document).ready(function() {
            let attempts = 0;
            const maxAttempts = 5;
            let lockoutTime = null;

            $('#password-form').submit(function(e) {
                e.preventDefault();

                if (lockoutTime && new Date() < lockoutTime) {
                    $('#error-message').text('Too many failed attempts. Please try again later.').show();
                    return;
                }

                const password = $('#password').val();

                $.ajax({
                    url: '/<%= code %>/verify',
                    method: 'POST',
                    data: { password: password },
                    success: function(response) {
                        if (response.success) {
                            window.location.href = response.redirectUrl;
                        }
                    },
                    error: function(xhr) {
                        attempts++;
                        if (attempts >= maxAttempts) {
                            lockoutTime = new Date(Date.now() + 5 * 60 * 1000); // 5 minutes from now
                            $('#error-message').text('Too many failed attempts. Please try again in 5 minutes.').show();
                        } else {
                            $('#error-message').text('Incorrect password. Please try again.').show();
                        }
                    }
                });
            });
        });
    </script>
</body>
</html>