import React, { useState } from 'react'; import { X, AlertTriangle, XCircle, Loader2, Info } from 'lucide-react'; import { cancelBooking, type Booking } from '../services/bookingService'; import { toast } from 'react-toastify'; import { useFormatCurrency } from '../../payments/hooks/useFormatCurrency'; interface CancelBookingModalProps { isOpen: boolean; booking: Booking | null; onClose: () => void; onSuccess: () => void; } const CancelBookingModal: React.FC = ({ isOpen, booking, onClose, onSuccess, }) => { const { formatCurrency } = useFormatCurrency(); const [cancelling, setCancelling] = useState(false); if (!isOpen || !booking) return null; // Check if booking is fully paid const isFullyPaid = (() => { // Check payment_status first if (booking.payment_status === 'paid') { return true; } // Check payment_balance if (booking.payment_balance?.is_fully_paid === true) { return true; } // Check payments array - sum all completed payments if (booking.payments && Array.isArray(booking.payments)) { const totalPaid = booking.payments .filter((p: { payment_status?: string }) => p.payment_status === 'completed') .reduce((sum: number, p: { amount?: number | string }) => sum + parseFloat(p.amount?.toString() || '0'), 0); return totalPaid >= booking.total_price - 0.01; // Allow small rounding differences } return false; })(); const cancellationFee = booking.total_price * 0.2; const refundAmount = isFullyPaid ? booking.total_price - cancellationFee : 0; const handleCancel = async () => { try { setCancelling(true); const response = await cancelBooking(booking.id); if (response.success || (response as { status?: string }).status === 'success') { toast.error( `Booking ${booking.booking_number} has been cancelled` ); onSuccess(); onClose(); } else { throw new Error(response.message || 'Unable to cancel booking'); } } catch (err: unknown) { console.error('Error cancelling booking:', err); const errorResponse = (err && typeof err === 'object' && 'response' in err && err.response && typeof err.response === 'object' && 'data' in err.response && err.response.data && typeof err.response.data === 'object') ? err.response.data as { detail?: string; message?: string } : null; const errorMessage = err instanceof Error ? err.message : undefined; const message = errorResponse?.detail || errorResponse?.message || errorMessage || 'Unable to cancel booking. Please try again.'; toast.error(message); } finally { setCancelling(false); } }; return (
{/* Header */}

Cancel Booking

Booking #{booking.booking_number}

{/* Content */}
{/* Warning Message */}

Are you sure you want to cancel this booking?

This action cannot be undone. The room will be made available for other guests.

{/* Booking Details */}

Booking Details

Room {booking.room?.room_number || 'N/A'}
Check-in {new Date(booking.check_in_date).toLocaleDateString('en-US')}
Check-out {new Date(booking.check_out_date).toLocaleDateString('en-US')}
Total Amount {formatCurrency(booking.total_price)}
{/* Cancellation Policy */}

Cancellation Policy

Cancellation Fee (20%) -{formatCurrency(cancellationFee)}
{isFullyPaid && ( <>
Refund Amount (80%) {formatCurrency(refundAmount)}

The refund will be processed to your original payment method within 5-7 business days.

)} {!isFullyPaid && (

Since this booking is not fully paid, no refund will be issued. The cancellation fee applies to the total booking amount.

)}
{/* Action Buttons */}
); }; export default CancelBookingModal;