import 'dart:convert';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:generp/Models/ViewVisitDetailsResponseNew.dart';
import 'package:generp/Models/financeModels/addDirectPaymentResponse.dart';
import 'package:generp/Models/financeModels/paymentRequisitionPaymentsListResponse.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/financeModels/addPaymentRequestionResponse.dart';
import '../Models/financeModels/paymentRequesitionListsResponse.dart';
import '../Models/financeModels/paymentRequisitionDetailsResponse.dart';
import '../Models/financeModels/paymentRequisitionPaymentsDetailsResponse.dart';
import '../Models/financeModels/paymentRequisitionPaymentsReceiptsDetailsResponse.dart';
import '../Models/financeModels/paymentRequisitionPaymentsReceiptsListResponse.dart';
import '../Models/generatorComplaintResponse.dart';
import '../Models/loadGeneratorDetailsResponse.dart';
import '../Models/financeModels/financeDashboardPagesResponse.dart';
import '../Notifiers/financeProvider/approveRejectPaymentRequestResponse.dart';
import '../Utils/commonServices.dart';

class ApiCalling {
  static Future download_files(empId, session, url, cntxt) async {
    try {
      Map<String, String> 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<VersionsResponse?> 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<SessionResponse?> CheckSessionExistsApi(empId, session) async {
    try {
      Map<String, String> 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<StatusResponse?> LoginFunctionApi(
    email,
    password,
    token,
    deviceID,
    deviceInfo,
  ) async {
    try {
      Map<String, String> 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<UpdatePasswordResponse?> UpdatePasswordApi(
    empId,
    session,
    password,
    conf_password,
  ) async {
    try {
      Map<String, String> 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<DashboardResponse?> DashboardFunctionApi(empId, session) async {
    try {
      Map<String, String> 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<ProfileResponse?> ProfileFunctionApi(empId, session) async {
    try {
      Map<String, String> 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<LoginQRResponse?> QRLoginRequestAPI(
    empId,
    session,
    type,
    token,
  ) async {
    try {
      Map<String, String> 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<LogoutResponse?> LogoutFunctionApi(empId, session) async {
    try {
      Map<String, String> 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<CheckInResponse?> CheckInApi(
    empId,
    sessioId,
    location,
    latlngs,
    check_in_pic,
  ) async {
    try {
      Map<String, String> 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<CheckOutResponse?> CheckOutApi(
    empId,
    sessioId,
    location,
    latlngs,
    image,
  ) async {
    try {
      Map<String, String> 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<AttendanceDashboard?> AttendanceListApi(empId, session) async {
    try {
      Map<String, String> 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<String, String> 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<AttendanceDaywiseResponse?> DateWiseAttendanceApi(
    empId,
    session,
    date,
  ) async {
    try {
      Map<String, String> 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<loadGeneratorDetailsResponse?> LoadGeneratorDetailsAPI(
    empId,
    session,
    gen_hash_id,
  ) async {
    try {
      Map<String, String> 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<generatorComplaintResponse?> LoadGeneratorComplaintListAPI(
    empId,
    session,
    gen_id,
    open_status,
  ) async {
    try {
      Map<String, String> 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<TagLocationResponse?> TagLocationAPI(
    empId,
    session,
    gen_hash_id,
    location,
  ) async {
    try {
      Map<String, String> 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<TagGeneratorResponse?> TagGeneratorAPI(
    empId,
    session,
    gen_hash_id,
    engine_no,
  ) async {
    try {
      Map<String, String> 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<SubmitComplaintResponse?> SubmitGeneratorComplaintAPI(
    empId,
    session,
    complaint_type_id,
    complaint_category_id,
    complaint_desc_id,
    running_hrs,
    gen_id,
    complaint_note,
  ) async {
    try {
      Map<String, String> 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<ComplaintsSelectionResponse?> ComplaintSelectionAPI(
    empId,
    session,
    gen_hash_id,
  ) async {
    try {
      Map<String, String> 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<Inventory_Part_details_response?> LoadPartDetailsAPI(
    empId,
    session,
    part_id,
  ) async {
    try {
      Map<String, String> 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<loadGeneratorDetailsResponse?> InventoryUpdateStockAPI(
    empId,
    session,
    qty,
    descr,
    part_id,
    tran_type,
  ) async {
    try {
      Map<String, String> 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<loadGeneratorDetailsResponse?>
  LoadTechnicianGeneratorDetailsAPI(empId, session, gen_id) async {
    try {
      Map<String, String> 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<TechnicianResponse?> loadTechnicianDashboardApi(
    empId,
    session,
  ) async {
    try {
      Map<String, String> 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<TodayVisitResponse?> getTodayVisitsListAPI(
    empId,
    session,
  ) async {
    try {
      Map<String, String> 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<TodayVisitResponse?> getMonthVisitsListAPI(
    empId,
    session,
  ) async {
    try {
      Map<String, String> 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<NearbyGeneratorsResponse?> loadNearbyGeneratorsAPI(
    empId,
    session,
    tech_loc,
    radius,
    status,
  ) async {
    try {
      Map<String, String> 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<AccountSuggestionResonse?> AccountSuggestionAPI(
    empId,
    session,
    search_string,
  ) async {
    try {
      Map<String, String> 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<TechnicianPendingComplaintsResponse?>
  LoadTechnicianComplaintsAPI(empId, session) async {
    try {
      Map<String, String> 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<TechnicianLoadNumbersResponse?> LoadContactsTechnicianAPI(
    empId,
    session,
    type,
    gen_id,
    account_id,
  ) async {
    try {
      Map<String, String> 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<TechnicianAddPaymentCollectionResponse?>
  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<String, String> 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<PaymentCollectionValidateOTPResponse?>
  TechnicianPaymentOTPValidateAPI(
    empId,
    session,
    payment_collection_id,
    otp,
  ) async {
    try {
      Map<String, String> 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<PaymentCollectionResponse?> paymentCollectionListAPI(
    empId,
    session,
  ) async {
    try {
      Map<String, String> 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<PaymentCollectionWalletResponse?> loadTransactionsListAPI(
    empId,
    session,
  ) async {
    try {
      Map<String, String> 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<ViewVisitDetailsResponseNew?> loadVisitDetailsAPI(
    empId,
    session,
    comp_id,
  ) async {
    try {
      Map<String, String> 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<FollowupListResponse?> loadFollowupListAPI(
    empId,
    session,
    comp_id,
  ) async {
    try {
      Map<String, String> 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<AddContactResponse?> AddContactAPI(
    empId,
    session,
    gen_id,
    name,
    designation,
    mob1,
    mob2,
    tel,
    mail,
    type,
    account_id,
  ) async {
    try {
      Map<String, String> 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<UpdateComplaintResponse?> UpdateComplaintAPI(
    empId,
    session,
    complaint_id,
    in_time,
    feedback,
    fsr_no,
    running_hrs,
    complaint_status,
    fsr_file,
  ) async {
    try {
      Map<String, String> 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;
    }
  }

  ///finance Module
  static Future<financeDashboardPagesResponse?> financeDashboardPagesAPI(
    empId,
    session,
  ) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
      };
      final res = await post(data, financeDashboardPagesUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return financeDashboardPagesResponse.fromJson(jsonDecode(res.body));
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<financeDashboardPagesResponse?>
  addFormfinanceFormAccessPagesAPI(empId, session) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
      };
      final res = await post(data, financeAddFormPagesAccessUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return financeDashboardPagesResponse.fromJson(jsonDecode(res.body));
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<addPaymentRequestionResponse?> addPaymentRequestionViewAPI(
    empId,
    session,
    mode,
  ) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
        'mode': (mode).toString(),
      };
      final res = await post(data, addPaymentRequestionViewUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return addPaymentRequestionResponse.fromJson(jsonDecode(res.body));
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<addDirectPaymentResponse?> addDirectPaymentRequestionViewAPI(
    empId,
    session,

  ) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
      };
      final res = await post(data, addDirectPaymentRequestionViewUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return addDirectPaymentResponse.fromJson(jsonDecode(res.body));
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }



  static addPaymentRequestionSubmitAPI(
    emp_id,
    session_id,
    type,
    account_id,
    requesting_purpose,
    description,
    amount,
    payment_mode_id,
    bank_name,
    bank_branch_name,
    bank_acc_number,
    bank_ifsc_code,
    acc_holder_name,
    bank_upi_id,
    attachment,
  ) async {
    try {
      Map<String, String> data = {
        'emp_id': emp_id.toString(),
        'session_id': session_id.toString(),
        'type': type.toString(),
        'account_id': account_id.toString(),
        'requesting_purpose': requesting_purpose.toString(),
        'description': description.toString(),
        'amount': amount.toString(),
        'payment_mode_id': payment_mode_id.toString(),
        'bank_name': bank_name.toString(),
        'bank_branch_name': bank_branch_name.toString(),
        'bank_acc_number': bank_acc_number.toString(),
        'bank_ifsc_code': bank_ifsc_code.toString(),
        'acc_holder_name': acc_holder_name.toString(),
        'bank_upi_id': bank_upi_id.toString(),
      };

      var res;
      if (attachment != null) {
        res = await postImageNew(
          data,
          {},
          addPaymentRequestionSubmitUrl,
          attachment,
          "attachment",
        );
        res = jsonDecode(res);
      } else {
        res = await post(data, addPaymentRequestionSubmitUrl, {});
        res = jsonDecode(res);
      }
      if (res != null) {
        return res;
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static addDirectPaymentRequestionSubmitAPI(
      emp_id,
      session_id,
      from_account_id,
      to_account_id,
      description,
      payment_mode_id,
      amount,
      payment_date,
      payment_reference_number,
      bank_name,
      bank_branch_name,
      bank_acc_number,
      bank_ifsc_code,
      acc_holder_name,
      bank_upi_id,
      attachment,
      ) async {
    try {
      Map<String, String> data = {
        'emp_id': emp_id.toString(),
        'session_id': session_id.toString(),
        'from_account_id': from_account_id.toString(),
        'to_account_id': to_account_id.toString(),
        'description': description.toString(),
        'payment_mode_id': payment_mode_id.toString(),
        'amount': amount.toString(),
        'payment_date': payment_date.toString(),
        'payment_reference_number': payment_reference_number.toString(),
        'bank_name': bank_name.toString(),
        'bank_branch_name': bank_branch_name.toString(),
        'bank_acc_number': bank_acc_number.toString(),
        'bank_ifsc_code': bank_ifsc_code.toString(),
        'acc_holder_name': acc_holder_name.toString(),
        'bank_upi_id': bank_upi_id.toString(),
      };

      var res;
      if (attachment != null) {
        res = await postImageNew(
          data,
          {},
          addDirectPaymentRequestionSubmitUrl,
          attachment,
          "attachment",
        );
        res = jsonDecode(res);
      } else {
        res = await post(data, addDirectPaymentRequestionSubmitUrl, {});
        res = jsonDecode(res);
      }
      if (res != null) {
        return res;
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<paymentRequesitionListsResponse?> paymentRequestionListsAPI(
    empId,
    session,
    mode,
  ) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
        'mode': (mode).toString(),
      };
      final res = await post(data, paymentRequestionListUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return paymentRequesitionListsResponse.fromJson(jsonDecode(res.body));
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<paymentRequisitionDetailsResponse?> paymentRequestionDetailsAPI(
    empId,
    session,
    payment_request_id,
  ) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
        'payment_request_id': (payment_request_id).toString(),
      };
      final res = await post(data, paymentRequestionDetailsUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return paymentRequisitionDetailsResponse.fromJson(jsonDecode(res.body));
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<approveRejectPaymentRequestResponse?>
  approveRejectPaymentRequestAPI(empId, session, payment_request_id) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
        'payment_request_id': (payment_request_id).toString(),
      };
      final res = await post(data, approveRejectPaymentRequestUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return approveRejectPaymentRequestResponse.fromJson(
          jsonDecode(res.body),
        );
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<approveRejectPaymentRequestResponse?>
  RejectPaymentRequestSubmitAPI(
    empId,
    session,
    mode,
    payment_request_id,
    approve_remarks,
  ) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
        'type': 'reject',
        'mode': mode.toString(),
        'payment_request_id': (payment_request_id).toString(),
        'approve_remarks': approve_remarks,
      };
      final res = await post(data, approveRejectPaymentRequestSubmitUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return approveRejectPaymentRequestResponse.fromJson(
          jsonDecode(res.body),
        );
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<approveRejectPaymentRequestResponse?>
  ApprovePaymentRequestSubmitAPI(
    empId,
    session,
    mode,
    payment_request_id,
    approved_amount,
    approve_remarks,
    proposed_payment_account_id,
  ) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
        'type': 'approve',
        'mode': mode.toString(),
        'payment_request_id': (payment_request_id).toString(),
        'approve_remarks': approve_remarks,
        'proposed_payment_account_id': proposed_payment_account_id,
      };
      final res = await post(data, approveRejectPaymentRequestSubmitUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return approveRejectPaymentRequestResponse.fromJson(
          jsonDecode(res.body),
        );
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<approveRejectPaymentRequestResponse?>
  processApproveRejectPaymentRequestSubmitAPI(
    empId,
    session,
    mode,
    payment_request_id,
    payment_account_id,
    processing_remarks,
    attachment,
  ) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
        'type': 'approve',
        'mode': mode.toString(),
        'payment_request_id': (payment_request_id).toString(),
        'payment_account_id': payment_account_id,
        'processing_remarks': processing_remarks,
      };
      var res;
      if (attachment != null) {
        res = await postImageNew(
          data,
          {},
          approveRejectPaymentRequestSubmitUrl,
          attachment,
          "attachment",
        );
        res = jsonDecode(res);
      } else {
        res = await post(data, approveRejectPaymentRequestSubmitUrl, {});
        res = jsonDecode(res);
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<paymentRequisitionPaymentsListResponse?> paymentRequisitionPaymentListAPI(empId,session,) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
      };
      final res = await post(data, paymentRequesitionPaymentsListUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return paymentRequisitionPaymentsListResponse.fromJson(
          jsonDecode(res.body),
        );
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<paymentRequisitionPaymentsReceiptsListResponse?> paymentRequisitionPaymentReceiptListAPI(empId,session,) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
      };
      final res = await post(data, paymentRequesitionPaymentsReceiptsListUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return paymentRequisitionPaymentsReceiptsListResponse.fromJson(
          jsonDecode(res.body),
        );
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<paymentRequisitionPaymentsDetailsResponse?> paymentRequisitionPaymentDetailsAPI(empId,session,payment_id) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
        'payment_id': (payment_id).toString(),
      };
      final res = await post(data, paymentRequesitionPaymentsDetailsUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return paymentRequisitionPaymentsDetailsResponse.fromJson(
          jsonDecode(res.body),
        );
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }

  static Future<paymentRequisitionPaymentsReceiptsDetailsResponse?> paymentRequisitionPaymentReceiptDetailsAPI(empId,session,payment_id) async {
    try {
      Map<String, String> data = {
        'emp_id': (empId).toString(),
        'session_id': (session).toString(),
        'payment_receipt_id': (payment_id).toString(),
      };
      final res = await post(data, paymentRequesitionPaymentsReceiptsDetailsUrl, {});
      if (res != null) {
        debugPrint(res.body);
        return paymentRequisitionPaymentsReceiptsDetailsResponse.fromJson(
          jsonDecode(res.body),
        );
      } else {
        debugPrint("Null Response");
        return null;
      }
    } catch (e) {
      debugPrint('hello bev=bug $e ');
      return null;
    }
  }
}
