const OrderManager = { async createOrder(orderData) { try { Utils.showLoading(); // Generate order ID const orderId = `ORD${Date.now()}`; // Prepare order data const order = { id: orderId, items: State.cart, customer: State.customer.personalInfo, total: this.calculateTotal(State.cart), status: 'pending', paymentMethod: orderData.paymentMethod, createdAt: new Date().toISOString() }; // Process payment const paymentResult = await PaymentManager.processPayment(order); if (paymentResult.success) { // Save order await this.saveOrder(order); // Send notifications await NotificationService.sendOrderNotification(order); // Update customer order history this.updateOrderHistory(order); // Clear cart CartManager.clearCart(); return { success: true, order, paymentData: paymentResult.data }; } throw new Error('การชำระเงินไม่สำเร็จ'); } catch (error) { console.error('Order creation failed:', error); NotificationManager.showError(error.message || 'เกิดข้อผิดพลาดในการสั่งซื้อ'); return { success: false, error }; } finally { Utils.hideLoading(); } }, calculateTotal(items) { const subtotal = items.reduce((sum, item) => sum + (item.price * item.quantity), 0); // Add delivery fee if subtotal is less than free delivery amount const deliveryFee = subtotal >= CONFIG.FREE_DELIVERY_AMOUNT ? 0 : CONFIG.DELIVERY_FEE; return subtotal + deliveryFee; }, async saveOrder(order) { // In real application, save to backend const orders = Storage.load('orders') || []; orders.push(order); Storage.save('orders', orders); }, updateOrderHistory(order) { State.customer.orderHistory.push({ id: order.id, total: order.total, items: order.items.length, date: order.createdAt }); CustomerManager.saveCustomerData(); }, showOrderConfirmation(order, paymentData) { const modal = document.createElement('div'); modal.className = 'modal active'; modal.innerHTML = `
`; document.body.appendChild(modal); }, getOrderHistory() { return State.customer.orderHistory; }, showOrderHistory() { const orders = this.getOrderHistory(); const modal = document.createElement('div'); modal.className = 'modal active'; modal.innerHTML = ` `; document.body.appendChild(modal); } };