389 lines
14 KiB
Dart
Executable File
389 lines
14 KiB
Dart
Executable File
import 'dart:convert';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:google_fonts/google_fonts.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:jwt_decode/jwt_decode.dart';
|
|
import 'package:nhancepolicy/responsive.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'config/environment.dart';
|
|
import 'customAppBar/customAppBar.dart';
|
|
import 'customAppBar/toastHelper.dart';
|
|
import 'package:nhancepolicy/logger.dart';
|
|
|
|
class oldPolicy extends StatefulWidget {
|
|
const oldPolicy({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<oldPolicy> createState() => _oldPolicyState();
|
|
}
|
|
|
|
class _oldPolicyState extends State<oldPolicy> {
|
|
dynamic _token;
|
|
dynamic empCodeString;
|
|
dynamic empPrimaryId;
|
|
dynamic client_id;
|
|
dynamic gpaEmpName;
|
|
dynamic oldPolicyList;
|
|
dynamic oldPolicyName;
|
|
dynamic oldPolicyClientId;
|
|
dynamic oldPolicyClientPolicyId;
|
|
dynamic oldPolicySiGstValue;
|
|
dynamic oldPolicySiPremiumValue;
|
|
dynamic oldPolicySiValue;
|
|
dynamic oldPolicyType;
|
|
dynamic oldPolicyEmployeePolicy;
|
|
dynamic oldPolicyDataIsEmpty = 1;
|
|
dynamic oldPolicyTypeHeading;
|
|
dynamic oldPolicyFloaterTextHeading;
|
|
dynamic oldPolicyStartDate;
|
|
dynamic oldPolicyEndDate;
|
|
dynamic empClientBranchId;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadToken();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _loadToken() async {
|
|
logDebug('_loadToken');
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
final String? token = prefs.getString('token');
|
|
if (token != null && token.isNotEmpty) {
|
|
setState(() {
|
|
_token = token;
|
|
});
|
|
// Decode the JWT token received from the API response
|
|
Map<String, dynamic>? decodedToken = Jwt.parseJwt(token);
|
|
logDebug(decodedToken);
|
|
empClientBranchId = prefs.getString('empClientBranchId');
|
|
empCodeString = prefs.getString('empCode');
|
|
logDebug(empCodeString); // Check if emp_code is correct
|
|
empPrimaryId = prefs.getString('empPrimaryId');
|
|
gpaEmpName = prefs.getString('gpaEmpName');
|
|
client_id = prefs.getString('client_id');
|
|
logDebug(client_id);
|
|
getOldPolicyDetails();
|
|
} else {
|
|
// Token is empty or null, handle accordingly (e.g., navigate to login screen)
|
|
// For now, let's navigate to the login screen
|
|
ToastHelper.showErrorToast(context, 'Session Out');
|
|
Navigator.pushReplacementNamed(context, 'phone');
|
|
}
|
|
}
|
|
|
|
Future<void> getOldPolicyDetails() async {
|
|
var url = Uri.parse(Environment.apiUrl +
|
|
'getEmployeeActiveOrInactivePolicy?client_id=$client_id&emp_code=$empCodeString&type=Inactive&client_branch_id=$empClientBranchId');
|
|
try {
|
|
var response = await http.get(
|
|
url,
|
|
headers: {
|
|
'Authorization':
|
|
'Bearer $_token', // Add token to the Authorization header
|
|
},
|
|
);
|
|
if (response.statusCode == 200) {
|
|
Map<String, dynamic> data = json.decode(response.body);
|
|
if (data['status'] == 'success') {
|
|
setState(() {
|
|
oldPolicyList = data['data'];
|
|
logDebug('gmcPolicies');
|
|
});
|
|
// Assuming data is a List
|
|
logDebug(oldPolicyList);
|
|
} else {
|
|
setState(() {
|
|
oldPolicyDataIsEmpty = 0;
|
|
});
|
|
// Handle other status messages if needed
|
|
// ToastHelper.showErrorToast(
|
|
// context, 'API request failed with status: ${data['status']}');
|
|
logDebug('API request failed with status: ${data['status']}');
|
|
}
|
|
} else {
|
|
setState(() {
|
|
oldPolicyDataIsEmpty = 0;
|
|
});
|
|
// Handle other status codes
|
|
// ToastHelper.showErrorToast(
|
|
// context, 'Request failed with status: ${response.statusCode}');
|
|
logDebug('Request failed with status: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
// Handle exceptions
|
|
logDebug('Exception occurred: $e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: CustomAppBar(),
|
|
body: _buildBody(),
|
|
);
|
|
}
|
|
|
|
Widget _buildBody() {
|
|
if (oldPolicyList == null) {
|
|
return Center(child: _buildNoDataWidget());
|
|
} else if (oldPolicyList!.isEmpty) {
|
|
return Center(child: _buildNoDataWidget());
|
|
} else {
|
|
return _buildPolicyListWidget();
|
|
}
|
|
}
|
|
|
|
Widget _buildNoDataWidget() {
|
|
return SingleChildScrollView(
|
|
child: Container(
|
|
color: Color(0xFFEFF3F6),
|
|
child: Column(
|
|
children: [
|
|
Center(
|
|
child: Text('No Data Available'),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildPolicyListWidget() {
|
|
return Stack(
|
|
children: [
|
|
SingleChildScrollView(
|
|
child: Container(
|
|
padding: Responsive.isDesktop(context)
|
|
? EdgeInsets.symmetric(
|
|
horizontal: MediaQuery.of(context).size.width *
|
|
0.2, // 30% of screen width as horizontal padding
|
|
vertical: MediaQuery.of(context).size.height *
|
|
0.03, // 5% of screen height as vertical padding
|
|
)
|
|
: EdgeInsets.all(10),
|
|
color: Color(0xFFEFF3F6),
|
|
child: Column(
|
|
children: [
|
|
if (oldPolicyDataIsEmpty == 1)
|
|
...generateGmcCards(oldPolicyList),
|
|
],
|
|
),
|
|
),
|
|
)
|
|
],
|
|
);
|
|
}
|
|
|
|
List<Widget> generateGmcCards(List<dynamic> data) {
|
|
List<Widget> cards = [];
|
|
|
|
logDebug('data');
|
|
logDebug(data);
|
|
|
|
for (var item in data) {
|
|
setState(() {
|
|
oldPolicyName = item['policy_name'];
|
|
oldPolicyTypeHeading = item['heading'];
|
|
oldPolicyFloaterTextHeading = item['floter_text_heading'];
|
|
oldPolicyClientId = item['client_id'];
|
|
oldPolicyClientPolicyId = item['client_policy_id'];
|
|
oldPolicySiGstValue = item['si_gst_value'];
|
|
oldPolicySiPremiumValue = item['si_premium_value'];
|
|
oldPolicySiValue = item['si_value'];
|
|
oldPolicyType = item['policy_type'];
|
|
oldPolicyStartDate = item['policy_start_date'];
|
|
oldPolicyEndDate = item['policy_end_date'];
|
|
oldPolicyEmployeePolicy = item['EmployeePolicy'];
|
|
});
|
|
|
|
Widget card = Card(
|
|
elevation: 0,
|
|
child: Padding(
|
|
padding: Responsive.isDesktop(context)
|
|
? EdgeInsets.all(20)
|
|
: EdgeInsets.all(10),
|
|
child: Column(
|
|
children: [
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: Color(
|
|
0xFFFFF1DD), // Set background color for the container
|
|
borderRadius: BorderRadius.circular(
|
|
5), // Set border radius for the container
|
|
),
|
|
padding: Responsive.isDesktop(context)
|
|
? EdgeInsets.only(
|
|
top: 15, bottom: 15, left: 25, right: 25)
|
|
: EdgeInsets.only(
|
|
top: 10, bottom: 10, left: 10, right: 10),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Expanded(
|
|
flex: 8,
|
|
child: Container(
|
|
alignment: Alignment.centerLeft,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
oldPolicyName ?? '',
|
|
textAlign: TextAlign.left,
|
|
style: GoogleFonts.poppins(
|
|
fontSize: Responsive.isDesktop(context)
|
|
? 18
|
|
: 14,
|
|
),
|
|
),
|
|
Text(
|
|
oldPolicyTypeHeading ?? '',
|
|
textAlign: TextAlign.left,
|
|
style: GoogleFonts.poppins(
|
|
fontSize: Responsive.isDesktop(context)
|
|
? 20
|
|
: 16,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
],
|
|
))),
|
|
Expanded(
|
|
flex: 4,
|
|
child: Container(
|
|
alignment: Alignment.centerRight,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
Text(
|
|
oldPolicyFloaterTextHeading ?? '',
|
|
textAlign: TextAlign.right,
|
|
style: GoogleFonts.poppins(
|
|
fontSize: Responsive.isDesktop(context)
|
|
? 18
|
|
: 14,
|
|
),
|
|
),
|
|
Text(
|
|
'₹ ${oldPolicySiValue != null ? oldPolicySiValue : 'NA'}',
|
|
textAlign: TextAlign.right,
|
|
style: GoogleFonts.poppins(
|
|
fontSize: Responsive.isDesktop(context)
|
|
? 20
|
|
: 16,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
],
|
|
))),
|
|
],
|
|
),
|
|
),
|
|
SizedBox(height: 15),
|
|
Container(
|
|
padding: Responsive.isDesktop(context)
|
|
? EdgeInsets.only(top: 0, bottom: 0, left: 15, right: 15)
|
|
: EdgeInsets.only(top: 0, bottom: 0, left: 5, right: 5),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: oldPolicyEmployeePolicy.map<Widget>((data) {
|
|
String formattedDate = formatDate(data['dob']);
|
|
return Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Icon(Icons.arrow_right,
|
|
color: Color(0xFFE26728)), // Arrow icon
|
|
SizedBox(
|
|
width: Responsive.isDesktop(context)
|
|
? 10
|
|
: 5), // Space between icon and text
|
|
Expanded(
|
|
child: Text(
|
|
'${data['name']} ~ ${data['relationship']} ~ DOB : $formattedDate',
|
|
style: GoogleFonts.poppins(
|
|
fontSize:
|
|
Responsive.isDesktop(context) ? 18 : 16,
|
|
color: Color(0xFF232526),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}).toList(),
|
|
),
|
|
),
|
|
SizedBox(height: 15),
|
|
Container(
|
|
padding: Responsive.isDesktop(context)
|
|
? EdgeInsets.only(top: 0, bottom: 0, left: 25, right: 25)
|
|
: EdgeInsets.only(top: 0, bottom: 0, left: 10, right: 10),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Expanded(
|
|
flex: 12,
|
|
child: Container(
|
|
alignment: Alignment.centerLeft,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
RichText(
|
|
text: TextSpan(
|
|
text: 'Policy Period : ',
|
|
style: GoogleFonts.poppins(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w500,
|
|
color: Color(0xFF232526),
|
|
),
|
|
children: <TextSpan>[
|
|
TextSpan(
|
|
text:
|
|
'$oldPolicyStartDate To $oldPolicyEndDate',
|
|
style: GoogleFonts.poppins(
|
|
fontSize: 14,
|
|
color: Color(0xFF232526),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
));
|
|
|
|
cards.add(card);
|
|
cards.add(SizedBox(height: 10));
|
|
}
|
|
|
|
return cards;
|
|
}
|
|
|
|
String formatDate(String inputDate) {
|
|
// Parse the input date string
|
|
DateTime dateTime = DateTime.parse(inputDate);
|
|
|
|
// Format the date to 'dd MMMM yyyy'
|
|
String formattedDate = DateFormat('dd MMMM yyyy').format(dateTime);
|
|
|
|
return formattedDate;
|
|
}
|
|
}
|