<?php
/**
 * UPGRADE PREMIUM BY MANUS AI
 * DESIGN: MODERN DARK GLASSMORPHISM
 */

function getIPAddress() {
    $ip = 'undefined';
    if (isset($_SERVER)) {
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
    } else {
        $ip = getenv('REMOTE_ADDR');
        if (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } else if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }
    }
    $ip = htmlspecialchars($ip, ENT_QUOTES, 'UTF-8');
    return $ip;
}

session_start();

// Carregar informações do arquivo JSON
$info = json_decode(file_get_contents('./includes/eggzie.json'), true);
$aa = $info['info']['aa'] ?? '2';

// Criar ou abrir banco de dados SQLite
$db = new SQLite3('./a/.eggziepanels.db');
$db->exec('CREATE TABLE IF NOT EXISTS USERS(id INT PRIMARY KEY, NAME TEXT, USERNAME TEXT, PASSWORD TEXT, LOGO TEXT)');

// Verificar se não há usuários e inserir um usuário padrão
$userCountQuery = $db->query('SELECT COUNT(*) as count FROM USERS');
$userCount = $userCountQuery->fetchArray()['count'];
if ($userCount == 0) {
    $db->exec('INSERT INTO USERS(id, NAME, USERNAME, PASSWORD, LOGO) VALUES(\'1\', \'Admin Master\', \'admin\', \'admin\', \'img/logo.png\')');
}

// Obter dados do usuário padrão
$userQuery = $db->query('SELECT * FROM USERS WHERE id=\'1\'');
$user = $userQuery->fetchArray();
$userName = $user['NAME'];
$userLogo = $user['LOGO'];

// Verificar login
$errorMsg = "";
if (isset($_POST['login'])) {
    $usernamee = $_POST['username'];
    $password = $_POST['password'];

    $query = 'SELECT * FROM USERS WHERE USERNAME="' . $usernamee . '"';
    $result = $db->query($query);
    $row = $result->fetchArray();

    if ($row && $row['PASSWORD'] == $password) {
        $_SESSION['eggzie_iboPlayer'] = true;
        $_SESSION['name'] = $userName;
        $_SESSION['logo'] = $userLogo;
        $_SESSION['id'] = $row['id'];
        header('Location: dashboard.php');
        exit;
    } else {
        $errorMsg = "Credenciais inválidas. Tente novamente.";
    }
}

// Gravar log de acesso
$date = date('d-m-Y H:i:s');
$ipAddress = getIPAddress();
$logDb = new SQLite3('./a/.logs.db');
$logDb->exec('CREATE TABLE IF NOT EXISTS logs(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, date TEXT, ipaddress TEXT)');
$logDb->exec('INSERT INTO logs(date, ipaddress) VALUES(\'' . $date . '\', \'' . $ipAddress . '\')');

?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IBO NOTIFICATION - ACESSO PREMIUM</title>
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="icon" href="favicon.ico" type="image/x-icon">

    <style>
        :root {
            --primary: #6366f1;
            --primary-glow: rgba(99, 102, 241, 0.5);
            --bg-dark: #0f172a;
            --surface: rgba(30, 41, 59, 0.7);
            --text-main: #f1f5f9;
            --text-muted: #94a3b8;
            --error: #ef4444;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Inter', sans-serif;
        }

        body {
            background-color: var(--bg-dark);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            color: var(--text-main);
        }

        /* Background Animado */
        .bg-animate {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
            background: radial-gradient(circle at 50% 50%, #1e293b 0%, #0f172a 100%);
        }

        .bg-blob {
            position: absolute;
            width: 500px;
            height: 500px;
            background: var(--primary);
            filter: blur(100px);
            opacity: 0.15;
            border-radius: 50%;
            animation: move 20s infinite alternate;
        }

        @keyframes move {
            from { transform: translate(-10%, -10%); }
            to { transform: translate(20%, 20%); }
        }

        /* Login Card */
        .login-card {
            background: var(--surface);
            backdrop-filter: blur(20px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            padding: 40px;
            border-radius: 24px;
            width: 100%;
            max-width: 400px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
            animation: fadeIn 0.8s ease-out;
        }

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

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

        .logo {
            width: 80px;
            height: 80px;
            margin-bottom: 15px;
            object-fit: contain;
            filter: drop-shadow(0 0 10px var(--primary-glow));
        }

        .title {
            font-size: 24px;
            font-weight: 800;
            letter-spacing: -0.5px;
            background: linear-gradient(to right, #fff, #94a3b8);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .subtitle {
            color: var(--text-muted);
            font-size: 14px;
            margin-top: 5px;
        }

        /* Form Elements */
        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .input-icon {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--text-muted);
            transition: 0.3s;
        }

        .input-field {
            width: 100%;
            background: rgba(15, 23, 42, 0.6);
            border: 1px solid rgba(255, 255, 255, 0.1);
            padding: 12px 15px 12px 45px;
            border-radius: 12px;
            color: white;
            font-size: 15px;
            transition: all 0.3s;
        }

        .input-field:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 4px var(--primary-glow);
        }

        .input-field:focus + .input-icon {
            color: var(--primary);
        }

        .btn-login {
            width: 100%;
            background: var(--primary);
            color: white;
            border: none;
            padding: 14px;
            border-radius: 12px;
            font-weight: 600;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .btn-login:hover {
            background: #4f46e5;
            transform: translateY(-2px);
            box-shadow: 0 10px 20px -5px var(--primary-glow);
        }

        .btn-login:active {
            transform: translateY(0);
        }

        .error-msg {
            background: rgba(239, 68, 68, 0.1);
            color: var(--error);
            padding: 10px;
            border-radius: 8px;
            font-size: 13px;
            text-align: center;
            margin-bottom: 20px;
            border: 1px solid rgba(239, 68, 68, 0.2);
        }

        .footer-links {
            text-align: center;
            margin-top: 25px;
            font-size: 13px;
            color: var(--text-muted);
        }

        .footer-links a {
            color: var(--primary);
            text-decoration: none;
            font-weight: 500;
        }

        .footer-links a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="bg-animate">
        <div class="bg-blob" style="top: 10%; left: 10%;"></div>
        <div class="bg-blob" style="bottom: 10%; right: 10%; background: #10b981;"></div>
    </div>

    <div class="login-card">
        <div class="header">
            <img src="<?php echo $userLogo; ?>" alt="Logo" class="logo" onerror="this.src='img/logo.png'">
            <h1 class="title">Painel de Acesso</h1>
            <p class="subtitle">Bem-vindo ao IBO Notification Premium</p>
        </div>

        <?php if ($errorMsg): ?>
            <div class="error-msg">
                <i class="fas fa-exclamation-circle"></i> <?php echo $errorMsg; ?>
            </div>
        <?php endif; ?>

        <form action="" method="POST">
            <div class="form-group">
                <i class="fas fa-user input-icon"></i>
                <input type="text" name="username" class="input-field" placeholder="Usuário" required autofocus>
            </div>
            <div class="form-group">
                <i class="fas fa-lock input-icon"></i>
                <input type="password" name="password" class="input-field" placeholder="Senha" required>
            </div>
            
            <button type="submit" name="login" class="btn-login">
                <span>Entrar no Sistema</span>
                <i class="fas fa-arrow-right"></i>
            </button>
        </form>

        <div class="footer-links">
            <p>&copy; <?php echo date('Y'); ?> <?php echo $userName; ?></p>
            <p>Precisa de ajuda? <a href="https://t.me/eonbry" target="_blank">Suporte Técnico</a></p>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</body>
</html>
