'use client'; // File: components/delegation/DelegationForm.tsx // Form สร้าง Delegation พร้อม date range picker (FR-011) import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Button } from '@/components/ui/button'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { CreateDelegationDto } from '@/hooks/use-delegation'; import { DelegationScope } from '@/types/review-team'; const delegationSchema = z .object({ delegateUserPublicId: z.string().uuid('Select a valid user'), scope: z.enum(['ALL', 'RFA_ONLY', 'CORRESPONDENCE_ONLY', 'SPECIFIC_TYPES'] as const), startDate: z.string().min(1, 'Start date is required'), endDate: z.string().min(1, 'End date is required'), reason: z.string().max(500).optional(), }) .refine((d: { startDate: string; endDate: string }) => new Date(d.startDate) < new Date(d.endDate), { message: 'End date must be after start date', path: ['endDate'], }); type DelegationFormValues = z.infer; interface User { publicId: string; fullName?: string; email?: string; } interface DelegationFormProps { availableUsers: User[]; onSubmit: (dto: CreateDelegationDto) => void; isLoading?: boolean; } const SCOPE_LABELS: Record = { ALL: 'All Documents', RFA_ONLY: 'RFA Only', CORRESPONDENCE_ONLY: 'Correspondence Only', SPECIFIC_TYPES: 'Specific Document Types', }; export function DelegationForm({ availableUsers, onSubmit, isLoading }: DelegationFormProps) { const form = useForm({ resolver: zodResolver(delegationSchema), defaultValues: { scope: 'ALL' }, }); const handleSubmit = (values: DelegationFormValues) => { onSubmit({ ...values, scope: values.scope as DelegationScope, }); }; return (
( Delegate To )} /> ( Scope )} />
( Start Date )} /> ( End Date )} />
( Reason (Optional)