<?php
// /admin/login.php
session_start();

// Se já estiver logado, pula direto para a ponte de comando
if (isset($_SESSION['admin_logado'])) {
    header("Location: dashboard.php");
    exit;
}

require 'db.php';

if(!defined('VX_OK')||!VX_OK){header('HTTP/1.0 403 Forbidden');exit;}

$erro = '';

// Processamento do Login
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $usuario = trim($_POST['usuario']);
    $senha = $_POST['senha'];

    // Busca o usuário na tabela 'usuarios'
    $stmt = $pdo->prepare("SELECT id, senha FROM usuarios WHERE usuario = ?");
    $stmt->execute([$usuario]);
    $admin = $stmt->fetch();

    if ($admin && password_verify($senha, $admin['senha'])) {
        $_SESSION['admin_logado'] = true;
        $_SESSION['admin_usuario'] = $usuario;
        header("Location: dashboard.php");
        exit;
    } else {
        $erro = "Usuário ou senha incorretos!";
    }
}

// 🛰️ BUSCANDO O NOME DO ARTISTA E PROJETO NO BANCO
$stmt = $pdo->query("SELECT valor FROM configs WHERE chave = 'nome_artista'");
$nome_artista = $stmt->fetchColumn() ?: 'Artista';

$projeto_nome = "VoxCore CMS";
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $projeto_nome ?> | Iniciar Sessão</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body { 
            background-color: #f8fafc; 
            height: 100vh; 
            display: flex; 
            align-items: center; 
            justify-content: center; 
            font-family: 'Inter', sans-serif;
        }
        .login-card { 
            background: white; 
            padding: 40px 30px; 
            border-radius: 12px; 
            box-shadow: 0 4px 15px rgba(0,0,0,0.05); 
            width: 100%; 
            max-width: 380px; 
        }
        .login-icon { color: #0ea5e9; font-weight: 800; font-size: 1.6rem; }
        .btn-login { background-color: #0ea5e9; border: none; border-radius: 6px; }
        .btn-login:hover { background-color: #0284c7; }
        .form-control:focus { box-shadow: none; border-color: #cbd5e1; }
        .input-group-text { border-right: none; }
        .form-control { border-left: none; }
    </style>


    <?php 
        // No admin, o caminho volta uma pasta para achar a imagem na raiz /img/
        $stmt_fav_adm = $pdo->query("SELECT valor FROM configs WHERE chave = 'favicon_admin'");
        $fav_adm_path = $stmt_fav_adm->fetchColumn() ?: 'img/favicon_admin.png';
    ?>
    
    <link rel="icon" href="../<?= $fav_adm_path ?>?v=<?= time() ?>" type="image/png">

</head>
<body>

    <div class="login-card">
        <div class="text-center mb-4">
            <h3 class="login-icon mb-1"><i class="fas fa-satellite-dish"></i> Painel de Controle</h3>
            <p class="text-muted small"><?= htmlspecialchars($nome_artista) ?> Oficial</p>
        </div>

        <?php if ($erro): ?>
            <div class="alert alert-danger text-center py-2 small fw-bold"><i class="fas fa-exclamation-circle"></i> <?= $erro ?></div>
        <?php endif; ?>

        <form method="POST">
            <div class="mb-3">
                <label class="form-label fw-bold text-secondary small">Usuário</label>
                <div class="input-group">
                    <span class="input-group-text bg-light text-dark"><i class="fas fa-user"></i></span>
                    <input type="text" name="usuario" class="form-control bg-light" required placeholder="admin">
                </div>
            </div>
            
            <div class="mb-4">
                <label class="form-label fw-bold text-secondary small">Senha</label>
                <div class="input-group">
                    <span class="input-group-text bg-light text-dark"><i class="fas fa-key"></i></span>
                    <input type="password" name="senha" class="form-control bg-light" required placeholder="••••••••">
                </div>
            </div>
            
            <button type="submit" class="btn btn-login w-100 text-white fw-bold py-2 mt-2">Iniciar Sessão</button>
        </form>
    </div>

</body>
</html>