import 'package:auto_size_text/auto_size_text.dart'; import 'package:cheminova/controller/get_dispatch_controller.dart'; import 'package:cheminova/controller/get_order_placed_controller.dart'; import 'package:cheminova/controller/get_single_invoice_controller.dart'; import 'package:cheminova/models/get_dispatch_model.dart'; import 'package:cheminova/models/oder_place_model.dart'; import 'package:cheminova/models/order_item_model.dart'; import 'package:cheminova/models/place_order_list_model.dart'; import 'package:cheminova/models/rd_get_order_model.dart'; import 'package:cheminova/screens/rd%20orders/rd_delivered_screen.dart'; import 'package:cheminova/utils/show_snackbar.dart'; import 'package:flutter/cupertino.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 'package:intl/intl.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../../controller/cart_controller.dart'; import '../../models/get_invoice_model.dart'; import '../../models/product_model1.dart'; class RdDispatchedDetailsDetailScreen extends StatefulWidget { //final Product? productModel; // PlacedOrderList and PlacedOrderModel are optional parameters passed to this screen GetDispatchModel? placedOrderList; PlacedOrderModel? placedOrderModel; GetInvoiceModel? placedInvoiceList; // Constructor for initializing the screen with placed order details RdDispatchedDetailsDetailScreen({super.key,this.placedOrderList,this.placedOrderModel,this.placedInvoiceList}); @override State createState() => _RdDispatchedDetailsDetailScreenState(); } class _RdDispatchedDetailsDetailScreenState extends State { // Controllers for managing cart and placed orders final CartController _cartController = Get.put(CartController()); final GetDispatchController _getPlacedOrderController = Get.put(GetDispatchController()); final GetSingleInvoiceController _getSingleInvoiceController = Get.put(GetSingleInvoiceController()); final GetDispatchController _getDispatchController = Get.put(GetDispatchController()); final List statusOptions = [ "new", "pending", "processing", "dispatched", "cancelled", "delivered", ]; String selectedStatus = "dispatched"; String _groupValue = "cheque"; // Function to format date from the API to a more readable format List _statusList = ["dispatched", "delivered"]; String formatDate(String apiDate) { // Parse the API date string into a DateTime object DateTime parsedDate = DateTime.parse(apiDate).toLocal(); // Convert to local time // Format the date and time according to your specified format String formattedDate = DateFormat('EEE MMM dd yyyy').format(parsedDate); return formattedDate; // Return the formatted date string } // Function to capitalize the first letter of a string String capitalizeFirstLetter(String text) { if (text.isEmpty) return text; return text[0].toUpperCase() + text.substring(1).toLowerCase(); } void _onPaymentModeChanged(String? value) { setState(() { _groupValue = value!; }); _saveSelectedPaymentMode(); } void _saveSelectedPaymentMode() async { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString('selectedPaymentMode', _groupValue); } void _loadSelectedPaymentMode() async { SharedPreferences prefs = await SharedPreferences.getInstance(); setState(() { _groupValue = prefs.getString('selectedPaymentMode') ?? 'cheque'; }); } // Function to collect unique order IDs and corresponding order details Future adduni()async { final Set uniqueOrderIds = {}; final List uniqueOrders = []; // Loop through placed orders and add unique orders to the list for (var order in _getPlacedOrderController.productProcessingRDList) { if (uniqueOrderIds.add(order.orderId.uniqueId)) { uniqueOrders.add(order); } } final order = uniqueOrders[0]; // Combine product names, categories, and quantities into strings final productNames = order.items .map((item) => (item.name)) .join(', '); final categotyName =order.items .map((item) => (item.categoryName)) .join(', '); final quantity = order.items .map((item) => (item.processquantity)) .join(', '); } @override void initState() { // TODO: implement initState super.initState(); selectedStatus= widget.placedOrderList?.courierStatus ?? 'new'; _getPlacedOrderController.getRDDispatchInvoiceProduct(); } void showConfirmDeliveryDialog(BuildContext context) { final TextEditingController _deliveryDetailsController = TextEditingController(); DateTime? selectedDate; // Variable to hold the selected delivery date showDialog( context: context, builder: (context) { return StatefulBuilder( builder: (context, setState) { // Use StatefulBuilder to update the dialog state return AlertDialog( title: Text('Confirm Delivery'), content: Column( mainAxisSize: MainAxisSize.min, // Shrink to fit the content children: [ SizedBox(height: 20), // Space between text field and date picker Text( selectedDate == null ? 'Select Delivery Date *' : 'Delivery Date: ${selectedDate!.toLocal().toString().split(' ')[0]}', // Show selected date style: TextStyle(fontSize: 16), ), SizedBox(height: 10), // Space ElevatedButton( onPressed: () async { // Show date picker DateTime? pickedDate = await showDatePicker( context: context, initialDate: DateTime.now(), firstDate: DateTime.now(), // Prevent past dates lastDate: DateTime(2101), ); if (pickedDate != null) { // Update the selected date and refresh the UI setState(() { selectedDate = pickedDate; }); } }, child: Text('Select Date'), ), ], ), actions: [ TextButton( onPressed: () { Navigator.of(context).pop(); // Close the dialog }, child: Text('Cancel'), ), TextButton( onPressed: () async { if (selectedDate != null) { // Handle the confirmation logic String deliveryDetails = _deliveryDetailsController.text; // Check if the selected status is "delivered" if (selectedStatus == "delivered") { // Call your API method here await _getDispatchController.RDDispatchToDeliveredProduct(widget.placedOrderList!.id); showSnackbar("Order Status updated Order Delivered"); } else { // Show a message if the status is not "Delivered" ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Cannot confirm delivery: Status is not Delivered.')), ); } } else { // Show a message if the date is not selected ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Please select a delivery date.')), ); } Navigator.push(context, MaterialPageRoute(builder: (context){ return RdDeliveredScreen(); })); // Navigator.of(context).pop(); // Close the dialog after submission }, child: Text('Confirm'), ), ], ); }, ); }, ); } // // void showConfirmDeliveryDialog() { // final TextEditingController _deliveryDetailsController = TextEditingController(); // DateTime? selectedDate; // Variable to hold the selected delivery date // // showDialog( // context: context, // builder: (context) { // return AlertDialog( // title: Text('Confirm Delivery'), // content: Column( // mainAxisSize: MainAxisSize.min, // Shrink to fit the content // children: [ // SizedBox(height: 20), // Space between text field and date picker // Text(selectedDate == null // ? 'Select Delivery Date *' // : 'Delivery Date: ${selectedDate!.toLocal()}'.split(' ')[0]), // Show selected date // SizedBox(height: 10), // Space // ElevatedButton( // onPressed: () async { // // Show date picker // DateTime? pickedDate = await showDatePicker( // context: context, // initialDate: DateTime.now(), // firstDate: DateTime.now(), // Prevent past dates // lastDate: DateTime(2101), // ); // if (pickedDate != null && pickedDate != selectedDate) { // selectedDate = pickedDate; // Update the selected date // } // }, // child: Text('Select Date'), // ), // ], // ), // actions: [ // TextButton( // onPressed: () { // Navigator.of(context).pop(); // Close the dialog // }, // child: Text('Cancel'), // ), // TextButton( // onPressed: () async { // if (selectedDate != null) { // // Handle the confirmation logic // String deliveryDetails = _deliveryDetailsController.text; // // // Check if the selected status is "Delivered" // if (selectedStatus == "delivered") { // // Call your API method here // await _getDispatchController.RDDispatchToDeliveredProduct(widget.placedOrderList!.id); // } else { // // Show a message if the status is not "Delivered" // ScaffoldMessenger.of(context).showSnackBar( // SnackBar(content: Text('Cannot confirm delivery: Status is not Delivered.')), // ); // } // } else { // // Show a message if the date is not selected // ScaffoldMessenger.of(context).showSnackBar( // SnackBar(content: Text('Please select a delivery date.')), // ); // } // Navigator.of(context).pop(); // Close the dialog after submission // }, // child: Text('Confirm'), // ), // ], // ); // }, // ); // } @override Widget build(BuildContext context) { return Scaffold( extendBodyBehindAppBar: true, appBar: AppBar( backgroundColor: Colors.transparent, elevation: 0, leading: GestureDetector( onTap: () {}, 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 Text( "Retailer Dispatched Order Details", ), ), body: Stack( fit: StackFit.expand, children: [ Image.asset( 'assets/images/image_1.png', fit: BoxFit.cover, ), SafeArea( child: SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, 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: [ Card( child: Column( children: [ SizedBox( width: Get.width, child: Padding( padding: const EdgeInsets.fromLTRB(8, 8, 8, 0), child: Text( "Invoices", style: GoogleFonts.roboto( fontSize: Get.width * 0.05, fontWeight: FontWeight.bold, ), ), ), ), SizedBox( width: Get.width, child: Padding( padding: const EdgeInsets.fromLTRB(8, 8, 8, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Invoice ID:", style: GoogleFonts.roboto( fontSize: Get.width * 0.04, fontWeight: FontWeight.bold, ), ), Text(widget.placedOrderList!.invoiceId), // Text(widget.placedOrderList!.uniqueId), ], ), ), ), SizedBox( width: Get.width, child: Padding( padding: const EdgeInsets.fromLTRB(8, 8, 8, 0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "Items: ", style: GoogleFonts.roboto( fontSize: Get.width * 0.04, fontWeight: FontWeight.bold, ), ), SizedBox(height: 10), // Add spacing between the title and the list of items Column( children: widget.placedOrderList!.items.map((item) { return Padding( padding: const EdgeInsets.symmetric(vertical: 4.0), // Add some spacing between items child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Expanded( child: Text( "${item.name.toString()} (${item.SKU.toString()})", style: GoogleFonts.roboto( fontSize: Get.width * 0.03, ), overflow: TextOverflow.ellipsis, // Handle long text ), ), Text("x ${item.processquantity.toString()}"), ], ), ); }).toList(), ), ], ), ), ), SizedBox( width: Get.width, child: Padding( padding: const EdgeInsets.fromLTRB(8, 8, 8, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Sub Total : ", style: GoogleFonts.roboto( fontSize: Get.width * 0.04, fontWeight: FontWeight.bold, ), ), Text("₹ ${widget.placedOrderList!.subtotal}"), ], ), ), ), SizedBox( width: Get.width, child: Padding( padding: const EdgeInsets.fromLTRB(8, 8, 8, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "GST : ", style: GoogleFonts.roboto( fontSize: Get.width * 0.04, fontWeight: FontWeight.bold, ), ), Text("₹ ${widget.placedOrderList!.gstTotal.toStringAsFixed(2)}"), ], ), ), ), SizedBox( width: Get.width, child: Padding( padding: const EdgeInsets.fromLTRB(8, 8, 8, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Invoice Amount: ", style: GoogleFonts.roboto( fontSize: Get.width * 0.04, fontWeight: FontWeight.bold, ), ), Text("₹ ${widget.placedOrderList!.invoiceAmount}"), ], ), ), ), SizedBox( width: Get.width, child: Padding( padding: const EdgeInsets.fromLTRB(8, 8, 8, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Courier Status : ", style: GoogleFonts.roboto( fontSize: Get.width * 0.04, fontWeight: FontWeight.bold, ), ), ElevatedButton( onPressed: (){}, // Get.to(() => // RdOrderDetailScreen( // placedOrderList: uniqueOrders[index])), // Navigate to detail screen style: ElevatedButton.styleFrom( foregroundColor: Colors.white, backgroundColor: Colors.lightBlue, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10)), ), child: Text(capitalizeFirstLetter(widget.placedOrderList!.courierStatus), style: GoogleFonts.roboto(fontSize: 14, fontWeight: FontWeight.w400)), ), //Text("₹ ${widget.placedOrderList!.gstTotal}"), ], ), ), ), ], ), ), const SizedBox(height: 8), const SizedBox(height: 8), SizedBox( height: Get.height* 0.19, child:_buildCustomerDetails(), ), const SizedBox(height: 8), _buildBillingInfo(), const SizedBox(height: 8), _buildShippingInfo(), const SizedBox(height: 8), _buildPaymentInfo(), // Card( // child: Column( // children: [ // SizedBox( // width: Get.width, // height: Get.height*0.05, // child: Padding( // padding: // const EdgeInsets.fromLTRB(8, 8, 8, 0), // child: Row( // children: [ // Text( // "Payment Mode : ", // style: GoogleFonts.roboto( // fontSize: Get.width * 0.04, // fontWeight: FontWeight.w500, // ), // ), // Text(capitalizeFirstLetter("Cheque")), // // Text("${widget.placedOrderList!.paymentMode}",maxLines: 4, // // overflow:TextOverflow.ellipsis,) // ], // ), // ), // ), // // // ], // ), // ), const SizedBox(height: 8), Card( child: Column( children: [ SizedBox( width: Get.width, height: Get.height*0.05, child: Padding( padding: const EdgeInsets.fromLTRB(8, 8, 8, 0), child: Row( children: [ Text( "Order Status :", style: GoogleFonts.roboto( fontSize: Get.width * 0.04, fontWeight: FontWeight.w500, ), ), SizedBox(width: Get.width*0.01,), //Text(capitalizeFirstLetter(widget.placedOrderList!.status)), Text("${"Dispatched"}",maxLines: 4, overflow:TextOverflow.ellipsis,) ], ), ), ), ], ), ), const SizedBox(height: 8), SizedBox( height: Get.height * 0.05, child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ const Text( "Status: ", style: TextStyle(fontWeight: FontWeight.bold), ), SizedBox(width: 10), // Space between label and dropdown Expanded( child: DropdownButtonFormField( value: selectedStatus, decoration: InputDecoration( filled: true, fillColor: Colors.white, // White background contentPadding: EdgeInsets.symmetric( vertical: 10, horizontal: 12), border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide( color: Colors.grey, width: 1, ), ), ), items: _statusList.map((String status){ return DropdownMenuItem( value: status, child: Text(capitalizeFirstLetter(status)), ); }).toList(), onChanged: (newValue) { setState(() { selectedStatus = newValue!; }); }, ), ), ], ), ), SizedBox( width: Get.width * 0.4, child: Padding( padding: const EdgeInsets.all(8.0), child: ElevatedButton( onPressed: (){ showConfirmDeliveryDialog(context); }, // Get.to(() => // RdOrderDetailScreen( // placedOrderList: uniqueOrders[index])), // Navigate to detail screen style: ElevatedButton.styleFrom( foregroundColor: Colors.white, backgroundColor: const Color(0xFF004791), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10)), ), child: Text("Update Status", style: GoogleFonts.roboto(fontSize: 14, fontWeight: FontWeight.w400)), ), ), ) ], ), ), ), SizedBox(height: Get.height * 0.04), ], ), ), ), ], ), ); } Widget _buildCustomerDetails() { return Card( child: Column( children: [ _buildSectionTitle("Customer Details"), _buildRow("Name:", widget.placedInvoiceList!.orderId!.addedBy!.name.toString(), Get.width * 0.04), _buildRow("Email:", widget.placedInvoiceList!.orderId!.addedBy!.email.toString(), Get.width * 0.04), _buildRow("Mobile Number:", widget.placedInvoiceList!.orderId!.addedBy!.mobileNumber.toString(), Get.width * 0.04), ], ), ); } Widget _buildBillingInfo() { return Card( child: Column( children: [ _buildSectionTitle("Billing Information"), _buildInfoRow("Address", widget.placedInvoiceList!.orderId!.billTo.toString(), Get.width * 0.04), ], ), ); } Widget _buildShippingInfo() { return Card( child: Column( children: [ _buildSectionTitle("Shipping Information"), _buildInfoRow("Address", widget.placedInvoiceList!.orderId!.shipTo.toString(), Get.width * 0.04), ], ), ); } Widget _buildPaymentInfo() { return Card( child: Padding( padding: const EdgeInsets.all(8.0), child: Row( children: [ Text( "Payment Mode: ", style: GoogleFonts.roboto( fontSize: Get.width * 0.04, fontWeight: FontWeight.w500, ), ), Text(capitalizeFirstLetter(widget.placedInvoiceList!.orderId!.paymentMode.toString())), ], ), ), ); } } Widget _buildSectionTitle(String title) { return SizedBox( width: Get.width, child: Padding( padding: const EdgeInsets.fromLTRB(8, 8, 8, 0), child: Text( title, style: GoogleFonts.roboto(fontSize: Get.width * 0.05, fontWeight: FontWeight.bold), ), ), ); } Widget _buildRow(String label, String value, double fontSize) { return Padding( padding: const EdgeInsets.fromLTRB(8, 2, 8, 2), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( label, style: GoogleFonts.roboto(fontSize: fontSize,fontWeight: FontWeight.bold), ), Text( value, style: GoogleFonts.roboto(fontSize: fontSize), ), ], ), ); } Widget _buildInfoRow(String label, String value, double fontSize) { return Padding( padding: const EdgeInsets.all(8.0), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "$label: ", style: GoogleFonts.roboto(fontSize: fontSize, fontWeight: FontWeight.w500), ), Expanded( child: Text( value, style: GoogleFonts.roboto(fontSize: fontSize), ), ), ], ), ); }