<?php
session_start();
ini_set('display_errors', 0);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);

$db_path = './a/.eggziepanels.db';

if (isset($_SESSION['eggzie_iboPlayer'])) {
    header('Location: index.php');
    exit();
}

$error = "";

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $user_input = $_POST['username'];
    $pass_input = $_POST['password'];

    try {
        $db = new SQLite3($db_path);
        
        // Busca o admin na tabela USERS usando as colunas que padronizamos
        $stmt = $db->prepare("SELECT * FROM USERS WHERE USER = :user AND PASS = :pass AND id = '1'");
        $stmt->bindValue(':user', $user_input, SQLITE3_TEXT);
        $stmt->bindValue(':pass', $pass_input, SQLITE3_TEXT);
        
        $result = $stmt->execute();
        $user = $result->fetchArray(SQLITE3_ASSOC);

        if ($user) {
            $_SESSION['eggzie_iboPlayer'] = true;
            $_SESSION['id'] = $user['id'];
            $_SESSION['name'] = $user['NAME'];
            header('Location: index.php');
            exit();
        } else {
            $error = "Credenciais inválidas!";
        }
        $db->close();
    } catch (Exception $e) {
        $error = "Erro de conexão com o banco.";
    }
}

// Definição da logo conforme solicitado
$logo = "https://5.zyx.lat/vu-02/rtx/logo/697b775635f23.png"; 

// Tentativa de buscar logo personalizada do banco, mantendo o fallback caso falhe
try {
    $db = new SQLite3($db_path);
    $res = $db->query("SELECT LOGO FROM USERS WHERE id='1'")->fetchArray(SQLITE3_ASSOC);
    if($res && !empty($res['LOGO'])) {
        $logo = $res['LOGO'];
    }
    $db->close();
} catch (Exception $e) {
    // Mantém a logo definida acima
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login | Ultra Painel</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet">
    <style>
        body { background-color: #06070a; font-family: 'Inter', sans-serif; }
        .login-card {
            background: #0f111a;
            border: 1px solid rgba(255,255,255,0.05);
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
        }
        .input-thai {
            background: rgba(255,255,255,0.03);
            border: 1px solid rgba(255,255,255,0.1);
            transition: all 0.3s;
        }
        .input-thai:focus {
            border-color: #8b5cf6;
            background: rgba(139, 92, 246, 0.05);
            outline: none;
        }
    </style>
</head>
<body class="flex items-center justify-center min-h-screen p-4">

    <div class="w-full max-w-md">
        <div class="text-center mb-8">
            <img src="<?php echo $logo; ?>" alt="Logo" class="mx-auto mb-4" style="max-height: 80px;">
            <h1 class="text-white font-black text-2xl uppercase tracking-tighter italic">Painel Administrativo</h1>
            <p class="text-[10px] text-violet-500 font-bold uppercase tracking-[0.4em]">Thailand Edition</p>
        </div>

        <div class="login-card p-8 rounded-[2.5rem]">
            <?php if($error): ?>
                <div class="bg-red-500/10 border border-red-500/50 text-red-500 text-xs font-bold p-3 rounded-xl mb-6 text-center uppercase">
                    <?php echo $error; ?>
                </div>
            <?php endif; ?>

            <form method="POST" class="space-y-6">
                <div>
                    <label class="block text-[10px] font-black text-gray-500 uppercase mb-2 ml-1">Usuário</label>
                    <input type="text" name="username" required class="w-full input-thai p-4 rounded-2xl text-white text-sm" placeholder="Digite seu usuário">
                </div>

                <div>
                    <label class="block text-[10px] font-black text-gray-500 uppercase mb-2 ml-1">Senha</label>
                    <input type="password" name="password" required class="w-full input-thai p-4 rounded-2xl text-white text-sm" placeholder="••••••••">
                </div>

                <button type="submit" class="w-full py-4 bg-violet-600 hover:bg-violet-500 text-white font-black rounded-2xl text-xs uppercase tracking-widest transition-all shadow-lg shadow-violet-900/40">
                    Entrar no Sistema
                </button>
            </form>
        </div>
        
        <p class="text-center mt-8 text-[9px] text-gray-600 font-bold uppercase tracking-widest">
            Proteção de Acesso Nível 4 • <?php echo date('Y'); ?>
        </p>
    </div>

</body>
</html>