<?php 
function get_client_ip() {
    $ipaddress = 'undefined';
    if (isset($_SERVER)) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        } else {
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        }
    } else {
        if (getenv('HTTP_X_FORWARDED_FOR')) {
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        } else if (getenv('HTTP_CLIENT_IP')) {
            $ipaddress = getenv('HTTP_CLIENT_IP');
        } else {
            $ipaddress = getenv('REMOTE_ADDR');
        }
    }
    return htmlspecialchars($ipaddress, ENT_QUOTES, 'UTF-8');
}

session_start();
$eggzie_json = file_get_contents('./includes/eggzie.json');
$eggzie_data = json_decode($eggzie_json, true);
$theme_id = $eggzie_data['info']['aa'];

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

$check_users = $db_users->query('SELECT COUNT(*) as count FROM USERS');
$user_count = $check_users->fetchArray()['count'];
if ($user_count == 0) {
    $db_users->exec("INSERT INTO USERS(id,NAME,USERNAME,PASSWORD,LOGO) VALUES('1','Admin','admin','admin','img/logo.png')");
}

$admin_data = $db_users->query("SELECT * FROM USERS WHERE id='1'")->fetchArray();
$admin_name = $admin_data['NAME'];
$admin_logo = $admin_data['LOGO'];

$error_msg = '';
if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $stmt = $db_users->prepare('SELECT * FROM USERS WHERE USERNAME = :user');
    $stmt->bindValue(':user', $username, SQLITE3_TEXT);
    $result = $stmt->execute();
    $user = $result->fetchArray();

    if (!$user) {
        $error_msg = 'Usuário inválido!';
    } else if ($user['PASSWORD'] == $password) {
        $_SESSION['eggzie_iboPlayer'] = true;
        $_SESSION['N'] = $user['id'];
        $_SESSION['id'] = $user['id'];
        header('Location: users.php');
        exit();
    } else {
        $error_msg = 'Senha incorreta!';
    }
}

$date_now = date('d-m-Y H:i:s');
$client_ip = get_client_ip();
$db_logs = new SQLite3('./a/.logs.db');
$db_logs->exec('CREATE TABLE IF NOT EXISTS logs(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, date TEXT, ipaddress TEXT)');
$db_logs->exec("INSERT INTO logs(date,ipaddress) VALUES('$date_now','$client_ip')");

?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Painel Admin - Ultra IBO</title>
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Poppins:300,400,600,700" rel="stylesheet">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <style>
        :root {
            --neon-blue: #00f2ff;
            --metallic-blue: #1a2a6c;
            --dark-bg: #0a0b10;
        }
        body {
            background: radial-gradient(circle at center, #1a2a6c 0%, #0a0b10 100%);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Poppins', sans-serif;
            margin: 0;
            overflow: hidden;
        }
        .login-container {
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(15px);
            border: 1px solid rgba(0, 242, 255, 0.2);
            border-radius: 20px;
            padding: 40px;
            width: 100%;
            max-width: 400px;
            box-shadow: 0 0 30px rgba(0, 242, 255, 0.1);
            text-align: center;
            position: relative;
            z-index: 1;
        }
        .login-container::before {
            content: '';
            position: absolute;
            top: -2px; left: -2px; right: -2px; bottom: -2px;
            background: linear-gradient(45deg, var(--neon-blue), transparent, var(--metallic-blue));
            z-index: -1;
            border-radius: 22px;
            opacity: 0.3;
        }
        .logo-img {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            border: 3px solid var(--neon-blue);
            padding: 5px;
            margin-bottom: 20px;
            box-shadow: 0 0 15px var(--neon-blue);
            object-fit: contain;
            background: #fff;
        }
        h3 {
            color: #fff;
            font-weight: 700;
            margin-bottom: 5px;
            text-transform: uppercase;
            letter-spacing: 2px;
        }
        h5 {
            color: var(--neon-blue);
            margin-bottom: 30px;
            font-weight: 300;
        }
        .form-control {
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(0, 242, 255, 0.3);
            border-radius: 10px;
            color: #fff;
            padding: 12px 20px;
            margin-bottom: 20px;
            transition: all 0.3s;
        }
        .form-control:focus {
            background: rgba(255, 255, 255, 0.15);
            border-color: var(--neon-blue);
            box-shadow: 0 0 10px rgba(0, 242, 255, 0.5);
            color: #fff;
        }
        .form-control::placeholder {
            color: rgba(255, 255, 255, 0.5);
        }
        .btn-login {
            background: linear-gradient(45deg, var(--metallic-blue), var(--neon-blue));
            border: none;
            border-radius: 10px;
            color: #fff;
            padding: 12px;
            width: 100%;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 1px;
            transition: all 0.3s;
            cursor: pointer;
            margin-top: 10px;
        }
        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 242, 255, 0.4);
        }
        .info-footer {
            margin-top: 30px;
            font-size: 0.8rem;
            color: rgba(255, 255, 255, 0.4);
        }
        .alert {
            background: rgba(255, 0, 0, 0.2);
            border: 1px solid #ff4d4d;
            color: #ff4d4d;
            border-radius: 10px;
            margin-bottom: 20px;
            padding: 10px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <img src="<?php echo $admin_logo; ?>" class="logo-img" alt="Logo">
        <h3>Ultra IBO</h3>
        <h5><?php echo $admin_name; ?></h5>
        
        <?php if ($error_msg): ?>
            <div class="alert"><?php echo $error_msg; ?></div>
        <?php endif; ?>

        <form method="post">
            <input type="text" class="form-control" placeholder="Usuário" name="username" required autofocus>
            <input type="password" class="form-control" placeholder="Senha" name="password" required>
            <button class="btn-login" name="login" type="submit">Entrar no Painel</button>
        </form>

        <div class="info-footer">
            <p>IP: <?php echo $client_ip; ?><br>
            <?php echo $date_now; ?></p>
        </div>
    </div>
</body>
</html>
