import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import '../../../config/environment.dart'; import '../../../customAppBar/toastHelper.dart'; import '../../service/TokenService.dart'; import 'package:nhance_app_pwa/logger.dart'; class ApiService { final BuildContext context; String? _token; bool _isSessionOutToastShown = false; // Flag to track toast message ApiService(this.context) { _initializeToken(); } Future _initializeToken() async { _token = await TokenService.getPreToken(); } Future> getClientLogoAndDetailsToApi(empClientId,empClientBranchId, String clientId, String branchID) async { logDebug(_token); if (_token == null) { await _initializeToken(); } // final SharedPreferences prefs = await SharedPreferences.getInstance(); // final empClientId = await prefs.getString('empClientId'); // final empClientBranchId = await prefs.getString('empClientBranchId'); final url = Uri.parse( '${Environment.apiUrlEnrollment}getClientDetails?post_client_id=$empClientId&post_branch_id=$empClientBranchId&pre_client_id=$clientId&pre_branch_id=$branchID'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getSelfEmployeeProfileToApi( String clientId, String empCode, String branchID) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}getEmployeeProfile?emp_code=$empCode&client_id=$clientId&client_branch_id=$branchID'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> fetchRelationshipListToApi() async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrlEnrollment}relationshipList'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getGpaEmpPolicyDetailsToApi(String empPrimaryId, String empCode, String clientId, String policy, String branchID) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}getEmployeePolicy?id=$empPrimaryId&emp_code=$empCode&client_id=$clientId&policy=$policy&client_branch_id=$branchID'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getGmcEmpPolicyDetailsToApi(String empPrimaryId, String empCode, String clientId, String policy, String branchID) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}getEmployeePolicy?id=$empPrimaryId&emp_code=$empCode&client_id=$clientId&policy=$policy&client_branch_id=$branchID'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getGmcSiTopUpToApi( String client_id, String emp_code, String policy, String branchID) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}getAddOnPolicy?client_id=$client_id&emp_code=$emp_code&policy=$policy&client_branch_id=$branchID'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getGmcSiParentTopUpToApi( String client_id, String emp_code, String policy, String branchID) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}getAddOnPolicy?client_id=$client_id&emp_code=$emp_code&policy=$policy&client_branch_id=$branchID'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getGmcDependentAddOnsToApi( String client_id, String emp_code, String policy, String branchID) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}getAddOnPolicy?client_id=$client_id&emp_code=$emp_code&policy=$policy&client_branch_id=$branchID'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> removeAddonsGmcDependentToAPI( String empCodeString, String addOnsDependentClientPolicyId) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}removeEmpAndEmpPolicyData?emp_code=$empCodeString&client_policy_id=$addOnsDependentClientPolicyId'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> removeAddonsGmcSiToAPI( String empCodeString, String topUpClientPolicyId) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}removeEmpAndEmpPolicyData?emp_code=$empCodeString&client_policy_id=$topUpClientPolicyId'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> removeAddonsGmcParentSiToAPI( String empCodeString, String topUpParentClientPolicyId) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}removeEmpAndEmpPolicyData?emp_code=$empCodeString&client_policy_id=$topUpParentClientPolicyId'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> deleteItemToApi(id,copyStatus,gmcClientPolicyId) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrlEnrollment}deleteDependence?id=$id&is_from_copy=$copyStatus&client_policy_id=$gmcClientPolicyId'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> copyActivePolicy(enrollmentClient_id,enrollmentEmpCodeString,newClientPolicy) async { logDebug(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrlEnrollment}copyActiveEmployeeAndDependentDetails?client_id=$enrollmentClient_id&emp_code=$enrollmentEmpCodeString&new_client_policy_id=$newClientPolicy'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> saveFamilyMemberDetailsToApi( List> formDataList) async { logDebug('saveFamilyMemberDetailsToApi API SERVICE'); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrlEnrollment}addEmployeeAndDependence'); final headers = { 'Authorization': 'Bearer $_token' ?? '', 'Content-Type': 'application/json', // Ensure content type is JSON }; // Convert formDataList to JSON string final formDataJson = jsonEncode(formDataList); // Send formDataJson as the body final response = await _makePostRequest(url, formDataJson, headers); return response; } Future> saveAddOnsDetailsToApi( List> formDataList) async { logDebug('saveAddOnsDetailsToApi API SERVICE'); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrlEnrollment}addEmployeeAndDependence'); final headers = { 'Authorization': 'Bearer $_token' ?? '', 'Content-Type': 'application/json', // Ensure content type is JSON }; // Convert formDataList to JSON string final formDataJson = jsonEncode(formDataList); // Send formDataJson as the body final response = await _makePostRequest(url, formDataJson, headers); return response; } Future> sendAddonsGmcDependentToAPI( List> formDataList) async { logDebug('saveAddOnsDetailsToApi API SERVICE'); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}createOrUpdateEmployeePolicySiAmount'); final headers = { 'Authorization': 'Bearer $_token' ?? '', 'Content-Type': 'application/json', // Ensure content type is JSON }; // Convert formDataList to JSON string final formDataJson = jsonEncode(formDataList); // Send formDataJson as the body final response = await _makePostRequest(url, formDataJson, headers); return response; } Future> sendAddonsGmcSiToAPI( List> formDataList) async { logDebug('saveAddOnsDetailsToApi API SERVICE'); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}createOrUpdateEmployeePolicySiAmount'); final headers = { 'Authorization': 'Bearer $_token' ?? '', 'Content-Type': 'application/json', // Ensure content type is JSON }; // Convert formDataList to JSON string final formDataJson = jsonEncode(formDataList); // Send formDataJson as the body final response = await _makePostRequest(url, formDataJson, headers); return response; } Future> sendAddonsGmcParentSiToAPI( List> formDataList) async { logDebug('saveAddOnsDetailsToApi API SERVICE'); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrlEnrollment}createOrUpdateEmployeePolicySiAmount'); final headers = { 'Authorization': 'Bearer $_token' ?? '', 'Content-Type': 'application/json', // Ensure content type is JSON }; // Convert formDataList to JSON string final formDataJson = jsonEncode(formDataList); // Send formDataJson as the body final response = await _makePostRequest(url, formDataJson, headers); return response; } Future> sendAddOnToAPI( Map formData) async { logDebug('sendAddOnToAPI API SERVICE'); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrlEnrollment}iAgreeForAddOn'); final headers = { 'Authorization': 'Bearer $_token' ?? '', 'Content-Type': 'application/json', // Ensure content type is JSON }; // Convert formDataList to JSON string final formDataJson = jsonEncode(formData); // Send formDataJson as the body final response = await _makePostRequest(url, formDataJson, headers); return response; } Future> topUpSiCalculationToAPI(formDataList) async { logDebug('saveAddOnsDetailsToApi API SERVICE'); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrlEnrollment}calculatePremium'); final headers = { 'Authorization': 'Bearer $_token' ?? '', 'Content-Type': 'application/json', // Ensure content type is JSON }; // Convert formDataList to JSON string final formDataJson = jsonEncode(formDataList); // Send formDataJson as the body final response = await _makePostRequest(url, formDataJson, headers); return response; } Future> topUpParentSiCalculationToAPI( formDataList) async { logDebug('saveAddOnsDetailsToApi API SERVICE'); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrlEnrollment}calculatePremium'); final headers = { 'Authorization': 'Bearer $_token' ?? '', 'Content-Type': 'application/json', // Ensure content type is JSON }; // Convert formDataList to JSON string final formDataJson = jsonEncode(formDataList); // Send formDataJson as the body final response = await _makePostRequest(url, formDataJson, headers); return response; } Future> addOnsDependentCalculationToAPI( formDataList) async { logDebug('saveAddOnsDetailsToApi API SERVICE'); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrlEnrollment}calculatePremium'); final headers = { 'Authorization': 'Bearer $_token' ?? '', 'Content-Type': 'application/json', // Ensure content type is JSON }; // Convert formDataList to JSON string final formDataJson = jsonEncode(formDataList); // Send formDataJson as the body final response = await _makePostRequest(url, formDataJson, headers); return response; } //HR API STARTS // Future> getCashDepositDetailsToApi( // String clintID, String empRefId) async { // logDebug(_hrtoken); // if (_hrtoken == null) { // await _initializeToken(); // } // final url = Uri.parse( // '${Environment.apiUrlEnrollment}getCashDepositData?client_id=$clintID&client_branch_id=$empRefId'); // final headers = { // 'Authorization': 'Bearer $_hrtoken' ?? '', // }; // final response = await _makeGetRequest(url, headers); // return response; // } // // Future> getEmployeeAndDependenceToApi( // String clintID, String getPolicyNo, String empRefId) async { // logDebug(_hrtoken); // if (_hrtoken == null) { // await _initializeToken(); // } // final url = Uri.parse( // '${Environment.apiUrlEnrollment}getEmployeeAndDependenceByClientId?client_id=$clintID&client_policy_id=$getPolicyNo&client_branch_id=$empRefId'); // final headers = { // 'Authorization': 'Bearer $_hrtoken' ?? '', // }; // final response = await _makeGetRequest(url, headers); // return response; // } // Future> getTrackClaimsList( // String empMobileNo, String empCode) async { // final url = Uri.parse( // '${Environment.apiUrlTicket}/ticket/get_ticket_data?emp_code=$empCode&mobile_number=$empMobileNo'); // final response = await _makeGetRequest(url, { // 'Token': Environment.ticketToken, // }); // return response; // } // // Future> fetchDepartmentList() async { // final url = Uri.parse('${Environment.apiUrlTicket}/departments'); // final response = await _makeGetRequest(url, { // 'Token': Environment.ticketToken, // }); // return response; // } // // Future> sendFormDataToApi( // Map formData) async { // final url = Uri.parse('${Environment.apiUrlTicket}/tickets/create'); // // Convert formData to Map // Map stringFormData = // formData.map((key, value) => MapEntry(key, value.toString())); // final response = await _makePostRequest(url, stringFormData, { // 'Content-Type': 'application/x-www-form-urlencoded', // 'Token': Environment.ticketToken, // }); // return response; // } // // Future> sendClaimsMessageToApi( // Map formData) async { // final url = Uri.parse('${Environment.apiUrlTicket}/messages/create'); // // Convert formData to Map // Map stringFormData = // formData.map((key, value) => MapEntry(key, value.toString())); // final response = await _makePostRequest(url, stringFormData, { // 'Content-Type': 'application/x-www-form-urlencoded', // 'Token': Environment.ticketToken, // }); // return response; // } Future> _makeGetRequest( Uri url, Map headers) async { // Add global APP_SIGNATURE header headers['APP-SIGNATURE'] = 'nhance-2025-signature-T8f6gV9k5fK2wf5V5c4vId3b0J8z6cAm2x8Y'; final response = await http.get(url, headers: headers); return _handleResponse(response); } Future> _makePostRequest( Uri url, String body, Map headers) async { // Add global APP_SIGNATURE header headers['APP-SIGNATURE'] = 'nhance-2025-signature-T8f6gV9k5fK2wf5V5c4vId3b0J8z6cAm2x8Y'; final response = await http.post(url, headers: headers, body: body); return _handleResponse(response); } Future> _handleResponse(http.Response response) async { if (response.statusCode == 200) { return jsonDecode(response.body); } else if (response.statusCode == 401) { await _clearLocalStorageAndRedirect(); return {}; } else if (response.statusCode == 403) { await _clearLocalStorageAndRedirect(); return {}; } else if (response.statusCode == 451) { final body = jsonDecode(response.body); final message = body['message']; ToastHelper.showWarningToast(context, message); return {}; } else if (response.statusCode == 429) { final body = jsonDecode(response.body); final message = body['message']; ToastHelper.showWarningToast(context, message); return {}; } else { throw Exception('Failed to load data'); } } Future _clearLocalStorageAndRedirect() async { if (!context.mounted) return; await TokenService().logout(context); } }