import 'package:siro_admin/constant/theme.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import '../../../constant/box_name.dart'; import '../../../controller/admin/captain_admin_controller.dart'; import '../../../main.dart'; // Import main to access myPhone import '../../widgets/elevated_btn.dart'; import '../../widgets/my_scafold.dart'; import '../../widgets/my_textField.dart'; import '../../widgets/snackbar.dart'; import '../quality/driver_scorecard_page.dart'; import 'form_captain.dart'; class CaptainDetailsPage extends StatelessWidget { const CaptainDetailsPage({super.key}); @override Widget build(BuildContext context) { final cs = Theme.of(context).colorScheme; final Map data = Get.arguments['data']; final controller = Get.find(); String myPhone = box.read(BoxName.adminPhone).toString(); // Define Super Admin Logic final bool isSuperAdmin = myPhone == '963942542053' || myPhone == '963992952235'; return MyScafolld( title: 'Captain Profile'.tr, isleading: true, body: [ SingleChildScrollView( padding: const EdgeInsets.only(bottom: 40), child: Column( children: [ // --- Header Section (Avatar & Name) --- _buildHeaderSection(context, data), const SizedBox(height: 20), Padding( padding: const EdgeInsets.symmetric(horizontal: 16.0), child: Column( children: [ // --- Personal Information Card --- _buildInfoCard( title: 'Personal Information', icon: Icons.person, children: [ _buildDetailTile( Icons.email_outlined, 'Email', isSuperAdmin ? data['email'] : _maskEmail( data['email']) // Mask email for non-super ), _buildDetailTile( Icons.phone_iphone, 'Phone', _formatPhoneNumber( data['phone'].toString(), isSuperAdmin)), _buildDetailTile(Icons.transgender, 'Gender', data['gender'] ?? 'Not specified'), _buildDetailTile(Icons.cake_outlined, 'Birthdate', data['birthdate'] ?? 'N/A'), ], ), const SizedBox(height: 16), // --- Ride Statistics Card --- _buildInfoCard( title: 'Performance & Stats', icon: Icons.bar_chart_rounded, children: [ _buildDetailTile(Icons.star_rate_rounded, 'Rating', '${data['ratingPassenger'] ?? 0.0} / 5.0', valueColor: cs.warning), _buildDetailTile(Icons.directions_car_filled_outlined, 'Total Rides', data['countPassengerRide']), _buildDetailTile(Icons.cancel_outlined, 'Canceled Rides', data['countPassengerCancel'], valueColor: cs.danger), ], ), const SizedBox(height: 30), // --- Action Buttons --- _buildActionButtons( context, controller, data, isSuperAdmin), ], ), ), ], ), ), ], ); } // --- Header with Gradient Background --- Widget _buildHeaderSection(BuildContext context, Map data) { final cs = Theme.of(context).colorScheme; return Container( width: double.infinity, padding: const EdgeInsets.symmetric(vertical: 25), // كان Colors.white ثابتاً: بطاقة بيضاء فوق خلفية داكنة في الوضع الداكن. decoration: BoxDecoration( color: cs.surface, border: Border(bottom: BorderSide(color: cs.outline)), ), child: Column( children: [ CircleAvatar( radius: 45, backgroundColor: cs.primary.withValues(alpha: 0.1), child: Text( data['first_name'] != null ? data['first_name'][0].toUpperCase() : 'C', style: TextStyle( fontSize: 35, fontWeight: FontWeight.bold, color: cs.primary), ), ), const SizedBox(height: 12), Text( '${data['first_name']} ${data['last_name']}', style: const TextStyle( fontSize: 22, fontWeight: FontWeight.bold, color: Colors.black87), ), const SizedBox(height: 4), Container( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4), decoration: BoxDecoration( color: cs.success.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(20), ), child: Text( 'Active Captain'.tr, style: TextStyle( fontSize: 12, color: cs.success, fontWeight: FontWeight.w600), ), ), ], ), ); } Widget _buildInfoCard( {required String title, required IconData icon, required List children}) { final cs = Theme.of(Get.context!).colorScheme; return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: cs.surfaceContainerHighest, borderRadius: BorderRadius.circular(16), border: Border.all(color: cs.outline), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Icon(icon, color: cs.primary, size: 22), const SizedBox(width: 10), Text(title.tr, style: const TextStyle( fontSize: 17, fontWeight: FontWeight.bold)), ], ), Divider(height: 25, color: cs.onSurfaceVariant.withValues(alpha: 0.2)), ...children, ], ), ); } Widget _buildDetailTile(IconData icon, String label, dynamic value, {Color? valueColor}) { final cs = Theme.of(Get.context!).colorScheme; return Padding( padding: const EdgeInsets.symmetric(vertical: 8.0), child: Row( children: [ Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: cs.onSurfaceVariant, borderRadius: BorderRadius.circular(8)), child: Icon(icon, color: cs.onSurfaceVariant, size: 18), ), const SizedBox(width: 14), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(label.tr, style: TextStyle(fontSize: 12, color: cs.onSurfaceVariant)), Text( value?.toString() ?? 'N/A', style: TextStyle( fontSize: 15, fontWeight: FontWeight.w600, color: valueColor ?? Colors.black87), ), ], ), ), ], ), ); } Widget _buildActionButtons( BuildContext context, CaptainAdminController controller, Map data, bool isSuperAdmin) { final cs = Theme.of(context).colorScheme; return Column( children: [ // Driver Scorecard Button SizedBox( width: double.infinity, height: 50, child: ElevatedButton.icon( icon: const Icon(Icons.analytics_outlined, color: Colors.white), label: Text("بطاقة الأداء (Scorecard)", style: const TextStyle(color: Colors.white, fontSize: 16)), style: ElevatedButton.styleFrom( backgroundColor: cs.info, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12)), ), onPressed: () { Get.to( () => DriverScorecardPage(driverId: data['id'].toString())); }, ), ), const SizedBox(height: 16), // Notification is available for everyone SizedBox( width: double.infinity, height: 50, child: ElevatedButton.icon( icon: Icon(Icons.notifications_active_outlined, color: cs.onPrimary), label: Text("Send Notification".tr, style: TextStyle(color: cs.onPrimary, fontSize: 16)), style: ElevatedButton.styleFrom( backgroundColor: cs.primary, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12)), ), onPressed: () => _showSendNotificationDialog(controller, data), ), ), // Edit and Delete ONLY for Super Admin if (isSuperAdmin) ...[ const SizedBox(height: 16), Row( children: [ Expanded( child: ElevatedButton.icon( icon: const Icon(Icons.edit_note_rounded, size: 20), label: Text("Edit".tr), style: ElevatedButton.styleFrom( backgroundColor: cs.warning.withValues(alpha: 0.10), foregroundColor: cs.warning, elevation: 0, side: BorderSide(color: cs.warning.withValues(alpha: 0.4)), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12)), padding: const EdgeInsets.symmetric(vertical: 12), ), onPressed: () { Get.to(() => const FormCaptain(), arguments: { 'isEditMode': true, 'captainData': data, }); }, ), ), const SizedBox(width: 16), Expanded( child: ElevatedButton.icon( icon: const Icon(Icons.delete_outline_rounded, size: 20), label: Text("Delete".tr), style: ElevatedButton.styleFrom( backgroundColor: cs.danger, foregroundColor: cs.danger, elevation: 0, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12)), padding: const EdgeInsets.symmetric(vertical: 12), ), onPressed: () => _showDeleteConfirmation(data), ), ), ], ), ] else ...[ // Message for normal admins const SizedBox(height: 15), Text( "Only Super Admins can edit or delete captains.", style: TextStyle( color: cs.onSurfaceVariant, fontSize: 12, fontStyle: FontStyle.italic), ) ] ], ); } // --- Helper Methods --- String _formatPhoneNumber(String phone, bool isSuperAdmin) { if (isSuperAdmin) return phone; if (phone.length <= 4) return phone; return '${'*' * (phone.length - 4)}${phone.substring(phone.length - 4)}'; } String _maskEmail(String? email) { if (email == null || email.isEmpty) return 'N/A'; int atIndex = email.indexOf('@'); if (atIndex <= 1) return email; // Too short to mask return '${email.substring(0, 2)}****${email.substring(atIndex)}'; } void _showSendNotificationDialog( CaptainAdminController controller, Map data) { final cs = Theme.of(Get.context!).colorScheme; Get.defaultDialog( title: 'Send Notification'.tr, titleStyle: const TextStyle(fontWeight: FontWeight.bold), content: Form( key: controller.formCaptainPrizeKey, child: Column( children: [ MyTextForm( controller: controller.titleNotify, label: 'Title'.tr, hint: 'Enter notification title'.tr, type: TextInputType.text, ), const SizedBox(height: 10), MyTextForm( controller: controller.bodyNotify, label: 'Body'.tr, hint: 'Enter message body'.tr, type: TextInputType.text, ), ], ), ), confirm: SizedBox( width: 100, child: MyElevatedButton( title: 'Send', onPressed: () { // Check if key is valid (might be recreated) if (controller.formCaptainPrizeKey.currentState?.validate() ?? true) { // FirebaseMessagesController().sendNotificationToAnyWithoutData( // controller.titleNotify.text, // controller.bodyNotify.text, // data['passengerToken'] ?? '', // Safety check // 'order.wav'); Get.back(); mySnackbarSuccess("Notification Sent"); } }, ), ), cancel: TextButton( onPressed: () => Get.back(), child: Text('Cancel'.tr, style: TextStyle(color: cs.onSurfaceVariant))), ); } void _showDeleteConfirmation(Map user) { final cs = Theme.of(Get.context!).colorScheme; Get.defaultDialog( title: 'Confirm Deletion'.tr, titleStyle: TextStyle(color: cs.danger, fontWeight: FontWeight.bold), middleText: 'Are you sure you want to delete ${user['first_name']}? This action cannot be undone.' .tr, confirm: ElevatedButton( style: ElevatedButton.styleFrom(backgroundColor: cs.danger), onPressed: () { // Call delete function here // controller.deleteCaptain(user['id']); Get.back(); mySnackbarWarning("Captain has been removed"); }, child: Text('Delete'.tr, style: const TextStyle(color: Colors.white)), ), cancel: TextButton( onPressed: () => Get.back(), child: Text('Cancel'.tr, style: TextStyle(color: cs.onSurfaceVariant))), ); } }