<?php
declare(strict_types=1);

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

if (session_status() !== PHP_SESSION_ACTIVE) session_start();

global $config;
$cfg = $config ?? eaq_load_config();
$baseUrl = $cfg['base_url'] ?? eaq_base_url();

if (eaq_is_installed() === false) {
  eaq_redirect($baseUrl . '/install/');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $username = (string)($_POST['username'] ?? '');
  $password = (string)($_POST['password'] ?? '');
  if (eaq_verify_admin_login($cfg, $username, $password)) {
    eaq_set_admin_session($cfg, $username);
    eaq_redirect($baseUrl . '/app/');
  }
  $error = 'Credenciais inválidas.';
}

$lang = $cfg['lang'] ?? 'pt-BR';
$title = ($lang === 'en-US') ? 'Admin Login' : 'Login do Admin';

?>
<!doctype html>
<html lang="<?php echo htmlspecialchars($lang, ENT_QUOTES); ?>">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?php echo htmlspecialchars($title, ENT_QUOTES); ?></title>
  <style>
    body { font-family: Arial, sans-serif; background:#0b1220; color:#e8eefc; margin:0; }
    .wrap { max-width: 900px; margin: 0 auto; padding: 28px 18px; }
    .card { background:#111a2e; border:1px solid #223053; border-radius: 14px; padding: 18px; }
    label { display:block; margin-top: 12px; font-size: 13px; color:#cfe0ff; }
    input { width:100%; padding:10px 12px; border-radius:10px; border:1px solid #2a3b63; background:#0b1220; color:#e8eefc; }
    .row { display:flex; gap:12px; margin-top: 14px; }
    button { padding: 10px 14px; border-radius: 10px; border:1px solid #2a3b63; background:#2454ff; color:#fff; cursor:pointer; }
    .error { background:#3b0f0f; border:1px solid #6a1f1f; color:#ffd2d2; padding:10px 12px; border-radius:10px; margin-top: 14px; }
    a { color:#7cc0ff; text-decoration:none; }
  </style>
</head>
<body>
<div class="wrap">
  <div class="card">
    <h2 style="margin:0 0 10px 0;"><?php echo htmlspecialchars($title, ENT_QUOTES); ?></h2>
    <p style="margin:0 0 14px 0; color:#b7c8f0; font-size:13px;">
      <?php echo htmlspecialchars('Acesso administrativo (para ações de teste).', ENT_QUOTES); ?>
    </p>

    <?php if (!empty($error)): ?>
      <div class="error"><?php echo htmlspecialchars($error, ENT_QUOTES); ?></div>
    <?php endif; ?>

    <form method="post" action="">
      <label>Usuário</label>
      <input type="text" name="username" autocomplete="username" required>

      <label>Senha</label>
      <input type="password" name="password" autocomplete="current-password" required>

      <div class="row">
        <button type="submit">Entrar</button>
      </div>
    </form>
  </div>
</div>
</body>
</html>

