import React, { useState, useEffect } from 'react';
import {
ShoppingBag,
Plus,
Minus,
Trash2,
CreditCard,
Receipt,
CheckCircle,
X,
ArrowLeft,
User
} from 'lucide-react';
const CartSidebar = ({
cart,
isOpen,
onClose,
onRemoveItem,
onUpdateQuantity,
onCheckout
}) => {
const [discount, setDiscount] = useState(0);
const [taxRate, setTaxRate] = useState(7);
const [paymentMethod, setPaymentMethod] = useState('cash');
const [showSuccess, setShowSuccess] = useState(false);
useEffect(() => {
// Reset form when cart is empty
if (cart.length === 0) {
setShowSuccess(false);
}
}, [cart]);
const subtotal = cart.reduce((sum, item) => {
const price = item.discount > 0
? item.price * (1 - item.discount/100)
: item.price;
return sum + (price * item.quantity);
}, 0);
const tax = subtotal * (taxRate / 100);
const total = subtotal + tax - discount;
const formatPrice = (price) => {
return new Intl.NumberFormat('th-TH', {
style: 'currency',
currency: 'THB'
}).format(price);
};
const handleCheckout = () => {
if (cart.length === 0) return;
setShowSuccess(true);
setTimeout(() => {
setShowSuccess(false);
onCheckout();
}, 3000);
};
if (isOpen && cart.length === 0 && !showSuccess) {
return (
ตะกร้าสินค้าว่างเปล่า
เริ่มเลือกสินค้าเพื่อเริ่มต้นขาย
);
}
return (
<>
{/* Overlay */}
{/* Sidebar */}
{showSuccess ? (
ขายสำเร็จ!
คำสั่งซื้อ #{Math.floor(Math.random() * 10000)} เสร็จสมบูรณ์
ยอดรวม
{formatPrice(subtotal)}
ภาษี (7%)
{formatPrice(tax)}
ส่วนลด
-{formatPrice(discount)}
ยอดรวมทั้งสิ้น
{formatPrice(total)}
กำลังสร้างใบเสร็จ...
) : (
{/* Header */}
ตะกร้าสินค้า
{cart.length} รายการ
{/* Cart Items */}
{cart.length === 0 ? (
) : (
{cart.map((item) => {
const price = item.discount > 0
? item.price * (1 - item.discount/100)
: item.price;
const itemTotal = price * item.quantity;
return (
{item.name}
{formatPrice(price)} x {item.quantity}
{item.quantity}
{formatPrice(itemTotal)}
{item.discount > 0 && (
-{item.discount}% ลดให้
)}
);
})}
)}
{/* Cart Summary */}
ยอดรวมสินค้า
{formatPrice(subtotal)}
ภาษี ({taxRate}%)
{formatPrice(tax)}
ยอดรวมทั้งสิ้น
{formatPrice(total)}
{/* Payment Options */}
วิธีการชำระเงิน
{[
{ id: 'cash', icon: 'Cash', label: 'เงินสด' },
{ id: 'card', icon: 'CreditCard', label: 'บัตรเครดิต' },
{ id: 'qr', icon: 'Smartphone', label: 'QR Code' },
].map((method) => (
))}
{/* Checkout Button */}
)}
>
);
};
export default CartSidebar;