import React, { useEffect, useMemo, useState } from 'react'; import { Link } from 'react-router'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { toast } from 'sonner'; import SearchableSelect from './ui/SearchableSelect'; import { api, ApiError, type ApiResponse } from '../lib/api'; import { useCatalogCategories, useCategoryIncludes } from '../hooks/useCatalogCategories'; import type { CatalogCategory } from '../types'; type Flat = { uuid: string; name: string; depth: number }; function flatten(nodes: CatalogCategory[], depth = 0): Flat[] { return nodes.flatMap((n) => [ { uuid: n.uuid, name: n.name, depth }, ...flatten(n.children ?? [], depth + 1), ]); } /** * دستهٔ یک سرویس — فقط **انتخاب** از کاتالوگ سراسری. * * ساخت، ویرایش و حذف دسته عمداً اینجا نیست؛ فقط در «تنظیمات ← دسته‌بندی‌ها». اگر هر * صفحه‌ای بتواند دسته بسازد، «تمام بدن» چند بار با املاهای مختلف ساخته می‌شود و یال * «شامل بودن» دیگر چیزی را نمی‌گیرد. */ export default function ServiceCategoryTab({ serviceUuid, categoryUuid, canEdit }: { serviceUuid: string; categoryUuid: string | null; canEdit: boolean; }) { const qc = useQueryClient(); const { tree, loading } = useCatalogCategories(); const [selected, setSelected] = useState(categoryUuid); useEffect(() => setSelected(categoryUuid), [categoryUuid]); const all = useMemo(() => flatten(tree), [tree]); const { includes } = useCategoryIncludes(selected ?? undefined); const save = useMutation({ mutationFn: (uuid: string | null) => api.patch>(`/api/v1/service-item/${serviceUuid}`, { catalog_category_uuid: uuid }), onSuccess: () => { toast.success('دسته‌بندی سرویس ذخیره شد'); qc.invalidateQueries({ queryKey: ['service-item', serviceUuid] }); }, onError: (e) => toast.error(e instanceof ApiError ? e.message : 'ذخیرهٔ دسته‌بندی ناموفق بود'), }); return (

دسته‌بندی سراسری کلینیک است و اینجا فقط انتخاب می‌شود. ساخت، ویرایش و حذف فقط در{' '} تنظیمات ← دسته‌بندی‌ها{' '} انجام می‌شود.

{loading ? ( در حال بارگذاری... ) : all.length === 0 ? (

هنوز هیچ دسته‌بندی‌ای تعریف نشده است.

) : (
({ value: c.uuid, label: '— '.repeat(c.depth) + c.name }))} value={selected} onChange={(v) => setSelected(v ? String(v) : null)} placeholder="بدون دسته‌بندی" isDisabled={!canEdit} isClearable height={38} />
)} {selected && includes.length > 0 && (
این دسته شامل:
{includes.map((c) => ( {c.name} ))}
انتخاب هم‌زمان این سرویس با سرویسی از این زیرمجموعه‌ها هنگام رزرو رد می‌شود.
)} {canEdit && (
)}
); }