import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import '../config/constants/app_constant.dart'; import '../config/custom_text.dart'; import '../models/ride_model.dart'; import '../screens/rideDetails_screen.dart'; import '../utils/colors.dart'; import 'Avatar.dart'; import 'custom_button.dart'; class RideCard extends StatelessWidget { const RideCard({required this.ride,bool? this.takaction,bool? this.cancellingRideId,VoidCallback? this.oncancel,VoidCallback? this.onupdate}); final RideDto ride; final bool? takaction; final bool? cancellingRideId; final VoidCallback? oncancel; final VoidCallback? onupdate; @override Widget build(BuildContext context) { // defensives (adjust field names to your model as needed) final pickup = ride.startLocationName ?? 'Pick up'; final drop = ride.endLocationName ?? 'Drop off'; final driverName = ride.drivername ?? ride.ridername ?? '—'; final plate = ride.licenseplate ??''; final rating = (ride.averagedriverrating).toStringAsFixed(1) ?? '—'; final avatarUrl = '$mediaUrl${ride.driverimageurl}' ; final dt = ride.rideStartTime ?? ride.timestamp; final timeStr = DateFormat('hh:mm a').format(dt); final dateStr = DateFormat('EEE d MMM').format(dt); final priceStr = '${(ride.totalPrice ?? 0).toStringAsFixed(0)} TND'.toUpperCase(); // badges final isCancelled = (ride.rideStatus ?? '').toUpperCase().contains('CANCEL'); final assigned = (ride.driverId!=null) || (ride.driverId != null && ride.driverId != 0); final isCorporate = (ride.rideclienttype ?? '').toUpperCase() == 'CORPORATE'; return InkWell( borderRadius: BorderRadius.circular(18), onTap: () { Navigator.push( context, MaterialPageRoute(builder: (_) => RideDetailClientScreen(ride: ride)), ); }, child: Container( decoration: BoxDecoration( color: isCorporate ? const Color(0xFFFFF8E1) : Colors.white, borderRadius: BorderRadius.circular(18), border: Border.all( color: isCorporate ? const Color(0xFFD4A017) : Colors.grey.shade500, width: isCorporate ? 1.8 : 1.0, ), boxShadow: [ BoxShadow( color: isCorporate ? const Color(0x22D4A017) : const Color(0x0F000000), blurRadius: 12, offset: const Offset(0, 6), ), ], ), padding: const EdgeInsets.fromLTRB(12, 12, 12, 14), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // header row (driver + car) if(ride.driverusername != "not_exist") Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ Avatar(url: avatarUrl), const SizedBox(width: 10), Expanded( child: Wrap( spacing: 6, runSpacing: 2, crossAxisAlignment: WrapCrossAlignment.center, children: [ Txt( text: driverName=="not_exist" ? "Driver not assigned" : driverName, style: const TextStyle( fontWeight: FontWeight.w600, fontSize: 14, color: Colors.black, ), ), if (plate.isNotEmpty) Txt( text: plate, style: const TextStyle( fontSize: 12, color: Color(0xFF6B6B6B), ), ), Row( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.star, size: 14, color: Color(0xFFFFC107)), const SizedBox(width: 2), Txt( text: rating, style: const TextStyle( fontSize: 12, color: Colors.black87), ), ], ), ], ), ), ClipRRect( borderRadius: BorderRadius.circular(10), child: Image.asset( "assets/images/wanis.png", width: 72, height: 48, fit: BoxFit.cover, ), ), ], ), const SizedBox(height: 12), // pickup / drop labels const Row( children: [ Expanded( child: Txt( text: 'PICK UP', style: TextStyle( fontSize: 11, letterSpacing: .4, color: Color(0xFF8D8D8D)), ), ), Expanded( child: Align( alignment: Alignment.centerRight, child: Txt( text: 'DROP OFF', style: TextStyle( fontSize: 11, letterSpacing: .4, color: Color(0xFF8D8D8D)), ), ), ), ], ), const SizedBox(height: 6), // pickup / drop titles Row( children: [ Expanded( child: Txt( text: pickup, style: const TextStyle( fontSize: 15, fontWeight: FontWeight.w600, color: Colors.black), overflow: TextOverflow.ellipsis, ), ), const SizedBox(width: 8), Expanded( child: Align( alignment: Alignment.centerRight, child: Txt( text: drop, style: const TextStyle( fontSize: 15, fontWeight: FontWeight.w600, color: Colors.black), overflow: TextOverflow.ellipsis, ), ), ), ], ), const SizedBox(height: 12), // date / time / price row Row( children: [ const Icon(Icons.calendar_today_outlined, size: 18), const SizedBox(width: 6), Txt( text: dateStr, style: const TextStyle(fontSize: 13, color: Color(0xFF333333)), ), const SizedBox(width: 16), const Icon(Icons.access_time, size: 18), const SizedBox(width: 6), Txt( text: timeStr, style: const TextStyle(fontSize: 13, color: Color(0xFF333333)), ), const Spacer(), if (!isCorporate) ...[ const Icon(Icons.credit_card, size: 18), const SizedBox(width: 6), Txt( text: priceStr, style: const TextStyle( fontSize: 13, color: Colors.black, fontWeight: FontWeight.w600), ), ], ], ), const SizedBox(height: 12), // status badges row Wrap( spacing: 8, runSpacing: 8, children: [ if (isCorporate) _chip( text: 'Business', bg: const Color(0xFFFFF3CD), fg: const Color(0xFFB8860B), icon: Icons.business_center, ), if (ride.programid!=null) _chip( text: '${ride.programid}', bg: const Color(0xFFFFF3CD), fg: const Color(0xFFB8860B), icon: Icons.group, ), if (isCancelled) _chip( text: 'Cancelled', bg: const Color(0xFFFFE8E8), fg: const Color(0xFFE53935), icon: Icons.close, ), if (!isCancelled && assigned) _chip( text: 'Driver Assigned', bg: const Color(0xFFE9F7EE), fg: const Color(0xFF2E7D32), icon: Icons.phone_in_talk, ), if (!isCancelled && !assigned) _chip( text: 'Driver not assign', bg: const Color(0xFFFFF1E6), fg: const Color(0xFFEF6C00), icon: Icons.content_cut, ), ], ), if(takaction!=null &&oncancel!=null &&onupdate!=null && takaction==true) const SizedBox(height: 12), if(takaction!=null &&oncancel!=null &&onupdate!=null && takaction==true) Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ CustomButton( onClick: () => oncancel!(), width: MediaQuery.of(context).size.width * .4, fontSize: 15, height: 40, borderRadius: 10, txtColor: AppColors.secondaryColor, title: "Cancel ride", backgroundColor: AppColors.white, borderColor: AppColors.secondaryColor, isloading: cancellingRideId, loadercolor: AppColors.secondaryColor, ), CustomButton( onClick: () => onupdate!(), width: MediaQuery.of(context).size.width * .4, fontSize: 15, height: 40, borderRadius: 10, title: 'Update', backgroundColor: AppNewColors.primaryBtnColor, borderColor: AppNewColors.primaryBtnColor, isloading: null, loadercolor: AppNewColors.primaryBtnColor, ), ], ) ], ), ), ); } Widget _chip({ required String text, required Color bg, required Color fg, required IconData icon, }) { return Container( padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6), decoration: BoxDecoration( color: bg, borderRadius: BorderRadius.circular(10), ), child: Row( mainAxisSize: MainAxisSize.min, children: [ Icon(icon, size: 14, color: fg), const SizedBox(width: 6), Txt( text: text, style: TextStyle( fontSize: 12, color: fg, fontWeight: FontWeight.w600, ), ), ], ), ); } }