import 'package:cheminova/controller/get_order_placed_controller.dart'; import 'package:cheminova/controller/rd_get_order_controller.dart'; import 'package:cheminova/models/rd_get_order_model.dart'; import 'package:cheminova/models/rd_order_item_model.dart'; import 'package:cheminova/models/rd_placed_order_model.dart'; import 'package:cheminova/screens/order/checkout_screen.dart'; import 'package:cheminova/widgets/my_drawer.dart'; import 'package:cheminova/widgets/product_card.dart'; import 'package:cheminova/widgets/product_card1.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:get/get.dart'; import 'package:google_fonts/google_fonts.dart'; import '../../controller/cart_controller.dart'; import '../../controller/rd_processing_order_controller.dart'; import '../../models/oder_place_model.dart'; import '../../models/product_model1.dart'; import '../../utils/show_snackbar.dart'; class PartialProcessingDialogScreen extends StatefulWidget { PlacedOrdersResponse? productModel; PartialProcessingDialogScreen({super.key, this.productModel}); @override State createState() => _PartialProcessingDialogScreenState(); } class _PartialProcessingDialogScreenState extends State { final RDOrderPlacedController controller = Get.put(RDOrderPlacedController()); bool _selectAll = true; // Default to true to select all products @override void initState() { super.initState(); } void _showPartialOrderDialog() { showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Text("Confirm Partial Order"), content: Text("Do you want to place a partial order for the selected items?"), actions: [ TextButton( onPressed: () async { // Create a map to store order items Map orderItemMap = {}; // Populate the map with items and their quantities for (var item in widget.productModel!.orderItem) { var productId = item.productId; // Adjust if needed if (orderItemMap.containsKey(productId)) { // If the product already exists, aggregate the quantity var existingItem = orderItemMap[productId]!; existingItem.quantity = (existingItem.quantity ?? 0) + (item.quantity ?? 0); } else { // If it's a new product, add it to the map orderItemMap[productId] = RDOrderItem( productId: productId, sku: item.sku, name: item.name, categoryName: item.categoryName, brandName: item.brandName, price: item.price, gst: item.gst, hsnCode: item.hsnCode, description: item.description, image: [], // Handle images appropriately quantity: item.quantity ?? 0, remainingQuantity: item.remainingQuantity ?? 0, processquantity: item.processquantity ?? 0, ); } } // Convert the map to a list List orderItems = orderItemMap.values.toList(); controller.placedOrder1.value = PlacedOrdersProcessing( orderId: widget.productModel!.id, invoiceItems: orderItems, ); // Debugging: Print the JSON payload print(controller.placedOrder1.value.toJson()); await controller.placeRDOrder(); // Show confirmation snackbar showSnackbar( "Partial Order processed and invoice created successfully"); Future.delayed(const Duration(seconds: 1), () { Get.back(); // Close the dialog }); setState(() {}); // Refresh the UI }, child: Text("Confirm"), ), TextButton( onPressed: () { Get.back(); // Close the dialog }, child: Text("Cancel"), ), ], ); }, ); } @override Widget build(BuildContext context) { return Scaffold( extendBodyBehindAppBar: true, appBar: AppBar( backgroundColor: Colors.transparent, elevation: 0, leading: Builder( builder: (context) { return GestureDetector( onTap: () => Scaffold.of(context).openDrawer(), child: Padding( padding: const EdgeInsets.all(16.0), child: SvgPicture.asset( 'assets/svg/menu.svg', ), ), ); }, ), actions: [ GestureDetector( onTap: () => Get.back(), child: Padding( padding: const EdgeInsets.all(8.0), child: SvgPicture.asset( 'assets/svg/back_arrow.svg', ), ), ), ], title: const Center( child: Text( "Modify Product Availability", ), ), ), drawer: MyDrawer(), body: Stack( fit: StackFit.expand, children: [ Image.asset( 'assets/images/image_1.png', fit: BoxFit.cover, ), SafeArea( child: Column( children: [ SizedBox(height: Get.height * 0.02), Card( margin: const EdgeInsets.symmetric(horizontal: 18), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(19), side: const BorderSide(color: Color(0xFFFDFDFD)), ), color: const Color(0xFFB4D1E5).withOpacity(0.9), child: Padding( padding: const EdgeInsets.all(12.0), child: Column( mainAxisSize: MainAxisSize.min, children: [ SizedBox( height: Get.height * 0.6, child: ListView.builder( padding: EdgeInsets.zero, itemCount: widget.productModel!.orderItem.length, itemBuilder: (context, index) { final orderItem = widget.productModel!.orderItem[index]; return Row( children: [ Expanded( child: ProductCard1( productModel: orderItem, ), ), ], ); }, ), ), const SizedBox(height: 10), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Subtotal ", style: GoogleFonts.roboto( fontSize: 15, color: Colors.black, fontWeight: FontWeight.bold, ), ), Text("₹ ${widget.productModel!.subtotal ?? 0}"), ], ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "GST ", style: GoogleFonts.roboto( fontSize: 15, color: Colors.black, fontWeight: FontWeight.bold, ), ), Text("₹ ${widget.productModel!.gstTotal ?? 0}"), ], ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Total Amount ", style: GoogleFonts.roboto( fontSize: 15, color: Colors.black, fontWeight: FontWeight.bold, ), ), Text("₹ ${widget.productModel!.grandTotal ?? 0}"), ], ), ], ), ), ), SizedBox(height: Get.height * 0.020), SizedBox( width: Get.width * 0.9, height: Get.height * 0.06, child: ElevatedButton( onPressed: _showPartialOrderDialog, // Show dialog when pressed style: ElevatedButton.styleFrom( foregroundColor: Colors.white, backgroundColor: const Color(0xFF00784C), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10), ), ), child: Text( "Submit", style: GoogleFonts.roboto( fontSize: 16, fontWeight: FontWeight.w600, ), ), ), ), ], ), ), ], ), ); } }