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

$id = intval($_GET['id'] ?? 0);
$u = ['nome'=>'','email'=>'','nivel'=>'','perfil'=>''];

if ($id > 0) {
    $stmt = $pdo->prepare("SELECT * FROM usuarios WHERE id = :id");
    $stmt->execute(['id' => $id]);
    $u = $stmt->fetch() ?: $u;
}
$titulo = $id > 0 ? 'Editar Usuario' : 'Novo Usuario';
?>

<div class="page-header">
    <div><h2><?= $titulo ?></h2><div class="subtitle">Preencha os dados do usuario</div></div>
    <div><a href="usuarios.php" class="btn btn-secondary">Voltar</a></div>
</div>

<form method="POST" action="usuario_salvar.php">
<input type="hidden" name="id" value="<?= $id ?>">

<div class="card-premium" style="padding:25px;margin-bottom:20px;">
    <h3 style="color:var(--accent);margin-bottom:15px;border-bottom:1px solid var(--border);padding-bottom:10px;">Dados do Usuario</h3>
    <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:15px;">
        <div class="form-group"><label>Nome Completo *</label><input name="nome" value="<?= sanitize($u['nome']) ?>" required></div>
        <div class="form-group"><label>Email *</label><input type="email" name="email" value="<?= sanitize($u['email']) ?>" required></div>
        <div class="form-group"><label><?= $id > 0 ? 'Nova Senha (deixe vazio para manter)' : 'Senha *' ?></label><input type="password" name="senha" <?= $id > 0 ? '' : 'required' ?>></div>
        <div class="form-group"><label>Nivel de Acesso</label>
            <select name="nivel"><option value="">Selecione</option><option value="Administrador" <?= ($u['nivel']??'')==='Administrador'?'selected':'' ?>>Administrador</option><option value="Secretaria" <?= ($u['nivel']??'')==='Secretaria'?'selected':'' ?>>Secretaria</option><option value="Financeiro" <?= ($u['nivel']??'')==='Financeiro'?'selected':'' ?>>Financeiro</option><option value="Professor" <?= ($u['nivel']??'')==='Professor'?'selected':'' ?>>Professor</option></select></div>
    </div>
</div>

<div style="display:flex;gap:10px;justify-content:flex-end;margin-bottom:30px;">
    <a href="usuarios.php" class="btn btn-secondary">Cancelar</a>
    <button type="submit" class="btn btn-novo">Salvar Usuario</button>
</div>
</form>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>
