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:google_fonts/google_fonts.dart'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; import 'package:universal_html/html.dart' as html; import 'package:universal_html/js.dart'; import '../../config/apiUrl.dart'; import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; class ApiService { Future getOrganizationData() async { try { print("ORG getUpdatedServices"); final result = await fetchOrganization(); print("UUPdatedServices - $result"); // ✅ Save to local storage final prefs = await SharedPreferences.getInstance(); final jsonString = jsonEncode(result); await prefs.setString('org_data', jsonString); print("✅ Organization data saved to SharedPreferences."); print("selectedOrg - $result"); } catch (e) { print('Error fetching updatedServices list: $e'); } } 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> fetchHotelsList() async { final String apiUrldata = '$apiUrl/api/getHotels'; 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("Hotelss - $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> fetchAirlineList() async { final String apiUrldata = '$apiUrl/api/getAirlineMaster'; 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("Airline - $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 fetchDepartmentCostCenter() async { final String apiUrldata = '$apiUrl/api/getDepartmentList'; 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?for=table_view&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> fetchFindGroup() async { String? orgId = await getOrgId(); final String apiUrldata = '$apiUrl/api/groups/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! 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?for=table_view&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/cancle_plan?plan_id=$planId'; // final String apiUrldata = '$apiUrl/api/plans/find/$planId'; print("API URL: $apiUrldata"); // final token = await getToken(); final token = await getToken(); final response = await http.get( 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> getViewPlanEdit(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, bool isMyTrips = false, }) async { try { Map planData = await getViewPlanEdit(planId); print("ViewAAA - $planData"); context.go( isMyTrips ? '/createPlan' : '/allTrips/trips', extra: {'planData': planData, 'isViewMode': isViewMode}, ); } catch (e) { print("Error fetching plan: $e"); } } static Future viewPlanForApprover( BuildContext context, String planId, String? approverId, String? delegaterId, String? approver_status, { bool isViewMode = false, bool isApprover = true, }) async { try { Map planData = await getViewPlanEdit(planId); print("ViewAAA - $planData"); context.replace( '/approver/plans', extra: { 'planData': planData, 'approver_status': approver_status, 'approverId': approverId, 'delegaterId': delegaterId, '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(String? tripType) async { print("FlightTripType- $tripType"); final String apiUrldata = '$apiUrl/api/getAirportCodeMaster?trip_type=$tripType'; 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'); } } Future getForexPdfDownload(forexId) async { final String apiUrldata = '$apiUrl/api/plans/forexDownload?forex_id=$forexId'; // 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', 'Forex_$forexId.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'); } } // download the Template for bulk upload the user Future> getDownloadUserTemplateForUpload() async { final String apiUrldata = '$apiUrl/api/user/userUploadTemplate'; // download the Template for bulk upload user => api // 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("XL 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', 'user_file.xlsx') ..click(); // Revoke the download URL to free up resources html.Url.revokeObjectUrl(url); return {'status': true, 'message': 'Successfully Downloaded'}; } catch (e) { return {'status': false, 'message': 'Download error: $e'}; } } else if (response.statusCode == 404) { return {'status': false, 'message': 'File not found (404).'}; } else { return {'status': false, 'message': 'Failed to download. Try again.'}; } } //---------------------------------------User Management----------------------------------- Future> getSingleUser(int userId) async { print('Single USer 1 - $userId'); final String apiUrldata = '$apiUrl/api/users/find/$userId'; 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", ); } print('Single USer 1'); Map plansJson = data['data']; // 'data' is a Map, not a List print('Single USer 2'); return plansJson; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load plans'); } } Future> getForexDetailsFind(int userId) async { print('Single Forez 1 - $userId'); // final String apiUrldata = '$apiUrl/api/users/find/$userId'; final String apiUrldata = '$apiUrl/api/findForexPerdiem?forex_perdiem_id=$userId'; 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("forexDat - $data"); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List", ); } final List forexList = data['data']; if (forexList.isEmpty) { throw Exception('No forex data found.'); } return forexList.first as Map; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load plans'); } } // --- Future> getDepartmentDetailsFind(int id) async { final String apiUrldata = '$apiUrl/api/findDepartment?id=$id'; 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('findout the result'); // print(data.runtimeType); // print(data); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List", ); } final List> listData = List>.from(data['data']); if (listData.isEmpty) { throw Exception("No department found with ID $id"); } return listData[0]; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load department details'); } } Future> getPurposeOfTravelDetailsFind(int id) async { final String apiUrldata = '$apiUrl/api/findPurposeOfTravel?id=$id'; 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('findout the result'); // print(data.runtimeType); // print(data); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List", ); } final List> listData = List>.from(data['data']); if (listData.isEmpty) { throw Exception("No department found with ID $id"); } return listData[0]; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load department details'); } } Future> getTemplateFind(int id) async { final String apiUrldata = '$apiUrl/api/template/find/$id'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.put( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); // print('findout the result'); // print(data.runtimeType); // print(data); // if (!data.containsKey('data') || data['data'] is! List) { // throw Exception( // "Invalid response format: 'data' field is missing or not a List"); // } // // final List> listData = // List>.from(data['data']); // // if (listData.isEmpty) { // throw Exception("No department found with ID $id"); // } // // return listData[0]; if (!data.containsKey('data') || data['data'] is! Map) { throw Exception( "Invalid response format: 'data' field is missing or not a Map", ); } return Map.from(data['data']); } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load department details'); } } Future> getForexTemplate() async { final String apiUrldata = '$apiUrl/api/getForexTemplate?template_name=forex'; 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('findout the result'); // print(data.runtimeType); print(data); // if (!data.containsKey('data') || data['data'] is! List) { // throw Exception( // "Invalid response format: 'data' field is missing or not a List"); // } // // final List> listData = // List>.from(data['data']); // // if (listData.isEmpty) { // throw Exception("No department found with ID $id"); // } // // return listData[0]; if (!data.containsKey('data') || data['data'] is! Map) { throw Exception( "Invalid response format: 'data' field is missing or not a Map", ); } return Map.from(data['data']); } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load department details'); } } Future showCancelConfirmationDialog( BuildContext context, Color? layoutColor, ) async { return await showDialog( context: context, builder: (BuildContext context) { return AlertDialog( backgroundColor: Colors.white, title: Text( 'Cancel Confirmation', style: GoogleFonts.poppins( fontSize: 18, fontWeight: FontWeight.w500, ), ), content: Text( 'Do you want to cancel?', style: GoogleFonts.poppins( fontSize: 14.5, fontWeight: FontWeight.w500, ), ), actions: [ ElevatedButton( style: ElevatedButton.styleFrom( backgroundColor: Colors.white, foregroundColor: layoutColor, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), side: BorderSide( color: layoutColor ?? Colors.grey, width: 2, ), ), padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12), ), onPressed: () { Navigator.of(context).pop(false); }, child: Text( "Cancel", style: GoogleFonts.poppins(fontSize: 12), ), ), ElevatedButton( style: ElevatedButton.styleFrom( backgroundColor: layoutColor, // Keep original color foregroundColor: Colors.white, // Keep original color disabledBackgroundColor: layoutColor, // Ensure color remains when disabled disabledForegroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), side: BorderSide( color: layoutColor ?? Colors.grey, width: 1, ), ), padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12), ), onPressed: () => Navigator.of( context, ).pop(true), // Disable when in view mode child: Text("OK", style: GoogleFonts.poppins(fontSize: 12)), ), ], ); }, ) ?? false; // Default to false if dismissed } static Future viewPlanTravelAgent( BuildContext context, String planId, { bool isViewMode = false, bool isMyTrips = false, }) async { try { Map planData = await getViewPlanEdit(planId); print("ViewAAA - $planData"); context.go( '/travelagent/trips', extra: {'planData': planData, 'isViewMode': isViewMode}, ); } catch (e) { print("Error fetching plan: $e"); } } Future> getCostCenterDetailsFind(int id) async { final String apiUrldata = '$apiUrl/api/findCostCenter?cost_center_id=$id'; 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('findout the result'); // print(data.runtimeType); // print(data); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List", ); } final List> listData = List>.from(data['data']); if (listData.isEmpty) { throw Exception("No CostCenter found with ID $id"); } return listData[0]; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load CostCenter details'); } } Future> getHotelsDetailsFind(int id) async { final String apiUrldata = '$apiUrl/api/findHotels?hotel_id=$id'; 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('findout the result'); // print(data.runtimeType); // print(data); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List", ); } final List> listData = List>.from(data['data']); if (listData.isEmpty) { throw Exception("No Hotel data found with ID $id"); } return listData[0]; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load Hotel details'); } } Future> getGroupDetailsFind(int id) async { final String apiUrldata = '$apiUrl/api/groups/find/$id'; 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", ); } print('Single USer 1'); Map plansJson = data['data']; // 'data' is a Map, not a List print('Single USer 2'); return plansJson; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load plans'); } } Future> getTravellerDetailsFind(int id) async { final String apiUrldata = '$apiUrl/api/travellers/find?traveller_id=$id'; //c 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); if (!data.containsKey('data') || data['data'] is! List) { throw Exception( "Invalid response format: 'data' field is missing or not a List", ); } final List> listData = List>.from(data['data']); if (listData.isEmpty) { throw Exception("No Traveller data found with ID $id"); } return listData[0]; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception('Failed to load Hotel details'); } } // ----------------------- User Management - CheckDuplicate --------------- Future> CheckDuplicate( String label, String field, String value, String? userId, ) async { String apiUrldata; if (userId == null || userId.isEmpty) { apiUrldata = '$apiUrl/api/checkDuplicate?$field=$value'; } else { apiUrldata = '$apiUrl/api/checkDuplicate?$field=$value&user_id=$userId'; } 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); // Updated to match actual response structure if (data['status'] == "exists") { print("${data['field']} - $value already exists"); return {"message": "$label already exists", "field": data['field']}; } else { print("$label - $value is a new value"); return {}; } } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception( 'Failed to load checkDuplicate data. Status code: ${response.statusCode}', ); } } // ----------------------- User Management - CheckDuplicate end here ---------------------------------- // ----------------------- Report ------------------------- Future> CallReports(String apiRoute, String body) async { String apiUrldata = '$apiUrl/api/$apiRoute'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.post( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, body: body, ); if (response.statusCode == 200) { try { final data = json.decode(response.body); return data; } catch (e) { throw Exception('Error parsing response: $e'); } } else { throw Exception( 'Failed to load data. Status code: ${response.statusCode}', ); } } Future> reportExcelDownload(String apiRoute,String body,String name,) async { final String apiUrldata = '$apiUrl/api/$apiRoute'; // Sanitize name for filename (optional) final safeName = name.replaceAll( RegExp(r'[^\w\s-]'), '', ); // remove any special chars if needed final excelName = '$safeName.xlsx'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.post( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, body: body, ); if (response.statusCode == 200) { try { print("report XL Dowloaded"); print(excelName); // 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', excelName) ..click(); // Revoke the download URL to free up resources html.Url.revokeObjectUrl(url); // throw Exception('Sucessfully Downloaded'); return {'status': true, 'message': 'Successfully Downloaded'}; } catch (e) { // throw Exception('Error parsing response: $e'); return {'status': false, 'message': 'Download error: $e'}; } } else if (response.statusCode == 404) { // throw Exception('File not found.'); return {'status': false, 'message': 'File not found (404).'}; } else { // throw Exception('Failed to Download'); return {'status': false, 'message': 'Failed to download. Try again.'}; } } // ----------------------- Report ------------------------- }