import 'package:flutter/material.dart'; import 'package:driver/core/l10n/app_localizations.dart'; import '../../../core/design/tokens.dart'; import '../../../core/ui/tripz_text_field.dart'; import '../../trip/data/driver_trip_repository.dart'; import '../../trip/data/models/chat_message_model.dart'; import '../../../core/storage/token_store.dart'; import '../../../core/di.dart'; /// شاشة دردشة السائق مع العميل داخل الرحلة. /// /// تُستخدم من شاشة تنفيذ الرحلة (Q4). class DriverChatScreen extends StatefulWidget { final String tripId; const DriverChatScreen({super.key, required this.tripId}); @override State createState() => _DriverChatScreenState(); } class _DriverChatScreenState extends State { final TextEditingController _controller = TextEditingController(); final ScrollController _scrollController = ScrollController(); late final DriverTripRepository _repo; List _messages = []; bool _loading = true; bool _sending = false; @override void initState() { super.initState(); _repo = DriverTripRepository(getIt()); _loadMessages(); } Future _loadMessages() async { final userId = await getIt().access(); final messages = await _repo.getMessages(widget.tripId, currentUserId: userId); if (mounted) { setState(() { _messages = messages; _loading = false; }); _scrollToBottom(); } } Future _sendMessage() async { final text = _controller.text.trim(); if (text.isEmpty || _sending) return; setState(() => _sending = true); _controller.clear(); final userId = await getIt().access(); final msg = await _repo.sendMessage(widget.tripId, text, currentUserId: userId); if (msg != null && mounted) { setState(() => _messages.add(msg)); _scrollToBottom(); } if (mounted) setState(() => _sending = false); } void _scrollToBottom() { Future.delayed(const Duration(milliseconds: 100), () { if (_scrollController.hasClients) { _scrollController.animateTo( _scrollController.position.maxScrollExtent, duration: const Duration(milliseconds: 200), curve: Curves.easeOut, ); } }); } @override void dispose() { _controller.dispose(); _scrollController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final l10n = AppLocalizations.of(context)!; final cs = Theme.of(context).colorScheme; return Scaffold( appBar: AppBar( title: Text(l10n.chat), ), body: Column( children: [ // الرسائل Expanded( child: _loading ? const Center(child: CircularProgressIndicator()) : _messages.isEmpty ? Center( child: Text( l10n.noMessagesYet, style: TextStyle(color: cs.onSurfaceVariant), ), ) : ListView.builder( controller: _scrollController, padding: const EdgeInsets.all(T.s16), itemCount: _messages.length, itemBuilder: (context, index) { final msg = _messages[index]; return _MessageBubble(message: msg); }, ), ), // حقل الإرسال Container( padding: const EdgeInsets.all(T.s12), decoration: BoxDecoration( color: cs.surface, boxShadow: [ BoxShadow( color: Colors.black.withValues(alpha: 0.05), blurRadius: 8, offset: const Offset(0, -2), ), ], ), child: SafeArea( child: Row( children: [ Expanded( child: TripzTextField( controller: _controller, hint: l10n.typeMessage, onSubmitted: (_) => _sendMessage(), ), ), const SizedBox(width: T.s8), IconButton( onPressed: _sending ? null : _sendMessage, icon: _sending ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2), ) : Icon(Icons.send, color: cs.primary), ), ], ), ), ), ], ), ); } } class _MessageBubble extends StatelessWidget { final ChatMessageModel message; const _MessageBubble({required this.message}); @override Widget build(BuildContext context) { final cs = Theme.of(context).colorScheme; return Align( alignment: message.isMe ? Alignment.centerRight : Alignment.centerLeft, child: Container( margin: const EdgeInsets.only(bottom: T.s8), padding: const EdgeInsets.symmetric( horizontal: T.s12, vertical: T.s8, ), constraints: BoxConstraints( maxWidth: MediaQuery.of(context).size.width * 0.75, ), decoration: BoxDecoration( color: message.isMe ? cs.primary : cs.surfaceContainerHighest, borderRadius: BorderRadius.circular(T.r12).copyWith( bottomRight: message.isMe ? const Radius.circular(4) : null, bottomLeft: !message.isMe ? const Radius.circular(4) : null, ), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( message.text, style: TextStyle( color: message.isMe ? cs.onPrimary : cs.onSurface, ), ), const SizedBox(height: 2), Text( '${message.createdAt.hour.toString().padLeft(2, '0')}:${message.createdAt.minute.toString().padLeft(2, '0')}', style: TextStyle( fontSize: 10, color: message.isMe ? cs.onPrimary.withValues(alpha: 0.7) : cs.onSurfaceVariant, ), ), ], ), ), ); } }