141 lines
4.0 KiB
Dart
141 lines
4.0 KiB
Dart
import 'dart:convert';
|
|
import 'package:cheminova/constants/constant.dart';
|
|
import 'package:cheminova/screens/data_submit_successfull.dart';
|
|
import 'package:cheminova/services/api_client.dart';
|
|
import 'package:cheminova/services/api_urls.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
import '../models/products_response.dart';
|
|
|
|
class ProductProvider extends ChangeNotifier {
|
|
final _apiClient = ApiClient();
|
|
ProductResponse? productResponse;
|
|
List<ProductModel> productList = [];
|
|
List<ProductModel> searchList = [];
|
|
|
|
bool _isLoading = false;
|
|
|
|
bool get isLoading => _isLoading;
|
|
|
|
List<ProductModel> selectedProducts = [];
|
|
|
|
void setLoading(bool loading) {
|
|
_isLoading = loading;
|
|
notifyListeners();
|
|
}
|
|
|
|
void filterProducts(String query) {
|
|
searchList = productList.where((product) {
|
|
final productNameLower = product.productName.toLowerCase();
|
|
final productSkuLower = product.sku.toLowerCase();
|
|
final searchLower = query.toLowerCase();
|
|
return productNameLower.contains(searchLower) ||
|
|
productSkuLower.contains(searchLower);
|
|
}).toList();
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> getProducts() async {
|
|
Response response = await _apiClient.get(ApiUrls.getProducts);
|
|
debugPrint('Response: $response');
|
|
setLoading(false);
|
|
if (response.statusCode == 200) {
|
|
productResponse = ProductResponse.fromJson(response.data);
|
|
productList = productResponse!.products!
|
|
.map((product) =>
|
|
ProductModel(sku: product.sKU!, productName: product.name!))
|
|
.toList();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> submitProducts(
|
|
{required String distributorType, required String pdRdId, String? inventoryId}) async {
|
|
setLoading(true);
|
|
try {
|
|
Response response = await _apiClient.post(ApiUrls.submitProductUrl,
|
|
data: json.encode({
|
|
"addedFor": distributorType.replaceAll(' ', ''),
|
|
"addedForId": pdRdId,
|
|
"products": selectedProducts.map((e) => e.toJson()).toList()
|
|
}));
|
|
setLoading(false);
|
|
if (response.statusCode == 201) {
|
|
ScaffoldMessenger.of(
|
|
navigatorKey.currentContext!,
|
|
).showSnackBar(
|
|
SnackBar(content: Text(response.data['message'])),
|
|
);
|
|
if (inventoryId!=null ) {
|
|
_apiClient.put(ApiUrls.updateTaskInventoryUrl+inventoryId,data:null).then((value) {
|
|
debugPrint('Task Updated');
|
|
if (value.statusCode == 200) {
|
|
resetProducts();
|
|
Navigator.push(
|
|
navigatorKey.currentContext!,
|
|
MaterialPageRoute(
|
|
builder: (context) => const DataSubmitSuccessFullScreen()));
|
|
}
|
|
else{
|
|
ScaffoldMessenger.of(
|
|
navigatorKey.currentContext!,
|
|
).showSnackBar(
|
|
const SnackBar(content: Text('Task not updated')),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
else{
|
|
resetProducts();
|
|
Navigator.push(
|
|
navigatorKey.currentContext!,
|
|
MaterialPageRoute(
|
|
builder: (context) => const DataSubmitSuccessFullScreen()));
|
|
}
|
|
}
|
|
} catch (e) {
|
|
setLoading(false);
|
|
debugPrint("Error: $e");
|
|
}
|
|
}
|
|
|
|
void resetProducts() {
|
|
selectedProducts.clear();
|
|
productList.clear();
|
|
productResponse = null;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
class ProductModel {
|
|
String sku;
|
|
String productName;
|
|
int? sale;
|
|
int? inventory;
|
|
|
|
ProductModel(
|
|
{required this.sku,
|
|
required this.productName,
|
|
this.sale,
|
|
this.inventory});
|
|
|
|
factory ProductModel.fromJson(Map<String, dynamic> json) {
|
|
return ProductModel(
|
|
sku: json['SKU'],
|
|
productName: json['ProductName'],
|
|
sale: json['Sale'],
|
|
inventory: json['Inventory']);
|
|
}
|
|
|
|
get comments => null;
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'SKU': sku,
|
|
'ProductName': productName,
|
|
'Sale': sale,
|
|
'Inventory': inventory
|
|
};
|
|
}
|
|
}
|