|
| 1 | +import React, { useState } from "react"; |
| 2 | +import Box from "@mui/material/Box"; |
| 3 | +import Modal from "@mui/material/Modal"; |
| 4 | +import Butt from "./button"; |
| 5 | +import DatePick from "./date_picker"; |
| 6 | +import TimePick from "./time_picker"; |
| 7 | +import { useRouter } from "next/navigation"; |
| 8 | +import moment from "moment"; |
| 9 | +import middleware from "@/middleware"; |
| 10 | +import BasicModalWait from "./modal_wait"; |
| 11 | + |
| 12 | +interface BasicModalProps { |
| 13 | + isOpen: boolean; |
| 14 | + onClose: () => void; |
| 15 | + chairId: string; |
| 16 | +} |
| 17 | + |
| 18 | +const style = { |
| 19 | + position: "absolute" as "absolute", |
| 20 | + top: "50%", |
| 21 | + left: "50%", |
| 22 | + transform: "translate(-50%, -50%)", |
| 23 | + width: 400, |
| 24 | + bgcolor: "background.paper", |
| 25 | + borderRadius: "20px", |
| 26 | + border: "2px solid #DC9D94", |
| 27 | + boxShadow: 24, |
| 28 | + p: 4, |
| 29 | +}; |
| 30 | + |
| 31 | +const BasicModal: React.FC<BasicModalProps> = ({ |
| 32 | + isOpen, |
| 33 | + onClose, |
| 34 | + chairId, |
| 35 | +}) => { |
| 36 | + const [formData, setFormData] = useState({ |
| 37 | + Date: null as Date | null, |
| 38 | + StartTime: null as string | null, |
| 39 | + EndTime: null as string | null, |
| 40 | + }); |
| 41 | + const router = useRouter(); |
| 42 | + |
| 43 | + const handleInputChange = ( |
| 44 | + field: keyof typeof formData, |
| 45 | + value: Date | string | null |
| 46 | + ) => { |
| 47 | + setFormData((prevFormData) => ({ |
| 48 | + ...prevFormData, |
| 49 | + [field]: value, |
| 50 | + })); |
| 51 | + }; |
| 52 | + |
| 53 | + const redirectUrl = "http://localhost:3000/qr_success_reservation"; |
| 54 | + const baseTableFee = 50; // Base price per hour |
| 55 | + |
| 56 | + const handleCreateReservation = async () => { |
| 57 | + try { |
| 58 | + const storedUserData = localStorage.getItem("user"); |
| 59 | + const parsedUserData = storedUserData ? JSON.parse(storedUserData) : null; |
| 60 | + const initialFormData = parsedUserData?.updated_user || null; |
| 61 | + let userID = initialFormData ? initialFormData.UserID : ""; |
| 62 | + if (userID == undefined || userID == null || userID === "") { |
| 63 | + userID = parsedUserData ? parsedUserData.UserID : ""; |
| 64 | + } |
| 65 | + |
| 66 | + const startMoment = moment(formData.StartTime, "HH:mm"); |
| 67 | + const endMoment = moment(formData.EndTime, "HH:mm"); |
| 68 | + const durationInHours = moment |
| 69 | + .duration(endMoment.diff(startMoment)) |
| 70 | + .asHours(); |
| 71 | + const tableFee = Math.ceil(durationInHours) * baseTableFee; |
| 72 | + |
| 73 | + const apiData = { |
| 74 | + seat: chairId, |
| 75 | + resdate: formData.Date |
| 76 | + ? moment(formData.Date).format("YYYY-MM-DD") |
| 77 | + : null, |
| 78 | + starttime: formData.StartTime, |
| 79 | + endtime: formData.EndTime, |
| 80 | + user_id: userID, |
| 81 | + tablefee: tableFee, |
| 82 | + }; |
| 83 | + |
| 84 | + console.log(apiData) |
| 85 | + console.log(tableFee) |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + const response = await fetch( |
| 90 | + "http://localhost:5000/api/create-reservation", |
| 91 | + { |
| 92 | + method: "POST", |
| 93 | + headers: { |
| 94 | + "Content-Type": "application/json", |
| 95 | + }, |
| 96 | + body: JSON.stringify(apiData), |
| 97 | + } |
| 98 | + ); |
| 99 | + |
| 100 | + if (response.ok) { |
| 101 | + console.log("Reserved successfully!"); |
| 102 | + // router.push( |
| 103 | + // `https://payment-gateway-weld.vercel.app/gcash/login?amountDue=${tableFee}&merchant=Brew and Brains&redirectUrl=${redirectUrl}` |
| 104 | + // ); |
| 105 | + } else { |
| 106 | + console.error("Error Reservation", await response.json()); |
| 107 | + <BasicModalWait isOpen={true} onClose={onClose} chairID={chairId}/> |
| 108 | + } |
| 109 | + } catch (error) { |
| 110 | + console.error("Error Reservation", error); |
| 111 | + <BasicModalWait isOpen={true} onClose={onClose} chairID={chairId}/> |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + return ( |
| 116 | + <Modal |
| 117 | + open={isOpen} |
| 118 | + onClose={onClose} |
| 119 | + aria-labelledby="modal-modal-title" |
| 120 | + aria-describedby="modal-modal-description" |
| 121 | + > |
| 122 | + <Box sx={style}> |
| 123 | + <div className="text-textcolor text-xl font-bold"> |
| 124 | + <h2>Arrange Reservation</h2> |
| 125 | + </div> |
| 126 | + |
| 127 | + <div className="container"> |
| 128 | + <div className="flex justify-center items-center mt-3"> |
| 129 | + <DatePick |
| 130 | + text="Date:" |
| 131 | + onDateChange={(value) => handleInputChange("Date", value)} |
| 132 | + /> |
| 133 | + </div> |
| 134 | + |
| 135 | + <div className="flex justify-center items-center mt-3"> |
| 136 | + <TimePick |
| 137 | + text="Start Time:" |
| 138 | + onInputChange={(value) => handleInputChange("StartTime", value)} |
| 139 | + /> |
| 140 | + </div> |
| 141 | + |
| 142 | + <div className="flex justify-center items-center mt-3"> |
| 143 | + <TimePick |
| 144 | + text="End Time:" |
| 145 | + onInputChange={(value) => handleInputChange("EndTime", value)} |
| 146 | + /> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + |
| 150 | + <Butt |
| 151 | + onClick={handleCreateReservation} |
| 152 | + title="Reserve" |
| 153 | + Bgcolor="#EBE0D0" |
| 154 | + width="325px" |
| 155 | + height="34px" |
| 156 | + /> |
| 157 | + </Box> |
| 158 | + </Modal> |
| 159 | + ); |
| 160 | +}; |
| 161 | + |
| 162 | +export default BasicModal; |
0 commit comments