import 'dart:convert'; import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_download_manager/flutter_download_manager.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:generp/Models/crmModels/crmSelectedProductDetailsResponse.dart'; import 'package:generp/screens/LoginScreen.dart'; import 'package:http/http.dart' as http; import 'package:intl/intl.dart'; import 'package:path_provider/path_provider.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; import '../../Models/crmModels/GetDistrictOnStateResponse.dart'; import '../../Models/crmModels/GetSegmentOnTeamResponse.dart'; import '../../Models/crmModels/GetSourceOnReferenceResponse.dart'; import '../../Models/crmModels/GetSubLocOnDistrictResponse.dart'; import '../../Models/crmModels/crmNewLeadsProspectsViewResponse.dart'; import '../../Utils/commonServices.dart'; import '../../services/api_calling.dart'; import '../HomeScreenNotifier.dart'; class Addnewleadsandprospectsprovider extends ChangeNotifier { final FlutterLocalNotificationsPlugin _notificationsPlugin = FlutterLocalNotificationsPlugin(); static const platform = MethodChannel('in.webgrid.generp/download'); final GlobalKey webViewKey = GlobalKey(); var dl = DownloadManager(); TextEditingController addProductPriceController = TextEditingController(); TextEditingController addQuantityController = TextEditingController(); TextEditingController addTotalAmountController = TextEditingController(); TextEditingController companyNameController = TextEditingController(); TextEditingController contactPersonNameController = TextEditingController(); TextEditingController customerMailIdController = TextEditingController(); TextEditingController mobileController = TextEditingController(); TextEditingController alternateMobileController = TextEditingController(); TextEditingController telephoneController = TextEditingController(); TextEditingController designationController = TextEditingController(); TextEditingController addressController = TextEditingController(); TextEditingController taxesController = TextEditingController(); TextEditingController SpecialNoteController = TextEditingController(); TextEditingController forController = TextEditingController(); TextEditingController paymentTermsController = TextEditingController(); List> _productRows = []; // For backend List> get productRows => _productRows; ProductsData? _selectedProductsDeatilsData; List _employeesList = []; List _sourcesList = []; List _teamsList = []; List _statesList = []; List _productsList = []; List _salutationList = []; List _districtsList = []; List _subLocationsList = []; List _referencesList = []; List _segmentsList = []; Employees? _selectedEmployees; String? _selectedEmployeesId; String? _selectedEmployeesValue; Sources? _selectedSources; String? _selectedSourcesId; String? _selectedSourcesValue; Teams? _selectedTeams; String? _selectedTeamsId; String? _selectedTeamsValue; States? _selectedStates; String? _selectedStatesId; String? _selectedStatesValue; Products? _selectedProducts; String? _selectedProductsId; String? _selectedProductsValue; String? _selectedSalutation; Districts? _selectedDistricts; SubLocations? _selectedSubLocations; References? _selectedReferences; Segments? _selectedSegments; String? _selectedDistrictId; String? _selectedDistrictValue; String? _selectedSubLocationId; String? _selectedSubLocationValue; String? _selectedReferenceId; String? _selectedReferenceValue; String? _selectedSegmentId; String? _selectedSegmentValue; String? _selectedLeadStatus; ProductsData? get selectedProductsDetailsData => _selectedProductsDeatilsData; String? get selectedLeadStatus => _selectedLeadStatus; List get employeesList => _employeesList; List get sourcesList => _sourcesList; List get teamsList => _teamsList; List get statesList => _statesList; List get productsList => _productsList; List get salutationList => _salutationList; Employees? get selectedEmployees => _selectedEmployees; String? get selectedEmployeesId => _selectedEmployeesId; String? get selectedEmployeesValue => _selectedEmployeesValue; Sources? get selectedSources => _selectedSources; String? get selectedSourcesId => _selectedSourcesId; String? get selectedSourcesValue => _selectedSourcesValue; Teams? get selectedTeams => _selectedTeams; String? get selectedTeamsId => _selectedTeamsId; String? get selectedTeamsValue => _selectedTeamsValue; States? get selectedStates => _selectedStates; String? get selectedStatesId => _selectedStatesId; String? get selectedStatesValue => _selectedStatesValue; Products? get selectedProducts => _selectedProducts; String? get selectedProductsId => _selectedProductsId; String? get selectedProductsValue => _selectedProductsValue; String? get selectedSalutation => _selectedSalutation; List get districtsList => _districtsList; List get subLocationsList => _subLocationsList; List get referencesList => _referencesList; List get segmentsList => _segmentsList; References? get selectedReference => _selectedReferences; Segments? get selectedSegment => _selectedSegments; Districts? get selectedDistricts => _selectedDistricts; SubLocations? get selectedSubLocations => _selectedSubLocations; String? get selectedDistrictId => _selectedDistrictId; String? get selectedDistrictValue => _selectedDistrictValue; String? get selectedSubLocationId => _selectedSubLocationId; String? get selectedSubLocationValue => _selectedSubLocationValue; String? get selectedReferenceId => _selectedReferenceId; String? get selectedReferenceValue => _selectedReferenceValue; String? get selectedSegmentId => _selectedSegmentId; String? get selectedSegmentValue => _selectedSegmentValue; set selectedLeadStatus(String? value) { _selectedLeadStatus = value; leadStatusError = null; notifyListeners(); } set selectedEmployees(Employees? value) { _selectedEmployees = value; _selectedEmployeesId = value!.id; _selectedEmployeesValue = value!.name; notifyListeners(); } set selectedEmployeesId(String? value) { _selectedEmployeesId = value; notifyListeners(); } set selectedEmployeesValue(String? value) { _selectedEmployeesValue = value; notifyListeners(); } set selectedSources(Sources? value) { _selectedSources = value; _selectedSourcesId = value!.id; _selectedSourcesValue = value!.name; sourceError = null; notifyListeners(); } set selectedSourcesId(String? value) { _selectedSourcesId = value; notifyListeners(); } set selectedSourcesValue(String? value) { _selectedSourcesValue = value; notifyListeners(); } set selectedTeams(Teams? value) { _selectedTeams = value; _selectedTeamsId = value!.id; _selectedTeamsValue = value!.name; teamsError = null; notifyListeners(); } set selectedTeamsId(String? value) { _selectedTeamsId = value; notifyListeners(); } set selectedTeamsValue(String? value) { _selectedTeamsValue = value; notifyListeners(); } set selectedStates(States? value) { _selectedStates = value; _selectedStatesId = value!.id; _selectedStatesValue = value!.name; statesError = null; notifyListeners(); } set selectedStatesId(String? value) { _selectedStatesId = value; notifyListeners(); } set selectedStatesValue(String? value) { _selectedStatesValue = value; notifyListeners(); } set selectedProducts(Products? value) { _selectedProducts = value; if(value!=null){ _selectedProductsId = value!.id; _selectedProductsValue = value!.name; } notifyListeners(); } set selectedProductsId(String? value) { _selectedProductsId = value; notifyListeners(); } set selectedProductsValue(String? value) { _selectedProductsValue = value; notifyListeners(); } set selectedSalutation(String? value) { _selectedSalutation = value; salutationError = null; notifyListeners(); } set selectedDistricts(Districts? value) { _selectedDistricts = value; _selectedDistrictId = value!.id!; _selectedDistrictValue = value.district; districtsError = null; notifyListeners(); } set selectedSubLocations(SubLocations? value) { _selectedSubLocations = value; _selectedSubLocationId = value!.id!; _selectedSubLocationValue = value.subLocality; subLocError = null; notifyListeners(); } set selectedDistrictId(String? value) { _selectedDistrictId = value; notifyListeners(); } set selectedDistrictValue(String? value) { _selectedDistrictValue = value; notifyListeners(); } set selectedSubLocationId(String? value) { _selectedSubLocationId = value; notifyListeners(); } set selectedSubLocationValue(String? value) { _selectedSubLocationValue = value; notifyListeners(); } set selectedReference(References? value) { _selectedReferences = value; _selectedReferenceId = value!.id!; _selectedReferenceValue = value.name; referenceError = null; notifyListeners(); } set selectedSegment(Segments? value) { _selectedSegments = value; _selectedSegmentId = value!.id!; _selectedSegmentValue = value.name; segmentsError = null; notifyListeners(); } set selectedReferenceId(String? value) { _selectedReferenceId = value; notifyListeners(); } set selectedReferenceValue(String? value) { _selectedReferenceValue = value; notifyListeners(); } set selectedSegmentId(String? value) { _selectedSegmentId = value; notifyListeners(); } set selectedSegmentValue(String? value) { _selectedSegmentValue = value; notifyListeners(); } set productRows(value) { _productRows = value; productsEmptyError = ""; notifyListeners(); } String getJsonEncodedProducts() { return jsonEncode(productRows); } String? mailIdError = ""; String? companynameError = ""; String? salutationError = ""; String? nameError = ""; String? AlternatemobileError = ""; String? TelephoneError = ""; String? mobileError = ""; String? addressError = ""; String? productsEmptyError = ""; String? designationError = ""; String? taxesError = ""; String? SpecialNoteError = ""; String? forError = ""; String? paymentTermsError = ""; String? sourceError = ""; String? referenceError = ""; String? teamsError = ""; String? segmentsError = ""; String? statesError = ""; String? districtsError = ""; String? subLocError = ""; String? leadStatusError = ""; bool _isLoading = true; bool _submitLoading = false; bool get isLoading => _isLoading; bool get submitLoading => _submitLoading; set submitLoading(bool value) { _submitLoading = value; notifyListeners(); } // New method to update an existing product void updateProduct(int index, Map updatedProduct) { productRows[index] = updatedProduct; notifyListeners(); } // provider.productRows.add({ // "product_id": provider.selectedProductsId!, // "price": provider.addProductPriceController.text, // "qty": provider.addQuantityController.text, // "net_price": provider.addTotalAmountController.text, // }); // Existing method to add a new product void addProduct(Map product) { productRows.add(product); notifyListeners(); } void prefillProductForEdit(int index) { final product = productRows[index]; selectedProducts = productsList.firstWhere( (p) => p.id == product['product_id'], orElse: () => Products(id: '', name: 'Unknown'), ); selectedProductsId = product['product_id']; selectedProductsValue = selectedProducts?.name; addProductPriceController.text = product['price'] ?? ''; addQuantityController.text = product['qty'] ?? ''; addTotalAmountController.text = product['net_price'] ?? ''; notifyListeners(); } void addEditUpdateTotalAmount(value) { final price = double.tryParse(addProductPriceController.text) ?? 0; final qty = int.tryParse(addQuantityController.text) ?? 0; addTotalAmountController.text = (price * qty).toString(); notifyListeners(); } void addInitializeForm(BuildContext context) { addProductPriceController.clear(); addQuantityController.clear(); addTotalAmountController.clear(); notifyListeners(); } void updateSelectedProductIds(int index, value) { notifyListeners(); } Future crmAddLeadsView(context) async { try { final prov = Provider.of(context, listen: false); final data = await ApiCalling.crmNewLeadsProspectsViewAPI( prov.empId, prov.session, ); if (data != null) { if(data.sessionExists==1){ if (data.error == "0") { _employeesList = data.employees!; _sourcesList = data.sources!; _teamsList = data.teams!; _statesList = data.states!; _productsList = data.products!; _salutationList = data.salutation!; notifyListeners(); } else {} } else {} }else{ sessionDoesNotExist(context); } } catch (e, s) {} } Future crmLeadListDistrictsOnStateAPIFunction( context, mode, stateID, ) async { try { var prov = Provider.of(context, listen: false); final data = await ApiCalling.crmDistrictsOnStateAPI( prov.empId, prov.session, stateID, ); if (data != null) { if (data.error == "0") { _districtsList = data.districts!; notifyListeners(); } } } catch (e, s) {} } Future crmLeadListSubLocOnDistrictAPIFunction( context, mode, districtID, ) async { try { var prov = Provider.of(context, listen: false); final data = await ApiCalling.crmSubLocOnDistrictAPI( prov.empId, prov.session, districtID, ); if (data != null) { if (data.error == "0") { print("Auto"); _subLocationsList = data.subLocations!; notifyListeners(); } } } catch (e, s) {} } Future crmLeadListSourceOnReferenceAPIFunction( context, mode, sourceID, ) async { try { var prov = Provider.of(context, listen: false); final data = await ApiCalling.crmLeadListSourceOnReferenceAPI( prov.empId, prov.session, sourceID, ); if (data != null) { if (data.error == "0") { _referencesList = data.references!; notifyListeners(); } } } catch (e, s) {} } Future crmLeadListSegmentOnTeamAPIFunction( context, mode, teamID, ) async { try { var prov = Provider.of(context, listen: false); final data = await ApiCalling.crmLeadListSegmentOnTeamAPI( prov.empId, prov.session, teamID, ); if (data != null) { if (data.error == "0") { _segmentsList = data.segments!; notifyListeners(); } } } catch (e, s) {} } Future crmAddNewLeadsAndProspectsAPIFunction( BuildContext context, mode, acc_manager_id, salutation_name, district, state, segment, source, reference, team, sub_locality, lead_status, products, ) async { try { final prov = Provider.of(context, listen: false); final data = await ApiCalling.crmNewLeadsProspectsSubmitAPI( prov.session, prov.empId, mode, acc_manager_id, salutation_name, companyNameController.text, contactPersonNameController.text, district, state, addressController.text, segment, source, reference, team, sub_locality, mobileController.text, alternateMobileController.text, telephoneController.text, customerMailIdController.text, designationController.text, lead_status, products, ); if (data != null) { if (data.error == "0") { Navigator.pop(context, true); toast(context, data.message); resetForm(); notifyListeners(); } else {} } else {} } catch (e, s) { print("Error: $e, Stack: $s"); } } Future crmSelectedProductDetailsApiFunction( BuildContext context, String productId, ) async { try { final prov = Provider.of(context, listen: false); final data = await ApiCalling.crmSelectedProductDetailsApi( prov.empId, prov.session, productId, ); if (data != null && data.error == "0") { _selectedProductsDeatilsData = data.productsData!; addProductPriceController.text= data.productsData!.price!; notifyListeners(); } } catch (e, s) { print("Error: $e, Stack: $s"); } } onChangemailId(value) { mailIdError = ""; notifyListeners(); } onChangeCompanyName(value) { companynameError = ""; notifyListeners(); } onChangeContactPersonName(value) { nameError = ""; notifyListeners(); } onChangeAlternatemobile(value) { AlternatemobileError = ""; notifyListeners(); } onChangeTelephone(value) { TelephoneError = ""; notifyListeners(); } onChangemobile(value) { mobileError = ""; notifyListeners(); } onChangeaddress(value) { addressError = ""; notifyListeners(); } onChangedesignation(value) { designationError = ""; notifyListeners(); } onChangetaxes(value) { taxesError = ""; notifyListeners(); } onChangeSpecialNote(value) { SpecialNoteError = ""; notifyListeners(); } onChangefor(value) { forError = ""; notifyListeners(); } onChangepaymentTerms(value) { paymentTermsError = ""; notifyListeners(); } void resetForm() { checkDropdownReset(); _productRows.clear(); addProductPriceController.clear(); addQuantityController.clear(); addTotalAmountController.clear(); addTotalAmountController.clear(); addProductPriceController.clear(); addQuantityController.clear(); addTotalAmountController.clear(); companyNameController.clear(); contactPersonNameController.clear(); customerMailIdController.clear(); mobileController.clear(); alternateMobileController.clear(); telephoneController.clear(); designationController.clear(); addressController.clear(); taxesController.clear(); _productRows = []; _selectedProductsDeatilsData = null; _selectedEmployees = null; _selectedEmployeesId = null; _selectedEmployeesValue = null; _selectedSources = null; _selectedSourcesId = null; _selectedSourcesValue = null; _selectedTeams = null; _selectedTeamsId = null; _selectedTeamsValue = null; _selectedStates = null; _selectedStatesId = null; _selectedStatesValue = null; _selectedProducts = null; _selectedProductsId = null; _selectedProductsValue = null; _selectedSalutation = null; _selectedDistricts = null; _selectedSubLocations = null; _selectedReferences = null; _selectedSegments = null; _selectedDistrictId = null; _selectedDistrictValue = null; _selectedSubLocationId = null; _selectedSubLocationValue = null; _selectedReferenceId = null; _selectedReferenceValue = null; _selectedSegmentId = null; _selectedSegmentValue = null; _selectedLeadStatus = null; mailIdError = ""; companynameError = ""; salutationError = ""; nameError = ""; AlternatemobileError = ""; TelephoneError = ""; mobileError = ""; addressError = ""; designationError = ""; taxesError = ""; SpecialNoteError = ""; forError = ""; paymentTermsError = ""; sourceError = ""; referenceError = ""; teamsError = ""; segmentsError = ""; statesError = ""; districtsError = ""; subLocError = ""; leadStatusError = ""; productsEmptyError = ""; notifyListeners(); } void checkDropdownReset() { if (!_productsList.contains(_selectedProducts) && _selectedProducts != null) { _selectedProductsId = null; _selectedProductsValue = null; } if (!_employeesList.contains(_selectedEmployees) && _selectedEmployees != null) { _selectedEmployeesId = null; _selectedEmployeesValue = null; } if (!_sourcesList.contains(_selectedSources) && _selectedSources != null) { _selectedSourcesId = null; _selectedSourcesValue = null; } if (!_referencesList.contains(_selectedReferences) && _selectedReferences != null) { _selectedReferenceId = null; _selectedReferenceValue = null; } if (!_segmentsList.contains(_selectedSegments) && _selectedSegments != null) { _selectedTeamsId = null; _selectedTeamsValue = null; } if (!_teamsList.contains(_selectedTeams) && _selectedTeams != null) { _selectedSegmentId = null; _selectedSegmentValue = null; } if (!_districtsList.contains(_selectedDistricts) && _selectedDistricts != null) { _selectedDistrictId = null; _selectedDistrictValue = null; } if (!_statesList.contains(_selectedStates) && _selectedStates != null) { _selectedStatesId = null; _selectedStatesValue = null; } if (!_subLocationsList.contains(_selectedSubLocations) && _selectedSubLocations != null) { _selectedSubLocationId = null; _selectedSubLocationValue = null; } notifyListeners(); } bool validateStep1(){ bool isValid = true; if(_selectedSalutation==null||_selectedSalutation==''){ isValid = false; salutationError = "Please Select Salutation"; } if(companyNameController.text.trim().isEmpty){ companynameError = "Please Enter Company Name"; isValid = false; } if(contactPersonNameController.text.trim().isEmpty){ nameError = "Please Enter Name"; isValid = false; } // if(customerMailIdController.text.trim().isEmpty){ // mailIdError = "Please enter Email Id"; // isValid = false; // } if(mobileController.text.trim().isEmpty){ mobileError = "Please enter Mobile Number"; isValid = false; }else{ if(mobileController.text.length<10){ mobileError = "Please enter 10 digits"; isValid = false; } } // if(designationController.text.trim().isEmpty){ // designationError = "Please enter Designation"; // isValid = false; // } notifyListeners(); return isValid; } bool validateStep2(){ bool isValid = true; if(alternateMobileController.text.trim().isNotEmpty && alternateMobileController.text.length<10){ AlternatemobileError = "Please enter 10 digits"; isValid = false; } // if(telephoneController.text.trim().isEmpty){ // isValid = false; // // } if(_selectedSources==null||_selectedSourcesId!.isEmpty){ sourceError = "Please Select Source"; isValid = false; } if(_selectedReferences==null||_selectedReferenceId!.isEmpty){ referenceError = "Please Select Reference"; isValid = false; } if(_selectedTeams==null||_selectedTeamsId!.isEmpty){ teamsError = "Please Select Team"; isValid = false; } if(_selectedSegments==null||_selectedSegmentId!.isEmpty){ segmentsError = "Please Select Segement"; isValid = false; } notifyListeners(); return isValid; } bool validateStep3(){ bool isValid = true; if(_selectedStates==null||_selectedStatesId!.isEmpty){ statesError = "Please Select State"; isValid = false; } if(_selectedDistricts==null||_selectedDistrictId!.isEmpty){ districtsError = "Please Select District"; isValid = false; } // if(_selectedSubLocations==null||_selectedSubLocationId!.isEmpty){ // subLocError = "Please Select Sub Location"; // isValid = false; // } if(_selectedLeadStatus==null||_selectedLeadStatus!.isEmpty){ leadStatusError = "Please Select Lead Status"; isValid = false; } if(addressController.text.trim().isEmpty){ addressError = "Please Enter address"; isValid = false; } if(productRows.isEmpty || _productRows.isEmpty){ productsEmptyError = "Please Add min. one Product"; isValid = false; } notifyListeners(); return isValid; } resetForm2() { customerMailIdController.clear(); mobileController.clear(); addressController.clear(); taxesController.clear(); SpecialNoteController.clear(); forController.clear(); paymentTermsController.clear(); mailIdError = ""; mobileError = ""; addressError = ""; designationError = ""; taxesError = ""; SpecialNoteError = ""; forError = ""; paymentTermsError = ""; } }