import { Fragment } from 'react' import { Trash2Icon, PlusIcon, CircleQuestionMarkIcon, ArrowRightIcon } from 'lucide-react' import { useForm, useFieldArray, Controller, useWatch } from 'react-hook-form' import { useParams } from 'react-router' import { ErrorMessage } from '@hookform/error-message' import { zodResolver } from '@hookform/resolvers/zod' import { z } from 'zod' import { Form } from '@repo/ui/components/ui/form' import { InputGroup, InputGroupAddon, InputGroupInput } from '@repo/ui/components/ui/input-group' import { Button } from '@repo/ui/components/ui/button' import { Separator } from '@repo/ui/components/ui/separator' import { Kbd } from '@repo/ui/components/ui/kbd' import { Spinner } from '@repo/ui/components/ui/spinner' import { HoverCard, HoverCardContent, HoverCardTrigger } from '@repo/ui/components/ui/hover-card' import { MAX_ITEMS, formSchema, type Enrollment, type Course, type User } from '../_.$orgid.enrollments.add/data' import { ScheduledForInput } from '../_.$orgid.enrollments.add/scheduled-for' import { Cell } from '../_.$orgid.enrollments.add/route' import { CoursePicker } from '../_.$orgid.enrollments.add/course-picker' import { UserPicker } from '../_.$orgid.enrollments.add/user-picker' import { Summary } from './bulk' import { currency } from './utils' import { useWizard } from '@/components/wizard' const emptyRow = { user: undefined, course: undefined, scheduled_for: undefined } type Item = { course: Enrollment['course'] quantity: number } const formSchemaAssigned = formSchema.extend({ coupon: z .object({ code: z.string(), type: z.enum(['FIXED', 'PERCENT']), amount: z.number().positive() }) .optional() }) type Schema = z.infer type AssignedProps = { onSubmit: (value: any) => void | Promise courses: Promise<{ hits: Course[] }> enrollments: object[] coupon?: object } export function Assigned({ courses, onSubmit, enrollments, coupon: couponInit }: AssignedProps) { const wizard = useWizard() const { orgid } = useParams() const form = useForm({ resolver: zodResolver(formSchemaAssigned), defaultValues: { coupon: couponInit, enrollments: enrollments?.map((e: any) => ({ ...e, scheduled_for: e.scheduled_for ? new Date(e.scheduled_for) : undefined })) || [emptyRow] } }) const { formState, control, handleSubmit, setValue } = form const { fields, remove, append } = useFieldArray({ control, name: 'enrollments' }) const items = useWatch({ control, name: 'enrollments' }) const coupon = useWatch({ control, name: 'coupon' }) const subtotal = items.reduce( (acc, { course }) => acc + (course?.unit_price || 0), 0 ) const onSearch = async (search: string) => { const params = new URLSearchParams({ q: search }) const r = await fetch(`/${orgid}/users.json?${params.toString()}`) const { hits } = (await r.json()) as { hits: User[] } return hits } const onSubmit_ = async ({ enrollments }: Schema) => { const items = Object.values( enrollments.reduce>((acc, e) => { const id = e.course.id acc[id] ??= { course: e.course, quantity: 0 } acc[id].quantity++ return acc }, {}) ) await onSubmit({ enrollments, items }) wizard('payment') } return (
{/* Header */} <> Colaborador Curso Matricular em

Escolha a data em que o colaborador será matriculado no curso.

Você poderá acompanhar as matrículas em{' '} Agendamentos

Valor unit. {/**/} {/* Rows */} {fields.map((field, index) => { const { unit_price } = items?.[index]?.course || { unit_price: 0 } return ( {/* Separator only for mobile */} {index >= 1 &&
} {/* User */} (
(

{message}

)} />
)} /> {/* Course */} (
(

{message}

)} />
)} /> {/* Scheduled for */} ( )} /> {/* Unit price */} Valor unit. {/* Action */}
) })} {/* Add button */}
{/* Summary */}
) }