<?php
session_start();

// Se já estiver logado, vai pro dashboard
if (isset($_SESSION['admin_logado']) && $_SESSION['admin_logado'] === true) {
    header('Location: index.php');
    exit;
}

$erro = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $usuario = $_POST['usuario'] ?? '';
    $senha = $_POST['senha'] ?? '';
    
    // Lê o arquivo de admin
    $adminData = json_decode(file_get_contents('../dados/admin.json'), true);
    
    if ($usuario === $adminData['usuario'] && $senha === $adminData['senha']) {
        $_SESSION['admin_logado'] = true;
        header('Location: index.php');
        exit;
    } else {
        $erro = 'Usuário ou senha inválidos!';
    }
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Admin</title>
    <link rel="stylesheet" href="css/estilo.css">
</head>
<body class="login-page">
    <div class="login-container">
        <div class="login-box">
            <img src="img/logo.png" alt="Logo" class="login-logo" id="logo-preview">
            <h1>🎮 Painel Admin</h1>
            <?php if ($erro): ?>
                <div class="erro"><?php echo $erro; ?></div>
            <?php endif; ?>
            <form method="POST">
                <input type="text" name="usuario" placeholder="Usuário" required>
                <input type="password" name="senha" placeholder="Senha" required>
                <button type="submit">Entrar</button>
            </form>
        </div>
    </div>
</body>
</html>