import { useState } from "react"; import { Check, X, Loader2 } from "lucide-react"; import { useLedgerPayouts, useLedgerPayoutAction, type LedgerPayoutRow, } from "~/requestHandler/use-admin-hooks"; import { useToast } from "~/hooks/use-toast"; import { AdminTable, AdminPagination, AdminToolbar, AdminSearch, AdminSelect, StatusBadge, faToman, faDate, useDebouncedValue, type Column, } from "~/components/admin/DataTable"; const PAGE_SIZE = 25; const STATUS: Record = { pending: { label: "در انتظار", tone: "yellow" }, completed: { label: "پرداخت‌شده", tone: "green" }, rejected: { label: "ردشده", tone: "red" }, }; export default function LedgerPayouts() { const [page, setPage] = useState(1); const [search, setSearch] = useState(""); const [status, setStatus] = useState(""); const debouncedSearch = useDebouncedValue(search); const action = useLedgerPayoutAction(); const { toast } = useToast(); const { data, isLoading } = useLedgerPayouts({ page, page_size: PAGE_SIZE, search: debouncedSearch, status, }); const reset = (fn: () => void) => { fn(); setPage(1); }; const run = (id: string, act: "complete" | "reject") => action.mutate( { id, action: act }, { onSuccess: () => toast({ title: act === "complete" ? "برداشت تأیید شد" : "برداشت رد شد", description: act === "complete" ? "مبلغ از مانده فروشنده کسر و در دفتر ثبت شد." : undefined, }), onError: (e: Error) => toast({ title: "خطا", description: e.message, variant: "destructive" }), } ); const pendingId = action.isPending ? action.variables?.id : null; const actBtn = "inline-flex items-center gap-1 px-2 py-1 rounded-lg text-[12px] font-semibold transition-colors disabled:opacity-40 cursor-pointer"; const columns: Column[] = [ { header: "فروشگاه", render: (p) => (

{p.sellerName || "—"}

@{p.sellerUsername}

), }, { header: "مبلغ", render: (p) => {faToman(p.amount)}, }, { header: "وضعیت", render: (p) => { const s = STATUS[p.status] || { label: p.status, tone: "yellow" as const }; return ; }, }, { header: "بانک", render: (p) => (

{p.bankName || "—"}

{p.iban || ""}

), }, { header: "درخواست", render: (p) => {faDate(p.requestedAt)}, }, { header: "پرداخت", render: (p) => {faDate(p.processedAt)}, }, { header: "عملیات", className: "text-left", render: (p) => { if (p.status !== "pending") return ; if (pendingId === p.id) return ; return (
); }, }, ]; return (
reset(() => setSearch(v))} placeholder="جستجوی فروشگاه…" /> reset(() => setStatus(v))} options={[ { value: "", label: "همه وضعیت‌ها" }, { value: "pending", label: "در انتظار" }, { value: "completed", label: "پرداخت‌شده" }, { value: "rejected", label: "ردشده" }, ]} /> p.id} />
); }