import { useCallback, useEffect, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import './IndividualBooking.scss'; import Compass from '../Tablebooking/Compass'; import { changeSelectChair, changeSelectTable, GlobalSelectedChair, GlobalSelectedTable, } from '../../Features/BookingScreen/BookingData/BookingData'; import { getTableDtlsForSelfBooking } from '../../Features/TableBooking/TableBooking'; import { decryptToObject, extractLastNumber, getSession, sessionStore, } from '../../Services/Others'; import Buttons from '../../Components/Forms/Buttons'; import { ArrowRightOutlined, ArrowLeftOutlined } from '@ant-design/icons'; import { useLocation, useNavigate } from 'react-router-dom'; import { getBranchDetail } from '../../Features/BrachLogin/BranchLogin'; import { IoIosArrowBack } from 'react-icons/io'; const subDirectory = import.meta.env.BASE_URL; const IndividualBooking = () => { const dispatch = useDispatch(); const navigate = useNavigate(); const AppId = getSession('AppId'); const CompId = getSession('CompId'); const BranchId = getSession('BranchId'); const tableCode = sessionStorage.getItem('SelfBookingId'); const location = useLocation(); const tableInfo = decryptToObject(getSession('bookInfo')); const selectedChairRedux = useSelector(GlobalSelectedChair); const selectedTableRedux = useSelector(GlobalSelectedTable); const dineInTypeId = getSession('table'); const [tableData, setTableData] = useState([]); const [selectedChairs, setSelectedChairs] = useState( selectedChairRedux?.length > 0 ? selectedChairRedux : [] ); const [selectedTables, setSelectedTables] = useState( selectedTableRedux || [] ); useEffect(() => { if (tableInfo?.TableId) { fetchTableData(); } }, [tableInfo?.TableId]); useEffect(() => { console.log(selectedChairs, 'selectedChairs'); }, [selectedChairs]); useEffect(() => { if ( (AppId === null || AppId === undefined) && (CompId === null || CompId === undefined) && (BranchId === null || BranchId === undefined) ) { fetchBranchData(); } }, []); const fetchBranchData = async () => { const response = await dispatch( getBranchDetail({ BrId: BranchId }) ).unwrap(); }; const fetchTableData = async () => { const res = await dispatch( getTableDtlsForSelfBooking({ tableId: tableInfo.TableId, AppId, CompId, BranchId, }) ).unwrap(); if (res?.data?.statusCode === 1) { setTableData(res.data.data); } }; const handleTableToggle = async (checked, tableId, chairDetails) => { const chairIds = chairDetails.map((c) => c.ChairId); if (checked) { const newChairs = chairIds.filter((id) => !selectedChairs.includes(id)); const updatedChairs = [...selectedChairs, ...newChairs]; const updatedTables = [...selectedTables, tableId]; setSelectedChairs(updatedChairs); setSelectedTables(updatedTables); await dispatch(changeSelectChair(updatedChairs)); await dispatch(changeSelectTable(updatedTables)); } else { const updatedChairs = selectedChairs.filter( (id) => !chairIds.includes(id) ); const updatedTables = selectedTables.filter((id) => id !== tableId); setSelectedChairs(updatedChairs); setSelectedTables(updatedTables); await dispatch(changeSelectChair(updatedChairs)); await dispatch(changeSelectTable(updatedTables)); } }; const handleChairToggle = async (checked, chairId, chairDetails, tableId) => { const updatedChairs = checked ? [...selectedChairs, chairId] : selectedChairs.filter((id) => id !== chairId); setSelectedChairs(updatedChairs); await dispatch(changeSelectChair(updatedChairs)); const allChairIds = chairDetails.map((c) => c.ChairId); const selectedForTable = allChairIds.every((id) => updatedChairs.includes(id) ); const updatedTables = selectedForTable ? [...new Set([...selectedTables, tableId])] : selectedTables.filter((id) => id !== tableId); setSelectedTables(updatedTables); await dispatch(changeSelectTable(updatedTables)); }; const handleBookingNavigate = () => { sessionStore('table', selectedChairs); navigate(`${subDirectory}kioskSelfBooking`, { state: { from: location.pathname }, }); }; const renderChair = (chair, index, chairDetails, tableId) => { const isSelected = selectedChairs.includes(chair.ChairId); const isOccupied = chair.SalesStatus === 'Y'; // const isOccupied = chair.OrderDetails?.length > 0; return (
); }; return (
navigate(`${subDirectory}selfBooking/${tableCode}`)} > Choose Chair
Available
Booked
Occupied
Reserved
Selected
{tableData.map((table) => { const chairCount = table.ChairLevelSerDetails?.length || 0; const topChairs = Math.ceil(chairCount / 2); const dineInTypeId = tableData?.[0]?.DineInType; const hasOrders = table.ChairLevelSerDetails?.some( (c) => c.SalesStatus=="Y" ); console.log(tableData,'tableData') return (
{!hasOrders && ( handleTableToggle( e.target.checked, table.TableId, table.ChairLevelSerDetails ) } /> )}

{table.TableName} {/* {table.TableName} - {extractLastNumber(table.TableId)} */}

{table.ChairLevelSerDetails?.slice(0, topChairs).map( (chair, idx) => renderChair( chair, idx, table.ChairLevelSerDetails, table.TableId, dineInTypeId ) )}
{/* {table.TableName} */}
{table.ChairLevelSerDetails?.slice(topChairs).map( (chair, idx) => renderChair( chair, topChairs + idx, table.ChairLevelSerDetails, table.TableId, dineInTypeId ) )}
} disabled={selectedChairs?.length === 0} />
); })}
); }; export default IndividualBooking;