import React from 'react'; import { View, Text, FlatList, StyleSheet, TouchableOpacity, ActivityIndicator, Alert, RefreshControl, } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { Ionicons } from '@expo/vector-icons'; import { useFocusEffect, useRouter } from 'expo-router'; import { useCart } from '../../hooks/useCart'; import CartItemCard from '../../components/CartItemCard'; export default function CartScreen() { const router = useRouter(); const { cart, loading, refreshing, refreshCart, updateQuantity, removeItem, clearCart, } = useCart(); // Tự động refresh cart mỗi khi vào trang useFocusEffect( React.useCallback(() => { console.log('=== CartScreen focused - Refreshing cart ==='); refreshCart(); }, [refreshCart]) ); const handleClearCart = () => { Alert.alert( 'Xác nhận', 'Bạn có chắc muốn xóa tất cả sản phẩm trong giỏ hàng?', [ { text: 'Hủy', style: 'cancel' }, { text: 'Xóa tất cả', onPress: () => clearCart(), style: 'destructive' }, ] ); }; const handleCheckout = () => { if (!cart || cart.items.length === 0) { Alert.alert('Thông báo', 'Giỏ hàng của bạn đang trống'); return; } // Navigate to checkout screen router.push('/checkout'); }; const formatPrice = (price: number) => { return new Intl.NumberFormat('vi-VN', { style: 'currency', currency: 'VND', }).format(price); }; if (loading && !cart) { return ( Đang tải giỏ hàng... ); } if (!cart || cart.items.length === 0) { return ( Giỏ hàng Giỏ hàng trống Hãy thêm sản phẩm vào giỏ hàng để mua sắm router.push('/')} > Tiếp tục mua sắm ); } return ( Giỏ hàng Xóa tất cả item.cartItemId.toString()} renderItem={({ item }) => ( )} contentContainerStyle={styles.listContent} refreshControl={ } /> Tổng số lượng: {cart.totalItems} sản phẩm Tổng tiền: {formatPrice(cart.totalAmount)} Thanh toán ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#f5f5f5', }, header: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', padding: 16, backgroundColor: '#fff', borderBottomWidth: 1, borderBottomColor: '#e0e0e0', }, headerTitle: { fontSize: 24, fontWeight: '700', color: '#333', }, clearButton: { flexDirection: 'row', alignItems: 'center', padding: 8, }, clearText: { fontSize: 14, color: '#ff4444', marginLeft: 4, fontWeight: '500', }, loadingContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', }, loadingText: { marginTop: 12, fontSize: 16, color: '#666', }, emptyContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 32, }, emptyText: { fontSize: 20, fontWeight: '600', color: '#666', marginTop: 16, }, emptySubtext: { fontSize: 14, color: '#999', marginTop: 8, textAlign: 'center', }, shopButton: { backgroundColor: '#ff6b6b', paddingHorizontal: 32, paddingVertical: 14, borderRadius: 12, marginTop: 24, }, shopButtonText: { color: '#fff', fontSize: 16, fontWeight: '700', }, listContent: { padding: 16, }, footer: { backgroundColor: '#fff', padding: 16, borderTopWidth: 1, borderTopColor: '#e0e0e0', }, summaryRow: { flexDirection: 'row', justifyContent: 'space-between', marginBottom: 12, }, summaryLabel: { fontSize: 15, color: '#666', }, summaryValue: { fontSize: 15, color: '#333', fontWeight: '500', }, totalLabel: { fontSize: 18, fontWeight: '600', color: '#333', }, totalValue: { fontSize: 20, fontWeight: '700', color: '#ff6b6b', }, checkoutButton: { backgroundColor: '#ff6b6b', flexDirection: 'row', justifyContent: 'center', alignItems: 'center', padding: 16, borderRadius: 12, marginTop: 16, }, checkoutText: { fontSize: 17, fontWeight: '700', color: '#fff', marginRight: 8, }, });