// Caminho: manager/usuario/novo/page.tsx "use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Save, Loader2 } from "lucide-react"; // 🔧 Correção: importava DashboardLayout, mas deve importar ManagerLayout import ManagerLayout from "@/app/manager/layout"; import { usuariosApi } from "@/services/usuariosApi"; // Removido import incorreto // import { api } from "services/api"; interface UserFormData { email: string; nomeCompleto: string; telefone: string; papel: string; senha: string; confirmarSenha: string; } const defaultFormData: UserFormData = { email: "", nomeCompleto: "", telefone: "", papel: "", senha: "", confirmarSenha: "", }; const cleanNumber = (value: string): string => value.replace(/\D/g, ""); const formatPhone = (value: string): string => { const cleaned = cleanNumber(value).substring(0, 11); if (cleaned.length === 11) return cleaned.replace(/(\d{2})(\d{5})(\d{4})/, "($1) $2-$3"); if (cleaned.length === 10) return cleaned.replace(/(\d{2})(\d{4})(\d{4})/, "($1) $2-$3"); return cleaned; }; export default function NovoUsuarioPage() { const router = useRouter(); const [formData, setFormData] = useState(defaultFormData); const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(null); const handleInputChange = (key: keyof UserFormData, value: string) => { const updatedValue = key === "telefone" ? formatPhone(value) : value; setFormData((prev) => ({ ...prev, [key]: updatedValue })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); if ( !formData.email || !formData.nomeCompleto || !formData.papel || !formData.senha || !formData.confirmarSenha ) { setError("Por favor, preencha todos os campos obrigatórios."); return; } if (formData.senha !== formData.confirmarSenha) { setError("A Senha e a Confirmação de Senha não coincidem."); return; } setIsSaving(true); try { const payload = { email: formData.email.trim().toLowerCase(), full_name: formData.nomeCompleto, phone: formData.telefone || null, phone_mobile: formData.telefone || null, role: formData.papel || "paciente", cpf: "00000000000", create_patient_record: true }; await usuariosApi.createUser(payload); router.push("/manager/usuario"); } catch (e: any) { console.error("Erro ao criar usuário:", e); setError( e?.message || "Não foi possível criar o usuário. Verifique os dados e tente novamente." ); } finally { setIsSaving(false); } }; return (

Novo Usuário

Preencha os dados para cadastrar um novo usuário no sistema.

{error && (

Erro no Cadastro:

{error}

)}
handleInputChange("nomeCompleto", e.target.value) } placeholder="Nome e Sobrenome" required />
handleInputChange("email", e.target.value)} placeholder="exemplo@dominio.com" required />
handleInputChange("senha", e.target.value)} placeholder="Mínimo 8 caracteres" minLength={8} required />
handleInputChange("confirmarSenha", e.target.value) } placeholder="Repita a senha" required /> {formData.senha && formData.confirmarSenha && formData.senha !== formData.confirmarSenha && (

As senhas não coincidem.

)}
handleInputChange("telefone", e.target.value) } placeholder="(00) 00000-0000" maxLength={15} />
); }