import { Button, Card, Checkbox, Divider, Image } from "antd"; import { ProBottomSheet } from "components/ProBottomSheet/ProBottomSheet.tsx"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import ArabicPrice from "components/ArabicPrice"; import ProText from "components/ProText"; import { selectCart, updateSplitBillAmount } from "features/order/orderSlice"; import { useAppDispatch, useAppSelector } from "redux/hooks"; import styles from "./SplitBill.module.css"; import { QRBottomSheet } from "./QRBottomSheet"; interface SplitBillChoiceBottomSheetProps { isOpen: boolean; onClose: () => void; onSave?: (value: string) => void; onRemoveSplitWay?: () => void; } export function PayForYourItemsChoiceBottomSheet({ isOpen, onClose, onRemoveSplitWay, }: SplitBillChoiceBottomSheetProps) { const { t } = useTranslation(); const dispatch = useAppDispatch(); const { items } = useAppSelector(selectCart); const [selectedItems, setSelectedItems] = useState>(new Set()); const [isQROpen, setIsQROpen] = useState(false); // Calculate total for selected items const selectedTotal = items .filter((item) => selectedItems.has(item.uniqueId || item.id.toString())) .reduce((sum, item) => sum + item.price * item.quantity, 0); useEffect(() => { if (isOpen) { setSelectedItems(new Set()); dispatch(updateSplitBillAmount(0)); } }, [isOpen, dispatch]); const handleItemToggle = (uniqueId: string) => { const newSelected = new Set(selectedItems); if (newSelected.has(uniqueId)) { newSelected.delete(uniqueId); } else { newSelected.add(uniqueId); } setSelectedItems(newSelected); // Calculate and update split bill amount const newTotal = items .filter((item) => newSelected.has(item.uniqueId || item.id.toString())) .reduce((sum, item) => sum + item.price * item.quantity, 0); dispatch(updateSplitBillAmount(newTotal)); }; const handleSave = () => { dispatch(updateSplitBillAmount(selectedTotal)); setIsQROpen(true); onClose(); }; const handleRemoveSplitWay = () => { setSelectedItems(new Set()); dispatch(updateSplitBillAmount(0)); onRemoveSplitWay?.(); onClose(); }; return ( <>
{items.length === 0 ? ( {t("cart.emptyCart")} ) : ( items.map((item) => { const itemId = item.uniqueId || item.id.toString(); const isSelected = selectedItems.has(itemId); const itemTotal = item.price * item.quantity; return ( <> handleItemToggle(itemId)} >
{item.name}
{item.name}
e.stopPropagation()} > handleItemToggle(itemId)} />
{item !== items[items.length - 1] && ( )} ); }) )}
{t("splitBill.selectedTotal")}:
setIsQROpen(false)} /> ); }