import 'package:flutter/material.dart'; import 'package:kmobile/api/services/yojna_service.dart'; import 'package:kmobile/data/models/user.dart'; import 'package:kmobile/di/injection.dart'; import 'package:kmobile/features/yojna/screens/pmjjby_screen.dart'; import 'package:kmobile/features/yojna/screens/pmsby_screen.dart'; import 'package:kmobile/l10n/app_localizations.dart'; class PMMainScreen extends StatefulWidget { final List users; final int selectedIndex; const PMMainScreen({ super.key, required this.users, required this.selectedIndex, }); @override State createState() => _PMMainScreenState(); } class _PMMainScreenState extends State { User? _selectedAccount; List _filteredUsers = []; String? _selectedScheme; final List _schemes = [ 'Pradhan Mantri Jeevan Jyoti Bima Yojana (PMJJBY)', 'Pradhan Mantri Suraksha Bima Yojana (PMSBY)', ]; @override void initState() { super.initState(); _filteredUsers = widget.users .where((user) => ['SA', 'SB', 'CA', 'CC'].contains(user.accountType)) .toList(); // Pre-fill the account number if possible if (widget.users.isNotEmpty && widget.selectedIndex < widget.users.length) { if (_filteredUsers.isNotEmpty) { if (_filteredUsers.contains(widget.users[widget.selectedIndex])) { _selectedAccount = widget.users[widget.selectedIndex]; } else { _selectedAccount = _filteredUsers.first; } } else { _selectedAccount = widget.users[widget.selectedIndex]; } } else { if (_filteredUsers.isNotEmpty) { _selectedAccount = _filteredUsers.first; } } } @override void dispose() { super.dispose(); } Future _handleCreate() async { if (_selectedAccount == null) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Please select account number')), ); return; } if (_selectedScheme == null) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Please select a scheme first')), ); return; } final String schemeCode = _selectedScheme!.contains('PMJJBY') ? '02' : '01'; // Show loading ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Row( children: [ CircularProgressIndicator(), SizedBox(width: 16), Text('Fetching details...'), ], ), duration: Duration(seconds: 2), ), ); try { final response = await getIt().fetchpmydetails( scheme: schemeCode, action: 'C', accountno: _selectedAccount!.accountNo!, ); if (mounted) { Map? data; if (response is Map) { data = response; } else if (response is List && response.isNotEmpty && response[0] is Map) { data = response[0] as Map; } if (data != null && data.isNotEmpty) { Navigator.push( context, MaterialPageRoute( builder: (context) { if (_selectedScheme!.contains('PMJJBY')) { return PMJJBYScreen(initialData: data); } else { return PMSBYScreen(initialData: data); } }, ), ); } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Failed to fetch details or no data found.')), ); } } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Error: ${e.toString()}')), ); } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Pradhan Mantri Yojana'), centerTitle: false, ), body: SingleChildScrollView( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Card( elevation: 2, child: Padding( padding: const EdgeInsets.all(16.0), child: Text( "Create and Enquire about your Pradhan Mantri Jeevan Jyoti Bima Yojana (PMJJBY) and Pradhan Mantri Suraksha Bima Yojana(PMSBY) schemes.", style: Theme.of(context).textTheme.titleMedium, ), ), ), const SizedBox(height: 16), Card( elevation: 2, child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ DropdownButtonFormField( value: _selectedAccount, decoration: InputDecoration( labelText: AppLocalizations.of(context).accountNumber, border: const OutlineInputBorder(), contentPadding: const EdgeInsets.symmetric( vertical: 20, horizontal: 12), ), items: _filteredUsers.map((user) { return DropdownMenuItem( value: user, child: Text(user.accountNo.toString()), ); }).toList(), onChanged: (User? newUser) { setState(() { _selectedAccount = newUser; }); }, validator: (value) { if (value == null) { return AppLocalizations.of(context) .accountNumberRequired; } return null; }, ), const SizedBox(height: 16), DropdownButtonFormField( value: _selectedScheme, isExpanded: true, isDense: false, itemHeight: null, decoration: const InputDecoration( labelText: 'Select Scheme', border: OutlineInputBorder(), contentPadding: EdgeInsets.symmetric(vertical: 12, horizontal: 12), ), selectedItemBuilder: (BuildContext context) { return _schemes.map((String scheme) { return Container( alignment: Alignment.centerLeft, child: Text( scheme, style: const TextStyle(fontSize: 14), softWrap: true, maxLines: 2, overflow: TextOverflow.visible, ), ); }).toList(); }, items: _schemes.map((String scheme) { return DropdownMenuItem( value: scheme, child: Padding( padding: const EdgeInsets.symmetric(vertical: 12.0), child: Text( scheme, style: const TextStyle(fontSize: 15), softWrap: true, ), ), ); }).toList(), onChanged: (String? newValue) { setState(() { _selectedScheme = newValue; }); }, ), ], ), ), ), const SizedBox(height: 24), Row( children: [ Expanded( child: ElevatedButton( onPressed: _handleCreate, style: ElevatedButton.styleFrom( backgroundColor: Theme.of(context).colorScheme.primaryContainer, foregroundColor: Theme.of(context).colorScheme.onPrimaryContainer, padding: const EdgeInsets.symmetric(vertical: 16), elevation: 4, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), ), child: const Text( "Create", style: TextStyle(fontWeight: FontWeight.bold), ), ), ), const SizedBox(width: 16), Expanded( child: ElevatedButton( onPressed: () { // Action for Enquiry button }, style: ElevatedButton.styleFrom( backgroundColor: Theme.of(context).colorScheme.primaryContainer, foregroundColor: Theme.of(context).colorScheme.onPrimaryContainer, padding: const EdgeInsets.symmetric(vertical: 16), elevation: 4, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), ), child: Text( AppLocalizations.of(context).enquiry, style: const TextStyle(fontWeight: FontWeight.bold), ), ), ), ], ), ], ), ), ); } }