<!DOCTYPE html>
<html lang="pt-BR">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Alemão Conveniência - Área do Usuário</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <style>
        :root {
            --primary: #f59e0b;
            --primary-dark: #d97706;
            --bg-dark: #0f172a;
            --bg-card: #1e293b;
            --text-light: #f1f5f9;
            --text-muted: #94a3b8;
            --glass-border: rgba(255, 255, 255, 0.08);
            --gradient-primary: linear-gradient(135deg, #f59e0b, #d97706);
        }

        body {
            font-family: 'Inter', sans-serif;
            background: var(--bg-dark);
            color: var(--text-light);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .login-card {
            background: var(--bg-card);
            border: 1px solid var(--glass-border);
            border-radius: 24px;
            padding: 40px;
            width: 100%;
            max-width: 450px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
        }

        .brand-icon {
            width: 48px;
            height: 48px;
            background: var(--gradient-primary);
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            margin: 0 auto 20px;
        }

        h2 {
            font-weight: 800;
            text-align: center;
            margin-bottom: 4px;
            font-size: 1.5rem;
        }

        p.subtitle {
            text-align: center;
            color: var(--text-muted);
            margin-bottom: 28px;
            font-size: 0.9rem;
        }

        /* ── Tabs ─────────────────────────────────────────────── */
        .tab-switcher {
            display: flex;
            background: rgba(15, 23, 42, 0.5);
            border-radius: 12px;
            padding: 4px;
            margin-bottom: 28px;
            gap: 4px;
        }

        .tab-btn {
            flex: 1;
            padding: 10px;
            border: none;
            border-radius: 9px;
            font-size: 0.85rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.25s;
            background: transparent;
            color: var(--text-muted);
        }

        .tab-btn.active {
            background: var(--gradient-primary);
            color: white;
            box-shadow: 0 4px 12px rgba(217, 119, 6, 0.35);
        }

        /* ── Form ─────────────────────────────────────────────── */
        .form-label {
            color: var(--text-muted);
            font-weight: 500;
            font-size: 0.82rem;
            margin-bottom: 6px;
        }

        .form-control {
            background: rgba(15, 23, 42, 0.5);
            border: 1px solid var(--glass-border);
            color: white;
            padding: 12px 16px;
            border-radius: 12px;
            transition: all 0.3s;
            font-size: 0.92rem;
        }

        .form-control::placeholder { color: #475569; }

        .form-control:focus {
            background: rgba(15, 23, 42, 0.8);
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.12);
            color: white;
        }

        .btn-primary {
            background: var(--gradient-primary);
            border: none;
            padding: 14px;
            border-radius: 12px;
            font-weight: 700;
            width: 100%;
            margin-top: 16px;
            transition: all 0.3s;
            font-size: 0.9rem;
            letter-spacing: 0.03em;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px -4px rgba(217, 119, 6, 0.4);
        }

        .btn-primary:disabled {
            opacity: 0.6;
            transform: none;
        }

        /* ── Alert ────────────────────────────────────────────── */
        .alert-box {
            padding: 12px 16px;
            border-radius: 10px;
            font-size: 0.85rem;
            margin-bottom: 20px;
            display: none;
            text-align: center;
            border: 1px solid;
        }

        .alert-box.error {
            color: #f87171;
            background: rgba(239, 68, 68, 0.08);
            border-color: rgba(239, 68, 68, 0.2);
        }

        .alert-box.success {
            color: #4ade80;
            background: rgba(74, 222, 128, 0.08);
            border-color: rgba(74, 222, 128, 0.2);
        }

        /* ── Input group icon ─────────────────────────────────── */
        .input-icon {
            position: relative;
        }

        .input-icon .bi {
            position: absolute;
            left: 14px;
            top: 50%;
            transform: translateY(-50%);
            color: #475569;
            font-size: 1rem;
            pointer-events: none;
        }

        .input-icon .form-control {
            padding-left: 40px;
        }

        .back-link {
            display: block;
            text-align: center;
            margin-top: 24px;
            color: var(--text-muted);
            text-decoration: none;
            font-size: 0.875rem;
            transition: color 0.3s;
        }

        .back-link:hover { color: var(--primary); }

        .panel { display: none; }
        .panel.active { display: block; }
    </style>
</head>

<body>
    <div class="login-card">
        <div class="brand-icon">
            <i class="bi bi-person-fill text-white"></i>
        </div>
        <h2>Área do Cliente</h2>
        <p class="subtitle">Acesse seus tickets e promoções</p>

        <!-- Tabs -->
        <div class="tab-switcher">
            <button class="tab-btn active" onclick="switchTab('login')">
                <i class="bi bi-box-arrow-in-right me-1"></i> Entrar
            </button>
            <button class="tab-btn" onclick="switchTab('register')">
                <i class="bi bi-person-plus me-1"></i> Cadastrar
            </button>
        </div>

        <!-- Alert -->
        <div id="alert-box" class="alert-box error"></div>

        <!-- ── Login Panel ───────────────────────────────────── -->
        <div id="panel-login" class="panel active">
            <form id="loginForm" autocomplete="on">
                <div class="mb-3">
                    <label class="form-label">CPF</label>
                    <div class="input-icon">
                        <i class="bi bi-card-text"></i>
                        <input type="text" class="form-control" id="login-cpf" placeholder="000.000.000-00" required autocomplete="username">
                    </div>
                </div>
                <div class="mb-4">
                    <label class="form-label">Telefone</label>
                    <div class="input-icon">
                        <i class="bi bi-phone"></i>
                        <input type="text" class="form-control" id="login-telefone" placeholder="(00) 00000-0000" required autocomplete="tel">
                    </div>
                </div>
                <button type="submit" class="btn btn-primary" id="btn-login">
                    ACESSAR MINHA CONTA
                </button>
            </form>

            <p style="text-align:center; color:var(--text-muted); font-size:0.82rem; margin-top:20px;">
                Ainda não tem cadastro?
                <a href="#" onclick="switchTab('register'); return false;" style="color:var(--primary); text-decoration:none; font-weight:600;">Cadastre-se</a>
            </p>
        </div>

        <!-- ── Register Panel ────────────────────────────────── -->
        <div id="panel-register" class="panel">
            <form id="registerForm" autocomplete="on">
                <div class="mb-3">
                    <label class="form-label">Nome completo</label>
                    <div class="input-icon">
                        <i class="bi bi-person"></i>
                        <input type="text" class="form-control" id="reg-nome" placeholder="Seu nome" required autocomplete="name">
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label">E-mail</label>
                    <div class="input-icon">
                        <i class="bi bi-envelope"></i>
                        <input type="email" class="form-control" id="reg-email" placeholder="seu@email.com" required autocomplete="email">
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label">CPF</label>
                    <div class="input-icon">
                        <i class="bi bi-card-text"></i>
                        <input type="text" class="form-control" id="reg-cpf" placeholder="000.000.000-00" required>
                    </div>
                </div>
                <div class="mb-4">
                    <label class="form-label">Telefone (WhatsApp)</label>
                    <div class="input-icon">
                        <i class="bi bi-whatsapp"></i>
                        <input type="text" class="form-control" id="reg-telefone" placeholder="(00) 00000-0000" required autocomplete="tel">
                    </div>
                </div>
                <button type="submit" class="btn btn-primary" id="btn-register">
                    CRIAR MINHA CONTA
                </button>
            </form>

            <p style="text-align:center; color:var(--text-muted); font-size:0.82rem; margin-top:20px;">
                Já tem cadastro?
                <a href="#" onclick="switchTab('login'); return false;" style="color:var(--primary); text-decoration:none; font-weight:600;">Entrar</a>
            </p>
        </div>

        <a href="/" class="back-link">
            <i class="bi bi-arrow-left me-1"></i> Voltar para o início
        </a>
    </div>

    <script src="https://unpkg.com/imask"></script>
    <script>
        // ── Masks ─────────────────────────────────────────────
        IMask(document.getElementById('login-cpf'),    { mask: '000.000.000-00' });
        IMask(document.getElementById('login-telefone'),{ mask: '(00) 00000-0000' });
        IMask(document.getElementById('reg-cpf'),      { mask: '000.000.000-00' });
        IMask(document.getElementById('reg-telefone'), { mask: '(00) 00000-0000' });

        // ── Tab switcher ──────────────────────────────────────
        function switchTab(tab) {
            document.querySelectorAll('.tab-btn').forEach((b, i) => {
                b.classList.toggle('active', (tab === 'login' && i === 0) || (tab === 'register' && i === 1));
            });
            document.getElementById('panel-login').classList.toggle('active', tab === 'login');
            document.getElementById('panel-register').classList.toggle('active', tab === 'register');
            clearAlert();
        }

        // ── Alert helper ──────────────────────────────────────
        function showAlert(msg, type = 'error') {
            const box = document.getElementById('alert-box');
            box.textContent = msg;
            box.className = 'alert-box ' + type;
            box.style.display = 'block';
        }

        function clearAlert() {
            const box = document.getElementById('alert-box');
            box.style.display = 'none';
        }

        function setLoading(btnId, loading) {
            const btn = document.getElementById(btnId);
            btn.disabled = loading;
            btn.textContent = loading ? 'Aguarde...' : btn.dataset.label;
        }

        // Save original labels
        document.getElementById('btn-login').dataset.label    = 'ACESSAR MINHA CONTA';
        document.getElementById('btn-register').dataset.label = 'CRIAR MINHA CONTA';

        // ── Login submit ──────────────────────────────────────
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            clearAlert();
            setLoading('btn-login', true);

            try {
                const res = await fetch('/api/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        cpf:      document.getElementById('login-cpf').value,
                        telefone: document.getElementById('login-telefone').value,
                    })
                });

                const data = await res.json();

                if (data.success) {
                    window.location.href = data.redirect;
                } else {
                    showAlert(data.error || 'Erro ao realizar login');
                }
            } catch {
                showAlert('Erro de conexão com o servidor');
            } finally {
                setLoading('btn-login', false);
            }
        });

        // ── Register submit ───────────────────────────────────
        document.getElementById('registerForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            clearAlert();
            setLoading('btn-register', true);

            try {
                const res = await fetch('/api/register', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        nome:     document.getElementById('reg-nome').value.trim(),
                        email:    document.getElementById('reg-email').value.trim().toLowerCase(),
                        cpf:      document.getElementById('reg-cpf').value,
                        telefone: document.getElementById('reg-telefone').value,
                    })
                });

                const data = await res.json();

                if (data.success) {
                    showAlert('Conta criada! Redirecionando...', 'success');
                    setTimeout(() => { window.location.href = data.redirect; }, 800);
                } else {
                    showAlert(data.error || 'Erro ao criar conta');
                }
            } catch {
                showAlert('Erro de conexão com o servidor');
            } finally {
                setLoading('btn-register', false);
            }
        });
    </script>
</body>

</html>
