'use client'; import { useState } from 'react'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Checkbox } from '@/components/ui/checkbox'; import { getScopesByCategory } from '@/lib/types/staff'; import { createDepartment } from '@/lib/actions/staff-management'; import { toast } from 'sonner'; import { Loader2, Plus } from 'lucide-react'; const DEPT_COLORS = ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6', '#EC4899', '#06B6D4']; interface CreateDepartmentDialogProps { onCreated: () => void; } export function CreateDepartmentDialog({ onCreated }: CreateDepartmentDialogProps) { const [open, setOpen] = useState(false); const [loading, setLoading] = useState(false); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [selectedScopes, setSelectedScopes] = useState([]); const [color, setColor] = useState(DEPT_COLORS[0]); const slug = name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, ''); const scopesByCategory = getScopesByCategory(); const toggleScope = (scope: string) => { setSelectedScopes(prev => prev.includes(scope) ? prev.filter(s => s !== scope) : [...prev, scope]); }; const handleSubmit = async () => { if (!name.trim()) { toast.error('Department name is required'); return; } setLoading(true); try { const res = await createDepartment({ name, slug, baseScopes: selectedScopes, color, description }); if (res.success) { toast.success(`Department "${name}" created`); setOpen(false); setName(''); setDescription(''); setSelectedScopes([]); onCreated(); } } catch { toast.error('Failed to create department'); } finally { setLoading(false); } }; return ( Create Department Define a new organizational unit with base permissions.
setName(e.target.value)} placeholder="e.g. Customer Support" /> {slug &&

Slug: {slug}

}
setDescription(e.target.value)} placeholder="What does this department handle?" />
{DEPT_COLORS.map(c => (

All members in this department will inherit these permissions.

{Object.entries(scopesByCategory).map(([category, scopes]) => (

{category}

{scopes.map(({ scope, label }) => ( ))}
))}
); }