<?php
session_start();
require_once '../config.php';

if (isset($_SESSION['admin_id'])) {
    header('Location: dashboard.php');
    exit;
}

$erro = '';

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

    if (empty($email) || empty($senha)) {
        $erro = 'Preencha e-mail e senha.';
    } else {
        $stmt = $pdo->prepare("SELECT * FROM admin_users WHERE email = ? LIMIT 1");
        $stmt->execute([$email]);
        $admin = $stmt->fetch();

        if ($admin && password_verify($senha, $admin['senha'])) {
            $_SESSION['admin_id'] = $admin['id'];
            $_SESSION['admin_nome'] = $admin['nome'];
            $_SESSION['admin_email'] = $admin['email'];

            header('Location: dashboard.php');
            exit;
        } else {
            $erro = 'E-mail ou senha inválidos.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <title>Login - PromoCreative LED Mapper</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>
        * {
            box-sizing: border-box;
            font-family: Arial, Helvetica, sans-serif;
        }

        body {
            margin: 0;
            min-height: 100vh;
            background:
                radial-gradient(circle at top left, rgba(0, 170, 255, .25), transparent 35%),
                radial-gradient(circle at bottom right, rgba(255, 111, 0, .18), transparent 35%),
                #060b16;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
        }

        .login-box {
            width: 100%;
            max-width: 420px;
            background: rgba(10, 18, 34, .92);
            border: 1px solid rgba(255,255,255,.08);
            border-radius: 22px;
            padding: 34px;
            box-shadow: 0 30px 80px rgba(0,0,0,.45);
        }

        .brand {
            text-align: center;
            margin-bottom: 28px;
        }

        .brand h1 {
            margin: 0;
            font-size: 24px;
            color: #fff;
        }

        .brand p {
            margin-top: 8px;
            color: #8fa3c7;
            font-size: 14px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #c8d6f0;
            font-size: 14px;
        }

        input {
            width: 100%;
            padding: 14px;
            border-radius: 12px;
            border: 1px solid rgba(255,255,255,.12);
            background: #0d1628;
            color: #fff;
            outline: none;
            margin-bottom: 18px;
        }

        input:focus {
            border-color: #00aaff;
        }

        button {
            width: 100%;
            padding: 15px;
            border: 0;
            border-radius: 14px;
            background: linear-gradient(135deg, #ff7a00, #00aaff);
            color: #fff;
            font-weight: bold;
            font-size: 15px;
            cursor: pointer;
        }

        button:hover {
            opacity: .92;
        }

        .erro {
            background: rgba(255, 60, 60, .12);
            border: 1px solid rgba(255, 60, 60, .35);
            color: #ffb3b3;
            padding: 12px;
            border-radius: 12px;
            margin-bottom: 18px;
            font-size: 14px;
            text-align: center;
        }

        .footer {
            text-align: center;
            margin-top: 22px;
            color: #6d7fa3;
            font-size: 12px;
        }
    </style>
</head>
<body>

<div class="login-box">
    <div class="brand">
        <h1>PromoCreative LED Mapper</h1>
        <p>Painel de controle de licenças</p>
    </div>

    <?php if (!empty($erro)): ?>
        <div class="erro"><?= htmlspecialchars($erro); ?></div>
    <?php endif; ?>

    <form method="POST">
        <label>E-mail</label>
        <input type="email" name="email" placeholder="admin@promocreative.com.br" required>

        <label>Senha</label>
        <input type="password" name="senha" placeholder="Digite sua senha" required>

        <button type="submit">Entrar no painel</button>
    </form>

    <div class="footer">
        PromoCreative Studio © <?= date('Y'); ?>
    </div>
</div>

</body>
</html>