import BasicTemplate from "../../templates/BasicTemplate";
import { Container, Row, Col, Card, Button, Modal, Form } from "react-bootstrap";
import { useState, useEffect } from "react";
import CacheStorage from "../../organisms/CacheStorage";
import DataStorage from "../../organisms/DataStorage";
import axios from "axios";
export default function CartPage() {
const [cartItems, setCartItems] = useState([]);
const [show, setShow] = useState(false);
const [branches, setBranches] = useState([]);
const defaultSelectedStore = CacheStorage.get('selectedStore') || "";
const [selectedStore, setSelectedStore] = useState(defaultSelectedStore);
const [loadingBranches, setLoadingBranches] = useState(true);
const [address, setAddress] = useState('');
// const [loading, setLoading] = useState(true);
useEffect(() => {
const fetchBranches = async () => {
try {
const response = await axios.get(process.env.REACT_APP_API_URL + '/branchs'); // Thay 'API_ENDPOINT' bằng URL của API
setBranches(response.data); // Lưu dữ liệu vào state
setLoadingBranches(false); // Đặt loading thành false khi hoàn tất
CacheStorage.set('stores', JSON.stringify(Object(response.data)));
} catch (error) {
console.error('Error fetching branches:', error);
setLoadingBranches(false); // Đặt loading thành false nếu lỗi
}
};
if (CacheStorage.get('stores')) {
setBranches(JSON.parse(CacheStorage.get('stores')));
setLoadingBranches(false);
} else {
fetchBranches();
}
}, [])
const handleSelectStore = (e) => {
setSelectedStore(e.target.value);
CacheStorage.set('selectedStore', e.target.value);
// setLoading(true);
};
let selectboxContent;
if (loadingBranches) {
selectboxContent = (
Đang tải dữ liệu...
)
} else {
selectboxContent = (
Chọn chi nhánh:
{branches.map((store) => (
))}
);
}
useEffect(() => {
// Lấy giỏ hàng từ sessionStorage
const cart = JSON.parse(DataStorage.get('cart')) || {};
// Chuyển cart thành mảng chứa các món có số lượng > 0
const items = Object.entries(cart).map(([id, item]) => ({
id: id,
name: item.name,
amount: item.amount,
imageSrc: item.imageSrc,
price: item.price
}));
console.log(items);
setCartItems(items);
}, []);
const handleShow = () => {
setShow(true);
}
const handleClose = () => {
setShow(false);
}
const handleSubmit = () => {
// tạo order, gửi order && gửi lấy urlpayment
if (selectedStore === '') {
setShow(false);
} else {
// try {
// let orderData;
// orderData.order_type = 2;
// orderData.order_items = cartItems.map((item) => { return { menu_id: item.id, quantity: item.amount } })
// const orderResponse = axios.post(process.env.REACT_APP_API_URL + `/branches/${selectedStore}/orders`, {
// headers: {
// Authorization: `Bearer ${DataStorage.get('accessToken')}`,
// },
// })
// let paymentData;
// paymentData.orderType='other';
// paymentData.orderDescription=''
// } catch (error) {
// console.log('error');
// }
setShow(false);
}
}
return (
Xác nhận thanh toán
{
(selectedStore !== '' && address !== '' ?
`Bạn sắp thanh toán ${Object.values(cartItems).reduce((total, item) => { return total + item.price * item.amount; }, 0)} VND qua VNPAY.
Vui lòng xác nhận để chuyển tiếp đến trang thanh toán.` : `Hãy đảm bảo bạn chọn địa điểm giao hàng, và chi nhánh đặt món`
)
}
{cartItems.length > 0 ? (
{selectboxContent}
Chọn địa chỉ giao hàng
setAddress(e.target.value)}
/>
Giỏ hàng của bạn
{cartItems.map((item, idx) => (
{item.name}
Đơn giá: {item.price} VND
Số lượng: {item.amount}
Tổng cộng: {item.price * item.amount} VND
))}
) : (
Giỏ hàng của bạn hiện đang trống.
)}
)
} />
)
}