import 'dart:convert'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:frontend/utils/auth_utils.dart'; import 'package:go_router/go_router.dart'; import 'package:http/http.dart' as http; import 'package:universal_html/html.dart' as html; import 'package:universal_html/js.dart'; import '../../config/apiUrl.dart'; class ApiService { Future> fetchCountryList() async { final String apiUrldata = '$apiUrl/api/getcountryMaster'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print("Country - $data"); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List"); } return data['data']; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load country list'); } } Future> fetchUsers() async { String? ordId = await getOrgId(); final String apiUrlData = '$apiUrl/api/users?org_id=$ordId'; final String? token = await getToken(); print("Fetch Users"); print("TOEKRWE: $token"); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrlData), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { final data = json.decode(response.body); return data['data']; // Returning raw JSON list } else { throw Exception('Failed to load users'); } } Future fetchCostCenter() async { final String apiUrldata = '$apiUrl/api/getCostCenterMaster'; final token = await getToken(); final userId = await getUserId(); // print("SUSRTRT- $userId"); // if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print(data); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List"); } List plansJson = data['data']; // 'data' is a Map, not a List // setState(() { // apiCostData = plansJson; // Store API response in state // if(apiCostData!.isNotEmpty){ // selectedCostCenterId =apiCostData?.first['department_id']; // } // if (apiCostData != null && apiCostData!.isNotEmpty) { // selectedCostCenterId ??= apiCostData!.first['department_id']?.toString(); // } // }); print('plansJSON'); return plansJson; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load plans'); } } Future> fetchMasterDropdown() async { final String apiUrldata = '$apiUrl/api/getDropdownMaster'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print(data); if (!data.containsKey('data') || data['data'] is! Map) { throw Exception( "Invalid response format: 'data' field is missing or not a Map"); } Map plansJson = data['data']; // 'data' is a Map, not a List return plansJson; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load plans'); } } Future> fetchAllServices() async { final String apiUrldata = '$apiUrl/api/service'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print(data); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List"); } return data['data']; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load plans'); } } Future> fetchAllGroup() async { String? orgId = await getOrgId(); final String apiUrldata = '$apiUrl/api/groups?org_id=$orgId'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print(data); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List"); } return data['data']; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load plans'); } } Future> fetchAllPolicy() async { String? orgId = await getOrgId(); final String apiUrldata = '$apiUrl/api/policy?org_id=$orgId'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print(data); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List"); } return data['data']; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load plans'); } } Future> getSinglePolicy(int policyId) async { final String apiUrldata = '$apiUrl/api/policy/find/${policyId}'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print(data); if (!data.containsKey('data') || data['data'] is! Map) { throw Exception( "Invalid response format: 'data' field is missing or not a Map"); } Map plansJson = data['data']; // 'data' is a Map, not a List return plansJson; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load plans'); } } Future> fetchOrganization() async { String? orgId = await getOrgId(); // final String apiUrldata = '$apiUrl/api/organizations'; final String apiUrldata = '$apiUrl/api/organizations/find/$orgId'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print(data); if (!data.containsKey('data') || data['data'] is! Map) { throw Exception( "Invalid response format: 'data' field is missing or not a Map"); } // Make sure each item is a Map // final List> orgList = // List>.from(data['data']); // return orgList; return Map.from(data['data']); } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load organizations'); } } static Future> getViewPlan(String planId) async { final String apiUrldata = '$apiUrl/api/plans/find/$planId'; print("API URL: $apiUrldata"); // final token = await getToken(); final token = await getToken(); final response = await http.put( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', // Add token here 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { final Map? resData = json.decode(response.body); return resData?["data"]; } else { throw Exception('Failed to load plans'); } } static Future viewPlan(BuildContext context, String planId, {bool isViewMode = false}) async { try { Map planData = await getViewPlan(planId); print("ViewAAA - $planData"); context.go('/createPlan', extra: {'planData': planData, 'isViewMode': isViewMode}); } catch (e) { print("Error fetching plan: $e"); } } static Future viewPlanForApprover(BuildContext context, String planId, {bool isViewMode = false, bool isApprover = true}) async { try { Map planData = await getViewPlan(planId); print("ViewAAA - $planData"); context.replace('/createPlan', extra: { 'planData': planData, 'isViewMode': isViewMode, 'isApprover': isApprover }); } catch (e) { print("Error fetching plan: $e"); } } Future> fetchUserApprovalList() async { String? orgId = await getOrgId(); String? userId = await getUserId(); // final String apiUrldata = '$apiUrl/api/organizations'; final String apiUrldata = '$apiUrl/api/plans/findApprovalList?user_id=$userId&org_id=$orgId'; // '$apiUrl/api/findApprovalList?user_id=$userId&org_id=$orgId'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print(data); if (!data.containsKey('data') || data['data'] is! Map) { throw Exception( "Invalid response format: 'data' field is missing or not a Map"); } // Make sure each item is a Map // final List> orgList = // List>.from(data['data']); // return orgList; return Map.from(data['data']); } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load organizations'); } } // Flight From - To Future> fetchFlightsCountryList() async { final String apiUrldata = '$apiUrl/api/getAirportCodeMaster?limit=1000&offset=0'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print("Country - $data"); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List"); } return data['data']; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load country list'); } } Future> fetchTrainCountryList() async { final String apiUrldata = '$apiUrl/api/getTrainCodeMaster'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); print("Country - $data"); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List"); } return data['data']; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load country list'); } } Future getPdfDownload(planId) async { final String apiUrldata = '$apiUrl/api/plans/download?plan_id=$planId'; // final String apiUrldata = '$apiUrl/auth/googlelogin'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { print("PDf Dowloaded"); // Create a blob from the response body final blob = html.Blob([response.bodyBytes]); // Generate a download URL for the blob final url = html.Url.createObjectUrlFromBlob(blob); // Create a link element to trigger the download final anchor = html.AnchorElement(href: url) ..setAttribute('download', 'trip_plan_$planId.pdf') ..click(); // Revoke the download URL to free up resources html.Url.revokeObjectUrl(url); } catch (e) { throw Exception('Error parsing response: $e'); } } else if (response.statusCode == 404) { // showDialog( // context: context, // builder: (BuildContext context) { // return AlertDialog( // title: Text('File not found.'), // // content: Text('File not found.'), // actions: [ // TextButton( // child: Text('OK'), // onPressed: () { // Navigator.of(context).pop(); // Close the dialog // }, // ), // ], // ); // }, // ); } else { throw Exception('Failed to load plans'); } } }