import 'package:cheminova/controller/product_mannual_service.dart';
import 'package:cheminova/controller/rd_get_order_service.dart';
import 'package:cheminova/controller/rd_processing_invoice_service.dart';
import 'package:cheminova/models/rd_get_order_model.dart';
import 'package:cheminova/models/rd_processing_invoice_model.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/product_mannual_model.dart'; // Your model import
// Your service import

class GetRDProcessingInvoiceController extends GetxController {
  var isLoading = true.obs; // Tracks the loading state
  var productProcessingRDList = <InvoiceResponseModel>[].obs; // List of products
  var productDispatchRDList = <InvoiceResponseModel>[].obs;
  var productDeliveredRDList = <InvoiceResponseModel>[].obs;
  @override
  void onInit() {
    getRDProcessingInvoiceProduct();
    super.onInit();
  }

  // Fetch the products from the API
  // Fetch the products from the API
  Future<void> getRDProcessingInvoiceProduct() async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String? token = prefs.getString('token');

      // Check if the token is null
      if (token == null) {
        // Handle the case where the token is not available
        print("Error: Token is null. Please login again.");
        return; // Optionally, you can navigate the user to the login screen
      }

      isLoading(true); // Start loading

      // Fetch products from API
      final response = await RdProcessingInvoiceService().getRDProcessingProduct(token);

      // Check if response is not null and assign it to the observable list
      if (response != null) {
        productProcessingRDList.assignAll(response); // Assign products to the observable list
      } else {
        // Optionally handle the case where the response is null
        print("No products found or response is null.");
      }
    } catch (e) {
      // Handle any exceptions that occur during the fetch
      print("An error occurred while fetching products: ${e.toString()}");
    } finally {
      isLoading(false); // End loading regardless of success or failure
    }
  }

}

