import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:nhance_app_pwa/customAppBar/toastHelper.dart'; import 'package:nhance_app_pwa/models/environment.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'dart:convert'; class ApiService { final BuildContext context; String? _token; ApiService(this.context) { _initializeToken(); } Future _initializeToken() async { final prefs = await SharedPreferences.getInstance(); _token = prefs.getString('_postToken') ?? ''; } Future> getActiveAndInactivePolicyDetails( String clientId, String empCode, String status, String branchID, String mobileNo) async { print(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrl}getEmployeeActiveOrInactivePolicy?client_id=$clientId&emp_code=$empCode&type=$status&client_branch_id=$branchID&mobile_no=$mobileNo'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getClaimsHistoryToApi( String ticket_type_id) async { print("getCashDepositDetailsToApi1"); final url = Uri.parse('${Environment.apiUrl}claimView?ticket_id=$ticket_type_id'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getAdvertisementImageToApi() async { print(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrl}getAdvertisementImage'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getCashlessAndReimbursementToApi() async { print(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrl}getFEContent'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getSelfEmployeeProfileDetails( String clientId, String empCode, String branchID) async { print(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrl}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> getTrackClaimsList(String empPrimaryId) async { print(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse( '${Environment.apiUrl}/get_ticket_data?emp_id=$empPrimaryId'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getTicketListByMobile(String empMobileNo) async { print(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrl}ticketList?mobile=$empMobileNo'); final url1 = Uri.parse('${Environment.apiUrl}ticketList?mobile=1234567890'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getTicketConverList(String thz_id) async { print(_token); if (_token == null) { await _initializeToken(); } final url1 = Uri.parse('${Environment.apiUrl}ticketList?mobile=$thz_id'); final url = Uri.parse('${Environment.apiUrl}ticketConversationList?thz_id=$thz_id'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> getWellnessLink() async { print(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrl}getWellnessURL'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> fetchDepartmentList(String clientId, String empCode) async { print(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrl}/get_ticket_type?client_id=$clientId&emp_code=$empCode'); final headers = { 'Authorization': 'Bearer $_token' ?? '', }; final response = await _makeGetRequest(url, headers); return response; } Future> sendTicketFormDataToApi( Map formData) async { print(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrl}ticketSave'); // 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', 'Authorization': 'Bearer $_token' ?? '', }); return response; } Future> sendClaimsFormDataToApi( Map formData) async { print(_token); if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrl}/initiateClaim'); // 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', 'Authorization': 'Bearer $_token' ?? '', }); return response; } Future> sendClaimsMessageToApi( Map formData) async { print("sendClaimsMessageToApi"); final url = Uri.parse('${Environment.apiUrl}ticketConversationSave'); // final url = Uri.parse('${Environment.apiUrlTicket}/messages/create'); // Convert formData to Map print("sendClaimsMessageToApi1"); Map stringFormData = formData.map((key, value) => MapEntry(key, value.toString())); final response = await _makePostRequest(url, stringFormData, { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Bearer $_token' ?? '', }); return response; } Future _clearLocalStorageAndRedirect() async { final prefs = await SharedPreferences.getInstance(); await prefs.clear(); // Assuming you have access to the context ToastHelper.showErrorToast(context, 'Session Out'); Navigator.pushNamed(context, 'login'); } Future> getBotDetails( Map formData) async { if (_token == null) { await _initializeToken(); } final url = Uri.parse('${Environment.apiUrl}getChatResponse'); // Convert formData to Map Map stringFormData = formData.map((key, value) => MapEntry(key, value.toString())); print(url); final response = await _makePostRequest(url, stringFormData, { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Bearer $_token' ?? '', }); return response; } // Future> getBotDetails(String requestFor, is_option, // option, returnMessage, mobileNo, policyID) async { // print('requestFor: $requestFor'); // print('is_option: $is_option'); // print('option: $option'); // print('returnMessage: $returnMessage'); // print('mobileNo: $mobileNo'); // print('policyID: $policyID'); // if (_token == null) { // await _initializeToken(); // } // final url; // if (is_option != '0') { // url = Uri.parse( // '${Environment.apiUrl}getChatResponse?request_for=$requestFor&is_option=$is_option&option=$option&mobile_no=$mobileNo&policy_id=$policyID'); // } else { // url = Uri.parse( // '${Environment.apiUrl}getChatResponse?request_for=$requestFor&is_option=$is_option&text=$returnMessage&value=$option&mobile_no=$mobileNo&policy_id=$policyID'); // } // print(url); // // final headers = { // 'Authorization': 'Bearer $_token' ?? '', // }; // final response = await _makeGetRequest(url, headers); // return response; // } Future> _makeGetRequest( Uri url, Map headers) async { final response = await http.get(url, headers: headers); return _handleResponse(response); } Future> _makePostRequest( Uri url, Map body, Map headers) async { 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 { throw Exception('Failed to load data'); } } }