import 'package:siro_admin/constant/theme.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:siro_admin/controller/admin/financial_v2_controller.dart'; class FinancialV2Page extends StatelessWidget { const FinancialV2Page({super.key}); @override Widget build(BuildContext context) { final cs = Theme.of(context).colorScheme; final controller = Get.put(FinancialV2Controller()); return Scaffold( backgroundColor: cs.surface, body: Column( children: [ Container( padding: const EdgeInsets.fromLTRB(16, 50, 16, 16), child: Row( children: [ GestureDetector( onTap: () => Get.back(), child: Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: cs.surfaceContainerHighest, borderRadius: BorderRadius.circular(10), border: Border.all(color: cs.outline), ), child: Icon(Icons.arrow_back_ios_new_rounded, color: cs.onSurfaceVariant, size: 16), ), ), const SizedBox(width: 12), Text('الإدارة المالية المتقدمة', style: TextStyle( color: cs.onSurface, fontSize: 18, fontWeight: FontWeight.w700)), const Spacer(), IconButton( icon: Icon(Icons.refresh_rounded, color: cs.onSurfaceVariant), onPressed: () => controller.fetchAllFinancials(), ), ], ), ), Expanded( child: GetBuilder( builder: (ctrl) { if (ctrl.isLoading) { return Center( child: CircularProgressIndicator(color: cs.primary)); } return SingleChildScrollView( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildMainFinancialStats(ctrl.stats, cs), const SizedBox(height: 32), _buildSectionTitle('محفظة سيرو', cs), _buildSiroWalletSection(ctrl.walletSummary, cs), const SizedBox(height: 24), _buildSectionTitle('طرق الدفع', cs), _buildPaymentMethodBreakdown(ctrl.stats, cs), const SizedBox(height: 32), _buildSectionTitle('تسويات الكباتن (مستحقات معلقة)', cs), _buildSettlementsList(ctrl.settlements, cs), const SizedBox(height: 40), ], ), ); }, ), ), ], ), ); } /// تنسيق مبلغ بفواصل آلاف وخانتين عشريتين. /// بلا رمز عملة عمداً: هذا النشر أردني بينما بطاقات هذه الشاشة الأخرى /// ما زالت تكتب "ج.م" ثابتة — راجع صحّتها قبل توحيد الرمز. String _money(double v) { final neg = v < 0; final parts = v.abs().toStringAsFixed(2).split('.'); final digits = parts[0]; final buf = StringBuffer(); for (int i = 0; i < digits.length; i++) { if (i > 0 && (digits.length - i) % 3 == 0) buf.write(','); buf.write(digits[i]); } return '${neg ? '-' : ''}$buf.${parts[1]}'; } /// محفظة سيرو من خادم المدفوعات: الشهر الجاري مقابل الشهر السابق، /// والإجمالي التراكمي، مع فصل الإيداع عن الصرف. Widget _buildSiroWalletSection( Map summary, ColorScheme cs) { if (summary.isEmpty) { return Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( color: cs.surfaceContainerHighest, borderRadius: BorderRadius.circular(16), border: Border.all(color: cs.outline), ), child: Row( children: [ Icon(Icons.account_balance_wallet_outlined, color: cs.onSurfaceVariant, size: 18), const SizedBox(width: 10), Expanded( child: Text( 'تعذّر جلب ملخّص المحفظة من خادم المدفوعات', style: TextStyle(color: cs.onSurfaceVariant, fontSize: 13), ), ), ], ), ); } final mtd = (summary['month_to_date'] as Map?) ?? {}; final allTime = (summary['all_time'] as Map?) ?? {}; final growth = summary['growth_percent']; double n(dynamic v) => double.tryParse('${v ?? 0}') ?? 0; return Column( children: [ Row( children: [ Expanded( child: _buildFinancialCard( 'صافي هذا الشهر', _money(n(mtd['net'])), Icons.calendar_month_rounded, cs.primary, cs, // المؤشّر يظهر فقط عند وجود شهر سابق يُقارن به subtitle: growth == null ? null : '${n(growth) >= 0 ? '▲' : '▼'} ${n(growth).abs().toStringAsFixed(1)}% عن الشهر السابق', subtitleColor: growth == null ? null : (n(growth) >= 0 ? cs.success : cs.danger), ), ), const SizedBox(width: 12), Expanded( child: _buildFinancialCard( 'الإجمالي التراكمي', _money(n(allTime['net'])), Icons.savings_rounded, cs.tertiary, cs, ), ), ], ), const SizedBox(height: 12), Row( children: [ Expanded( child: _buildFinancialCard( 'إيداع هذا الشهر', _money(n(mtd['credits'])), Icons.south_west_rounded, cs.success, cs, subtitle: '${mtd['tx_count'] ?? 0} حركة', ), ), const SizedBox(width: 12), Expanded( child: _buildFinancialCard( 'صرف هذا الشهر', _money(n(mtd['debits'])), Icons.north_east_rounded, cs.warning, cs, ), ), ], ), ], ); } Widget _buildSectionTitle(String title, ColorScheme cs) { return Padding( padding: const EdgeInsets.only(bottom: 16), child: Text( title, style: TextStyle( color: cs.onSurface, fontSize: 16, fontWeight: FontWeight.bold, ), ), ); } Widget _buildMainFinancialStats(Map stats, ColorScheme cs) { return Column( children: [ _buildFinancialCard( 'إجمالي عمولة المنصة', '${stats['total_platform_commission'] ?? 0} ج.م', Icons.account_balance_rounded, cs.primary, cs, ), const SizedBox(height: 12), Row( children: [ Expanded( child: _buildFinancialCard( 'إجمالي دخل الكباتن', '${stats['total_driver_pay'] ?? 0}', Icons.person_pin_rounded, cs.tertiary, cs, isSmall: true, ), ), const SizedBox(width: 12), Expanded( child: _buildFinancialCard( 'إجمالي الإيرادات', '${stats['total_revenue'] ?? 0}', Icons.payments_rounded, cs.success, cs, isSmall: true, ), ), ], ), ], ); } Widget _buildFinancialCard( String title, String value, IconData icon, Color color, ColorScheme cs, {bool isSmall = false, String? subtitle, Color? subtitleColor}) { return Container( padding: EdgeInsets.all(isSmall ? 16 : 24), decoration: BoxDecoration( color: cs.surfaceContainerHighest, borderRadius: BorderRadius.circular(20), border: Border.all(color: color.withValues(alpha: 0.2)), gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [cs.surfaceContainerHighest, color.withValues(alpha: 0.05)], ), ), child: Row( children: [ Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: color.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(14), ), child: Icon(icon, color: color, size: isSmall ? 20 : 28), ), const SizedBox(width: 16), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(title, style: TextStyle(color: cs.onSurfaceVariant, fontSize: 12)), const SizedBox(height: 4), Text(value, style: TextStyle( color: cs.onSurface, fontSize: isSmall ? 18 : 24, fontWeight: FontWeight.bold)), if (subtitle != null) ...[ const SizedBox(height: 4), Text(subtitle, style: TextStyle( color: subtitleColor ?? cs.onSurfaceVariant, fontSize: 11, fontWeight: FontWeight.w600)), ], ], ), ), ], ), ); } Widget _buildPaymentMethodBreakdown( Map stats, ColorScheme cs) { double cash = double.tryParse(stats['cash_payments'].toString()) ?? 0; double digital = double.tryParse(stats['digital_payments'].toString()) ?? 0; double total = cash + digital; if (total == 0) total = 1; return Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( color: cs.surfaceContainerHighest, borderRadius: BorderRadius.circular(20), ), child: Column( children: [ _buildPaymentBar('نقدي (Cash)', cash, total, cs.tertiary, cs), const SizedBox(height: 16), _buildPaymentBar('إلكتروني / محفظة', digital, total, cs.tertiary, cs), ], ), ); } Widget _buildPaymentBar( String label, double value, double total, Color color, ColorScheme cs) { double percent = value / total; return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(label, style: TextStyle(color: cs.onSurface, fontSize: 13)), Text('${value.toStringAsFixed(0)} ج.م', style: TextStyle(color: color, fontWeight: FontWeight.bold)), ], ), const SizedBox(height: 8), ClipRRect( borderRadius: BorderRadius.circular(4), child: LinearProgressIndicator( value: percent, backgroundColor: cs.outline, color: color, minHeight: 8, ), ), ], ); } Widget _buildSettlementsList(List settlements, ColorScheme cs) { if (settlements.isEmpty) { return Center( child: Text('لا توجد تسويات معلقة', style: TextStyle(color: cs.onSurfaceVariant))); } return ListView.builder( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), itemCount: settlements.length, itemBuilder: (ctx, i) { final s = settlements[i]; return Container( margin: const EdgeInsets.only(bottom: 12), padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: cs.surfaceContainerHighest, borderRadius: BorderRadius.circular(16), border: Border.all(color: cs.outline), ), child: Row( children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('${s['first_name']} ${s['last_name']}', style: TextStyle( color: cs.onSurface, fontWeight: FontWeight.bold)), Text(s['phone'] ?? '', style: TextStyle( color: cs.onSurfaceVariant, fontSize: 12)), const SizedBox(height: 4), Text('${s['total_rides']} رحلة مكتملة', style: TextStyle(color: cs.tertiary, fontSize: 11)), ], ), ), Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ Text('المستحقات', style: TextStyle(color: cs.onSurfaceVariant, fontSize: 10)), Text('${s['total_earned']} ج.م', style: TextStyle( color: cs.primary, fontSize: 18, fontWeight: FontWeight.bold)), const SizedBox(height: 4), ElevatedButton( onPressed: () {}, style: ElevatedButton.styleFrom( backgroundColor: cs.primary.withValues(alpha: 0.1), foregroundColor: cs.primary, elevation: 0, minimumSize: const Size(80, 32), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8)), ), child: const Text('تسوية', style: TextStyle( fontSize: 11, fontWeight: FontWeight.bold)), ), ], ), ], ), ); }, ); } }