import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:kmobile/api/services/cheque_service.dart'; import 'package:kmobile/data/models/user.dart'; import 'package:kmobile/di/injection.dart'; class StopSingleChequeScreen extends StatefulWidget { final User selectedAccount; final String date; final String instrType; final String fromCheque; final String toCheque; const StopSingleChequeScreen( {super.key, required this.selectedAccount, required this.date, required this.instrType, required this.fromCheque, required this.toCheque}); @override State createState() => _StopSingleChequeScreenState(); } class _StopSingleChequeScreenState extends State { final _formKey = GlobalKey(); final _stopFromChequeNoController = TextEditingController(); final _stopIssueDateController = TextEditingController(); final _stopExpiryDateController = TextEditingController(); final _stopAmountController = TextEditingController(); final _stopCommentController = TextEditingController(); final _chequeService = getIt(); String _formatDate(String dateString) { if (dateString.length != 8) { return dateString; // Return as is if not in expected ddmmyyyy format } try { final day = dateString.substring(0, 2); final month = dateString.substring(2, 4); final year = dateString.substring(4, 8); return '$day/$month/$year'; } catch (e) { return dateString; // Return original string on error } } Future _showResponseDialog(String title, String message) async { return showDialog( context: context, barrierDismissible: false, // user must tap button! builder: (BuildContext context) { return AlertDialog( title: Text(title), content: SingleChildScrollView( child: ListBody( children: [ Text(message), ], ), ), actions: [ TextButton( child: const Text('Close'), onPressed: () { Navigator.of(context).pop(); }, ), ], ); }, ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Stop Single Cheque'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: Form( key: _formKey, child: ListView( children: [ Card( elevation: 0, margin: const EdgeInsets.symmetric(vertical: 8.0), child: ListTile( leading: Image.asset( 'assets/images/logo.png', width: 40, height: 40, ), title: Text(widget.selectedAccount.accountNo!), subtitle: const Text("Account Number"), ), ), const SizedBox(height: 24), TextFormField( controller: _stopFromChequeNoController, decoration: const InputDecoration( labelText: 'Cheque Number *', border: OutlineInputBorder(), ), keyboardType: TextInputType.number, validator: (value) { if (value == null || value.isEmpty) { return 'Please enter a cheque number'; } final chequeNumber = int.tryParse(value); final fromCheque = int.tryParse(widget.fromCheque); final toCheque = int.tryParse(widget.toCheque); if (chequeNumber == null || fromCheque == null || toCheque == null) { return 'Invalid cheque number format'; } if (chequeNumber < fromCheque || chequeNumber > toCheque) { return 'Cheque number must be between ${widget.fromCheque} and ${widget.toCheque}'; } return null; }, ), const SizedBox(height: 16), TextFormField( initialValue: widget.instrType, readOnly: true, decoration: const InputDecoration( labelText: 'Instrument Type *', border: OutlineInputBorder(), ), ), const SizedBox(height: 16), TextFormField( controller: _stopIssueDateController, decoration: const InputDecoration( labelText: 'Stop Issue Date', border: OutlineInputBorder(), ), keyboardType: TextInputType.datetime, ), const SizedBox(height: 16), TextFormField( controller: _stopExpiryDateController, decoration: const InputDecoration( labelText: 'Stop Expiry Date', border: OutlineInputBorder(), ), keyboardType: TextInputType.datetime, ), const SizedBox(height: 16), TextFormField( controller: _stopAmountController, decoration: const InputDecoration( labelText: 'Stop Amount', border: OutlineInputBorder(), ), keyboardType: TextInputType.number, ), const SizedBox(height: 16), TextFormField( controller: _stopCommentController, decoration: const InputDecoration( labelText: 'Stop Comment', border: OutlineInputBorder(), ), ), const SizedBox(height: 16), TextFormField( initialValue: _formatDate(widget.date), readOnly: true, decoration: const InputDecoration( labelText: 'Chequebook Issue Date', border: OutlineInputBorder(), ), ), const SizedBox(height: 32), ElevatedButton( onPressed: () async { if (_formKey.currentState!.validate()) { try { final response = await _chequeService.stopCheque( accountno: widget.selectedAccount.accountNo!, stopFromChequeNo: _stopFromChequeNoController.text, instrType: widget.instrType, stopToChequeNo: _stopFromChequeNoController.text, stopIssueDate: _stopIssueDateController.text, stopExpiryDate: _stopExpiryDateController.text, stopAmount: _stopAmountController.text, stopComment: _stopCommentController.text, chequeIssueDate: widget.date, ); if (!mounted) return; final decodedResponse = jsonDecode(response); final status = decodedResponse['status']; final message = decodedResponse['message']; if (status == 'SUCCESS') { _showResponseDialog('Success', message); } else { _showResponseDialog('Error', message); } } catch (e) { _showResponseDialog('Error', e.toString()); } } }, child: const Text('Stop Cheque'), ), ], ), ), ), ); } }