import 'dart:convert'; import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:generp/Models/ViewVisitDetailsResponseNew.dart'; import 'package:generp/services/api_names.dart'; import 'package:generp/services/api_post_request.dart'; import 'package:path_provider/path_provider.dart'; import '../Models/AccountSuggestionResponse.dart'; import '../Models/AddContactResponse.dart'; import '../Models/AttendanceListResponse.dart'; import '../Models/CheckInResponse.dart'; import '../Models/CheckOutResponse.dart'; import '../Models/ComplaintsSelectionResponse.dart'; import '../Models/DashboardResponse.dart'; import '../Models/DayWiseAttendance.dart'; import '../Models/FollowUpResponse.dart'; import '../Models/Inventory_Part_details_response.dart'; import '../Models/LoginQRResponse.dart'; import '../Models/LogoutResponse.dart'; import '../Models/NearbyGeneratorsResponse.dart'; import '../Models/PaymentCollectionResponse.dart'; import '../Models/PaymentCollectionValidateOTPResponse.dart'; import '../Models/PaymentCollectionWalletResponse.dart'; import '../Models/ProfileResponse.dart'; import '../Models/SessionResponse.dart'; import '../Models/StatusResponse.dart'; import '../Models/SubmitComplaintResponse.dart'; import '../Models/TagGeneratorResponse.dart'; import '../Models/TagLocationResponse.dart'; import '../Models/TechnicianAddPaymentCollectionResponse.dart'; import '../Models/TechnicianDashboardResponse.dart'; import '../Models/TechnicianLoadNumbersResponse.dart'; import '../Models/TechniciansPendingComplaintsResponse.dart'; import '../Models/TodayVisitResponse.dart'; import '../Models/UpdateComplaintResponse.dart'; import '../Models/UpdatePasswordResponse.dart'; import '../Models/VersionsResponse.dart'; import '../Models/ViewVisitDetailsResponse.dart'; import '../Models/generatorComplaintResponse.dart'; import '../Models/loadGeneratorDetailsResponse.dart'; import '../Utils/commonServices.dart'; class ApiCalling { static Future download_files(empId, session, url, cntxt) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, url, {}); if (res != null) { final bytes = res.bodyBytes; var directory = ""; if (Platform.isAndroid) { directory = "/storage/emulated/0/Download"; var _directory = Directory(directory); if (!_directory.existsSync()) { // If the directory does not exist, create it _directory.createSync(recursive: true); debugPrint('Directory created: $_directory'); } else { debugPrint('Directory already exists: $_directory'); } // final androiddirectory = await getDownloadsDirectory(); // directory = androiddirectory!.path; } else if (Platform.isIOS) { final iosDirectory = await getApplicationSupportDirectory(); directory = iosDirectory!.path; } final contentDisposition = res.headers['content-disposition']; debugPrint( "contentDisposition ${contentDisposition?.split('filename=')[1]}", ); // final filename = contentDisposition != null // ? contentDisposition.split('filename=')[1] // : 'file'; var filename = (contentDisposition?.split('filename=')[1])?.replaceAll( '"', "", ); // ignore: unnecessary_brace_in_string_interps final file = File('${directory}/${filename}'); await file.writeAsBytes(bytes); toast( cntxt, "File saved to your downloads as ${filename} to ${directory}, Successfully", ); debugPrint('File saved successfully'); return jsonDecode(res.body); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future checkAppVersionApi() async { try { final response = await post({}, getAppVersionUrl, {}); if (response != null) { print("${response.body}"); return VersionsResponse.fromJson(jsonDecode(response.body)); } else { return null; } } catch (e, s) { return null; } } static Future CheckSessionExistsApi(empId, session) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; // print(data); final res = await post(data, getEmployeeSessionDetailsUrl, {}); if (res != null) { // print("check_session: ${res.body}"); return SessionResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future LoginFunctionApi( email, password, token, deviceID, deviceInfo, ) async { try { Map data = { 'email_id': (email).toString(), 'password': (password).toString(), 'token_id': (token).toString(), 'device_id': (deviceID).toString(), 'device_details': (deviceInfo).toString(), }; final res = await post(data, loginUrl, {}); if (res != null) { // debugPrint("Login: ${res.body}"); // print(data); return StatusResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future UpdatePasswordApi( empId, session, password, conf_password, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'password': (password).toString(), 'confirm_password': (conf_password).toString(), }; final res = await post(data, updatePasswordUrl, {}); if (res != null) { // debugPrint("update password ${res.body}"); return UpdatePasswordResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future DashboardFunctionApi(empId, session) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, employeeDashboardUrl, {}); if (res != null) { // debugPrint("Dashboard: ${res.body}"); return DashboardResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future ProfileFunctionApi(empId, session) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, employeeProfileUrl, {}); if (res != null) { // debugPrint("profile-details: ${res.body}"); return ProfileResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future QRLoginRequestAPI( empId, session, type, token, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'type': (type).toString(), 'token': (token).toString(), }; final res = await post(data, qrCodeLoginUrl, {}); if (res != null) { // debugPrint(res.body); return LoginQRResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future LogoutFunctionApi(empId, session) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, logoutAppUrl, {}); if (res != null) { // debugPrint(res.body); return LogoutResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future CheckInApi( empId, sessioId, location, latlngs, check_in_pic, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (sessioId).toString(), 'posit': (latlngs).toString(), 'location': (location).toString(), }; var res; if (check_in_pic != null) { res = await postImage(data, employeeChekInUrl, {}, check_in_pic); res = jsonDecode(res); } else { res = await post(data, employeeChekInUrl, {}); res = jsonDecode(res); } if (res != null) { return CheckInResponse.fromJson(res); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future CheckOutApi( empId, sessioId, location, latlngs, image, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (sessioId).toString(), 'location': (location).toString(), 'posit': (latlngs).toString(), }; var res; if (image != null) { res = await postImage2(data, {}, employeeCheckOutUrl, image); res = jsonDecode(res); } else { res = await post(data, employeeCheckOutUrl, {}); res = jsonDecode(res); } if (res != null) { return CheckOutResponse.fromJson(res); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future AttendanceListApi(empId, session) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, employeeAttendanceDashboardUrl, {}); if (res != null) { // debugPrint(res.body); return AttendanceDashboard.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static LoadAttendanceDetails(empId, session, month, year) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'year': (year).toString(), 'month': month.toString(), }; final res = await post(data, employeeMonthwiseAttendanceUrl, {}); if (res != null) { // debugPrint("montly attendance: ${res.body}"); // return AttendanceHistory.fromJson(jsonDecode(res.body)); return res.body; } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future DateWiseAttendanceApi( empId, session, date, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'date': (date).toString(), }; final res = await post(data, employeeDayAttendanceDetailsUrl, {}); if (res != null) { // debugPrint("Attendacnce API /; ${res.body}"); return AttendanceDaywiseResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } ///gen tracker static Future LoadGeneratorDetailsAPI( empId, session, gen_hash_id, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'gen_hash_id': (gen_hash_id).toString(), }; final res = await post(data, genTrackerGeneratorDetailsUrl, {}); if (res != null) { debugPrint(res.body); return loadGeneratorDetailsResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future LoadGeneratorComplaintListAPI( empId, session, gen_id, open_status, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'gen_id': (gen_id).toString(), 'open_status': (open_status).toString(), }; final res = await post(data, genTrackerGeneratorComplaintsUrl, {}); if (res != null) { // debugPrint(res.body); return generatorComplaintResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future TagLocationAPI( empId, session, gen_hash_id, location, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'gen_hash_id': (gen_hash_id).toString(), 'location': (location).toString(), }; final res = await post(data, genTrackerTagLocationUrl, {}); if (res != null) { // debugPrint(res.body); return TagLocationResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future TagGeneratorAPI( empId, session, gen_hash_id, engine_no, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'gen_hash_id': (gen_hash_id).toString(), 'engine_no': (engine_no).toString(), }; final res = await post(data, genTrackerTagGeneratorUrl, {}); if (res != null) { // debugPrint(res.body); return TagGeneratorResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future SubmitGeneratorComplaintAPI( empId, session, complaint_type_id, complaint_category_id, complaint_desc_id, running_hrs, gen_id, complaint_note, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'complaint_type_id': (complaint_type_id).toString(), 'complaint_category_id': (complaint_category_id).toString(), 'complaint_desc_id': (complaint_desc_id).toString(), 'running_hrs': (running_hrs).toString(), 'gen_id': (gen_id).toString(), 'complaint_note': (complaint_note).toString(), }; final res = await post(data, genTrackerRegisterComplaint, {}); if (res != null) { // debugPrint(res.body); return SubmitComplaintResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future ComplaintSelectionAPI( empId, session, gen_hash_id, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'gen_hash_id': (gen_hash_id).toString(), }; final res = await post(data, complaintsSelectionUrl, {}); if (res != null) { // debugPrint(res.body); return ComplaintsSelectionResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } ///Inventory Module API's static Future LoadPartDetailsAPI( empId, session, part_id, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'part_id': (part_id).toString(), }; final res = await post(data, inventoryPartDetailsUrl, {}); if (res != null) { // debugPrint(res.body); return Inventory_Part_details_response.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future InventoryUpdateStockAPI( empId, session, qty, descr, part_id, tran_type, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'qty': (qty).toString(), 'descr': (descr).toString(), 'product_id': (part_id).toString(), 'tran_type': (tran_type).toString(), }; final res = await post(data, inventoryStockUpdateUrl, {}); if (res != null) { // debugPrint(res.body); return loadGeneratorDetailsResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } ///technician static Future LoadTechnicianGeneratorDetailsAPI(empId, session, gen_id) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'gen_id': (gen_id).toString(), }; final res = await post(data, technicianGeneratorDetailsUrl, {}); if (res != null) { // debugPrint(res.body); return loadGeneratorDetailsResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future loadTechnicianDashboardApi( empId, session, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, technicianDashboardUrl, {}); if (res != null) { // debugPrint(res.body); return TechnicianResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future getTodayVisitsListAPI( empId, session, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, technicianTodayVisitsUrl, {}); if (res != null) { // debugPrint(res.body); return TodayVisitResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future getMonthVisitsListAPI( empId, session, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, technicianMonthlyVisitsUrl, {}); if (res != null) { // debugPrint(res.body); return TodayVisitResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future loadNearbyGeneratorsAPI( empId, session, tech_loc, radius, status, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'tech_loc': (tech_loc).toString(), 'radius': (radius).toString(), 'status': (status).toString(), }; final res = await post(data, technicianNearbyGeneratorsUrl, {}); if (res != null) { // print(data); // debugPrint(res.body); return NearbyGeneratorsResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future AccountSuggestionAPI( empId, session, search_string, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'search_string': (search_string).toString(), }; final res = await post(data, technicianAccountSearchUrl, {}); if (res != null) { // debugPrint(res.body); return AccountSuggestionResonse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future LoadTechnicianComplaintsAPI(empId, session) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, technicianPendingComplaintUrl, {}); if (res != null) { // debugPrint(res.body); return TechnicianPendingComplaintsResponse.fromJson( jsonDecode(res.body), ); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future LoadContactsTechnicianAPI( empId, session, type, gen_id, account_id, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'type': (type).toString(), 'gen_id': (gen_id).toString(), 'account_id': (account_id).toString(), }; final res = await post(data, technicianAddPaymentUrl, {}); if (res != null) { // debugPrint(res.body); return TechnicianLoadNumbersResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future TechnicianUpdatepaymentAPI( empId, session, ref_type, ref_id, payment_mode_id, payment_ref_no, amount, otp_validated_name, otp_validated_mobile_number, payment_proof, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'ref_type': (ref_type).toString(), 'ref_id': (ref_id).toString(), 'payment_mode_id': (payment_mode_id).toString(), 'payment_ref_no': (payment_ref_no).toString(), 'amount': (amount).toString(), 'otp_validated_name': (otp_validated_name).toString(), 'otp_validated_mobile_number': (otp_validated_mobile_number).toString(), }; var res; if (payment_proof != null) { res = await postImage3( data, {}, technicianAddPaymentCollectionUrl, payment_proof, ); res = jsonDecode(res); } else { res = await post(data, technicianAddPaymentCollectionUrl, {}); res = jsonDecode(res); } if (res != null) { return TechnicianAddPaymentCollectionResponse.fromJson(res); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future TechnicianPaymentOTPValidateAPI( empId, session, payment_collection_id, otp, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'payment_collection_id': (payment_collection_id).toString(), 'otp': (otp).toString(), }; final res = await post(data, technicianPaymentCollectionOtpUrl, {}); if (res != null) { // debugPrint(res.body); return PaymentCollectionValidateOTPResponse.fromJson( jsonDecode(res.body), ); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future paymentCollectionListAPI( empId, session, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, technicianPaymentCollectionUrl, {}); if (res != null) { // debugPrint(res.body); return PaymentCollectionResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future loadTransactionsListAPI( empId, session, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), }; final res = await post(data, technicianWalletCollectionUrl, {}); if (res != null) { // debugPrint(res.body); return PaymentCollectionWalletResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future loadVisitDetailsAPI( empId, session, comp_id, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'comp_id': (comp_id).toString(), }; final res = await post(data, technicianComplaintDetailsUrl, {}); if (res != null) { // debugPrint(res.body); return ViewVisitDetailsResponseNew.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future loadFollowupListAPI( empId, session, comp_id, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'comp_id': (comp_id).toString(), }; final res = await post(data, technicianComplaintFollowUpUrl, {}); if (res != null) { // debugPrint(res.body); return FollowupListResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future AddContactAPI( empId, session, gen_id, name, designation, mob1, mob2, tel, mail, type, account_id, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'gen_id': (gen_id).toString(), 'name': (name).toString(), 'designation': (designation).toString(), 'mob1': (mob1).toString(), 'mob2': (mob2).toString(), 'tel': (tel).toString(), 'mail': (mail).toString(), 'type': (type).toString(), 'account_id': (account_id).toString(), }; final res = await post(data, technicianAddContactUrl, {}); if (res != null) { // debugPrint(res.body); return AddContactResponse.fromJson(jsonDecode(res.body)); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } static Future UpdateComplaintAPI( empId, session, complaint_id, in_time, feedback, fsr_no, running_hrs, complaint_status, fsr_file, ) async { try { Map data = { 'emp_id': (empId).toString(), 'session_id': (session).toString(), 'complaint_id': (complaint_id).toString(), 'in_time': (in_time).toString(), 'feedback': (feedback).toString(), 'fsr_no': (fsr_no).toString(), 'running_hrs': (running_hrs).toString(), 'complaint_status': (complaint_status).toString(), }; // print(data); // print(fsr_file); var res; if (fsr_file != null) { res = await postImage4(data, {}, technicianUpdateVisitUrl, fsr_file); res = jsonDecode(res); } else { res = await post(data, technicianUpdateVisitUrl, {}); res = jsonDecode(res); } // debugPrint(res); if (res != null) { // debugPrint(res); return UpdateComplaintResponse.fromJson(res); } else { debugPrint("Null Response"); return null; } } catch (e) { debugPrint('hello bev=bug $e '); return null; } } }