<?php

require_once __DIR__ . '/includes/auth.php';



if (is_logged_in()) {

    header('Location: dashboard.php');

    exit;

}



$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        if (attempt_login($_POST['username'] ?? '', $_POST['password'] ?? '')) {
            header('Location: dashboard.php');
            exit;
        }
        $error = 'Usuário ou senha inválidos.';
    } catch (Throwable $e) {
        error_log('Falha no login do painel: ' . $e->getMessage());
        $error = 'Não foi possível conectar ao banco de dados. Verifique as configurações do MySQL.';
    }
}

?>

<!DOCTYPE html>

<html lang="pt-BR">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Login — LauncherIO2026</title>

    <link rel="stylesheet" href="assets/css/admin.css">

</head>

<body class="login-page">

<div class="login-card">

    <div class="login-brand">

        <div class="logo-icon">📺</div>

        <h1>LauncherIO2026</h1>

        <p>Painel SaaS de controle do launcher Android TV</p>

    </div>

    <?php if ($error): ?><div class="flash error"><?= htmlspecialchars($error) ?></div><?php endif; ?>

    <form method="post">

        <div class="form-group">

            <label>Usuário</label>

            <input type="text" name="username" required autofocus placeholder="admin">

        </div>

        <div class="form-group">

            <label>Senha</label>

            <input type="password" name="password" required placeholder="••••••••">

        </div>

        <button type="submit" class="btn primary" style="width:100%;margin-top:8px;">Entrar no painel</button>

    </form>

</div>

</body>

</html>

