<?php
require_once __DIR__ . '/includes/functions.php';

$error = '';
$access = str_in($_POST['access'] ?? $_GET['access'] ?? '');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($access === '') {
        $error = 'Informe o acesso que aparece no aplicativo.';
    } else {
        $client = find_client_for_renewal_access($access);
        if (!$client) {
            $error = 'Acesso nao encontrado. Confira os numeros e tente novamente.';
        } else {
            $waUrl = build_renewal_whatsapp_url($client);
            if ($waUrl === '') {
                $error = 'WhatsApp de renovacao nao configurado. Fale com o suporte.';
            } else {
                header('Location: ' . $waUrl);
                exit;
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <meta name="theme-color" content="#5f0707">
    <title>Renovar - VODs Express</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --purple-1: #140026;
            --purple-2: #2b075a;
            --purple-3: #8d2cff;
            --purple-4: #b85cff;
            --green: #7CFF6B;
            --text: #ffffff;
            --muted: #d7c7f6;
            --border: rgba(255,205,205,.22);
            --shadow: 0 28px 80px rgba(0,0,0,.42);
        }

        html,
        body {
            min-height: 100%;
        }

        body {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 22px;
            color: var(--text);
            font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
            background:
                radial-gradient(circle at 50% 28%, rgba(255,92,92,.32), transparent 32%),
                linear-gradient(135deg, rgba(33,0,0,.94), rgba(125,0,0,.72)),
                #260000;
            overflow-x: hidden;
        }

        body::before {
            content: "";
            position: fixed;
            inset: 0;
            pointer-events: none;
            background:
                radial-gradient(circle at 12% 22%, rgba(124,255,107,.08), transparent 18%),
                radial-gradient(circle at 88% 78%, rgba(255,92,92,.14), transparent 24%),
                linear-gradient(90deg, rgba(0,0,0,.34), transparent 48%, rgba(0,0,0,.28));
        }

        .renew-shell {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 460px;
        }

        .renew-card {
            width: 100%;
            padding: 30px;
            border-radius: 30px;
            background: rgba(23, 3, 50, .84);
            border: 1px solid var(--border);
            box-shadow: var(--shadow);
            backdrop-filter: blur(18px);
        }

        .renew-header {
            text-align: center;
            margin-bottom: 24px;
        }

        .renew-logo {
            width: 50px;
            height: 50px;
            object-fit: contain;
            display: block;
            margin: 0 auto 12px;
            animation: logoZoom 2.8s ease-in-out infinite;
            filter: drop-shadow(0 10px 24px rgba(0,0,0,.35));
        }

        @keyframes logoZoom {
            0%, 100% {
                transform: scale(1);
            }
            50% {
                transform: scale(1.08);
            }
        }

        .renew-header h1 {
            font-size: 30px;
            margin-bottom: 8px;
        }

        .renew-header p {
            color: var(--muted);
            font-size: 14px;
            line-height: 1.45;
        }

        .form-group {
            margin-bottom: 16px;
        }

        .form-group label {
            display: block;
            margin-bottom: 9px;
            color: #fff;
            font-weight: 900;
            font-size: 14px;
        }

        input {
            width: 100%;
            min-height: 54px;
            border: 1px solid rgba(255,205,205,.28);
            border-radius: 17px;
            background: rgba(0,0,0,.25);
            color: #fff;
            outline: none;
            font-size: 16px;
            padding: 0 15px;
        }

        input::placeholder {
            color: rgba(246,199,199,.62);
        }

        input:focus {
            border-color: rgba(255,92,92,.85);
            box-shadow: 0 0 0 4px rgba(255,92,92,.18);
            background: rgba(255,255,255,.06);
        }

        .error-message {
            background: rgba(255, 67, 105, .15);
            color: #ffd7df;
            padding: 13px 15px;
            border-radius: 16px;
            margin-bottom: 18px;
            font-size: 14px;
            border: 1px solid rgba(255, 67, 105, .30);
        }

        .renew-btn {
            width: 100%;
            min-height: 56px;
            margin-top: 8px;
            background: linear-gradient(135deg, #ff5c5c 0%, #ff2c2c 48%, #cd1818 100%);
            color: #fff;
            border: 1px solid rgba(255,255,255,.18);
            border-radius: 18px;
            font-size: 16px;
            font-weight: 950;
            cursor: pointer;
            box-shadow: 0 18px 42px rgba(255,44,44,.34);
        }

        @media (max-width: 480px) {
            body {
                padding: 14px;
            }

            .renew-card {
                padding: 22px;
                border-radius: 24px;
            }

            .renew-header h1 {
                font-size: 25px;
            }
        }
    </style>
</head>
<body>
    <main class="renew-shell">
        <section class="renew-card">
            <div class="renew-header">
                <img class="renew-logo" src="uploads/panel_tvexpress_logo.png" alt="TV Express">
                <h1>Renovar VODs Express</h1>
                <p>Digite Acesso que aparece no QR do seu App:</p>
            </div>

            <?php if ($error): ?>
                <div class="error-message"><?php echo htmlspecialchars($error, ENT_QUOTES, 'UTF-8'); ?></div>
            <?php endif; ?>

            <form method="post">
                <div class="form-group">
                    <label for="access">Digite seu Acesso:</label>
                    <input
                        type="text"
                        id="access"
                        name="access"
                        value="<?php echo htmlspecialchars($access, ENT_QUOTES, 'UTF-8'); ?>"
                        placeholder="Ex: 5727236156"
                        autocomplete="off"
                        required>
                </div>

                <button type="submit" class="renew-btn">Renovar</button>
            </form>
        </section>
    </main>
</body>
</html>
