// lib/screens/order_home_screen.dart import "package:flutter/material.dart"; import "../services/api.dart"; class OrderHomeScreen extends StatefulWidget { // OPTIONAL so routes that call `const OrderHomeScreen()` compile. // You can wire real values later. final int? businessId; final int? servicePointId; final int? userId; const OrderHomeScreen({ super.key, this.businessId, this.servicePointId, this.userId, }); @override State createState() => _OrderHomeScreenState(); } class _OrderHomeScreenState extends State { final _businessCtl = TextEditingController(); final _servicePointCtl = TextEditingController(); final _userCtl = TextEditingController(); bool _busy = false; String? _error; int? _orderId; @override void initState() { super.initState(); _businessCtl.text = (widget.businessId ?? 0).toString(); _servicePointCtl.text = (widget.servicePointId ?? 0).toString(); _userCtl.text = (widget.userId ?? 0).toString(); } @override void dispose() { _businessCtl.dispose(); _servicePointCtl.dispose(); _userCtl.dispose(); super.dispose(); } int _parseInt(TextEditingController c) { final s = c.text.trim(); return int.tryParse(s) ?? 0; } Future _run(Future Function() fn) async { if (_busy) return; setState(() { _busy = true; _error = null; }); try { await fn(); } catch (e) { setState(() => _error = e.toString()); } finally { if (mounted) setState(() => _busy = false); } } Future _createOrLoadCart() async { await _run(() async { final businessId = _parseInt(_businessCtl); final servicePointId = _parseInt(_servicePointCtl); final userId = _parseInt(_userCtl); final cartData = await Api.getOrCreateCart( userId: userId, businessId: businessId, servicePointId: servicePointId, orderTypeId: 1, // MVP default: dine-in ); final oid = _asInt(cartData is Map ? cartData["OrderID"] : null); setState(() => _orderId = oid == 0 ? null : oid); }); } Future _refreshCart() async { if (_orderId == null) return; await _run(() async { await Api.getCart(orderId: _orderId!); }); } Future _addDemoItem(int itemId) async { if (_orderId == null) { setState(() => _error = "No cart yet. Tap 'Create/Load Cart' first."); return; } await _run(() async { await Api.setLineItem( orderId: _orderId!, parentOrderLineItemId: 0, itemId: itemId, qty: 1, selectedChildItemIds: const [], ); }); } Future _submit() async { if (_orderId == null) { setState(() => _error = "No cart yet."); return; } await _run(() async { await Api.submitOrder(orderId: _orderId!); }); } @override Widget build(BuildContext context) { final orderId = _orderId; return Scaffold( appBar: AppBar(title: const Text("Order (Compile-Only MVP)")), body: ListView( padding: const EdgeInsets.all(16), children: [ const Text( "This screen exists only to compile cleanly against Api.dart.\n" "No menu, no models, no polish.", ), const SizedBox(height: 16), _LabeledField(label: "BusinessID", controller: _businessCtl, enabled: !_busy), const SizedBox(height: 10), _LabeledField(label: "ServicePointID", controller: _servicePointCtl, enabled: !_busy), const SizedBox(height: 10), _LabeledField(label: "UserID", controller: _userCtl, enabled: !_busy), const SizedBox(height: 16), Row( children: [ Expanded( child: ElevatedButton( onPressed: _busy ? null : _createOrLoadCart, child: const Text("Create/Load Cart"), ), ), const SizedBox(width: 12), Expanded( child: OutlinedButton( onPressed: (_busy || orderId == null) ? null : _refreshCart, child: const Text("Refresh Cart"), ), ), ], ), const SizedBox(height: 16), Card( child: Padding( padding: const EdgeInsets.all(12), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("Cart OrderID: ${orderId ?? "(none)"}"), const SizedBox(height: 10), Wrap( spacing: 10, runSpacing: 10, children: [ ElevatedButton( onPressed: _busy ? null : () => _addDemoItem(101), child: const Text("Add Demo Item 101"), ), ElevatedButton( onPressed: _busy ? null : () => _addDemoItem(102), child: const Text("Add Demo Item 102"), ), ElevatedButton( onPressed: _busy ? null : () => _addDemoItem(103), child: const Text("Add Demo Item 103"), ), ], ), const SizedBox(height: 12), ElevatedButton( onPressed: (_busy || orderId == null) ? null : _submit, child: const Text("Submit Order"), ), ], ), ), ), const SizedBox(height: 16), if (_busy) ...[ const Center(child: CircularProgressIndicator()), const SizedBox(height: 16), ], if (_error != null) Card( child: Padding( padding: const EdgeInsets.all(12), child: Text(_error!), ), ), ], ), ); } } class _LabeledField extends StatelessWidget { final String label; final TextEditingController controller; final bool enabled; const _LabeledField({ required this.label, required this.controller, required this.enabled, }); @override Widget build(BuildContext context) { return TextField( controller: controller, enabled: enabled, keyboardType: TextInputType.number, decoration: InputDecoration( labelText: label, border: const OutlineInputBorder(), ), ); } } int _asInt(dynamic v) { if (v is int) return v; if (v is double) return v.toInt(); if (v is String) return int.tryParse(v) ?? 0; return 0; }