<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OAuth Test Server - Login</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .login-container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
            padding: 40px;
            width: 100%;
            max-width: 400px;
        }
        .logo {
            text-align: center;
            margin-bottom: 30px;
        }
        .logo h1 {
            color: #333;
            font-size: 24px;
            margin-bottom: 8px;
        }
        .logo p {
            color: #666;
            font-size: 14px;
        }
        .client-info {
            background: #f5f5f5;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 25px;
        }
        .client-info h3 {
            color: #333;
            font-size: 14px;
            margin-bottom: 8px;
        }
        .client-info p {
            color: #666;
            font-size: 13px;
            word-break: break-all;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            color: #333;
            font-size: 14px;
            font-weight: 500;
            margin-bottom: 8px;
        }
        .form-group input[type="text"],
        .form-group input[type="password"] {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.2s;
        }
        .form-group input[type="text"]:focus,
        .form-group input[type="password"]:focus {
            outline: none;
            border-color: #667eea;
        }
        .consent-section {
            background: #f9f9f9;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 25px;
        }
        .consent-section h3 {
            color: #333;
            font-size: 14px;
            margin-bottom: 10px;
        }
        .consent-section ul {
            list-style: none;
            margin-bottom: 15px;
        }
        .consent-section li {
            color: #666;
            font-size: 13px;
            padding: 5px 0;
            padding-left: 20px;
            position: relative;
        }
        .consent-section li::before {
            content: "✓";
            position: absolute;
            left: 0;
            color: #667eea;
        }
        .consent-checkbox {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .consent-checkbox input[type="checkbox"] {
            width: 18px;
            height: 18px;
            cursor: pointer;
        }
        .consent-checkbox label {
            color: #333;
            font-size: 14px;
            cursor: pointer;
        }
        .button-group {
            display: flex;
            gap: 12px;
        }
        .btn {
            flex: 1;
            padding: 14px 20px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
        }
        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        .btn-primary:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }
        .btn-secondary {
            background: #e0e0e0;
            color: #333;
        }
        .btn-secondary:hover {
            background: #d0d0d0;
        }
        .error-message {
            background: #fee;
            border: 1px solid #fcc;
            border-radius: 8px;
            padding: 12px 16px;
            margin-bottom: 20px;
            color: #c00;
            font-size: 14px;
        }
        .test-credentials {
            text-align: center;
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #eee;
        }
        .test-credentials p {
            color: #999;
            font-size: 12px;
        }
        .test-credentials code {
            background: #f0f0f0;
            padding: 2px 6px;
            border-radius: 4px;
            font-family: monospace;
        }
        /* Auto-submit timer styles */
        .auto-submit-banner {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 20px;
            text-align: center;
            color: white;
        }
        .auto-submit-banner.cancelled {
            background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
        }
        .auto-submit-banner h3 {
            font-size: 14px;
            margin-bottom: 8px;
        }
        .timer-display {
            font-size: 32px;
            font-weight: bold;
            margin: 10px 0;
        }
        .timer-progress {
            width: 100%;
            height: 6px;
            background: rgba(255,255,255,0.3);
            border-radius: 3px;
            overflow: hidden;
            margin: 10px 0;
        }
        .timer-progress-bar {
            height: 100%;
            background: white;
            border-radius: 3px;
            transition: width 0.1s linear;
        }
        .btn-cancel-timer {
            background: rgba(255,255,255,0.2);
            border: 2px solid white;
            color: white;
            padding: 8px 20px;
            border-radius: 6px;
            font-size: 14px;
            cursor: pointer;
            margin-top: 5px;
        }
        .btn-cancel-timer:hover {
            background: rgba(255,255,255,0.3);
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo">
            <h1>🔐 OAuth Test Server</h1>
            <p>Sign in to authorize the application</p>
        </div>

        <!-- Auto-submit timer banner -->
        <div id="autoSubmitBanner" class="auto-submit-banner">
            <h3>⏱️ Auto-submitting in</h3>
            <div id="timerDisplay" class="timer-display">5</div>
            <div class="timer-progress">
                <div id="timerProgressBar" class="timer-progress-bar" style="width: 100%"></div>
            </div>
            <button type="button" id="cancelTimerBtn" class="btn-cancel-timer" onclick="cancelAutoSubmit()">
                Cancel Auto-Submit
            </button>
        </div>

        {{if .Error}}
        <div class="error-message">
            {{.Error}}
        </div>
        {{end}}

        <div class="client-info">
            <h3>Application requesting access:</h3>
            <p><strong>{{.ClientName}}</strong></p>
            <p>Client ID: {{.ClientID}}</p>
        </div>

        <form id="loginForm" method="POST" action="/authorize">
            <!-- Preserve OAuth parameters -->
            <input type="hidden" name="response_type" value="{{.ResponseType}}">
            <input type="hidden" name="client_id" value="{{.ClientID}}">
            <input type="hidden" name="redirect_uri" value="{{.RedirectURI}}">
            <input type="hidden" name="scope" value="{{.Scope}}">
            <input type="hidden" name="state" value="{{.State}}">
            <input type="hidden" name="code_challenge" value="{{.CodeChallenge}}">
            <input type="hidden" name="code_challenge_method" value="{{.CodeChallengeMethod}}">
            <input type="hidden" name="resource" value="{{.Resource}}">
            <input type="hidden" id="actionInput" name="action" value="approve">

            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" id="username" name="username" required autocomplete="username" placeholder="Enter username" value="testuser">
            </div>

            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" id="password" name="password" required autocomplete="current-password" placeholder="Enter password" value="testpass">
            </div>

            <div class="consent-section">
                <h3>This application is requesting:</h3>
                <ul>
                    {{range .Scopes}}
                    <li>{{.}}</li>
                    {{end}}
                </ul>
                <div class="consent-checkbox">
                    <input type="checkbox" id="consent" name="consent" value="on" checked>
                    <label for="consent">I authorize this application</label>
                </div>
            </div>

            <div class="button-group">
                <button type="submit" onclick="document.getElementById('actionInput').value='deny'" class="btn btn-secondary">Deny</button>
                <button type="submit" onclick="document.getElementById('actionInput').value='approve'" class="btn btn-primary">Approve</button>
            </div>
        </form>

        <div class="test-credentials">
            <p>Test credentials: <code>testuser</code> / <code>testpass</code> (pre-filled)</p>
        </div>
    </div>

    <script>
        // Auto-submit configuration
        const AUTO_SUBMIT_SECONDS = 5;
        let timeRemaining = AUTO_SUBMIT_SECONDS;
        let timerInterval = null;
        let isCancelled = false;

        function updateTimerDisplay() {
            const display = document.getElementById('timerDisplay');
            const progressBar = document.getElementById('timerProgressBar');

            display.textContent = timeRemaining;
            const progressPercent = (timeRemaining / AUTO_SUBMIT_SECONDS) * 100;
            progressBar.style.width = progressPercent + '%';
        }

        function cancelAutoSubmit() {
            isCancelled = true;
            if (timerInterval) {
                clearInterval(timerInterval);
                timerInterval = null;
            }

            const banner = document.getElementById('autoSubmitBanner');
            banner.classList.add('cancelled');
            banner.innerHTML = '<h3>⏹️ Auto-submit cancelled</h3><p style="font-size: 14px; margin-top: 10px;">Click Approve to continue manually</p>';
        }

        function startAutoSubmitTimer() {
            // Don't auto-submit if there's an error
            {{if .Error}}
            cancelAutoSubmit();
            return;
            {{end}}

            updateTimerDisplay();

            timerInterval = setInterval(function() {
                if (isCancelled) {
                    clearInterval(timerInterval);
                    return;
                }

                timeRemaining--;
                updateTimerDisplay();

                if (timeRemaining <= 0) {
                    clearInterval(timerInterval);
                    // Submit the form
                    document.getElementById('actionInput').value = 'approve';
                    document.getElementById('loginForm').submit();
                }
            }, 1000);
        }

        // Start timer when page loads
        document.addEventListener('DOMContentLoaded', startAutoSubmitTimer);
    </script>
</body>
</html>
