import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import '../../services/apiService.dart'; class FlightListWidget extends StatefulWidget { final bool hasAction; final String? tripType; final List> flightList; final Function(bool, Map, String) onOpen; final Function(Map) onDeleteFlight; final Map? apiData; final Function(String, bool) onAddNew; final bool isViewMode; const FlightListWidget({ Key? key, required this.flightList, required this.onOpen, required this.onDeleteFlight, required this.onAddNew, required this.apiData, required this.isViewMode, required this.hasAction, this.tripType, }) : super(key: key); @override _FlightListWidgetState createState() => _FlightListWidgetState(); } class _FlightListWidgetState extends State { ApiService apiService = ApiService(); // late Map countryMap; Map countryMap = {}; @override void initState() { super.initState(); loadCountryList(); // Call your method here } Future loadCountryList() async { final result = await apiService.fetchFlightsCountryList(); print("ResultCountry : $result"); // Create a map: Country_Code -> "City, Airport" Map tempCountryMap = {}; for (var country in result) { String city = country['City'] ?? ''; String airport = country['Airport'] ?? ''; String displayName = '${country['City']} - ${country['Airport']}'; // String displayName = '${country['City']} | ${country['Airport']}'; tempCountryMap[country['Code']] = displayName; } setState(() { countryMap = tempCountryMap; // Update the map }); } @override Widget build(BuildContext context) { final isDesktop = MediaQuery.of(context).size.width > 1024; ApiService apiService = ApiService(); late Map countryMap; late List countryCodes; void checkClass() { if (widget.hasAction) { if (widget.tripType?.isNotEmpty == true) { print("Teppp - $widget.tripType"); widget.onAddNew("Flight", true); } else { showDialog( context: context, builder: (context) { return AlertDialog( title: const Text('Select Trip Type'), content: const Text( 'Please select a trip type before adding a flight.'), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text('OK'), ), ], ); }, ); } } else { print("Teppp No - $widget.tripType"); widget.onAddNew("Flight", true); } } return Padding( padding: const EdgeInsets.all(16.0), child: Container( margin: const EdgeInsets.only(top: 16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // Header with title and button Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ // Text( // "Flight Booking List", // style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), // ), MouseRegion( cursor: widget.isViewMode ? SystemMouseCursors.forbidden : SystemMouseCursors.click, child: GestureDetector( onTap: widget.isViewMode ? null : () { checkClass(); print("New data"); }, child: Row( mainAxisSize: MainAxisSize.min, // Ensures content fits nicely children: [ // Text( // "Add New", // style: TextStyle(fontSize: 13), // ), // SizedBox(width: 8), // spacing between icon and text Icon( Icons.add_circle_sharp, size: 30, color: Color(0xFF114D8B), ) ], ), ), ), ], ), const SizedBox(height: 16), // Scroll behavior based on device _buildData(context, isDesktop) ], ), ), ); } Widget _buildData(BuildContext context, bool isDesktop) { List> filteredList = widget.flightList.where((item) => item["is_active"] == "1").toList(); print("filteredList- $filteredList"); List visatypeList = widget.apiData?['flight_class'] ?? []; String getRequestForClass(String? specialRequestKey) { if (specialRequestKey == null) return "N/A"; return visatypeList .firstWhere( (element) => element["dropdown_key"].toString() == specialRequestKey, orElse: () => {"dropdown_value": "N/A"}, )["dropdown_value"] .toString(); } String formatDate(String dateString) { try { DateTime date = DateTime.parse(dateString); return DateFormat('d MMM yy').format(date); // Example: 4 Apr 25 } catch (e) { return "Invalid Date"; } } String formatTime(String timeString) { try { final parts = timeString.split(':'); final now = DateTime.now(); final dateTime = DateTime( now.year, now.month, now.day, int.parse(parts[0]), int.parse(parts[1]), ); return DateFormat.jm().format(dateTime); // e.g., 12:16 PM or 12 AM } catch (e) { return "Invalid Time"; } } return ListView.builder( shrinkWrap: true, physics: NeverScrollableScrollPhysics(), itemCount: filteredList.length, itemBuilder: (context, index) { final item = filteredList[index]; return Container( margin: EdgeInsets.symmetric(horizontal: 8, vertical: 6), decoration: BoxDecoration( borderRadius: BorderRadius.circular(12), // color: Colors.orange.shade50, color: Colors.white, boxShadow: [ BoxShadow( color: Colors.black12, blurRadius: 2, offset: Offset(0, 2), // down ), // Top shadow BoxShadow( color: Colors.black12, blurRadius: 0.5, offset: Offset(0, -1), // up ), ], border: Border( top: BorderSide( color: Colors.white70, // Change color to match your theme width: 2, ), ), ), child: Padding( padding: EdgeInsets.all(12), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Text( item["trip_type"]?.toString() ?? "N/A", style: TextStyle( fontSize: 14, fontWeight: FontWeight.w800, fontFamily: "Archivo"), ), Spacer(), GestureDetector( onTap: () => widget.onOpen(true, item, "Flight"), child: Image.asset('assets/images/IconsImg/edit.png', width: 20, height: 15), ), SizedBox(width: 10), GestureDetector( onTap: () => widget.onDeleteFlight(item), child: Image.asset('assets/images/IconsImg/delete.png', width: 20, height: 15), ), ], ), Divider( color: Colors.blueGrey.shade50, ), SizedBox(height: 4), Row( children: [ Expanded( flex: 2, child: Text( "Class", style: TextStyle(fontSize: 11, fontFamily: "Archivo"), )), Expanded( flex: 2, child: Text( "Sector", style: TextStyle(fontSize: 11, fontFamily: "Archivo"), )), Expanded( flex: 2, child: Text( "Date", style: TextStyle(fontSize: 11, fontFamily: "Archivo"), )), Expanded( flex: 2, child: Text( "Time", style: TextStyle(fontSize: 11, fontFamily: "Archivo"), )), ], ), SizedBox(height: 4), // Trip Rows if (isDesktop) if (item["trips"] != null && item["trips"].isNotEmpty) ...item["trips"].map((trip) { String fromPlaceCountry = countryMap[trip["from_place"]?.toString()] ?? "Unknown Country"; String toPlaceCountry = countryMap[trip["to_place"]?.toString()] ?? "Unknown Country"; return Padding( padding: const EdgeInsets.symmetric(vertical: 4.0), child: Row( children: [ Expanded( flex: 2, child: Text( getRequestForClass(trip["class"].toString()) ?? "N/A", style: TextStyle( fontSize: 12, fontWeight: FontWeight.bold), ), ), Expanded( flex: 2, child: Text( "$fromPlaceCountry (from) - (to) $toPlaceCountry", // "${trip["from_place"]?.toString()} - ${trip["to_place"]?.toString()}", style: TextStyle( fontSize: 12, fontWeight: FontWeight.w600, fontFamily: "Archivo"), ), ), Expanded( flex: 2, child: Text( formatDate(trip["date"] ?? ""), style: TextStyle( fontSize: 12, fontWeight: FontWeight.bold, fontFamily: "Archivo"), ), ), Expanded( flex: 2, child: Text(formatTime(trip["time"] ?? ""), style: TextStyle( fontSize: 12, fontWeight: FontWeight.bold, fontFamily: "Archivo")), ), ], ), ); }).toList(), if (!isDesktop && item["trips"] != null && item["trips"].isNotEmpty) ...item["trips"].map((trip) { return Padding( padding: const EdgeInsets.symmetric(vertical: 6.0), child: Container( decoration: BoxDecoration( border: Border.all(color: Colors.grey.shade300), borderRadius: BorderRadius.circular(6), color: Colors.grey.shade50, ), padding: const EdgeInsets.all(8), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildKeyValueRow( "Class", getRequestForClass(trip["class"].toString()) ?? "N/A"), _buildKeyValueRow("From - To", "${trip["from_place"] ?? "N/A"} - ${trip["to_place"] ?? "N/A"}"), _buildKeyValueRow( "Date", formatDate(trip["date"] ?? "")), _buildKeyValueRow( "Time", formatTime(trip["time"] ?? "")), ], ), ), ); }).toList(), ], ), ), ); }, ); } Widget _buildKeyValueRow(String key, String value) { return Padding( padding: const EdgeInsets.only(bottom: 4.0), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox( width: 80, // adjust width as needed child: Text( "$key:", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 12), ), ), Expanded( child: Text( value, style: TextStyle(fontSize: 12), ), ), ], ), ); } }