import { Fragment } from 'react' import { Trash2Icon, PlusIcon, CircleQuestionMarkIcon } 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 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 { applyDiscount } from './discount' import { currency } from './utils' const emptyRow = { user: undefined, course: undefined, scheduled_for: undefined } const formSchema_ = 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[] }> } export function Assigned({ courses, onSubmit }: AssignedProps) { const { orgid } = useParams() const form = useForm({ resolver: zodResolver(formSchema_), defaultValues: { enrollments: [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 discount = coupon ? applyDiscount(subtotal, coupon.amount, coupon.type) * -1 : 0 const total = subtotal > 0 ? subtotal + discount : 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 (data: Schema) => { await onSubmit(data) } 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 */}
) }