Browse Source

commit

master
andrymodeste 4 months ago
parent
commit
afbdbe19e7
  1. 55
      lib/pages/cart_page.dart
  2. 147
      lib/pages/commandes_screen.dart

55
lib/pages/cart_page.dart

@ -2,6 +2,10 @@ import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'dart:convert'; import 'dart:convert';
import 'package:itrimobe/pages/tables.dart';
import '../layouts/main_layout.dart';
class CartPage extends StatefulWidget { class CartPage extends StatefulWidget {
final int tableId; final int tableId;
final int personne; final int personne;
@ -119,8 +123,7 @@ class _CartPageState extends State<CartPage> {
child: Text('Annuler', style: TextStyle(color: Colors.grey[600])), child: Text('Annuler', style: TextStyle(color: Colors.grey[600])),
), ),
ElevatedButton( ElevatedButton(
onPressed: onPressed: _isValidating
_isValidating
? null ? null
: () { : () {
Navigator.of(context).pop(); Navigator.of(context).pop();
@ -130,8 +133,7 @@ class _CartPageState extends State<CartPage> {
backgroundColor: Colors.green[700], backgroundColor: Colors.green[700],
foregroundColor: Colors.white, foregroundColor: Colors.white,
), ),
child: child: _isValidating
_isValidating
? SizedBox( ? SizedBox(
width: 16, width: 16,
height: 16, height: 16,
@ -163,8 +165,7 @@ class _CartPageState extends State<CartPage> {
"reservation_id": 1, // Peut être null si pas de réservation "reservation_id": 1, // Peut être null si pas de réservation
"serveur": "Serveur par défaut", // Valeur par défaut comme demandé "serveur": "Serveur par défaut", // Valeur par défaut comme demandé
"commentaires": _getOrderComments(), "commentaires": _getOrderComments(),
"items": "items": _cartItems
_cartItems
.map( .map(
(item) => { (item) => {
"menu_id": item.id, "menu_id": item.id,
@ -206,8 +207,7 @@ class _CartPageState extends State<CartPage> {
String _getOrderComments() { String _getOrderComments() {
// Concaténer toutes les notes des articles pour les commentaires généraux // Concaténer toutes les notes des articles pour les commentaires généraux
List<String> allNotes = List<String> allNotes = _cartItems
_cartItems
.where((item) => item.notes.isNotEmpty) .where((item) => item.notes.isNotEmpty)
.map((item) => '${item.nom}: ${item.notes}') .map((item) => '${item.nom}: ${item.notes}')
.toList(); .toList();
@ -215,6 +215,7 @@ class _CartPageState extends State<CartPage> {
return allNotes.join('; '); return allNotes.join('; ');
} }
// FONCTION CORRIGÉE POUR LA NAVIGATION VERS LES TABLES
void _showSuccessDialog() { void _showSuccessDialog() {
showDialog( showDialog(
context: context, context: context,
@ -234,14 +235,13 @@ class _CartPageState extends State<CartPage> {
actions: [ actions: [
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: () {
Navigator.of(context).pop(); // Fermer le dialog Navigator.of(context).pushAndRemoveUntil(
Navigator.of(context).pop(); // Retourner au menu MaterialPageRoute(
Navigator.of(context).pop(); // Retourner aux tables builder: (context) => MainLayout(child: TablesScreen()),
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green[700],
foregroundColor: Colors.white,
), ),
(route) => false,
);
},
child: Text('OK'), child: Text('OK'),
), ),
], ],
@ -253,7 +253,8 @@ class _CartPageState extends State<CartPage> {
Future<void> _updateTableStatus() async { Future<void> _updateTableStatus() async {
try { try {
final updateResponse = await http.put( final updateResponse = await http.put(
Uri.parse('https://restaurant.careeracademy.mg/api/tables/${widget.tableId}'), Uri.parse(
'https://restaurant.careeracademy.mg/api/tables/${widget.tableId}'),
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
body: json.encode({"status": "occupied"}), body: json.encode({"status": "occupied"}),
); );
@ -262,7 +263,6 @@ class _CartPageState extends State<CartPage> {
} }
} }
void _showErrorDialog(String message) { void _showErrorDialog(String message) {
showDialog( showDialog(
context: context, context: context,
@ -317,8 +317,7 @@ class _CartPageState extends State<CartPage> {
SizedBox(width: 16), SizedBox(width: 16),
], ],
), ),
body: body: _cartItems.isEmpty
_cartItems.isEmpty
? Center( ? Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
@ -354,8 +353,7 @@ class _CartPageState extends State<CartPage> {
child: ListView.separated( child: ListView.separated(
padding: EdgeInsets.all(16), padding: EdgeInsets.all(16),
itemCount: _cartItems.length, itemCount: _cartItems.length,
separatorBuilder: separatorBuilder: (context, index) => SizedBox(height: 12),
(context, index) => SizedBox(height: 12),
itemBuilder: (context, index) { itemBuilder: (context, index) {
final item = _cartItems[index]; final item = _cartItems[index];
return Container( return Container(
@ -375,8 +373,7 @@ class _CartPageState extends State<CartPage> {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Row( Row(
mainAxisAlignment: mainAxisAlignment: MainAxisAlignment.spaceBetween,
MainAxisAlignment.spaceBetween,
children: [ children: [
Expanded( Expanded(
child: Text( child: Text(
@ -418,15 +415,13 @@ class _CartPageState extends State<CartPage> {
], ],
SizedBox(height: 16), SizedBox(height: 16),
Row( Row(
mainAxisAlignment: mainAxisAlignment: MainAxisAlignment.spaceBetween,
MainAxisAlignment.spaceBetween,
children: [ children: [
// Contrôles de quantité // Contrôles de quantité
Row( Row(
children: [ children: [
IconButton( IconButton(
onPressed: onPressed: () => _updateQuantity(
() => _updateQuantity(
index, index,
item.quantity - 1, item.quantity - 1,
), ),
@ -446,8 +441,7 @@ class _CartPageState extends State<CartPage> {
), ),
SizedBox(width: 16), SizedBox(width: 16),
IconButton( IconButton(
onPressed: onPressed: () => _updateQuantity(
() => _updateQuantity(
index, index,
item.quantity + 1, item.quantity + 1,
), ),
@ -553,8 +547,7 @@ class _CartPageState extends State<CartPage> {
SizedBox( SizedBox(
width: double.infinity, width: double.infinity,
child: ElevatedButton( child: ElevatedButton(
onPressed: onPressed: _cartItems.isNotEmpty && !_isValidating
_cartItems.isNotEmpty && !_isValidating
? _showConfirmationDialog ? _showConfirmationDialog
: null, : null,
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(

147
lib/pages/commandes_screen.dart

@ -601,17 +601,6 @@ class OrderCard extends StatelessWidget {
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
GestureDetector(
onTap: onViewDetails,
child: Text(
'Table ${order.tableId}',
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
),
Container( Container(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
horizontal: 12, horizontal: 12,
@ -630,6 +619,13 @@ class OrderCard extends StatelessWidget {
), ),
), ),
), ),
const SizedBox(width: 4),
if (order.statut == 'en_attente')
IconButton(
icon: const Icon(Icons.add_circle_outline, size: 20, color: Colors.blue),
tooltip: 'Ajouter un article',
onPressed: () => _showAddItemDialog(context, order),
),
], ],
), ),
const SizedBox(height: 8), const SizedBox(height: 8),
@ -821,6 +817,135 @@ class OrderCard extends StatelessWidget {
} }
} }
void _showAddItemDialog(BuildContext context, Order order) async {
List<MenuItem> menuItems = [];
MenuItem? selectedMenuItem;
int quantity = 1;
try {
final response = await http.get(
Uri.parse('https://restaurant.careeracademy.mg/api/menus'),
headers: {'Accept': 'application/json'},
);
if (response.statusCode == 200) {
final data = json.decode(response.body);
final rawItems = data['data']?['menus'] ?? data['data']?['items'];
if (rawItems != null && rawItems is List) {
menuItems = rawItems.map((item) => MenuItem.fromJson(item)).toList();
} else {
throw Exception('Aucun article trouvé dans la réponse de l\'API.');
}
}
} catch (e) {
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Erreur: $e'),
backgroundColor: Colors.red,
));
return;
}
showDialog(
context: context,
builder: (BuildContext dialogContext) {
return StatefulBuilder(builder: (context, setState) {
return AlertDialog(
title: const Text('Ajouter un article'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
DropdownButton<MenuItem>(
hint: const Text("Sélectionnez un article"),
value: selectedMenuItem,
isExpanded: true,
onChanged: (value) {
setState(() {
selectedMenuItem = value;
});
},
items: menuItems.map((item) {
return DropdownMenuItem<MenuItem>(
value: item,
child: Text(item.nom),
);
}).toList(),
),
const SizedBox(height: 12),
TextFormField(
initialValue: quantity.toString(),
keyboardType: TextInputType.number,
decoration: const InputDecoration(labelText: 'Quantité'),
onChanged: (val) {
quantity = int.tryParse(val) ?? 1;
},
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext),
child: const Text('Annuler'),
),
ElevatedButton(
onPressed: () async {
if (selectedMenuItem == null || quantity < 1) return;
final body = {
'menu_id': selectedMenuItem!.id,
'quantite': quantity,
'commande_id': order.id,
'table_id': order.tableId,
};
print(body);
final response = await http.post(
Uri.parse('https://restaurant.careeracademy.mg/api/commandes/${order.id}/items'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(body),
);
// print(response.body);
if (response.statusCode == 200 || response.statusCode == 201) {
Navigator.pop(dialogContext);
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Article ajouté à la commande"),
backgroundColor: Colors.green,
));
} else {
Navigator.pop(dialogContext);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("Erreur ajout article: ${response.body}"),
backgroundColor: Colors.red,
));
}
},
child: const Text('Ajouter'),
)
],
);
});
},
);
}
class MenuItem {
final int id;
final String nom;
MenuItem({required this.id, required this.nom});
factory MenuItem.fromJson(Map<String, dynamic> json) {
return MenuItem(
id: json['id'],
nom: json['nom'],
);
}
}
// Updated Order model to include items // Updated Order model to include items
class Order { class Order {
final int id; final int id;

Loading…
Cancel
Save