import 'package:cheminova/controller/rd_processing_invoice_service.dart';
import 'package:cheminova/models/get_invoice_model.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../models/rd_processing_invoice_model.dart';
import '../utils/show_snackbar.dart';
import 'get_single_invoice_Service.dart';

class GetSingleInvoiceController extends GetxController {
// Import your Invoice model file

  // Observable for holding the fetched invoice
  var invoice = <GetInvoiceModel?>[].obs;
  var isLoading = true.obs; // Observable for loading state
  var errorMessage = ''.obs; // Observable for error message

  @override
  void onInit() {
    super.onInit();

    // Call fetchInvoice with a specific ID
  }

  Future<void> fetchInvoice(String invoiceId) async {
    isLoading.value = true; // Set loading state to true
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String? token = prefs.getString('token');
      final List<InvoiceResponseModel>? allOrders = await RdProcessingInvoiceService().getRDProcessingProduct(token!);

      // Check if the token is null or empty
      if (token == null || token.isEmpty) {
        errorMessage.value = 'Token not found'; // Handle token not being available
        return;
      }




      if (allOrders == null || allOrders.isEmpty) {
        print("No orders found.");
        showSnackbar("No orders found.");
        return;
      }

      // Display the number of fetched orders
      print("Fetched ${allOrders.length} orders.");

      // Get the ID of the first order and convert it to a String
      final String firstOrderId = allOrders[0].id.toString(); // Convert to String
      print("First order ID: $firstOrderId"); // Debugging: Log the order ID

      // Fetch the single order using the first order ID
      final GetInvoiceModel? result = await GetSingleInvoiceService().fetchInvoice(token, firstOrderId);

      // Check if the result is not null
      if (result != null) {
        // Here, you should add the single order to your observable list
        invoice.clear(); // Clear the previous data if necessary
        invoice.add(result); // Add the single order to the list
      } else {
        // If the result is null, you can clear the list
        invoice.clear();
      }

      // For debugging, log the fetched orders count
      print("Fetched orders count: ${invoice.length}");
    }

      // Attempt to fetch the invoice
    //  final fetchedInvoice = await GetSingleInvoiceService().fetchInvoice(token, invoiceId);

      // Check if the fetchedInvoice is null before accessing it
    //   if (fetchedInvoice != null) {
    //     invoice.value = fetchedInvoice as List<GetInvoiceModel?>; // Assign the fetched invoice to the observable
    //   } else {
    //     errorMessage.value = 'Invoice not found'; // Handle the case where the invoice could not be fetched
    //   }
    // }
    catch (e) {
      errorMessage.value = 'Error fetching invoice: $e'; // Set the error message with more context
    } finally {
      isLoading.value = false; // Set loading state to false
    }
  }


}
