
<?php
require_once __DIR__ . '/includes/bootstrap.php';

if (!empty($_SESSION['admin_id'])) {
    header('Location: painel.php');
    exit;
}

$erro = '';

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

    $stmt = $pdo->prepare(
        "SELECT id, usuario, senha
         FROM admins
         WHERE usuario = :usuario
         LIMIT 1"
    );

    $stmt->execute(['usuario' => $usuario]);
    $admin = $stmt->fetch();

    if ($admin && password_verify($senha, $admin['senha'])) {
        session_regenerate_id(true);

        $_SESSION['admin_id'] = $admin['id'];
        $_SESSION['admin_usuario'] = $admin['usuario'];

        header('Location: painel.php');
        exit;
    }

    $erro = 'Usuário ou senha incorretos.';
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login administrativo - INFOGUARDA</title>
<style>
body {
    margin: 0;
    background: #edf1f5;
    font-family: Arial, sans-serif;
}
.login {
    max-width: 380px;
    margin: 80px auto;
    background: white;
    padding: 30px;
    border-radius: 12px;
    box-shadow: 0 3px 15px #0002;
}
h2 {
    text-align: center;
    color: #102d50;
}
input {
    width: 100%;
    box-sizing: border-box;
    padding: 13px;
    margin: 8px 0 15px;
    border: 1px solid #ccc;
    border-radius: 6px;
}
button {
    width: 100%;
    padding: 13px;
    background: #102d50;
    color: white;
    border: 0;
    border-radius: 6px;
    cursor: pointer;
    font-weight: bold;
}
.erro {
    color: #b3261e;
    text-align: center;
}
</style>
</head>
<body>

<div class="login">
    <h2>INFOGUARDA</h2>
    <p style="text-align:center">ACESSO ADMINISTRATIVO</p>

    <?php if ($erro): ?>
        <p class="erro"><?= limpar($erro) ?></p>
    <?php endif; ?>

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

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

        <button type="submit">ENTRAR NO SISTEMA</button>
    </form>
</div>

</body>
</html>
