import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:lucide_icons_flutter/lucide_icons.dart'; import 'package:intl/intl.dart'; import '../../transactions/data/models.dart'; import '../../transactions/providers/providers.dart'; class MaturityDialog extends ConsumerStatefulWidget { final Transaction transaction; const MaturityDialog({super.key, required this.transaction}); @override ConsumerState createState() => _MaturityDialogState(); } class _MaturityDialogState extends ConsumerState { final _amountController = TextEditingController(); DateTime _maturityDate = DateTime.now(); Wallet? _toWallet; @override void initState() { super.initState(); _amountController.text = widget.transaction.amount.toStringAsFixed(2); } void _pickDate() async { final date = await showDatePicker( context: context, initialDate: _maturityDate, firstDate: DateTime(2000), lastDate: DateTime(2100), builder: (context, child) { return Theme( data: Theme.of(context).copyWith( colorScheme: const ColorScheme.light( primary: Color(0xFF6C63FF), onPrimary: Colors.white, onSurface: Colors.black, ), ), child: child!, ); }, ); if (date != null) { setState(() => _maturityDate = date); } } @override Widget build(BuildContext context) { final walletsState = ref.watch(walletProvider); List validWallets = []; if (walletsState.hasValue) { validWallets = walletsState.value!.where((w) => w.nature == 'CASH' || w.nature == 'SAVINGS').toList(); if (_toWallet == null && validWallets.length == 1) { _toWallet = validWallets.first; } } return Dialog( shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)), elevation: 0, backgroundColor: Colors.transparent, child: GestureDetector( onTap: () => FocusScope.of(context).unfocus(), child: Container( padding: const EdgeInsets.all(24), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(28), boxShadow: [ BoxShadow( color: Colors.black.withValues(alpha: 0.1), blurRadius: 20, offset: const Offset(0, 10), ), ], ), child: SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // Header Row( children: [ Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: const Color(0xFF6C63FF).withValues(alpha: 0.1), shape: BoxShape.circle, ), child: const Icon(LucideIcons.checkCircle, color: Color(0xFF6C63FF)), ), const SizedBox(width: 16), const Expanded( child: Text( 'Close Investment', style: TextStyle( fontSize: 20, fontWeight: FontWeight.bold, color: Color(0xFF1E1E2D), ), ), ), ], ), const SizedBox(height: 24), // Original Investment Details Card Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.grey.shade50, borderRadius: BorderRadius.circular(16), border: Border.all(color: Colors.grey.shade200), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Original Investment', style: TextStyle(fontSize: 12, color: Colors.grey.shade600, fontWeight: FontWeight.w600), ), const SizedBox(height: 8), Text( widget.transaction.description ?? 'Investment', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16), ), const SizedBox(height: 4), Text( 'Rs. ${widget.transaction.amount.toStringAsFixed(2)}', style: const TextStyle(color: Color(0xFF6C63FF), fontWeight: FontWeight.bold), ), ], ), ), const SizedBox(height: 24), // Maturity Amount Field Text( 'Maturity Amount', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Colors.grey.shade700), ), const SizedBox(height: 8), TextField( controller: _amountController, keyboardType: const TextInputType.numberWithOptions(decimal: true, signed: false), textInputAction: TextInputAction.done, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500), decoration: InputDecoration( prefixText: 'Rs. ', prefixStyle: const TextStyle(color: Colors.black87, fontWeight: FontWeight.w500, fontSize: 16), filled: true, fillColor: Colors.grey.shade100, border: OutlineInputBorder( borderRadius: BorderRadius.circular(16), borderSide: BorderSide.none, ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(16), borderSide: const BorderSide(color: Color(0xFF6C63FF), width: 2), ), contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16), ), ), const SizedBox(height: 20), // Date Picker Field Text( 'Closure Date', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Colors.grey.shade700), ), const SizedBox(height: 8), InkWell( onTap: _pickDate, borderRadius: BorderRadius.circular(16), child: Container( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16), decoration: BoxDecoration( color: Colors.grey.shade100, borderRadius: BorderRadius.circular(16), ), child: Row( children: [ Icon(LucideIcons.calendar, color: Colors.grey.shade600, size: 20), const SizedBox(width: 12), Text( DateFormat('dd MMM yyyy').format(_maturityDate), style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500), ), ], ), ), ), const SizedBox(height: 20), // To Account Dropdown Text( 'Deposit To (Savings/Cash)', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Colors.grey.shade700), ), const SizedBox(height: 8), DropdownButtonFormField( decoration: InputDecoration( filled: true, fillColor: Colors.grey.shade100, border: OutlineInputBorder( borderRadius: BorderRadius.circular(16), borderSide: BorderSide.none, ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(16), borderSide: const BorderSide(color: Color(0xFF6C63FF), width: 2), ), contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16), ), icon: Icon(LucideIcons.chevronDown, color: Colors.grey.shade600), value: _toWallet, hint: const Text('Select an account'), items: validWallets.map((w) { return DropdownMenuItem( value: w, child: Text(w.name, style: const TextStyle(fontWeight: FontWeight.w500)), ); }).toList(), onChanged: (val) { setState(() => _toWallet = val); }, ), const SizedBox(height: 32), // Action Buttons Row( children: [ Expanded( child: TextButton( onPressed: () => Navigator.pop(context), style: TextButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 16), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), ), child: Text( 'Cancel', style: TextStyle(color: Colors.grey.shade600, fontWeight: FontWeight.bold, fontSize: 16), ), ), ), const SizedBox(width: 12), Expanded( child: ElevatedButton( onPressed: () async { final amount = double.tryParse(_amountController.text); if (amount == null || amount <= 0) { ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Enter a valid amount'))); return; } if (_toWallet == null) { ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Select a To Account'))); return; } try { await ref.read(transactionProvider.notifier).closeInvestment( widget.transaction.id, amount, _maturityDate, _toWallet!.id ); if (mounted) { Navigator.pop(context); ScaffoldMessenger.of(context).showSnackBar(const SnackBar( content: Text('Closure processed successfully!'), backgroundColor: Colors.green, behavior: SnackBarBehavior.floating, )); } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text('Error: $e'), backgroundColor: Colors.red, behavior: SnackBarBehavior.floating, )); } } }, style: ElevatedButton.styleFrom( backgroundColor: const Color(0xFF6C63FF), foregroundColor: Colors.white, padding: const EdgeInsets.symmetric(vertical: 16), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), elevation: 0, ), child: const Text( 'Confirm', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16), ), ), ), ], ), ], ), ), ), ), ); } }