import 'package:flutter/material.dart'; import 'package:local_auth/local_auth.dart'; import 'package:sefer_driver/constant/box_name.dart'; import 'package:sefer_driver/main.dart'; import '../../../controller/payment/smsPaymnet/pay_out_syria_controller.dart'; class PayoutScreen extends StatefulWidget { // استقبال كل البيانات المطلوبة جاهزة final double amountToWithdraw; final String payoutPhoneNumber; final String walletType; const PayoutScreen({ super.key, required this.amountToWithdraw, required this.payoutPhoneNumber, required this.walletType, }); @override _PayoutScreenState createState() => _PayoutScreenState(); } class _PayoutScreenState extends State { final _payoutService = PayoutService(); final _localAuth = LocalAuthentication(); bool _isLoading = false; Future _handlePayoutRequest() async { try { // 1. طلب المصادقة البيومترية bool didAuthenticate = await _localAuth.authenticate( localizedReason: 'استخدم بصمة الإصبع لتأكيد عملية السحب', options: const AuthenticationOptions( biometricOnly: true, sensitiveTransaction: true, ), ); if (didAuthenticate && mounted) { setState(() => _isLoading = true); // 2. إرسال الطلب إلى السيرفر بالبيانات الجاهزة final result = await _payoutService.requestPayout( driverId: box.read(BoxName.driverID).toString(), // استبدله بـ box.read amount: widget.amountToWithdraw, payoutPhoneNumber: widget.payoutPhoneNumber, walletType: widget.walletType, ); setState(() => _isLoading = false); if (result != null && result.contains("successfully")) { // 3. عرض رسالة النجاح النهائية _showSuccessDialog(); } else { _showErrorDialog(result ?? "حدث خطأ غير معروف."); } } } catch (e) { setState(() => _isLoading = false); _showErrorDialog("جهازك لا يدعم المصادقة البيومترية أو لم يتم إعدادها."); debugPrint("Biometric error: $e"); } } @override Widget build(BuildContext context) { // حساب المبلغ الإجمالي المخصوم final totalDeducted = widget.amountToWithdraw + PayoutService.payoutFee; return Scaffold( appBar: AppBar(title: const Text("تأكيد سحب الأموال")), body: SingleChildScrollView( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Icon(Icons.wallet, size: 64, color: Colors.blue), const SizedBox(height: 16), Text( "تأكيد تفاصيل عملية السحب", style: Theme.of(context).textTheme.headlineSmall, textAlign: TextAlign.center, ), const SizedBox(height: 24), _buildSummaryCard(totalDeducted), const SizedBox(height: 32), _isLoading ? const Center(child: CircularProgressIndicator()) : ElevatedButton.icon( onPressed: _handlePayoutRequest, icon: const Icon(Icons.fingerprint), label: const Text("تأكيد السحب بالبصمة"), style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 16), textStyle: const TextStyle( fontSize: 18, fontWeight: FontWeight.bold), ), ), ], ), ), ); } Widget _buildSummaryCard(double totalDeducted) { return Card( elevation: 2, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), child: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ _summaryRow("المبلغ المسحوب:", "${widget.amountToWithdraw.toStringAsFixed(2)} ل.س"), const Divider(), _summaryRow("عمولة السحب:", "${PayoutService.payoutFee.toStringAsFixed(2)} ل.س"), const Divider(thickness: 1.5), _summaryRow( "الإجمالي المخصوم من رصيدك:", "${totalDeducted.toStringAsFixed(2)} ل.س", isTotal: true, ), const SizedBox(height: 16), _summaryRow("سيتم التحويل إلى هاتف:", widget.payoutPhoneNumber), _summaryRow("عبر محفظة:", widget.walletType), ], ), ), ); } Widget _summaryRow(String title, String value, {bool isTotal = false}) { final titleStyle = TextStyle( fontSize: 16, color: isTotal ? Theme.of(context).primaryColor : Colors.black87, fontWeight: isTotal ? FontWeight.bold : FontWeight.normal, ); final valueStyle = titleStyle.copyWith( fontWeight: FontWeight.bold, ); return Padding( padding: const EdgeInsets.symmetric(vertical: 6.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(title, style: titleStyle), Text(value, style: valueStyle), ], ), ); } void _showErrorDialog(String message) { if (!mounted) return; showDialog( context: context, builder: (ctx) => AlertDialog( title: const Text('حدث خطأ'), content: Text(message), actions: [ TextButton( child: const Text('موافق'), onPressed: () => Navigator.of(ctx).pop()) ], ), ); } void _showSuccessDialog() { if (!mounted) return; showDialog( context: context, barrierDismissible: false, builder: (ctx) => AlertDialog( title: const Text('تم إرسال طلبك بنجاح'), content: Text( "سيتم تحويل المال إلى المحفظة التي أوردتها (${widget.walletType})، إلى الرقم ${widget.payoutPhoneNumber}، خلال مدة قصيرة. يرجى الانتظار، ستصلك رسالة تأكيد من محفظتك حال وصولها. شكراً لك."), actions: [ TextButton( child: const Text('موافق'), onPressed: () { Navigator.of(ctx).pop(); Navigator.of(context).pop(); }, ), ], ), ); } }