import { useState } from "react"; import { Banknote, Pencil, Receipt } from "lucide-react"; import { Button, Divider, Group, NumberInput, Paper, Stack, Text, Textarea, } from "@mantine/core"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import toast from "react-hot-toast"; import type { BookingDetail } from "@/types/booking"; import { bookingsService } from "@/services/bookings.service"; import { SectionCard } from "./detail/SectionCard"; import { detailStyles } from "./detail/booking-detail.styles"; export function BookingPricingSummary({ booking }: { booking: BookingDetail }) { const qc = useQueryClient(); const computed = Number(booking.totalAmount); const isAdjusted = booking.adjustedTotalAmount !== null && booking.adjustedTotalAmount !== undefined; const effective = isAdjusted ? Number(booking.adjustedTotalAmount) : computed; const lineItems = booking.pricingBreakdown?.lineItems ?? []; const [editing, setEditing] = useState(false); const [amount, setAmount] = useState(effective); const [reason, setReason] = useState(""); const adjustMutation = useMutation({ mutationFn: (payload: { amount: number | null; reason?: string }) => bookingsService.adjustPrice(booking.id, payload.amount, payload.reason), onSuccess: () => { toast.success("Price updated"); setEditing(false); qc.invalidateQueries({ queryKey: ["bookings"] }); }, onError: () => toast.error("Could not update price"), }); const fmt = (n: number) => `${booking.paymentCurrency} ${n.toLocaleString(undefined, { minimumFractionDigits: 2 })}`; return (
{isAdjusted ? "Adjusted total" : "Total amount"} {fmt(effective)} {isAdjusted && ( Computed: {fmt(computed)} {booking.adjustmentReason ? ` ยท ${booking.adjustmentReason}` : ""} )}
{!editing && ( )}
{editing && ( setAmount(v === "" ? "" : Number(v))} min={0} radius="md" prefix={`${booking.paymentCurrency} `} thousandSeparator="," />