import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import '../../../core/network/api_client.dart'; import '../../../core/theme/app_theme.dart'; import '../../membership/cubit/membership_cubit.dart'; class SubscriptionCheckoutScreen extends StatefulWidget { const SubscriptionCheckoutScreen({super.key}); @override State createState() => _SubscriptionCheckoutScreenState(); } class _SubscriptionCheckoutScreenState extends State { String _selectedMethod = 'SUPER_QI'; final _refController = TextEditingController(); bool _isSubmitting = false; @override void dispose() { _refController.dispose(); super.dispose(); } Future _submitPayment() async { final ref = _refController.text.trim(); if (ref.isEmpty) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('يرجى كتابة رقم الحركة / المعاملة المسجل في إشعار التحويل.')), ); return; } setState(() => _isSubmitting = true); try { final client = ApiClient(); final res = await client.post('/api/v1/subscription/submit', body: { 'method': _selectedMethod, 'reference_number': ref, }); final data = res['data'] as Map; final isActivated = data['status'] == 'ACTIVE'; if (mounted) { showDialog( context: context, barrierDismissible: false, builder: (_) => Directionality( textDirection: TextDirection.rtl, child: AlertDialog( backgroundColor: AppTheme.cardDark, title: Row( children: [ Icon( isActivated ? Icons.check_circle : Icons.hourglass_top, color: isActivated ? AppTheme.emeraldGreen : Colors.amber, ), const SizedBox(width: 8), Text( isActivated ? 'تم تفعيل العضوية فورياً!' : 'تم استلام طلب العضوية', style: const TextStyle(color: Colors.white, fontSize: 16), ), ], ), content: Text( data['message'] ?? 'تم تسجيل المعاملة بنجاح.', style: const TextStyle(color: Colors.white70), ), actions: [ ElevatedButton( onPressed: () { Navigator.of(context).pop(); context.read().fetchCardDetails(); Navigator.of(context).pop(); }, child: const Text('العودة للبطاقة'), ), ], ), ), ); } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('حدث خطأ: $e'), backgroundColor: AppTheme.crimsonRed), ); } } finally { if (mounted) setState(() => _isSubmitting = false); } } @override Widget build(BuildContext context) { return Directionality( textDirection: TextDirection.rtl, child: Scaffold( appBar: AppBar( title: const Text('الاشتراك وسداد الرسوم'), ), body: SingleChildScrollView( padding: const EdgeInsets.all(20), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // Plan Summary Card Container( padding: const EdgeInsets.all(18), decoration: BoxDecoration( gradient: const LinearGradient( colors: [Color(0xFF1E293B), Color(0xFF0F172A)], ), borderRadius: BorderRadius.circular(16), border: Border.all(color: AppTheme.goldAccent.withOpacity(0.5)), ), child: const Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'عضوية جائزة أوروك الدولية (سنتان)', style: TextStyle(color: AppTheme.goldLight, fontSize: 18, fontWeight: FontWeight.bold), ), SizedBox(height: 8), Text( 'قيمة الاشتراك: 100 دولار أمريكي (132,000 دينار عراقي)', style: TextStyle(color: Colors.white, fontSize: 15, fontWeight: FontWeight.bold), ), SizedBox(height: 6), Text( 'تشمل: خصم 50% في المستشفيات، 40-60% في الفنادق، والوصول لأكثر من 30 تخصصاً تدريبياً مع شهادات معتمدة.', style: TextStyle(color: AppTheme.textMuted, fontSize: 13), ), ], ), ), const SizedBox(height: 24), const Text( 'اختر وسيلة التحويل أو السداد المعتمدة:', style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold), ), const SizedBox(height: 12), _buildMethodRadio('SUPER_QI', 'سوبر كي (SuperQi / كي كارد)', Icons.credit_card), _buildMethodRadio('ZAIN_CASH', 'محفظة زين كاش (ZainCash)', Icons.phone_android), _buildMethodRadio('CLIQ_JORDAN', 'نظام كليك (CliQ للأردن)', Icons.swap_horiz), _buildMethodRadio('SWIFTPAY_IQ', 'دفع إلكتروني مباشر عبر فيزا / ماستركارد', Icons.payment), const SizedBox(height: 18), // Account details display box _buildPaymentInstructionsBox(), const SizedBox(height: 24), // Reference Number Input TextField( controller: _refController, style: const TextStyle(color: Colors.white), decoration: InputDecoration( labelText: 'رقم الحركة / المعاملة (من إيصال التحويل)', labelStyle: const TextStyle(color: AppTheme.textMuted), hintText: 'مثال: 2026091810121420010100166521408745772', hintStyle: const TextStyle(color: Colors.white24, fontSize: 12), prefixIcon: const Icon(Icons.receipt_long, color: AppTheme.goldAccent), filled: true, fillColor: AppTheme.cardDark, border: OutlineInputBorder(borderRadius: BorderRadius.circular(12), borderSide: BorderSide.none), ), ), const SizedBox(height: 24), SizedBox( height: 52, child: ElevatedButton( onPressed: _isSubmitting ? null : _submitPayment, child: _isSubmitting ? const CircularProgressIndicator(color: AppTheme.primaryDark) : const Text('تأكيد التحويل وإصدار البطاقة فورياً'), ), ), ], ), ), ), ); } Widget _buildMethodRadio(String value, String title, IconData icon) { final isSelected = _selectedMethod == value; return Container( margin: const EdgeInsets.only(bottom: 8), decoration: BoxDecoration( color: AppTheme.cardDark, borderRadius: BorderRadius.circular(12), border: Border.all( color: isSelected ? AppTheme.goldAccent : Colors.transparent, width: 1.5, ), ), child: RadioListTile( value: value, groupValue: _selectedMethod, activeColor: AppTheme.goldAccent, secondary: Icon(icon, color: isSelected ? AppTheme.goldAccent : AppTheme.textMuted), title: Text( title, style: TextStyle( color: isSelected ? Colors.white : Colors.white70, fontWeight: isSelected ? FontWeight.bold : FontWeight.normal, fontSize: 14, ), ), onChanged: (val) { if (val != null) setState(() => _selectedMethod = val); }, ), ); } Widget _buildPaymentInstructionsBox() { String content = ''; if (_selectedMethod == 'SUPER_QI') { content = 'قم بالتحويل عبر تطبيق SuperQi إلى رقم الحساب المعتمد:\n' '• رقم الحساب المستلم: 6181121 705 (حساب د. ثامر)\n' '• بعد إتمام الحوالة، انسخ "رقم الحركة" والصقه أدناه لتفعيل البطاقة آلياً.'; } else if (_selectedMethod == 'ZAIN_CASH') { content = 'قم بالتحويل إلى محفظة زين كاش التجارية:\n' '• رقم المحفظة: +964 780 000 0000\n' '• الصق رقم العملية في الخانة أدناه.'; } else if (_selectedMethod == 'CLIQ_JORDAN') { content = 'للمشتركين في الأردن:\n' '• تحويل CliQ إلى الاسم المستعار: URUKPRIZE\n' '• ضع رقم المعاملة البنكية أدناه.'; } else { content = 'سيتم توجيهك إلى بوابة SwiftPayIQ الآمنة لسداد الرسوم عبر بطاقات الدفع.'; } return Container( padding: const EdgeInsets.all(14), decoration: BoxDecoration( color: AppTheme.goldAccent.withOpacity(0.08), borderRadius: BorderRadius.circular(12), border: Border.all(color: AppTheme.goldAccent.withOpacity(0.25)), ), child: Text( content, style: const TextStyle(color: Colors.white70, fontSize: 13, height: 1.5), ), ); } }