diff --git a/app/(dashboard)/task/client-detail/[client_id]/project-assignments/ProjectAssignmentForm.tsx b/app/(dashboard)/task/client-detail/[client_id]/project-assignments/ProjectAssignmentForm.tsx index 8addd5b..8748c27 100644 --- a/app/(dashboard)/task/client-detail/[client_id]/project-assignments/ProjectAssignmentForm.tsx +++ b/app/(dashboard)/task/client-detail/[client_id]/project-assignments/ProjectAssignmentForm.tsx @@ -5,13 +5,19 @@ import { useRouter } from 'next/navigation'; import { useForm } from 'react-hook-form'; import { yupResolver } from '@hookform/resolvers/yup'; import { motion, AnimatePresence } from 'motion/react'; -import { ArrowLeft, ArrowRight, Save, Briefcase, Clock, Search, Check, CheckCircle, Plus, Building2, CheckCircle2 } from 'lucide-react'; +import { + ArrowLeft, ArrowRight, Save, Briefcase, Clock, Search, Check, CheckCircle, + Plus, CheckCircle2, RotateCcw, X, ChevronDown, FolderGit2 +} from 'lucide-react'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; -import { Textarea } from '@/components/ui/textarea'; -import { Card, CardContent, CardHeader, CardTitle, CardDescription, Card900 } from '@/components/ui/card'; +import { Checkbox } from '@/components/ui/checkbox'; +import { + InputGroup, InputGroupAddon, InputGroupInput, InputGroupText, InputGroupTextarea +} from '@/components/ui/input-group'; +import { CardContent, CardHeader, CardTitle, CardDescription, Card900 } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Separator } from '@/components/ui/separator'; import { @@ -50,8 +56,8 @@ export const assignmentSchema = yup.object({ code: yup.string().required('Project code is required'), year: yup.string().required('Year is required'), startDate: yup.date().required('Start date is required'), - endDate: yup.date().nullable().min(yup.ref('startDate'), 'End date cannot be before start date').default(null), - description: yup.string().optional().nullable().default(''), + endDate: yup.date().required().min(yup.ref('startDate'), 'End date cannot be before start date'), + description: yup.string().optional().nullable().max(500, 'Description cannot exceed 500 characters').default(''), // Work Orders Step (Conditional based on 'type') workOrder: yup.object().when('type', { @@ -60,8 +66,8 @@ export const assignmentSchema = yup.object({ name: yup.string().required('Work order name is required'), code: yup.string().required('Work order code is required'), startDate: yup.date().required('Start date is required'), - endDate: yup.date().nullable().default(null), - description: yup.string().optional().nullable().default(''), + endDate: yup.date().required().min(yup.ref('startDate'), 'End date cannot be before start date'), + description: yup.string().optional().nullable().max(500, 'Description cannot exceed 500 characters').default(''), }), otherwise: (schema) => schema.nullable().notRequired() }) @@ -81,6 +87,7 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign const [showClientForm, setShowClientForm] = useState(false) const [selectedClient, setSelectedClient] = useState(client || null) const [selectedEndUser, setSelectedEndUser] = useState(null) + const [isSameAsProject, setIsSameAsProject] = useState(false); const router = useRouter(); @@ -109,6 +116,34 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign const { data: endUserList, mutate: mutateEndUserList } = useFetchWithAxios('/end-user') const { data: projectList, mutate: mutateProjectList } = useFetchWithAxios(`/client/project/${formValues.client_id}`) + // Sync work order name when "Same as Project Name" is enabled + useEffect(() => { + if (isSameAsProject) { + form.setValue('workOrder.name' as any, formValues.name || '', { shouldValidate: true }); + } + }, [isSameAsProject, formValues.name, form]); + + const handleClearProject = () => { + form.setValue('name', '', { shouldValidate: false }); + form.setValue('code', '', { shouldValidate: false }); + form.setValue('year', new Date().getFullYear().toString(), { shouldValidate: false }); + form.setValue('startDate', undefined as any, { shouldValidate: false }); + form.setValue('endDate', undefined as any, { shouldValidate: false }); + form.setValue('description', '', { shouldValidate: false }); + setIsExistProject(false); + toast.info('Project form cleared'); + }; + + const handleClearWorkOrder = () => { + setIsSameAsProject(false); + form.setValue('workOrder.name' as any, '', { shouldValidate: false }); + form.setValue('workOrder.code' as any, '', { shouldValidate: false }); + form.setValue('workOrder.startDate' as any, undefined as any, { shouldValidate: false }); + form.setValue('workOrder.endDate' as any, undefined as any, { shouldValidate: false }); + form.setValue('workOrder.description' as any, '', { shouldValidate: false }); + toast.info('Work order form cleared'); + }; + // 3. Navigation & Validation Logic const steps: { id: IStep; label: string }[] = [ { id: 'type', label: 'Type' }, @@ -142,11 +177,20 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign form.setValue('description', data.project?.description || data.im_description) form.setValue('workOrder.name' as any, data.wo_name) - form.setValue('workOrder.code' as any, data.wo_code) + let initialWoCode = data.wo_code || ''; + const prjCode = data.project?.code || data.im_code || ''; + if (prjCode && initialWoCode.startsWith(`${prjCode}-`)) { + initialWoCode = initialWoCode.slice(prjCode.length + 1); + } + form.setValue('workOrder.code' as any, initialWoCode) form.setValue('workOrder.startDate' as any, data.wo_start_date) form.setValue('workOrder.endDate' as any, data.wo_end_date) form.setValue('workOrder.description' as any, data.wo_description) + if (data.wo_name && (data.project?.name || data.im_name) && data.wo_name === (data.project?.name || data.im_name)) { + setIsSameAsProject(true); + } + setStep('clients') setIsExistAssignment(res.data.data) @@ -193,6 +237,15 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign const onSubmit = async (payload: AssignmentFormValues) => { setIsLoading(true); + const projectCode = payload.code?.trim() || ''; + const rawWoCode = ((payload.workOrder as any)?.code || '').trim(); + let finalWoCode = ''; + if (rawWoCode) { + finalWoCode = rawWoCode.startsWith(`${projectCode}-`) + ? rawWoCode + : `${projectCode}-${rawWoCode}`; + } + const newPayload = { "client_id": Number(payload.client_id), "end_user_id": payload.end_user_id ? Number(payload.end_user_id) : null, @@ -211,8 +264,8 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign "project_end_date": assignmentType === 'Project' ? payload.endDate ? dayjs(payload.endDate).format('YYYY-MM-DD') : '' : '', "project_description": assignmentType === 'Project' ? payload.description : '', - "wo_code": (payload.workOrder as any).code, - "wo_name": (payload.workOrder as any).name, + "wo_code": assignmentType === 'Project' ? finalWoCode : '', + "wo_name": (payload.workOrder as any)?.name || '', "wo_start_date": (payload.workOrder as any).startDate ? dayjs((payload.workOrder as any).startDate).format('YYYY-MM-DD') : '', "wo_end_date": (payload.workOrder as any).endDate ? dayjs((payload.workOrder as any).endDate).format('YYYY-MM-DD') : '', "wo_description": (payload.workOrder as any).description, @@ -301,7 +354,7 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign (
- field.onChange('Project')} + field.onChange('Project')} className={cn("cursor-pointer transition-all hover:border-his dark:hover:border-his duration-400", field.value === 'Project' && "ring-2 ring-his/10 border-his dark:ring-his/10 dark:border-his")} > @@ -315,9 +368,9 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign

- + - field.onChange('IM')} + field.onChange('IM')} className={cn("cursor-pointer transition-all hover:border-his dark:hover:border-his duration-400", field.value === 'IM' && "ring-2 ring-his/10 border-his dark:ring-his/10 dark:border-his")} > @@ -331,7 +384,7 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign

-
+
@@ -401,7 +454,7 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign renderOption={(option, isSelected) => { const user = option.originalData as IEndUser; return ( -
+
@@ -435,65 +488,221 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign {/* STEP 3: DETAILS */} {step === 'details' && ( - - {assignmentType === 'IM' ? 'Internal Memo' : 'Project'} Details - Provide the basic information for this assignment. + +
+ {assignmentType === 'IM' ? 'Internal Memo' : 'Project'} Details + Provide the basic information for this assignment. +
+
+ {/* Existing Project Selector / Linked Hero Card */} + {assignmentType === 'Project' && ( + !isExistProject ? ( +
+
+
+ + Import Existing Project + + Optional + +
+

+ Select an existing project for {selectedClient?.name || 'this client'} to autofill information, or create a new one below. +

+
+ + + + + + + + + No existing projects found. + + {projectList?.map((p: IProject) => ( + { + form.setValue('name', p.name!, { shouldValidate: true }); + form.setValue('code', p.code!, { shouldValidate: true }); + form.setValue('year', String(p.year), { shouldValidate: true }); + form.setValue('startDate', new Date(p.start_date!), { shouldValidate: true }); + form.setValue('endDate', new Date(p.end_date!), { shouldValidate: true }); + form.setValue('description', p.description || '', { shouldValidate: true }); + + setOpenProjectSearch(false); + setIsExistProject(true); + toast.info(`Autofilled details from ${p.name}`); + }} + className="cursor-pointer py-2.5 px-3" + > +
+ +
+ + {p.name} + +
+ + # {p.code} + + • + Year: {p.year} +
+
+
+
+ ))} +
+
+
+
+
+
+ ) : ( +
+
+
+ + + Existing Project Linked + + + Code: # {formValues.code} + +
+
+ + + + + + + + + No existing projects found. + + {projectList?.map((p: IProject) => ( + { + form.setValue('name', p.name!, { shouldValidate: true }); + form.setValue('code', p.code!, { shouldValidate: true }); + form.setValue('year', String(p.year), { shouldValidate: true }); + form.setValue('startDate', new Date(p.start_date!), { shouldValidate: true }); + form.setValue('endDate', new Date(p.end_date!), { shouldValidate: true }); + form.setValue('description', p.description || '', { shouldValidate: true }); + + setOpenProjectSearch(false); + setIsExistProject(true); + toast.info(`Switched project to ${p.name}`); + }} + className="cursor-pointer py-2.5 px-3" + > +
+ +
+ + {p.name} + +
+ + {p.code} + + • + Year: {p.year} +
+
+
+
+ ))} +
+
+
+
+
+ +
+
+ +
+

+ {formValues.name} +

+
+ Year: {formValues.year} + {formValues.startDate && ( + <> + • + + Timeline:{" "} + + {dayjs(formValues.startDate).format('DD MMM YYYY')} - {formValues.endDate ? dayjs(formValues.endDate).format('DD MMM YYYY') : 'Ongoing'} + + + + )} +
+
+ +

+ Fields below are locked and autofilled from this existing project. Click Clear / Create New to enter new project details manually. +

+
+ ) + )} + ( {assignmentType === 'Project' ? "Project" : "Internal Memo"} Name -
-
- - - - {assignmentType === 'Project' && ( -
- - - - - - - - - No projects found. - - {projectList.map((p: IProject) => ( - { - form.setValue('name', p.name!, { shouldValidate: true }); - form.setValue('code', p.code!, { shouldValidate: true }); - form.setValue('year', String(p.year), { shouldValidate: true }); - form.setValue('startDate', new Date(p.start_date!), { shouldValidate: true }); - form.setValue('endDate', new Date(p.end_date!), { shouldValidate: true }); - form.setValue('description', p.description!, { shouldValidate: true }); - - setOpenProjectSearch(false); - setIsExistProject(true) - toast.info(`Autofilled details from ${p.name}`); - }} - > - - {p?.name!} - - ))} - - - - - -
- )} -
-
+ + +
)} @@ -533,70 +742,198 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign )} />
- ( - - Description - -