"use client"; import * as React from "react"; import { useActionState, useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { Loader2, Trash2 } from "lucide-react"; import { toast } from "sonner"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { deleteConnectionAction } from "@/lib/appwrite/connection-actions"; import { initialConnectionActionState } from "@/lib/appwrite/connection-types"; import type { ConnectionWithCounterpart } from "@/lib/appwrite/connection-queries"; import type { ClinicPricing, TenantKind } from "@/lib/appwrite/schema"; import { PROSTHETIC_TYPE_OPTIONS } from "@/lib/appwrite/prosthetic-types"; import { ClinicPricingDialog } from "./clinic-pricing-dialog"; const TYPE_LABELS = Object.fromEntries( PROSTHETIC_TYPE_OPTIONS.map((o) => [o.value, o.label]), ) as Record; const dateFormatter = new Intl.DateTimeFormat("tr-TR", { day: "2-digit", month: "2-digit", year: "numeric", }); export function ConnectionsTable({ rows, selfKind, pricingByCounterpart = {}, defaultCurrency = "TRY", }: { rows: ConnectionWithCounterpart[]; selfKind: TenantKind | null; pricingByCounterpart?: Record; defaultCurrency?: string; }) { if (rows.length === 0) { return (

Henüz onaylanmış bağlantınız yok. Yukarıdan talep gönderebilir veya kodunuzu paylaşabilirsiniz.

); } const isLab = selfKind === "lab"; return ( Karşı taraf Tür Onay tarihi {isLab ? "Fiyat kuralları" : "Sizin için kurallar"} İşlem {rows.map((r) => { const counterpartId = isLab ? r.clinicTenantId : r.labTenantId; const pricing = pricingByCounterpart[counterpartId] ?? []; return ( ); })}
); } function ApprovedRow({ row, isLab, pricing, defaultCurrency, }: { row: ConnectionWithCounterpart; isLab: boolean; pricing: ClinicPricing[]; defaultCurrency: string; }) { const [state, action, pending] = useActionState( deleteConnectionAction, initialConnectionActionState, ); const router = useRouter(); const [open, setOpen] = useState(false); useEffect(() => { if (state.ok) { toast.success("Bağlantı silindi."); setOpen(false); router.refresh(); } else if (state.error) { toast.error(state.error); } }, [state, router]); const kindLabel = row.counterpart?.kind === "lab" ? "Laboratuvar" : row.counterpart?.kind === "clinic" ? "Klinik" : "—"; const counterpartId = isLab ? row.clinicTenantId : row.labTenantId; return ( {row.counterpart?.companyName ?? "—"} {kindLabel} {row.approvedAt ? dateFormatter.format(new Date(row.approvedAt)) : "—"} {pricing.length === 0 ? ( Katalog fiyatı ) : ( {pricing.slice(0, 3).map((p, idx) => ( {idx > 0 && ", "} {TYPE_LABELS[p.prostheticType] ?? p.prostheticType} {": "} {p.customUnitPrice !== undefined && p.customUnitPrice !== null ? `${p.customUnitPrice} ${p.currency || defaultCurrency}` : p.discountPercent ? `%${p.discountPercent} indirim` : "—"} ))} {pricing.length > 3 && `, +${pricing.length - 3} kural`} )}
{isLab && ( )} Bağlantı silinsin mi? {row.counterpart?.companyName ?? "Karşı taraf"} ile bağlantınız sonlandırılacak. Mevcut işleriniz etkilenmez ancak yeni iş gönderemezsiniz.
); }