<?php
require_once __DIR__ . '/../config/bootstrap.php';
$pdo = db();

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

  $st = $pdo->prepare('SELECT * FROM admin_users WHERE email=? AND ativo=1 LIMIT 1');
  $st->execute([$email]);
  $u = $st->fetch();

  if ($u && password_verify($senha, $u['senha_hash'])) {
    $_SESSION['admin_user'] = ['id' => (int)$u['id'], 'nome' => $u['nome'], 'email' => $u['email']];
    $pdo->prepare('UPDATE admin_users SET last_login=NOW() WHERE id=?')->execute([(int)$u['id']]);
    header('Location: index.php');
    exit;
  }

  $error = 'Login inválido.';
}

$base = app_base_path();
$logo = $base . '/assets/img/fb-logo.svg';
$iconMail = $base . '/assets/img/icon-mail.svg';
$iconLock = $base . '/assets/img/icon-lock.svg';
?>
<!doctype html>
<html lang="pt-br">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Admin - Login</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="<?= $base; ?>/assets/css/fb-admin.css" rel="stylesheet">
  <style>
    .fb-login-card {
      max-width: 460px;
      margin: 0 auto;
    }

    .fb-login-card .card {
      border-radius: 22px;
    }

    .fb-ico-input {
      position: relative;
    }

    .fb-ico-input img {
      position: absolute;
      left: 16px;
      top: 50%;
      transform: translateY(-50%);
      width: 18px;
      height: 18px;
      opacity: .7;
    }

    .fb-ico-input input {
      /* padding-left: 48px; */
    }
  </style>
</head>

<body class="fb-body">
  <div class="container fb-wrap py-5">
    <div class="fb-login-card">
      <div class="text-center mb-4">
        <img src="<?= htmlspecialchars($logo); ?>" alt="FrameBuilder" style="height:64px;width:auto;">
        <h1 class="mt-3 mb-1" style="font-weight:900;font-size:1.7rem;">Bem-vindo(a) de volta</h1>
        <div class="text-muted">Faça login para continuar o painel</div>
      </div>

      <div class="card shadow-sm">
        <div class="card-body p-4">
          <?php if ($error): ?>
            <div class="alert alert-danger mb-3"><?= htmlspecialchars($error); ?></div>
          <?php endif; ?>

          <form method="post" autocomplete="off">
            <div class="mb-3">
              <label class="form-label fw-semibold">E-mail</label>
              <div class="fb-ico-input">

                <input name="email" type="email" class="form-control fb-input" required placeholder="seuemail@dominio.com">
              </div>
            </div>
            <div class="mb-3">
              <label class="form-label fw-semibold">Senha</label>
              <div class="fb-ico-input">

                <input name="senha" type="password" class="form-control fb-input" required placeholder="••••••••">
              </div>
            </div>
            <button class="btn btn-primary w-100 fb-btn" style="font-size: 20px;"><strong>Acessar</strong> sistema</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</body>

</html>