<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Enter Table Code - RestPO</title>
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800&family=Playfair+Display:ital,wght@0,600;0,700;1,600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --bg: #09090b;
            --card-bg: rgba(18, 18, 24, 0.7);
            --border: rgba(255, 255, 255, 0.08);
            --border-focus: #d4a54a;
            --gold: #d4a54a;
            --gold-glow: rgba(212, 165, 74, 0.3);
            --text-1: #f4f4f5;
            --text-2: #a1a1aa;
            --text-3: #71717a;
            --error: #ef4444;
            --r-lg: 16px;
            --r-xl: 24px;
            --ease: cubic-bezier(0.16, 1, 0.3, 1);
        }

        * {
            margin: 0; padding: 0; box-sizing: border-box;
            font-family: 'Outfit', sans-serif;
            -webkit-tap-highlight-color: transparent;
        }

        body {
            background-color: var(--bg);
            color: var(--text-1);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            overflow: hidden;
            position: relative;
        }

        /* Ambient Glow Orbs */
        .glow-orb {
            position: absolute;
            border-radius: 50%;
            filter: blur(120px);
            pointer-events: none;
            opacity: 0.5;
            z-index: 0;
        }
        .glow-1 {
            width: 300px; height: 300px;
            top: -50px; right: -50px;
            background: radial-gradient(circle, var(--gold-glow) 0%, transparent 70%);
        }
        .glow-2 {
            width: 400px; height: 400px;
            bottom: -100px; left: -100px;
            background: radial-gradient(circle, rgba(139, 92, 246, 0.15) 0%, transparent 70%);
        }

        .container {
            width: 100%;
            max-width: 420px;
            z-index: 10;
            animation: fadeUp 0.8s var(--ease);
        }

        .card {
            background: var(--card-bg);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border: 1px solid var(--border);
            border-radius: var(--r-xl);
            padding: 40px 30px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.05);
            text-align: center;
            position: relative;
            overflow: hidden;
        }

        .card::before {
            content: '';
            position: absolute;
            top: 0; left: 0; right: 0; height: 2px;
            background: linear-gradient(90deg, transparent, var(--gold), transparent);
        }

        .logo-area {
            margin-bottom: 28px;
        }
        .logo-icon {
            width: 64px; height: 64px;
            background: rgba(212, 165, 74, 0.1);
            border: 1px solid var(--border-focus);
            border-radius: var(--r-lg);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-size: 28px;
            color: var(--gold);
            margin-bottom: 16px;
            box-shadow: 0 0 20px var(--gold-glow);
        }

        h1 {
            font-family: 'Playfair Display', serif;
            font-size: 26px;
            font-weight: 700;
            color: var(--text-1);
            margin-bottom: 8px;
            letter-spacing: -0.02em;
        }
        .subtitle {
            font-size: 14px;
            color: var(--text-2);
            margin-bottom: 32px;
        }

        /* 6-Digit input layout */
        .code-inputs {
            display: flex;
            justify-content: space-between;
            gap: 8px;
            margin-bottom: 28px;
            direction: ltr;
        }
        .code-inputs input {
            width: 48px;
            height: 58px;
            background: rgba(255, 255, 255, 0.03);
            border: 1.5px solid var(--border);
            border-radius: 12px;
            font-size: 22px;
            font-weight: 700;
            text-align: center;
            color: var(--text-1);
            outline: none;
            transition: all 0.3s var(--ease);
        }
        .code-inputs input:focus {
            border-color: var(--border-focus);
            background: rgba(255, 255, 255, 0.06);
            box-shadow: 0 0 12px var(--gold-glow);
            transform: translateY(-2px);
        }

        .error-msg {
            font-size: 13px;
            color: var(--error);
            margin-bottom: 20px;
            display: none;
            align-items: center;
            justify-content: center;
            gap: 6px;
            animation: shake 0.4s ease;
        }

        .btn-submit {
            width: 100%;
            padding: 16px;
            border-radius: var(--r-lg);
            background: linear-gradient(135deg, var(--gold), #c9952a);
            border: none;
            color: #1a1200;
            font-size: 15px;
            font-weight: 700;
            cursor: pointer;
            box-shadow: 0 4px 20px rgba(212, 165, 74, 0.25);
            transition: all 0.3s var(--ease);
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }
        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 25px rgba(212, 165, 74, 0.35);
        }
        .btn-submit:active {
            transform: translateY(0);
        }
        .btn-submit:disabled {
            background: var(--text-3);
            color: var(--bg);
            box-shadow: none;
            cursor: not-allowed;
            transform: none;
        }

        .btn-submit .spinner {
            display: none;
            animation: spin 1s linear infinite;
        }

        .brand-footer {
            margin-top: 32px;
            font-size: 11px;
            color: var(--text-3);
            letter-spacing: 0.05em;
            text-transform: uppercase;
        }

        @keyframes fadeUp {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        @keyframes spin {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-6px); }
            75% { transform: translateX(6px); }
        }
    </style>
</head>
<body>

    <div class="glow-orb glow-1"></div>
    <div class="glow-orb glow-2"></div>

    <div class="container">
        <div class="card">
            <div class="logo-area">
                <div class="logo-icon">
                    <i class="fas fa-utensils"></i>
                </div>
                <h1>Welcome to Our Restaurant</h1>
                <p class="subtitle">Please enter the 6-digit code on your table to view the menu and place your order.</p>
            </div>

            <form id="codeForm" onsubmit="handleSubmit(event)">
                <div class="code-inputs" id="inputsContainer">
                    <input type="text" maxlength="1" pattern="[0-9]*" inputmode="numeric" required autocomplete="off" autofocus>
                    <input type="text" maxlength="1" pattern="[0-9]*" inputmode="numeric" required autocomplete="off">
                    <input type="text" maxlength="1" pattern="[0-9]*" inputmode="numeric" required autocomplete="off">
                    <input type="text" maxlength="1" pattern="[0-9]*" inputmode="numeric" required autocomplete="off">
                    <input type="text" maxlength="1" pattern="[0-9]*" inputmode="numeric" required autocomplete="off">
                    <input type="text" maxlength="1" pattern="[0-9]*" inputmode="numeric" required autocomplete="off">
                </div>

                <div class="error-msg" id="errorMsg">
                    <i class="fas fa-exclamation-circle"></i>
                    <span id="errorText">Invalid code</span>
                </div>

                <button type="submit" class="btn-submit" id="submitBtn">
                    <span>View Menu</span>
                    <i class="fas fa-circle-notch spinner" id="submitSpinner"></i>
                </button>
            </form>

            <div class="brand-footer">
                Powered by Respo
            </div>
        </div>
    </div>

    <script>
        const container = document.getElementById('inputsContainer');
        const inputs = container.querySelectorAll('input');
        const errorMsg = document.getElementById('errorMsg');
        const errorText = document.getElementById('errorText');
        const submitBtn = document.getElementById('submitBtn');
        const spinner = document.getElementById('submitSpinner');

        inputs.forEach((input, index) => {
            // Handle paste
            input.addEventListener('paste', (e) => {
                e.preventDefault();
                const pastedText = e.clipboardData.getData('text').trim();
                if (/^\d{6}$/.test(pastedText)) {
                    pastedText.split('').forEach((char, i) => {
                        inputs[i].value = char;
                    });
                    inputs[5].focus();
                }
            });

            // Focus next input on input
            input.addEventListener('input', (e) => {
                const val = e.target.value;
                // Only allow numbers
                if (!/^\d*$/.test(val)) {
                    input.value = '';
                    return;
                }
                if (val && index < 5) {
                    inputs[index + 1].focus();
                }
            });

            // Backspace key
            input.addEventListener('keydown', (e) => {
                if (e.key === 'Backspace') {
                    if (!input.value && index > 0) {
                        inputs[index - 1].value = '';
                        inputs[index - 1].focus();
                    } else {
                        input.value = '';
                    }
                }
            });
        });

        function showError(msg) {
            errorText.textContent = msg;
            errorMsg.style.display = 'flex';
            setTimeout(() => {
                errorMsg.classList.add('shake');
            }, 50);
            errorMsg.addEventListener('animationend', () => {
                errorMsg.classList.remove('shake');
            });
        }

        function clearError() {
            errorMsg.style.display = 'none';
        }

        function handleSubmit(e) {
            e.preventDefault();
            clearError();

            let code = '';
            inputs.forEach(input => code += input.value);

            if (code.length !== 6) {
                showError('Please enter all 6 digits.');
                return;
            }

            submitBtn.disabled = true;
            spinner.style.display = 'inline-block';

            fetch('<?= BASE_URL ?>/menu/lookup', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ code: code })
            })
            .then(res => res.json())
            .then(data => {
                if (data.success && data.redirect) {
                    window.location.href = data.redirect;
                } else {
                    showError(data.message || 'Table not found.');
                    submitBtn.disabled = false;
                    spinner.style.display = 'none';
                }
            })
            .catch(err => {
                showError('Network error. Please try again.');
                submitBtn.disabled = false;
                spinner.style.display = 'none';
            });
        }

        window.addEventListener('DOMContentLoaded', () => {
            const urlParams = new URLSearchParams(window.location.search);
            const codeParam = urlParams.get('code');
            if (codeParam && /^\d{6}$/.test(codeParam)) {
                codeParam.split('').forEach((char, i) => {
                    if (inputs[i]) inputs[i].value = char;
                });
                const mockEvent = { preventDefault: () => {} };
                handleSubmit(mockEvent);
            }
            <?php if (!empty($error)): ?>
            showError(<?= json_encode($error) ?>);
            <?php endif; ?>
        });
    </script>
</body>
</html>
