<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/database.php';

gpc_db();

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = trim((string) ($_POST['username'] ?? ''));
    $pass = (string) ($_POST['password'] ?? '');
    if ($user === '' || $pass === '') {
        $error = 'Preencha usuário e senha.';
    } elseif (gpc_login($user, $pass)) {
        header('Location: dashboard.php');
        exit;
    } else {
        $error = 'Usuário ou senha incorretos.';
    }
}

if (gpc_logged_in()) {
    header('Location: dashboard.php');
    exit;
}

$panelName = gpc_setting('panel_name', 'GPC Panel');
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= gpc_h($panelName) ?></title>
    <link rel="stylesheet" href="assets/style.css">
</head>
<body>
<div class="login-wrap">
    <div class="login-card">
        <div class="login-brand">
            <div class="mark"><i data-lucide="tv-minimal-play"></i></div>
            <h1><?= gpc_h($panelName) ?></h1>
        </div>
        <?php if ($error): ?>
            <div style="margin-bottom:1rem;padding:0.75rem 1rem;background:var(--bad-soft);color:var(--bad);border-radius:10px;font-size:0.88rem">
                <?= gpc_h($error) ?>
            </div>
        <?php endif; ?>
        <form method="post">
            <div class="form-grid" style="grid-template-columns:1fr">
                <div class="field">
                    <label for="username">Usuário</label>
                    <div class="input-wrap">
                        <i data-lucide="user"></i>
                        <input type="text" id="username" name="username" required autofocus autocomplete="username">
                    </div>
                </div>
                <div class="field">
                    <label for="password">Senha</label>
                    <div class="input-wrap">
                        <i data-lucide="lock"></i>
                        <input type="password" id="password" name="password" required autocomplete="current-password">
                    </div>
                </div>
            </div>
            <button type="submit" class="btn" style="width:100%;margin-top:1.25rem">
                <i data-lucide="arrow-right"></i> Entrar
            </button>
        </form>
    </div>
</div>
<script src="https://unpkg.com/lucide@latest/dist/umd/lucide.min.js"></script>
<script src="assets/app.js"></script>
</body>
</html>
