import 'package:cheminova/models/place_order_list_model.dart'; import 'package:cheminova/screens/order_management/order_management_detail_screen.dart'; import 'package:cheminova/widgets/my_drawer.dart'; import 'package:flutter/foundation.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 '../../controller/cart_controller.dart'; import '../../controller/get_order_placed_controller.dart'; import '../../models/product_model1.dart'; class OrderManagementScreen extends StatefulWidget { final Product? productModel; PlacedOrderList? placeOrder; OrderManagementScreen({super.key, this.productModel, this.placeOrder}); @override State createState() => _OrderManagementScreenState(); } class _OrderManagementScreenState extends State { final GetPlacedOrderController _getPlacedOrderController = Get.put(GetPlacedOrderController()); final CartController _cartController = Get.put(CartController()); final GlobalKey _refreshIndicatorKey = GlobalKey(); @override void initState() { super.initState(); getOrder1(); } Future _onRefresh() async { await getOrder1(); await Future.delayed(const Duration(seconds: 1)); } Future getOrder1() async { await _getPlacedOrderController.getOrders(); if (kDebugMode) { print("Order fetched successfully"); } } String capitalizeFirstLetter(String text) { if (text.isEmpty) return text; return text[0].toUpperCase() + text.substring(1).toLowerCase(); } String formatDate(String apiDate) { DateTime parsedDate = DateTime.parse(apiDate); String formattedDate = DateFormat('dd-MMM-yyyy').format(parsedDate); return formattedDate; } @override Widget build(BuildContext context) { return Stack( children: [ Scaffold( extendBodyBehindAppBar: true, appBar: AppBar( centerTitle: true, 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 Text("Order Management"), ), drawer: const 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), Expanded( child: 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( children: [ Obx( () => Container( height: Get.height * 0.05, padding: const EdgeInsets.symmetric(horizontal: 12), margin: const EdgeInsets.only(bottom: 8), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(5), ), child: DropdownButtonHideUnderline( child: DropdownButton( isExpanded: true, value: _getPlacedOrderController.productStatus.value, onChanged: (newValue) { if (newValue != null) { _getPlacedOrderController.updateProductStatus(newValue); } }, items: ['All', 'Delivered', 'Processing', 'Cancelled'] .map>((String value) { return DropdownMenuItem( value: value, child: Text(value), ); }).toList(), ), ), ), ), Expanded( child: RefreshIndicator( key: _refreshIndicatorKey, onRefresh: _onRefresh, color: Colors.black, backgroundColor: Colors.white, child: Obx(() { final Set uniqueOrderIds = {}; final List uniqueOrders = []; final orders = _getPlacedOrderController.filterOrder.isNotEmpty ? _getPlacedOrderController.filterOrder : _getPlacedOrderController.placedOrders; for (var order in orders) { if (uniqueOrderIds.add(order.sId ?? '')) { uniqueOrders.add(order); } } return ListView.builder( padding: EdgeInsets.zero, itemCount: uniqueOrders.length, itemBuilder: (context, index) { final order = uniqueOrders[index]; final productNames = order.orderItem! .map((item) => capitalizeFirstLetter(item.name ?? '')) .join(', '); return Card( margin: const EdgeInsets.symmetric(vertical: 8), child: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildInfoRow("Order ID:", "${order.uniqueId}"), const SizedBox(height: 8), _buildInfoRow("Product Names:", productNames), const SizedBox(height: 8), _buildInfoRow("Order Date:", formatDate("${order.createdAt}")), const SizedBox(height: 8), _buildInfoRow("Status:", capitalizeFirstLetter("${order.status}")), const SizedBox(height: 16), Center( child: ElevatedButton( onPressed: () => Get.to(() => OrderManagementDetailScreen(placedOrderList: uniqueOrders[index])), style: ElevatedButton.styleFrom( foregroundColor: Colors.white, backgroundColor: const Color(0xFF004791), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)), minimumSize: Size(Get.width * 0.4, 40), ), child: Text("View Details", style: GoogleFonts.roboto(fontSize: 14, fontWeight: FontWeight.w400)), ), ) ], ), ), ); }, ); }), ), ), ], ), ), ), ), SizedBox(height: Get.height * 0.02), ], ), ), ], ), ), Obx( () { if (_getPlacedOrderController.isLoading.value) { return Container( color: Colors.black.withOpacity(0.5), child: const Center( child: CircularProgressIndicator(strokeWidth: 1), ), ); } return const SizedBox.shrink(); }, ) ], ); } Widget _buildInfoRow(String label, String value) { return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( label, style: GoogleFonts.roboto(fontSize: 14, fontWeight: FontWeight.bold), ), const SizedBox(width: 4), Expanded( child: Text( value, style: GoogleFonts.roboto(fontSize: 14), ), ), ], ); } }