import * as React from "react" import { CheckIcon } from "lucide-react" import { cn } from "../lib/utils" import { formatMoney } from "../lib/format" import { Button } from "../ui/button" import { Checkbox } from "../ui/checkbox" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "../ui/dialog" import { ScrollArea } from "../ui/scroll-area" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../ui/select" import { ContentTabs } from "./content-tabs" import { SearchInput } from "./search-input" export interface ServiceOption { readonly code: string readonly name: string readonly category: string readonly price: number readonly funding?: string } export interface ServiceScope { readonly id: string readonly label: string readonly match: (item: T) => boolean } const ANY = "__any" /** * Диалог «Выбор услуг» как в МИС: набор (услуги врача / внутренние / все), * источник финансирования, категория, поиск, отметки и итог выбранного. * Уже добавленные услуги показаны отмеченными и недоступны. */ export function ServicePicker({ open, onOpenChange, items, scopes, defaultScope, defaultFunding, added = [], onAdd, title = "Выбор услуг", }: { open: boolean onOpenChange: (open: boolean) => void items: readonly T[] scopes: readonly ServiceScope[] defaultScope?: string defaultFunding?: string | null /** Коды уже выбранных услуг. */ added?: readonly string[] onAdd: (items: T[]) => void title?: string }) { const [scope, setScope] = React.useState(defaultScope ?? scopes[0]?.id ?? "") const [funding, setFunding] = React.useState(defaultFunding ?? ANY) const [category, setCategory] = React.useState(ANY) const [query, setQuery] = React.useState("") const [selected, setSelected] = React.useState>(new Set()) // Каждое открытие — с чистого листа и с фильтрами по умолчанию. React.useEffect(() => { if (!open) return setScope(defaultScope ?? scopes[0]?.id ?? "") setFunding(defaultFunding ?? ANY) setCategory(ANY) setQuery("") setSelected(new Set()) }, [open, defaultScope, defaultFunding, scopes]) const fundings = [...new Set(items.map((i) => i.funding).filter(Boolean))] as string[] const scoped = items.filter((i) => scopes.find((s) => s.id === scope)?.match(i) ?? true) const categories = [...new Set(scoped.map((i) => i.category))] const q = query.trim().toLowerCase() const visible = scoped.filter( (i) => (funding === ANY || i.funding === funding) && (category === ANY || i.category === category) && (!q || `${i.code} ${i.name}`.toLowerCase().includes(q)), ) const picked = items.filter((i) => selected.has(i.code)) const pickedSum = picked.reduce((sum, i) => sum + i.price, 0) const toggle = (code: string) => setSelected((set) => { const next = new Set(set) if (next.has(code)) next.delete(code) else next.add(code) return next }) const submit = () => { if (!picked.length) return onAdd(picked) onOpenChange(false) } return ( {title} Отметьте услуги и нажмите «Добавить услуги»
({ id: s.id, label: s.label, count: items.filter(s.match).length }))} value={scope} onValueChange={(v) => { setScope(v) setCategory(ANY) }} />
    {visible.map((item) => { const already = added.includes(item.code) const checked = already || selected.has(item.code) return (
  • ) })} {!visible.length &&
  • Услуги не найдены
  • }
{picked.length ? ( <> Выбрано {picked.length} на{" "} {formatMoney(pickedSum)} ) : ( "Отметьте услуги в списке" )}
) }