"use client"; import { useState } from "react"; import { z } from "zod"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; import { Button, Dialog, FormField, Input } from "@/components/ds"; import { loyaltyApi } from "@/modules/loyalty/services/loyalty-api"; import { createLoyaltyListPage } from "@/modules/loyalty/components/LoyaltyListCrudPage"; import { LoyaltyStatusChip } from "@/modules/loyalty/design-system"; import type { WalletAccount } from "@/modules/loyalty/types"; import { useTenantId } from "@/hooks/useTenantId"; const createSchema = z.object({ program_id: z.string().uuid(), member_id: z.string().uuid(), account_number: z.string().optional(), currency_code: z.string().default("IRR"), status: z.enum(["open", "frozen", "closed"]).default("open"), }); const editSchema = z.object({ status: z.enum(["open", "frozen", "closed"]).optional(), }); function WalletOps({ id }: { id: string }) { const { tenantId } = useTenantId(); const qc = useQueryClient(); const [op, setOp] = useState<"credit" | "debit" | "adjust" | null>(null); const [amount, setAmount] = useState("1000"); const [reason, setReason] = useState(""); const bal = useQuery({ queryKey: ["loyalty", tenantId, "wallet-bal", id], queryFn: () => loyaltyApi.wallets.balance(tenantId!, id), enabled: !!tenantId, }); const m = useMutation({ mutationFn: () => { const body = { amount: Number(amount), reason: reason || undefined }; if (op === "credit") return loyaltyApi.wallets.credit(tenantId!, id, body); if (op === "debit") return loyaltyApi.wallets.debit(tenantId!, id, body); return loyaltyApi.wallets.adjust(tenantId!, id, { amount: Number(amount), reason: reason || "adjust", }); }, onSuccess: () => { toast.success("تراکنش کیف پول ثبت شد"); setOp(null); qc.invalidateQueries({ queryKey: ["loyalty", tenantId, "wallet"] }); }, onError: (e: Error) => toast.error(e.message), }); return (