'use client'; import { useState } from 'react'; import { X, Loader2 } from 'lucide-react'; import { inventoryApi } from '@/lib/api'; import { toast } from 'react-hot-toast'; interface CreateUserModalProps { show: boolean; onClose: () => void; onUserCreated: () => void; } export default function CreateUserModal({ show, onClose, onUserCreated }: CreateUserModalProps) { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [errors, setErrors] = useState<{ username?: string; password?: string }>({}); const [loading, setLoading] = useState(false); if (!show) return null; const validateForm = () => { const newErrors: typeof errors = {}; if (!username.trim()) { newErrors.username = 'Username is required'; } if (!password.trim()) { newErrors.password = 'Password is required'; } else if (password.length < 6) { newErrors.password = 'Password must be at least 6 characters'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) return; setLoading(true); try { await inventoryApi.createUser({ username: username.trim(), password, role: 'user' }); toast.success('User created successfully'); setUsername(''); setPassword(''); setErrors({}); onUserCreated(); onClose(); } catch (error: any) { const errorMsg = error?.response?.data?.detail || 'Failed to create user'; if (errorMsg.includes('already exists')) { setErrors({ username: 'Username already exists' }); } else { toast.error(errorMsg); } } finally { setLoading(false); } }; const isFormValid = username.trim() && password.length >= 6 && !Object.keys(errors).length; return (