diff --git a/lib/screens/business_selector_screen.dart b/lib/screens/business_selector_screen.dart index 7c2d0bb..f504493 100644 --- a/lib/screens/business_selector_screen.dart +++ b/lib/screens/business_selector_screen.dart @@ -3,7 +3,9 @@ import "package:provider/provider.dart"; import "../app/app_router.dart"; import "../app/app_state.dart"; +import "../models/cart.dart"; import "../services/api.dart"; +import "../services/auth_storage.dart"; import "../widgets/rescan_button.dart"; class BusinessSelectorScreen extends StatefulWidget { @@ -172,7 +174,6 @@ class _BusinessSelectorScreenState extends State { } Widget _buildHeaderBanner(BuildContext context, int? parentBusinessId, String parentName) { - const imageBaseUrl = _imageBaseUrl; return SizedBox( height: 200, width: double.infinity, @@ -255,10 +256,94 @@ class _BusinessSelectorScreenState extends State { ); } - void _selectBusiness(BuildContext context, _BusinessItem business, int? parentBusinessId, String? parentBusinessName) { + void _selectBusiness(BuildContext context, _BusinessItem business, int? parentBusinessId, String? parentBusinessName) async { + // Check if user is logged in and has an existing cart for this business + final auth = await AuthStorage.loadAuth(); + if (auth != null && auth.userId > 0) { + try { + final existingCart = await Api.getActiveCart(userId: auth.userId); + if (existingCart != null && existingCart.hasItems && existingCart.businessId == business.businessId) { + // Show existing cart dialog + if (!mounted) return; + _showExistingCartDialog(existingCart, business, parentBusinessId, parentBusinessName); + return; + } + } catch (e) { + // Ignore - proceed without cart check + } + } + + if (!mounted) return; + _proceedToMenu(business, parentBusinessId, parentBusinessName); + } + + void _showExistingCartDialog(ActiveCartInfo cart, _BusinessItem business, int? parentBusinessId, String? parentBusinessName) { + showDialog( + context: context, + barrierDismissible: false, + builder: (ctx) => AlertDialog( + title: const Text("Existing Order Found"), + content: Text( + "You have ${cart.itemCount} item${cart.itemCount == 1 ? '' : 's'} in your cart at ${cart.businessName}.\n\nWould you like to continue that order or start fresh?", + ), + actions: [ + TextButton( + onPressed: () async { + Navigator.of(ctx).pop(); + // Abandon the old order and proceed with clean cart + try { + await Api.abandonOrder(orderId: cart.orderId); + } catch (e) { + // Ignore - proceed anyway + } + if (!mounted) return; + final appState = context.read(); + appState.clearCart(); + _proceedToMenu(business, parentBusinessId, parentBusinessName); + }, + child: const Text("Start Fresh"), + ), + ElevatedButton( + onPressed: () { + Navigator.of(ctx).pop(); + // Continue existing order - load cart and go to menu + if (!mounted) return; + final appState = context.read(); + appState.setBusinessAndServicePoint( + cart.businessId, + cart.servicePointId, + businessName: cart.businessName, + servicePointName: cart.servicePointName, + parentBusinessId: parentBusinessId, + parentBusinessName: parentBusinessName, + ); + appState.setCartOrder( + orderId: cart.orderId, + orderUuid: cart.orderUuid, + itemCount: cart.itemCount, + ); + appState.setOrderType(OrderType.dineIn); + Api.setBusinessId(cart.businessId); + + Navigator.of(context).pushReplacementNamed( + AppRoutes.menuBrowse, + arguments: { + "businessId": cart.businessId, + "servicePointId": cart.servicePointId, + }, + ); + }, + child: const Text("Continue Order"), + ), + ], + ), + ); + } + + void _proceedToMenu(_BusinessItem business, int? parentBusinessId, String? parentBusinessName) { final appState = context.read(); - // Clear any existing cart + // Clear any existing cart (for different business) appState.clearCart(); // Set the selected business and service point (with parent info for back navigation)