<?php
declare(strict_types=1);

require_once __DIR__ . '/_bootstrap.php';

if (admin_is_logged()) {
    header('Location: index.php');
    exit;
}

$erro = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $usuario = trim((string) ($_POST['usuario'] ?? ''));
    $senha = (string) ($_POST['senha'] ?? '');

    if (hash_equals(ADMIN_USER, $usuario) && hash_equals(ADMIN_PASS, $senha)) {
        session_regenerate_id(true);
        $_SESSION['admin_logged'] = true;
        $_SESSION['admin_user'] = $usuario;
        header('Location: index.php');
        exit;
    }

    $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">
  <title>Bulo Admin - Login</title>
  <link rel="stylesheet" href="assets/admin.css?v=20260626-1">
</head>
<body>
  <main class="login-page">
    <section class="panel login-card">
      <div class="panel-header">
        <h1>Bulo Admin</h1>
        <p>Acesse para ajustar as configurações do aplicativo.</p>
      </div>
      <div class="panel-body">
        <?php if ($erro !== ''): ?>
          <div class="flash error"><?= e($erro) ?></div>
        <?php endif; ?>

        <form method="post" action="login.php">
          <div class="field">
            <label for="usuario">Usuário</label>
            <input id="usuario" name="usuario" autocomplete="username" required>
          </div>
          <br>
          <div class="field">
            <label for="senha">Senha</label>
            <input id="senha" name="senha" type="password" autocomplete="current-password" required>
          </div>
          <div class="actions">
            <button class="button" type="submit">Entrar</button>
          </div>
        </form>
      </div>
    </section>
  </main>
</body>
</html>
