import 'package:flutter/material.dart'; import 'package:kmobile/api/services/yojna_service.dart'; import 'package:kmobile/di/injection.dart'; import 'package:kmobile/features/yojna/screens/pmjjby_screen.dart'; import 'package:kmobile/l10n/app_localizations.dart'; class PMMainScreen extends StatefulWidget { const PMMainScreen({super.key}); @override State createState() => _PMMainScreenState(); } class _PMMainScreenState extends State { final TextEditingController _accountController = TextEditingController(); String? _selectedScheme; final List _schemes = [ 'Pradhan Mantri Jeevan Jyoti Bima Yojana (PMJJBY)', 'Pradhan Mantri Suraksha Bima Yojana (PMSBY)', ]; @override void dispose() { _accountController.dispose(); super.dispose(); } Future _handleCreate() async { if (_accountController.text.isEmpty) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Please enter 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: _accountController.text, ); 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) => PMJJBYScreen(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: [ TextField( controller: _accountController, decoration: const InputDecoration( labelText: 'Account Number', border: OutlineInputBorder(), contentPadding: EdgeInsets.symmetric(vertical: 20, horizontal: 12), ), keyboardType: TextInputType.number, ), 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), ), ), ), ], ), ], ), ), ); } }